summaryrefslogtreecommitdiffstats
path: root/usr.sbin/daemon
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2005-08-24 17:24:39 +0000
committerpjd <pjd@FreeBSD.org>2005-08-24 17:24:39 +0000
commite8bb52f26ca47a74c30d88599fd1eb6a1a50cb92 (patch)
tree006038d720b64ac39eb558a6d8673d6d14abbd2c /usr.sbin/daemon
parenta5fe3401b9f1cc44c8011e8aa5cdbbe2637084e4 (diff)
downloadFreeBSD-src-e8bb52f26ca47a74c30d88599fd1eb6a1a50cb92.zip
FreeBSD-src-e8bb52f26ca47a74c30d88599fd1eb6a1a50cb92.tar.gz
Teach daemon(8) how to use pidfile(3).
Diffstat (limited to 'usr.sbin/daemon')
-rw-r--r--usr.sbin/daemon/Makefile3
-rw-r--r--usr.sbin/daemon/daemon.89
-rw-r--r--usr.sbin/daemon/daemon.c23
3 files changed, 24 insertions, 11 deletions
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 <bsd.prog.mk>
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 <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]);
OpenPOWER on IntegriCloud