From e8bb52f26ca47a74c30d88599fd1eb6a1a50cb92 Mon Sep 17 00:00:00 2001 From: pjd Date: Wed, 24 Aug 2005 17:24:39 +0000 Subject: Teach daemon(8) how to use pidfile(3). --- usr.sbin/daemon/Makefile | 3 +++ usr.sbin/daemon/daemon.8 | 9 +++++++-- usr.sbin/daemon/daemon.c | 23 ++++++++++++++--------- 3 files changed, 24 insertions(+), 11 deletions(-) (limited to 'usr.sbin/daemon') diff --git a/usr.sbin/daemon/Makefile b/usr.sbin/daemon/Makefile index 3480393..3ca3e91 100644 --- a/usr.sbin/daemon/Makefile +++ b/usr.sbin/daemon/Makefile @@ -3,6 +3,9 @@ PROG= daemon MAN= daemon.8 +DPADD= ${LIBUTIL} +LDADD= -lutil + WARNS?= 2 .include diff --git a/usr.sbin/daemon/daemon.8 b/usr.sbin/daemon/daemon.8 index 74b5b81..d769319 100644 --- a/usr.sbin/daemon/daemon.8 +++ b/usr.sbin/daemon/daemon.8 @@ -53,7 +53,10 @@ Redirect standard input, standard output and standard error to .Pa /dev/null . .It Fl p Ar file Write the ID of the created process into the -.Ar file . +.Ar file +using +.Xr pidfile 3 +functionality. Note, that the file will be created shortly before the process is actually executed, and will remain after the process exits (although it will be removed if the execution fails). @@ -65,7 +68,8 @@ utility exits 1 if an error is returned by the .Xr daemon 3 library routine, 2 if the .Ar pidfile -is requested, but cannot be opened, +is requested, but cannot be opened, 3 if process is already running (pidfile +exists and is locked), otherwise 0. .Sh DIAGNOSTICS If the command cannot be executed, an error message is displayed on @@ -75,6 +79,7 @@ flag is specified. .Sh SEE ALSO .Xr daemon 3 , .Xr exec 3 , +.Xr pidfile 3 , .Xr termios 4 , .Xr tty 4 .Sh HISTORY diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index b30a7f4..571bc3b 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -31,10 +31,11 @@ #include __FBSDID("$FreeBSD$"); -#include +#include #include #include +#include #include #include #include @@ -44,9 +45,10 @@ static void usage(void); int main(int argc, char *argv[]) { + struct pidfh *pfh; int ch, nochdir, noclose, errcode; - FILE *pidf; const char *pidfile; + pid_t otherpid; nochdir = noclose = 1; pidfile = NULL; @@ -75,19 +77,22 @@ main(int argc, char *argv[]) * to be able to report the error intelligently */ if (pidfile) { - pidf = fopen(pidfile, "w"); - if (pidf == NULL) + pfh = pidfile_open(pidfile, 0600, &otherpid); + if (pfh == NULL) { + if (errno == EEXIST) { + errx(3, "process already running, pid: %d", + otherpid); + } err(2, "pidfile ``%s''", pidfile); + } } if (daemon(nochdir, noclose) == -1) err(1, NULL); /* Now that we are the child, write out the pid */ - if (pidfile) { - fprintf(pidf, "%lu\n", (unsigned long)getpid()); - fclose(pidf); - } + if (pidfile) + pidfile_write(pfh); execvp(argv[0], argv); @@ -97,7 +102,7 @@ main(int argc, char *argv[]) */ errcode = errno; /* Preserve errcode -- unlink may reset it */ if (pidfile) - unlink(pidfile); + pidfile_remove(pfh); /* The child is now running, so the exit status doesn't matter. */ errc(1, errcode, "%s", argv[0]); -- cgit v1.1