summaryrefslogtreecommitdiffstats
path: root/usr.sbin
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2005-08-24 17:32:41 +0000
committerpjd <pjd@FreeBSD.org>2005-08-24 17:32:41 +0000
commit6de559363a45c73089490b656d056d541858e5fd (patch)
tree0fc1b2496ed2613ad007996490149667ea6b705f /usr.sbin
parent276e85f997204fb14f3641230f8ce2693f5610bd (diff)
downloadFreeBSD-src-6de559363a45c73089490b656d056d541858e5fd.zip
FreeBSD-src-6de559363a45c73089490b656d056d541858e5fd.tar.gz
Add '-P' option which allows to specify pidfile.
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/powerd/Makefile3
-rw-r--r--usr.sbin/powerd/powerd.85
-rw-r--r--usr.sbin/powerd/powerd.c35
3 files changed, 36 insertions, 7 deletions
diff --git a/usr.sbin/powerd/Makefile b/usr.sbin/powerd/Makefile
index 6a4aaf8..207e728 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 <bsd.prog.mk>
diff --git a/usr.sbin/powerd/powerd.8 b/usr.sbin/powerd/powerd.8
index 57bf801..755bf730 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 80547d5..90b89da 100644
--- a/usr.sbin/powerd/powerd.c
+++ b/usr.sbin/powerd/powerd.c
@@ -28,9 +28,15 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/sysctl.h>
+#include <sys/resource.h>
+
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <libutil.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@@ -41,10 +47,6 @@ __FBSDID("$FreeBSD$");
#include <machine/apm_bios.h>
#endif
-#include <sys/ioctl.h>
-#include <sys/sysctl.h>
-#include <sys/resource.h>
-
#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);
}
OpenPOWER on IntegriCloud