From 84148fd1daa61b8f9e18e53047e826ba7e958322 Mon Sep 17 00:00:00 2001 From: Pawel Jakub Dawidek Date: Wed, 24 Aug 2005 17:32:41 +0000 Subject: [PATCH] Add '-P' option which allows to specify pidfile. --- usr.sbin/powerd/Makefile | 3 +++ usr.sbin/powerd/powerd.8 | 5 +++++ usr.sbin/powerd/powerd.c | 35 ++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/usr.sbin/powerd/Makefile b/usr.sbin/powerd/Makefile index 6a4aaf84fd73..207e728eb7ec 100644 --- a/usr.sbin/powerd/Makefile +++ b/usr.sbin/powerd/Makefile @@ -4,4 +4,7 @@ PROG= powerd MAN= powerd.8 WARNS?= 6 +DPADD= ${LIBUTIL} +LDADD= -lutil + .include diff --git a/usr.sbin/powerd/powerd.8 b/usr.sbin/powerd/powerd.8 index 57bf801de765..755bf730c2db 100644 --- a/usr.sbin/powerd/powerd.8 +++ b/usr.sbin/powerd/powerd.8 @@ -37,6 +37,7 @@ .Op Fl i Ar percent .Op Fl n Ar mode .Op Fl p Ar ival +.Op Fl P Ar pidfile .Op Fl r Ar percent .Op Fl v .Sh DESCRIPTION @@ -82,6 +83,10 @@ to use normally when the AC line state is unknown. Specifies a different polling interval (in milliseconds) for AC line state and system idle levels. The default is 500 ms. +.It Fl P +Specify an alternative file in which to store the process ID. +The default is +.Pa /var/run/powerd.pid . .It Fl r Ar percent Specifies the CPU idle percent level where adaptive diff --git a/usr.sbin/powerd/powerd.c b/usr.sbin/powerd/powerd.c index 80547d544ab5..90b89da1c9e4 100644 --- a/usr.sbin/powerd/powerd.c +++ b/usr.sbin/powerd/powerd.c @@ -28,9 +28,15 @@ #include __FBSDID("$FreeBSD$"); +#include +#include +#include +#include + #include #include #include +#include #include #include #include @@ -41,10 +47,6 @@ __FBSDID("$FreeBSD$"); #include #endif -#include -#include -#include - #define DEFAULT_ACTIVE_PERCENT 65 #define DEFAULT_IDLE_PERCENT 90 #define DEFAULT_POLL_INTERVAL 500 /* Poll interval in milliseconds */ @@ -247,13 +249,15 @@ usage(void) { fprintf(stderr, -"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%]\n"); +"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%] [-P pidfile]\n"); exit(1); } int main(int argc, char * argv[]) { + struct pidfh *pfh; + const char *pidfile = NULL; long idle, total; int curfreq, *freqs, i, *mwatts, numfreqs; int ch, mode_ac, mode_battery, mode_none, acline, mode, vflag; @@ -273,7 +277,7 @@ main(int argc, char * argv[]) if (geteuid() != 0) errx(1, "must be root to run"); - while ((ch = getopt(argc, argv, "a:b:i:n:p:r:v")) != EOF) + while ((ch = getopt(argc, argv, "a:b:i:n:p:P:r:v")) != EOF) switch (ch) { case 'a': parse_mode(optarg, &mode_ac, ch); @@ -299,6 +303,9 @@ main(int argc, char * argv[]) usage(); } break; + case 'P': + pidfile = optarg; + break; case 'r': cpu_running_mark = atoi(optarg); if (cpu_running_mark < 0 || cpu_running_mark > 100) { @@ -338,8 +345,20 @@ main(int argc, char * argv[]) acline_init(); /* Run in the background unless in verbose mode. */ - if (!vflag) + if (!vflag) { + pid_t otherpid; + + pfh = pidfile_open(pidfile, 0600, &otherpid); + if (pfh == NULL) { + if (errno == EEXIST) { + errx(1, "powerd already running, pid: %d", + otherpid); + } + warn("cannot open pid file"); + } daemon(0, 0); + pidfile_write(pfh); + } signal(SIGINT, handle_sigs); signal(SIGTERM, handle_sigs); @@ -460,6 +479,8 @@ main(int argc, char * argv[]) } free(freqs); free(mwatts); + if (!vflag) + pidfile_remove(pfh); exit(0); }