summaryrefslogtreecommitdiffstats
path: root/usr.sbin/watchdogd
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2005-08-24 19:28:33 +0000
committerpjd <pjd@FreeBSD.org>2005-08-24 19:28:33 +0000
commit9de6caadfaaf2914c0753081337f979387c3a7db (patch)
tree28db68ffa5202c48c7e0e48ab4493e67dcc1275b /usr.sbin/watchdogd
parenta834e2610e36c1ba370a2d6b444f1916e46cf280 (diff)
downloadFreeBSD-src-9de6caadfaaf2914c0753081337f979387c3a7db.zip
FreeBSD-src-9de6caadfaaf2914c0753081337f979387c3a7db.tar.gz
Use pidfile(3) in watchdogd(8).
Diffstat (limited to 'usr.sbin/watchdogd')
-rw-r--r--usr.sbin/watchdogd/Makefile4
-rw-r--r--usr.sbin/watchdogd/watchdogd.c23
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 <bsd.prog.mk>
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 <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)
OpenPOWER on IntegriCloud