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 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 __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)