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
This commit is contained in:
Yaroslav Tykhiy 2006-01-21 12:21:41 +00:00
parent 9c013503bb
commit 125b96351a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=154630
2 changed files with 25 additions and 30 deletions

View File

@ -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

View File

@ -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);
@ -461,32 +475,6 @@ main(int argc, char *argv[], char **envp)
maxfd = ctl_sock[i];
}
/*
* 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);