summaryrefslogtreecommitdiffstats
path: root/usr.sbin/daemon/daemon.c
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/daemon.c
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/daemon.c')
-rw-r--r--usr.sbin/daemon/daemon.c23
1 files changed, 14 insertions, 9 deletions
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