diff options
author | Pawel Jakub Dawidek <pjd@FreeBSD.org> | 2005-08-24 17:24:39 +0000 |
---|---|---|
committer | Pawel Jakub Dawidek <pjd@FreeBSD.org> | 2005-08-24 17:24:39 +0000 |
commit | c6262cb601a0159637789563708f64368c30537c (patch) | |
tree | 006038d720b64ac39eb558a6d8673d6d14abbd2c /usr.sbin/daemon | |
parent | 412fa8f1145545ad30922007c6cb607d7ddb1003 (diff) | |
download | src-c6262cb601a0159637789563708f64368c30537c.tar.gz src-c6262cb601a0159637789563708f64368c30537c.zip |
Notes
Diffstat (limited to 'usr.sbin/daemon')
-rw-r--r-- | usr.sbin/daemon/Makefile | 3 | ||||
-rw-r--r-- | usr.sbin/daemon/daemon.8 | 9 | ||||
-rw-r--r-- | usr.sbin/daemon/daemon.c | 23 |
3 files changed, 24 insertions, 11 deletions
diff --git a/usr.sbin/daemon/Makefile b/usr.sbin/daemon/Makefile index 34803934d623..3ca3e91ad193 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 <bsd.prog.mk> diff --git a/usr.sbin/daemon/daemon.8 b/usr.sbin/daemon/daemon.8 index 74b5b81bc874..d7693192d475 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 b30a7f40e0b9..571bc3b94084 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -31,10 +31,11 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); -#include <sys/types.h> +#include <sys/param.h> #include <err.h> #include <errno.h> +#include <libutil.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -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]); |