diff options
author | yar <yar@FreeBSD.org> | 2006-01-29 13:21:05 +0000 |
---|---|---|
committer | yar <yar@FreeBSD.org> | 2006-01-29 13:21:05 +0000 |
commit | 5972439952ac140625925d340da3804beb9490c8 (patch) | |
tree | 5a87ce42a0ecd763e5ebde5ff469caec76dbd452 /libexec | |
parent | 6b09b896962fd7d5e95b64140fdff54770b96b3d (diff) | |
download | FreeBSD-src-5972439952ac140625925d340da3804beb9490c8.zip FreeBSD-src-5972439952ac140625925d340da3804beb9490c8.tar.gz |
MFC recent ftpd fixes related to handling the pidfile and unneeded
open sockets when in stand-alone daemon mode:
ftpd.c 1.207-1.209
ftpd.8 1.70-1.71
Diffstat (limited to 'libexec')
-rw-r--r-- | libexec/ftpd/ftpd.8 | 10 | ||||
-rw-r--r-- | libexec/ftpd/ftpd.c | 72 |
2 files changed, 43 insertions, 39 deletions
diff --git a/libexec/ftpd/ftpd.8 b/libexec/ftpd/ftpd.8 index aed7d5d..7f8a3ca 100644 --- a/libexec/ftpd/ftpd.8 +++ b/libexec/ftpd/ftpd.8 @@ -32,7 +32,7 @@ .\" @(#)ftpd.8 8.2 (Berkeley) 4/19/94 .\" $FreeBSD$ .\" -.Dd January 27, 2000 +.Dd January 21, 2006 .Dt FTPD 8 .Os .Sh NAME @@ -144,7 +144,9 @@ port. When .Fl D is specified, write the daemon's process ID to -.Ar file . +.Ar file +instead of the default pid file, +.Pa /var/run/ftpd.pid . .It Fl R With this option set, .Nm @@ -519,7 +521,7 @@ executable need not be placed into the chrooted tree, nor need the .Pa ~/bin directory exist. .Sh FILES -.Bl -tag -width ".Pa /var/log/xferlog" -compact +.Bl -tag -width ".Pa /var/run/ftpd.pid" -compact .It Pa /etc/ftpusers List of unwelcome/restricted users. .It Pa /etc/ftpchroot @@ -530,6 +532,8 @@ Virtual hosting configuration file. Welcome notice. .It Pa /etc/ftpmotd Welcome notice after login. +.It Pa /var/run/ftpd.pid +Default pid file for daemon mode. .It Pa /var/run/nologin Displayed and access refused. .It Pa /var/log/ftpd diff --git a/libexec/ftpd/ftpd.c b/libexec/ftpd/ftpd.c index 1334757..60779b4 100644 --- a/libexec/ftpd/ftpd.c +++ b/libexec/ftpd/ftpd.c @@ -187,7 +187,7 @@ static struct opie opiedata; static char opieprompt[OPIE_CHALLENGE_MAX+1]; static int pwok; -char *pid_file = NULL; +char *pid_file = NULL; /* means default location to pidfile(3) */ /* * Limit number of pathnames that glob can return. @@ -431,6 +431,16 @@ main(int argc, char *argv[], char **envp) int *ctl_sock, fd, maxfd = -1, nfds, i; fd_set defreadfds, readfds; pid_t pid; + struct pidfh *pfh; + + if ((pfh = pidfile_open(pid_file, 0600, &pid)) == NULL) { + if (errno == EEXIST) { + syslog(LOG_ERR, "%s already running, pid %d", + getprogname(), (int)pid); + exit(1); + } + syslog(LOG_WARNING, "pidfile_open: %m"); + } /* * Detach from parent. @@ -439,6 +449,10 @@ main(int argc, char *argv[], char **envp) syslog(LOG_ERR, "failed to become a daemon"); exit(1); } + + if (pfh != NULL && pidfile_write(pfh) == -1) + syslog(LOG_WARNING, "pidfile_write: %m"); + sa.sa_handler = reapchild; (void)sigaction(SIGCHLD, &sa, NULL); @@ -462,32 +476,6 @@ main(int argc, char *argv[], char **envp) } /* - * Atomically write process ID - */ - if (pid_file) - { - int fd; - char buf[20]; - - fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC - | O_NONBLOCK | O_EXLOCK, 0644); - if (fd < 0) { - if (errno == EAGAIN) - syslog(LOG_ERR, - "%s: already locked", pid_file); - else - syslog(LOG_ERR, "%s: %m", pid_file); - exit(1); - } - snprintf(buf, sizeof(buf), - "%lu\n", (unsigned long) getpid()); - if (write(fd, buf, strlen(buf)) < 0) { - syslog(LOG_ERR, "%s: write: %m", pid_file); - exit(1); - } - /* Leave the pid file open and locked */ - } - /* * Loop forever accepting connection requests and forking off * children to handle them. */ @@ -507,18 +495,29 @@ main(int argc, char *argv[], char **envp) fd = accept(ctl_sock[i], (struct sockaddr *)&his_addr, &addrlen); - if (fd >= 0) { - if ((pid = fork()) == 0) { - /* child */ - (void) dup2(fd, 0); - (void) dup2(fd, 1); + if (fd == -1) { + syslog(LOG_WARNING, + "accept: %m"); + continue; + } + switch (pid = fork()) { + case 0: + /* child */ + (void) dup2(fd, 0); + (void) dup2(fd, 1); + (void) close(fd); + for (i = 1; i <= *ctl_sock; i++) close(ctl_sock[i]); - } else - close(fd); + if (pfh != NULL) + pidfile_close(pfh); + goto gotchild; + case -1: + syslog(LOG_WARNING, "fork: %m"); + /* FALLTHROUGH */ + default: + close(fd); } } - if (pid == 0) - break; } } else { addrlen = sizeof(his_addr); @@ -528,6 +527,7 @@ main(int argc, char *argv[], char **envp) } } +gotchild: sa.sa_handler = SIG_DFL; (void)sigaction(SIGCHLD, &sa, NULL); |