1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
Description: Patch which fix bug related to pid file creation
Author: Emmanuel Bouthenot <kolter@debian.org>
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 <sys/types.h>
-#endif
-
#include <stdio.h>
-
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h>
#include <string.h>
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
+#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
@@ -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 <sys/types.h>
-#endif
-#ifdef HAVE_SYS_STAT_H
-# include <sys/stat.h>
-#endif
-#ifdef HAVE_FCNTL_H
-# include <fcntl.h>
-#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);
+ }
+}
|