| 1 |
Description: Patch which fix bug related to pid file creation |
| 2 |
Author: Emmanuel Bouthenot <[email protected]> |
| 3 |
Last-Update: 2006-09-11 |
| 4 |
--- a/cfg.h |
| 5 |
+++ b/cfg.h |
| 6 |
@@ -39,6 +39,8 @@ |
| 7 |
|
| 8 |
int nowarn; |
| 9 |
int first; |
| 10 |
+ |
| 11 |
+ char piddir[256]; |
| 12 |
}; |
| 13 |
|
| 14 |
#define MAX_CHILDS 1024 |
| 15 |
--- a/db.c |
| 16 |
+++ b/db.c |
| 17 |
@@ -373,7 +373,13 @@ |
| 18 |
} else if (strcmp("timeout_connect", obuf) == 0) { |
| 19 |
config.to_con = atoi(abuf); |
| 20 |
continue; |
| 21 |
- } else if (!config.first) { |
| 22 |
+ } |
| 23 |
+ else if (strcmp("pid_dir", obuf) == 0) { |
| 24 |
+ (void) strncpy(config.piddir, abuf, sizeof(config.piddir) - 1); |
| 25 |
+ config.piddir[sizeof(config.piddir) - 1] = 0; |
| 26 |
+ continue; |
| 27 |
+ } |
| 28 |
+ else if (!config.first) { |
| 29 |
continue; |
| 30 |
} else if (*obuf != '#') { |
| 31 |
warn("unknown option in config file %s: %s", config.file, obuf); |
| 32 |
--- a/ffproxy.8.s |
| 33 |
+++ b/ffproxy.8.s |
| 34 |
@@ -19,6 +19,7 @@ |
| 35 |
.Op Fl x Ar proxyip|proxyhost Fl X Ar proxyport |
| 36 |
.Op Fl a Ar ip|hostname |
| 37 |
.Op Fl A Ar port |
| 38 |
+.Op Fl n Ar piddir |
| 39 |
.Op Fl f Ar configfile |
| 40 |
.Op Fl ds4bBhv |
| 41 |
.Sh DESCRIPTION |
| 42 |
@@ -85,6 +86,9 @@ |
| 43 |
Auxiliary forward HTTP server to use (see section HTTP ACCELERATOR). |
| 44 |
.It Fl A Ar port |
| 45 |
Port to use for above. Defaults to 80. |
| 46 |
+.It Fl n Ar piddir |
| 47 |
+directory to store file ffproxy.pid with ffproxy pid inside. Default is |
| 48 |
+.Pa /var/run |
| 49 |
.It Fl f Ar configfile |
| 50 |
User configuration file to load. Please note that command |
| 51 |
line options get overwritten by set configuration file options. |
| 52 |
@@ -296,13 +300,12 @@ |
| 53 |
.Xr ffproxy.conf 5 |
| 54 |
for details on options that may be changed at runtime. |
| 55 |
.Pp |
| 56 |
-If daemonized, the master process writes the pid file |
| 57 |
+ffproxy write its pid file |
| 58 |
.Pa ffproxy.pid |
| 59 |
-to the working directory, that is, the directory |
| 60 |
-specified by db_files_path or the command line parameter -D. |
| 61 |
-It defaults to |
| 62 |
-.Pa _BASE_ . |
| 63 |
-The program will terminate if writing fails. |
| 64 |
+in the directory specified by the command line parameter -n or the |
| 65 |
+.Pa pid_dir |
| 66 |
+setting in config file. Default is |
| 67 |
+.Pa /var/run . |
| 68 |
.Sh LOGGING |
| 69 |
By default, the proxy logs incorrect and filtered requests. |
| 70 |
To log all requests, use the configuration file keyword |
| 71 |
@@ -323,12 +326,6 @@ |
| 72 |
and who is allowed to connect and use ffproxy |
| 73 |
.El |
| 74 |
.Pp |
| 75 |
-If daemonized, ffproxy writes the pid of its master |
| 76 |
-process to the file named |
| 77 |
-.Pa ffproxy.pid |
| 78 |
-in its working directory -- |
| 79 |
-.Pa _BASE_ |
| 80 |
-by default. |
| 81 |
.Sh SEE ALSO |
| 82 |
.Pa sample.config |
| 83 |
for a sample configuration file |
| 84 |
--- a/ffproxy.conf.5.s |
| 85 |
+++ b/ffproxy.conf.5.s |
| 86 |
@@ -38,10 +38,9 @@ |
| 87 |
.Ss Reloading Configuration |
| 88 |
To let the proxy reload its configuration files, that is, besides |
| 89 |
the configuration file specified, the contents of db/ and html/, |
| 90 |
-send the signal HUP to the program's master process. If |
| 91 |
-ffproxy runs daemonized, the PID can be found in |
| 92 |
-.Pa db_files_path/ffproxy.conf . |
| 93 |
-Otherwise look into your system's syslog log files or process table. |
| 94 |
+send the signal HUP to the program's master process. ffproxy pid can be found |
| 95 |
+by default in file /var/run/ffproxy.pid, you can override /var/run directory |
| 96 |
+with command line paramter -n or with pid_dir setting in config file. |
| 97 |
.Pp |
| 98 |
Options that can be successfully altered at runtime are |
| 99 |
.Bd -literal -offset indent |
| 100 |
@@ -204,6 +203,12 @@ |
| 101 |
# (default: 4) |
| 102 |
#backlog_size 16 |
| 103 |
#backlog_size 4 |
| 104 |
+ |
| 105 |
+# directory to store file ffproxy.pid (with ffproxy pid inside) |
| 106 |
+# (default: /var/run) |
| 107 |
+#pid_dir /tmp |
| 108 |
+#pid_dir /var/run |
| 109 |
+ |
| 110 |
.Ed |
| 111 |
.Sh VERSION |
| 112 |
This manual documents ffproxy 1.6 (2005-01-05). |
| 113 |
--- a/main.c |
| 114 |
+++ b/main.c |
| 115 |
@@ -20,19 +20,12 @@ |
| 116 |
*/ |
| 117 |
|
| 118 |
#include "configure.h" |
| 119 |
-#ifdef HAVE_SYS_TYPES_H |
| 120 |
-# include <sys/types.h> |
| 121 |
-#endif |
| 122 |
- |
| 123 |
#include <stdio.h> |
| 124 |
- |
| 125 |
-#ifdef HAVE_STDLIB_H |
| 126 |
-# include <stdlib.h> |
| 127 |
-#endif |
| 128 |
+#include <sys/types.h> |
| 129 |
+#include <sys/stat.h> |
| 130 |
+#include <stdlib.h> |
| 131 |
#include <string.h> |
| 132 |
-#ifdef HAVE_UNISTD_H |
| 133 |
-# include <unistd.h> |
| 134 |
-#endif |
| 135 |
+#include <unistd.h> |
| 136 |
#include <time.h> |
| 137 |
#include <pwd.h> |
| 138 |
#include <grp.h> |
| 139 |
@@ -44,34 +37,9 @@ |
| 140 |
#include "dns.h" |
| 141 |
#include "signals.h" |
| 142 |
|
| 143 |
-#if !defined(HAVE_DAEMON) || defined(NEED_DAEMON) || defined(__sun__) |
| 144 |
-#ifdef HAVE_SYS_TYPES_H |
| 145 |
-# include <sys/types.h> |
| 146 |
-#endif |
| 147 |
-#ifdef HAVE_SYS_STAT_H |
| 148 |
-# include <sys/stat.h> |
| 149 |
-#endif |
| 150 |
-#ifdef HAVE_FCNTL_H |
| 151 |
-# include <fcntl.h> |
| 152 |
-#endif |
| 153 |
- |
| 154 |
-static int daemon(int, int); |
| 155 |
-int |
| 156 |
-daemon(int nochdir, int noclose) |
| 157 |
-{ |
| 158 |
- int f; |
| 159 |
- f = open("/dev/null", O_RDWR); |
| 160 |
- (void) dup2(STDIN_FILENO, f); |
| 161 |
- (void) dup2(STDERR_FILENO, f); |
| 162 |
- (void) dup2(STDOUT_FILENO, f); |
| 163 |
- if (fork()) |
| 164 |
- _exit(0); |
| 165 |
- return 0; |
| 166 |
-} |
| 167 |
-#endif |
| 168 |
- |
| 169 |
static void usage(void); |
| 170 |
static void drop_privileges(void); |
| 171 |
+static void write_pid(char*, pid_t); |
| 172 |
|
| 173 |
static const char version[] = "1.6"; |
| 174 |
static const char rcsid[] = "$Id: main.c,v 2.2 2005/01/05 15:12:49 niklas Exp niklas $"; |
| 175 |
@@ -84,6 +52,7 @@ |
| 176 |
{ |
| 177 |
int c, nowarn; |
| 178 |
char *prgname; |
| 179 |
+ pid_t pid; |
| 180 |
|
| 181 |
prgname = argv[0]; |
| 182 |
nowarn = 0; |
| 183 |
@@ -119,8 +88,9 @@ |
| 184 |
config.unr_con = 0; |
| 185 |
config.to_con = 5; |
| 186 |
config.first = 1; |
| 187 |
+ (void) strncpy(config.piddir, "/var/run", sizeof(config.piddir) - 1); |
| 188 |
|
| 189 |
- while ((c = getopt(argc, argv, "vdbBc:C:p:x:X:l:u:g:r:D:F:f:s4a:A:h")) != -1) { |
| 190 |
+ while ((c = getopt(argc, argv, "vdbBc:C:p:x:X:l:u:g:r:D:F:f:s4a:A:n:h")) != -1) { |
| 191 |
switch (c) { |
| 192 |
case 'v': |
| 193 |
(void) printf("ffproxy version %s, %s\n", |
| 194 |
@@ -240,6 +210,14 @@ |
| 195 |
usage(); |
| 196 |
/* NOTREACHED */ |
| 197 |
break; |
| 198 |
+ case 'n': |
| 199 |
+ if (strlen(optarg) > sizeof(config.piddir) - 1 ) { |
| 200 |
+ (void) fprintf(stderr, "piddir directory too long\n"); |
| 201 |
+ exit(1); |
| 202 |
+ } |
| 203 |
+ (void) strncpy(config.piddir, optarg, sizeof(config.piddir) - 1); |
| 204 |
+ config.piddir[sizeof(config.piddir) - 1] = '\0'; |
| 205 |
+ break; |
| 206 |
default: |
| 207 |
(void) fprintf(stderr, "Error, type `%s -h' for help on usage\n", prgname); |
| 208 |
exit(1); |
| 209 |
@@ -258,26 +236,30 @@ |
| 210 |
info("started, initializing"); |
| 211 |
load_databases(); |
| 212 |
(void) resolve("localhost"); |
| 213 |
- drop_privileges(); |
| 214 |
- |
| 215 |
+ |
| 216 |
+ pid = getpid(); |
| 217 |
if (config.daemon) { |
| 218 |
- FILE *fp; |
| 219 |
- |
| 220 |
- if (daemon(1, 0) != 0) |
| 221 |
- fatal("daemon() failed"); |
| 222 |
+ pid = fork(); |
| 223 |
+ if (pid == -1) |
| 224 |
+ fatal("daemonize() failed"); |
| 225 |
+ |
| 226 |
+ if (pid > 0) { |
| 227 |
+ write_pid(config.piddir, pid); |
| 228 |
+ _exit (0); |
| 229 |
+ } |
| 230 |
+ else { |
| 231 |
(void) close(0); |
| 232 |
(void) close(1); |
| 233 |
(void) close(2); |
| 234 |
+ } |
| 235 |
+ } |
| 236 |
+ else |
| 237 |
+ write_pid(config.piddir, pid); |
| 238 |
|
| 239 |
- (void) chdir(config.dbdir); |
| 240 |
- if ((fp = fopen("ffproxy.pid", "w")) == NULL) |
| 241 |
- fatal("cannot create pid file ffproxy.pid in %s", config.dbdir); |
| 242 |
+ drop_privileges(); |
| 243 |
|
| 244 |
- (void) fprintf(fp, "%ld", (long) getpid()); |
| 245 |
- (void) fclose(fp); |
| 246 |
- } |
| 247 |
(void) snprintf(loop_header, sizeof(loop_header), "X-Loop-%d-%d: true", getpid(), (int) time(NULL)); |
| 248 |
- |
| 249 |
+ |
| 250 |
init_sighandlers(); |
| 251 |
open_socket(); |
| 252 |
|
| 253 |
@@ -312,8 +294,9 @@ |
| 254 |
" -D dir databases are in dir (default is %s)\n" |
| 255 |
" -f file use config file (default is %s; *overwrites*)\n" |
| 256 |
" -a host|ip auxiliary forward server to use\n" |
| 257 |
- " -A port auxiliary forward server port (default is 80)\n", |
| 258 |
- DATADIR, CFGFILE); |
| 259 |
+ " -A port auxiliary forward server port (default is 80)\n" |
| 260 |
+ " -n dir pid file (ffproxy.pid) will be written in dir\n", |
| 261 |
+ DATADIR, CFGFILE); |
| 262 |
exit(1); |
| 263 |
} |
| 264 |
|
| 265 |
@@ -348,3 +331,23 @@ |
| 266 |
|
| 267 |
info("=> UID(%d), EUID(%d), GID(%d), EGID(%d)", getuid(), geteuid(), getgid(), getegid()); |
| 268 |
} |
| 269 |
+ |
| 270 |
+static void |
| 271 |
+write_pid(char *dir, pid_t p) |
| 272 |
+{ |
| 273 |
+ FILE *fp; |
| 274 |
+ |
| 275 |
+ if (*dir != '\0') { |
| 276 |
+ if (chdir(dir) == 0) { |
| 277 |
+ if ((fp = fopen("ffproxy.pid", "w")) == NULL) |
| 278 |
+ fatal("cannot create pid file ffproxy.pid in %s", dir); |
| 279 |
+ else { |
| 280 |
+ info("writing pid in %s/ffproxy.pid", dir); |
| 281 |
+ (void) fprintf(fp, "%ld", (long) p); |
| 282 |
+ (void) fclose(fp); |
| 283 |
+ } |
| 284 |
+ } |
| 285 |
+ else |
| 286 |
+ fatal("cannot change directory for %s to write pid file", dir); |
| 287 |
+ } |
| 288 |
+} |