Path: blob/main/ftp/ftpproxy/files/patch-src_daemon.c
16461 views
--- src/daemon.c.orig 2003-10-26 22:10:19 UTC1+++ src/daemon.c2@@ -33,6 +33,7 @@3#include <signal.h>4#include <sys/wait.h>5#include <pwd.h>6+#include <err.h>78#include <sys/types.h>9#include <sys/stat.h>10@@ -51,20 +52,35 @@1112int acceptloop(int sock)13{14- int connect, pid, len;15+ int connect, pid, otherpid;16+ socklen_t len;17struct sockaddr_in client;1819/*20* Go into background.21*/2223- if (debug != 0)24- ; /* Do not fork in debug mode */25+ pfh = NULL;2627- else if ((pid = fork()) > 0)28- exit (0);29+ if (debug == 0) {30+ pfh = pidfile_open(pidfile, 0600, &otherpid);31+ if (pfh == NULL) {32+ if (errno == EEXIST) {33+ errx(EXIT_FAILURE, "ftp.proxy already running, pid %jd.",34+ (intmax_t)otherpid);35+ }36+ warnx("Cannot open or create pid file %s", pidfile);37+ }38+ if (daemon(0, 0) == -1) {39+ warnx("Cannot daemonize");40+ pidfile_remove(pfh);41+ exit(EXIT_FAILURE);42+ }43+ pidfile_write(pfh);44+ }4546fprintf (stderr, "\nstarting ftp.proxy %s in daemon mode ...\n", VERSION);47+48while (1) {4950/*51@@ -82,7 +98,7 @@ int acceptloop(int sock)5253if ((pid = fork()) < 0) {54fprintf (stderr, "%04X: can't fork process: %s\n", getpid(), strerror(errno));55- exit (1);56+ exit(EXIT_FAILURE);57}58else if (pid == 0) {59int optlen;60@@ -112,7 +128,9 @@ int acceptloop(int sock)6162close (1);63fprintf (stderr, "%04X: terminating\n", getpid());64-65- exit (0);66+ if (pfh != NULL) {67+ pidfile_remove(pfh);68+ }69+ exit (EXIT_SUCCESS);70}71727374