Use pidfile(3).

OK'ed by:	imp
This commit is contained in:
Pawel Jakub Dawidek 2006-01-30 22:50:13 +00:00
parent aaf8e1867b
commit 1a0cc6b19e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=155073
3 changed files with 35 additions and 12 deletions

View File

@ -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}

View File

@ -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);

View File

@ -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);