diff options
author | pjd <pjd@FreeBSD.org> | 2006-01-30 22:50:13 +0000 |
---|---|---|
committer | pjd <pjd@FreeBSD.org> | 2006-01-30 22:50:13 +0000 |
commit | f9836a378dd449473e8e5a10774d2be75247f682 (patch) | |
tree | 7e2a62d8062adb9592f5134d9b3a50b27a438bed /sbin | |
parent | 74978a10e17d9d7c790f0696d2811484358de145 (diff) | |
download | FreeBSD-src-f9836a378dd449473e8e5a10774d2be75247f682.zip FreeBSD-src-f9836a378dd449473e8e5a10774d2be75247f682.tar.gz |
Use pidfile(3).
OK'ed by: imp
Diffstat (limited to 'sbin')
-rw-r--r-- | sbin/devd/Makefile | 4 | ||||
-rw-r--r-- | sbin/devd/devd.cc | 39 | ||||
-rw-r--r-- | sbin/devd/devd.hh | 4 |
3 files changed, 35 insertions, 12 deletions
diff --git a/sbin/devd/Makefile b/sbin/devd/Makefile index 3514b1b..8f52d90 100644 --- a/sbin/devd/Makefile +++ b/sbin/devd/Makefile @@ -7,8 +7,8 @@ WARNS?= 4 NO_SHARED?=YES -DPADD= ${LIBL} -LDADD= -ll +DPADD= ${LIBL} ${LIBUTIL} +LDADD= -ll -lutil YFLAGS+=-v CFLAGS+=-I. -I${.CURDIR} diff --git a/sbin/devd/devd.cc b/sbin/devd/devd.cc index 47a2257..3dadde4 100644 --- a/sbin/devd/devd.cc +++ b/sbin/devd/devd.cc @@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$"); #include <errno.h> #include <err.h> #include <fcntl.h> +#include <libutil.h> #include <regex.h> #include <signal.h> #include <stdlib.h> @@ -78,6 +79,8 @@ static const char nomatch = '?'; static const char attach = '+'; static const char detach = '-'; +static struct pidfh *pfh; + int Dflag; int dflag; int nflag; @@ -365,17 +368,32 @@ config::parse(void) } void -config::drop_pidfile() +config::open_pidfile() { - FILE *fp; + pid_t otherpid; if (_pidfile == "") return; - fp = fopen(_pidfile.c_str(), "w"); - if (fp == NULL) - return; - fprintf(fp, "%d\n", getpid()); - fclose(fp); + pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid); + if (pfh == NULL) { + if (errno == EEXIST) + errx(1, "devd already running, pid: %d", (int)otherpid); + warn("cannot open pid file"); + } +} + +void +config::write_pidfile() +{ + + pidfile_write(pfh); +} + +void +config::remove_pidfile() +{ + + pidfile_remove(pfh); } void @@ -749,8 +767,10 @@ event_loop(void) if (rv == 0) { if (Dflag) fprintf(stderr, "Calling daemon\n"); + cfg.remove_pidfile(); + cfg.open_pidfile(); daemon(0, 0); - cfg.drop_pidfile(); + cfg.write_pidfile(); once++; } } @@ -931,8 +951,9 @@ main(int argc, char **argv) cfg.parse(); if (!dflag && nflag) { + cfg.open_pidfile(); daemon(0, 0); - cfg.drop_pidfile(); + cfg.write_pidfile(); } signal(SIGPIPE, SIG_IGN); signal(SIGHUP, gensighand); diff --git a/sbin/devd/devd.hh b/sbin/devd/devd.hh index 55af222..a1ee9cd 100644 --- a/sbin/devd/devd.hh +++ b/sbin/devd/devd.hh @@ -153,7 +153,9 @@ public: void set_pidfile(const char *); void reset(); void parse(); - void drop_pidfile(); + void open_pidfile(); + void write_pidfile(); + void remove_pidfile(); void push_var_table(); void pop_var_table(); void set_variable(const char *var, const char *val); |