diff options
author | Pawel Jakub Dawidek <pjd@FreeBSD.org> | 2005-08-24 19:28:33 +0000 |
---|---|---|
committer | Pawel Jakub Dawidek <pjd@FreeBSD.org> | 2005-08-24 19:28:33 +0000 |
commit | 0ea90af02ed1f3d76a26ab4b8866a3530be9e353 (patch) | |
tree | 28db68ffa5202c48c7e0e48ab4493e67dcc1275b | |
parent | a032b226c8fdaeeae21a2c8f8d24b1bfaf95a1da (diff) | |
download | src-0ea90af02ed1f3d76a26ab4b8866a3530be9e353.tar.gz src-0ea90af02ed1f3d76a26ab4b8866a3530be9e353.zip |
Use pidfile(3) in watchdogd(8).
Notes
Notes:
svn path=/head/; revision=149434
-rw-r--r-- | usr.sbin/watchdogd/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/watchdogd/watchdogd.c | 23 |
2 files changed, 18 insertions, 9 deletions
diff --git a/usr.sbin/watchdogd/Makefile b/usr.sbin/watchdogd/Makefile index 68675c76e154..08880e149168 100644 --- a/usr.sbin/watchdogd/Makefile +++ b/usr.sbin/watchdogd/Makefile @@ -5,8 +5,8 @@ LINKS= ${BINDIR}/watchdogd ${BINDIR}/watchdog MAN= watchdogd.8 watchdog.8 WARNS?= 6 -LDADD= -lm -DPADD= ${LIBM} +LDADD= -lm -lutil +DPADD= ${LIBM} ${LIBUTIL} .include <bsd.prog.mk> diff --git a/usr.sbin/watchdogd/watchdogd.c b/usr.sbin/watchdogd/watchdogd.c index 321fc5db79a8..d8ab3615e835 100644 --- a/usr.sbin/watchdogd/watchdogd.c +++ b/usr.sbin/watchdogd/watchdogd.c @@ -31,6 +31,7 @@ #include <sys/types.h> __FBSDID("$FreeBSD$"); +#include <sys/param.h> #include <sys/rtprio.h> #include <sys/stat.h> #include <sys/time.h> @@ -39,6 +40,7 @@ __FBSDID("$FreeBSD$"); #include <err.h> #include <errno.h> #include <fcntl.h> +#include <libutil.h> #include <math.h> #include <paths.h> #include <signal.h> @@ -75,7 +77,8 @@ int main(int argc, char *argv[]) { struct rtprio rtp; - FILE *fp; + struct pidfh *pfh; + pid_t otherpid; if (getuid() != 0) errx(EX_SOFTWARE, "not super user"); @@ -94,8 +97,18 @@ main(int argc, char *argv[]) if (watchdog_onoff(1) == -1) exit(EX_SOFTWARE); + pfh = pidfile_open(pidfile, 0644, &otherpid); + if (pfh == NULL) { + if (errno == EEXIST) { + errx(EX_SOFTWARE, "%s already running, pid: %d", + getprogname(), otherpid); + } + warn("Cannot open or create pidfile"); + } + if (debugging == 0 && daemon(0, 0) == -1) { watchdog_onoff(0); + pidfile_remove(pfh); err(EX_OSERR, "daemon"); } @@ -103,17 +116,13 @@ main(int argc, char *argv[]) signal(SIGINT, sighandler); signal(SIGTERM, sighandler); - fp = fopen(pidfile, "w"); - if (fp != NULL) { - fprintf(fp, "%d\n", getpid()); - fclose(fp); - } + pidfile_write(pfh); watchdog_loop(); /* exiting */ watchdog_onoff(0); - unlink(pidfile); + pidfile_remove(pfh); return (EX_OK); } else { if (passive) |