在主函数中加入代码:
if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR)
{
unix_error("mask signal pipe error");
}
修改Rio_writen:
void Rio_writen(int fd, void *usrbuf, size_t n)
{
if (rio_writen(fd, usrbuf, n) != n)
{
unix_error("Rio_writen error");
if(errno == EPIPE)
{
unix_error("EPIPE error\nConnection ended");
}
}
}
修改CGI运行时:
if (Fork() == 0)
{ /* Child */
/* Real server would set all CGI vars here */
setenv("QUERY_STRING", cgiargs, 1);
Dup2(fd, STDOUT_FILENO); /* Redirect stdout to client */
if (Signal(SIGPIPE, SIG_DFL) == SIG_ERR)
{
unix_error("mask signal pipe error");
}
Execve(filename, emptylist, environ); /* Run CGI program */
}