mirror of
https://git.hardenedbsd.org/hardenedbsd/HardenedBSD.git
synced 2024-11-25 18:12:57 +01:00
Add '-P' option which allows to specify pidfile.
This commit is contained in:
parent
a80d5fc227
commit
84148fd1da
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=149428
@ -4,4 +4,7 @@ PROG= powerd
|
||||
MAN= powerd.8
|
||||
WARNS?= 6
|
||||
|
||||
DPADD= ${LIBUTIL}
|
||||
LDADD= -lutil
|
||||
|
||||
.include <bsd.prog.mk>
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user