From 9de6caadfaaf2914c0753081337f979387c3a7db Mon Sep 17 00:00:00 2001 From: pjd Date: Wed, 24 Aug 2005 19:28:33 +0000 Subject: Use pidfile(3) in watchdogd(8). --- usr.sbin/watchdogd/Makefile | 4 ++-- 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 68675c7..08880e1 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 diff --git a/usr.sbin/watchdogd/watchdogd.c b/usr.sbin/watchdogd/watchdogd.c index 321fc5d..d8ab361 100644 --- a/usr.sbin/watchdogd/watchdogd.c +++ b/usr.sbin/watchdogd/watchdogd.c @@ -31,6 +31,7 @@ #include __FBSDID("$FreeBSD$"); +#include #include #include #include @@ -39,6 +40,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -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) -- cgit v1.1