diff options
author | yar <yar@FreeBSD.org> | 2006-01-21 12:21:41 +0000 |
---|---|---|
committer | yar <yar@FreeBSD.org> | 2006-01-21 12:21:41 +0000 |
commit | c86031149b98392f2ac755ec253d69e9de5a32d1 (patch) | |
tree | 5281ddf179ebc9c48629a8843ff9024a9887f171 | |
parent | f9cd8b5d9fa9549bd689754887b1ff6e550bcd37 (diff) | |
download | FreeBSD-src-c86031149b98392f2ac755ec253d69e9de5a32d1.zip FreeBSD-src-c86031149b98392f2ac755ec253d69e9de5a32d1.tar.gz |
Use pidfile(3) provided by libutil to manage the deamon's pid file.
By default, create a pid file at the standard location, /var/run/ftpd.pid,
in accord with the expected behavior of a stock system daemon.
MFC after: 5 days
-rw-r--r-- | libexec/ftpd/ftpd.8 | 8 | ||||
-rw-r--r-- | libexec/ftpd/ftpd.c | 47 |
2 files changed, 25 insertions, 30 deletions
diff --git a/libexec/ftpd/ftpd.8 b/libexec/ftpd/ftpd.8 index aed7d5d..7d62e6b 100644 --- a/libexec/ftpd/ftpd.8 +++ b/libexec/ftpd/ftpd.8 @@ -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..9590f78 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. */ @@ -517,8 +505,11 @@ main(int argc, char *argv[], char **envp) close(fd); } } - if (pid == 0) + if (pid == 0) { + if (pfh != NULL) + pidfile_close(pfh); break; + } } } else { addrlen = sizeof(his_addr); |