Description: Patch which fix bug related to pid file creation Author: Emmanuel Bouthenot Last-Update: 2006-09-11 --- a/cfg.h +++ b/cfg.h @@ -39,6 +39,8 @@ int nowarn; int first; + + char piddir[256]; }; #define MAX_CHILDS 1024 --- a/db.c +++ b/db.c @@ -373,7 +373,13 @@ } else if (strcmp("timeout_connect", obuf) == 0) { config.to_con = atoi(abuf); continue; - } else if (!config.first) { + } + else if (strcmp("pid_dir", obuf) == 0) { + (void) strncpy(config.piddir, abuf, sizeof(config.piddir) - 1); + config.piddir[sizeof(config.piddir) - 1] = 0; + continue; + } + else if (!config.first) { continue; } else if (*obuf != '#') { warn("unknown option in config file %s: %s", config.file, obuf); --- a/ffproxy.8.s +++ b/ffproxy.8.s @@ -19,6 +19,7 @@ .Op Fl x Ar proxyip|proxyhost Fl X Ar proxyport .Op Fl a Ar ip|hostname .Op Fl A Ar port +.Op Fl n Ar piddir .Op Fl f Ar configfile .Op Fl ds4bBhv .Sh DESCRIPTION @@ -85,6 +86,9 @@ Auxiliary forward HTTP server to use (see section HTTP ACCELERATOR). .It Fl A Ar port Port to use for above. Defaults to 80. +.It Fl n Ar piddir +directory to store file ffproxy.pid with ffproxy pid inside. Default is +.Pa /var/run .It Fl f Ar configfile User configuration file to load. Please note that command line options get overwritten by set configuration file options. @@ -296,13 +300,12 @@ .Xr ffproxy.conf 5 for details on options that may be changed at runtime. .Pp -If daemonized, the master process writes the pid file +ffproxy write its pid file .Pa ffproxy.pid -to the working directory, that is, the directory -specified by db_files_path or the command line parameter -D. -It defaults to -.Pa _BASE_ . -The program will terminate if writing fails. +in the directory specified by the command line parameter -n or the +.Pa pid_dir +setting in config file. Default is +.Pa /var/run . .Sh LOGGING By default, the proxy logs incorrect and filtered requests. To log all requests, use the configuration file keyword @@ -323,12 +326,6 @@ and who is allowed to connect and use ffproxy .El .Pp -If daemonized, ffproxy writes the pid of its master -process to the file named -.Pa ffproxy.pid -in its working directory -- -.Pa _BASE_ -by default. .Sh SEE ALSO .Pa sample.config for a sample configuration file --- a/ffproxy.conf.5.s +++ b/ffproxy.conf.5.s @@ -38,10 +38,9 @@ .Ss Reloading Configuration To let the proxy reload its configuration files, that is, besides the configuration file specified, the contents of db/ and html/, -send the signal HUP to the program's master process. If -ffproxy runs daemonized, the PID can be found in -.Pa db_files_path/ffproxy.conf . -Otherwise look into your system's syslog log files or process table. +send the signal HUP to the program's master process. ffproxy pid can be found +by default in file /var/run/ffproxy.pid, you can override /var/run directory +with command line paramter -n or with pid_dir setting in config file. .Pp Options that can be successfully altered at runtime are .Bd -literal -offset indent @@ -204,6 +203,12 @@ # (default: 4) #backlog_size 16 #backlog_size 4 + +# directory to store file ffproxy.pid (with ffproxy pid inside) +# (default: /var/run) +#pid_dir /tmp +#pid_dir /var/run + .Ed .Sh VERSION This manual documents ffproxy 1.6 (2005-01-05). --- a/main.c +++ b/main.c @@ -20,19 +20,12 @@ */ #include "configure.h" -#ifdef HAVE_SYS_TYPES_H -# include -#endif - #include - -#ifdef HAVE_STDLIB_H -# include -#endif +#include +#include +#include #include -#ifdef HAVE_UNISTD_H -# include -#endif +#include #include #include #include @@ -44,34 +37,9 @@ #include "dns.h" #include "signals.h" -#if !defined(HAVE_DAEMON) || defined(NEED_DAEMON) || defined(__sun__) -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#ifdef HAVE_SYS_STAT_H -# include -#endif -#ifdef HAVE_FCNTL_H -# include -#endif - -static int daemon(int, int); -int -daemon(int nochdir, int noclose) -{ - int f; - f = open("/dev/null", O_RDWR); - (void) dup2(STDIN_FILENO, f); - (void) dup2(STDERR_FILENO, f); - (void) dup2(STDOUT_FILENO, f); - if (fork()) - _exit(0); - return 0; -} -#endif - static void usage(void); static void drop_privileges(void); +static void write_pid(char*, pid_t); static const char version[] = "1.6"; static const char rcsid[] = "$Id: main.c,v 2.2 2005/01/05 15:12:49 niklas Exp niklas $"; @@ -84,6 +52,7 @@ { int c, nowarn; char *prgname; + pid_t pid; prgname = argv[0]; nowarn = 0; @@ -119,8 +88,9 @@ config.unr_con = 0; config.to_con = 5; config.first = 1; + (void) strncpy(config.piddir, "/var/run", sizeof(config.piddir) - 1); - while ((c = getopt(argc, argv, "vdbBc:C:p:x:X:l:u:g:r:D:F:f:s4a:A:h")) != -1) { + while ((c = getopt(argc, argv, "vdbBc:C:p:x:X:l:u:g:r:D:F:f:s4a:A:n:h")) != -1) { switch (c) { case 'v': (void) printf("ffproxy version %s, %s\n", @@ -240,6 +210,14 @@ usage(); /* NOTREACHED */ break; + case 'n': + if (strlen(optarg) > sizeof(config.piddir) - 1 ) { + (void) fprintf(stderr, "piddir directory too long\n"); + exit(1); + } + (void) strncpy(config.piddir, optarg, sizeof(config.piddir) - 1); + config.piddir[sizeof(config.piddir) - 1] = '\0'; + break; default: (void) fprintf(stderr, "Error, type `%s -h' for help on usage\n", prgname); exit(1); @@ -258,26 +236,30 @@ info("started, initializing"); load_databases(); (void) resolve("localhost"); - drop_privileges(); - + + pid = getpid(); if (config.daemon) { - FILE *fp; - - if (daemon(1, 0) != 0) - fatal("daemon() failed"); + pid = fork(); + if (pid == -1) + fatal("daemonize() failed"); + + if (pid > 0) { + write_pid(config.piddir, pid); + _exit (0); + } + else { (void) close(0); (void) close(1); (void) close(2); + } + } + else + write_pid(config.piddir, pid); - (void) chdir(config.dbdir); - if ((fp = fopen("ffproxy.pid", "w")) == NULL) - fatal("cannot create pid file ffproxy.pid in %s", config.dbdir); + drop_privileges(); - (void) fprintf(fp, "%ld", (long) getpid()); - (void) fclose(fp); - } (void) snprintf(loop_header, sizeof(loop_header), "X-Loop-%d-%d: true", getpid(), (int) time(NULL)); - + init_sighandlers(); open_socket(); @@ -312,8 +294,9 @@ " -D dir databases are in dir (default is %s)\n" " -f file use config file (default is %s; *overwrites*)\n" " -a host|ip auxiliary forward server to use\n" - " -A port auxiliary forward server port (default is 80)\n", - DATADIR, CFGFILE); + " -A port auxiliary forward server port (default is 80)\n" + " -n dir pid file (ffproxy.pid) will be written in dir\n", + DATADIR, CFGFILE); exit(1); } @@ -348,3 +331,23 @@ info("=> UID(%d), EUID(%d), GID(%d), EGID(%d)", getuid(), geteuid(), getgid(), getegid()); } + +static void +write_pid(char *dir, pid_t p) +{ + FILE *fp; + + if (*dir != '\0') { + if (chdir(dir) == 0) { + if ((fp = fopen("ffproxy.pid", "w")) == NULL) + fatal("cannot create pid file ffproxy.pid in %s", dir); + else { + info("writing pid in %s/ffproxy.pid", dir); + (void) fprintf(fp, "%ld", (long) p); + (void) fclose(fp); + } + } + else + fatal("cannot change directory for %s to write pid file", dir); + } +}