X-Git-Url: http://www.privoxy.org/gitweb/?a=blobdiff_plain;f=jcc.c;h=eac7a762a0b85b31f39534d60e7c74ac9ac46d02;hb=3d62ec9da999408652b23d7407cef099965e0780;hp=de785a4c208ecbd41f1ef88beaa13b0e5c644016;hpb=c9041d8a4fdaea7bb4a08009db3c9281d3e4ae3e;p=privoxy.git diff --git a/jcc.c b/jcc.c index de785a4c..eac7a762 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.305 2009/12/22 13:03:30 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.311 2010/01/03 12:37:14 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -119,7 +119,7 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.305 2009/12/22 13:03:30 fabiankeil Exp $" const char jcc_h_rcs[] = JCC_H_VERSION; const char project_h_rcs[] = PROJECT_H_VERSION; -int no_daemon = 0; +int daemon_mode = 1; struct client_state clients[1]; struct file_list files[1]; @@ -2319,7 +2319,7 @@ static void chat(struct client_state *csp) { log_error(LOG_LEVEL_ERROR, "Empty server or forwarder response received on socket %d. " - "Closing client connection %d without sending data.", + "Closing client socket %d without sending data.", csp->server_connection.sfd, csp->cfd); } else @@ -2951,7 +2951,7 @@ int main(int argc, char **argv) else if (strcmp(argv[argc_pos], "--no-daemon" ) == 0) { set_debug_level(LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO); - no_daemon = 1; + daemon_mode = 0; } else if (strcmp(argv[argc_pos], "--pidfile" ) == 0) @@ -3118,12 +3118,11 @@ int main(int argc, char **argv) #if defined(unix) { pid_t pid = 0; -#if 0 - int fd; -#endif - if (!no_daemon) + if (daemon_mode) { + int fd; + pid = fork(); if ( pid < 0 ) /* error */ @@ -3148,33 +3147,50 @@ int main(int argc, char **argv) exit( 0 ); } /* child */ -#if 1 - /* Should be more portable, but not as well tested */ + setsid(); -#else /* !1 */ -#ifdef __FreeBSD__ - setpgrp(0,0); -#else /* ndef __FreeBSD__ */ - setpgrp(); -#endif /* ndef __FreeBSD__ */ - fd = open("/dev/tty", O_RDONLY); - if ( fd ) - { - /* no error check here */ - ioctl( fd, TIOCNOTTY,0 ); - close ( fd ); - } -#endif /* 1 */ + /* * stderr (fd 2) will be closed later on, * when the config file has been parsed. */ + close(0); + close(1); + + /* + * Reserve fd 0 and 1 to prevent abort() and friends + * from sending stuff to the clients or servers. + */ + fd = open("/dev/null", O_RDONLY); + if (fd == -1) + { + log_error(LOG_LEVEL_FATAL, "Failed to open /dev/null: %E"); + } + else if (fd != 0) + { + if (dup2(fd, 0) == -1) + { + log_error(LOG_LEVEL_FATAL, "Failed to reserve fd 0: %E"); + } + close(fd); + } + fd = open("/dev/null", O_WRONLY); + if (fd == -1) + { + log_error(LOG_LEVEL_FATAL, "Failed to open /dev/null: %E"); + } + else if (fd != 1) + { + if (dup2(fd, 1) == -1) + { + log_error(LOG_LEVEL_FATAL, "Failed to reserve fd 1: %E"); + } + close(fd); + } - close( 0 ); - close( 1 ); chdir("/"); - } /* -END- if (!no_daemon) */ + } /* -END- if (daemon_mode) */ /* * As soon as we have written the PID file, we can switch