diff options
Diffstat (limited to 'sbin/devd')
-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); |