summaryrefslogtreecommitdiff
path: root/usr.sbin/apmd
diff options
context:
space:
mode:
Diffstat (limited to 'usr.sbin/apmd')
-rw-r--r--usr.sbin/apmd/apmd.89
-rw-r--r--usr.sbin/apmd/apmd.c74
2 files changed, 48 insertions, 35 deletions
diff --git a/usr.sbin/apmd/apmd.8 b/usr.sbin/apmd/apmd.8
index 15466634fda..84672afbc35 100644
--- a/usr.sbin/apmd/apmd.8
+++ b/usr.sbin/apmd/apmd.8
@@ -1,4 +1,4 @@
-.\" $OpenBSD: apmd.8,v 1.49 2017/08/29 06:13:20 jmc Exp $
+.\" $OpenBSD: apmd.8,v 1.50 2018/12/04 18:00:57 tedu Exp $
.\"
.\" Copyright (c) 1995 John T. Kohl
.\" All rights reserved.
@@ -26,7 +26,7 @@
.\" ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd $Mdocdate: August 29 2017 $
+.Dd $Mdocdate: December 4 2018 $
.Dt APMD 8
.Os
.Sh NAME
@@ -77,9 +77,8 @@ ignored if the system is connected to line current and not running from
batteries (user requests are still honored).
.It Fl d
.Nm
-enters debug mode, logging to facility
-.Dv LOG_LOCAL1 ,
-and stays in the foreground on the controlling terminal.
+enters debug mode, staying in the foreground.
+Logging output is printed to stdout.
.It Fl f Ar devname
Specify an alternate device file name,
.Ar devname .
diff --git a/usr.sbin/apmd/apmd.c b/usr.sbin/apmd/apmd.c
index 78e1685163f..8254b791a21 100644
--- a/usr.sbin/apmd/apmd.c
+++ b/usr.sbin/apmd/apmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: apmd.c,v 1.83 2018/12/01 23:35:59 kn Exp $ */
+/* $OpenBSD: apmd.c,v 1.84 2018/12/04 18:00:57 tedu Exp $ */
/*
* Copyright (c) 1995, 1996 John T. Kohl
@@ -38,6 +38,7 @@
#include <sys/time.h>
#include <sys/sched.h>
#include <sys/sysctl.h>
+#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <fcntl.h>
@@ -92,6 +93,21 @@ sigexit(int signo)
}
void
+logmsg(int prio, const char *msg, ...)
+{
+ va_list ap;
+
+ va_start(ap, msg);
+ if (debug) {
+ vfprintf(stdout, msg, ap);
+ fprintf(stdout, "\n");
+ } else {
+ vsyslog(prio, msg, ap);
+ }
+ va_end(ap);
+}
+
+void
usage(void)
{
fprintf(stderr,
@@ -123,7 +139,8 @@ void
set_driver_messages(int fd, int mode)
{
if (ioctl(fd, APM_IOC_PRN_CTL, &mode) == -1)
- syslog(LOG_DEBUG, "can't disable driver messages, error: %m");
+ logmsg(LOG_DEBUG, "can't disable driver messages, error: %s",
+ strerror(errno));
}
int
@@ -168,7 +185,7 @@ power_status(int fd, int force, struct apm_power_info *pinfo)
#else
if ((int)bstate.minutes_left > 0)
#endif
- syslog(LOG_NOTICE, "battery status: %s. "
+ logmsg(LOG_NOTICE, "battery status: %s. "
"external power status: %s. "
"estimated battery life %d%% (%u minutes)",
battstate(bstate.battery_state),
@@ -176,7 +193,7 @@ power_status(int fd, int force, struct apm_power_info *pinfo)
bstate.battery_life,
bstate.minutes_left);
else
- syslog(LOG_NOTICE, "battery status: %s. "
+ logmsg(LOG_NOTICE, "battery status: %s. "
"external power status: %s. "
"estimated battery life %d%%",
battstate(bstate.battery_state),
@@ -187,7 +204,7 @@ power_status(int fd, int force, struct apm_power_info *pinfo)
if (pinfo)
*pinfo = bstate;
} else
- syslog(LOG_ERR, "cannot fetch power status: %m");
+ logmsg(LOG_ERR, "cannot fetch power status: %m");
return acon;
}
@@ -248,13 +265,13 @@ handle_client(int sock_fd, int ctl_fd)
fromlen = sizeof(from);
cli_fd = accept(sock_fd, (struct sockaddr *)&from, &fromlen);
if (cli_fd == -1) {
- syslog(LOG_INFO, "client accept failure: %m");
+ logmsg(LOG_INFO, "client accept failure: %s", strerror(errno));
return NORMAL;
}
if (recv(cli_fd, &cmd, sizeof(cmd), 0) != sizeof(cmd)) {
(void) close(cli_fd);
- syslog(LOG_INFO, "client size botch");
+ logmsg(LOG_INFO, "client size botch");
return NORMAL;
}
@@ -278,20 +295,20 @@ handle_client(int sock_fd, int ctl_fd)
case SETPERF_LOW:
doperf = PERF_MANUAL;
reply.newstate = NORMAL;
- syslog(LOG_NOTICE, "setting hw.perfpolicy to low");
+ logmsg(LOG_NOTICE, "setting hw.perfpolicy to low");
setperfpolicy("low");
break;
case SETPERF_HIGH:
doperf = PERF_MANUAL;
reply.newstate = NORMAL;
- syslog(LOG_NOTICE, "setting hw.perfpolicy to high");
+ logmsg(LOG_NOTICE, "setting hw.perfpolicy to high");
setperfpolicy("high");
break;
case SETPERF_AUTO:
case SETPERF_COOL:
doperf = PERF_AUTO;
reply.newstate = NORMAL;
- syslog(LOG_NOTICE, "setting hw.perfpolicy to auto");
+ logmsg(LOG_NOTICE, "setting hw.perfpolicy to auto");
setperfpolicy("auto");
break;
default:
@@ -300,13 +317,13 @@ handle_client(int sock_fd, int ctl_fd)
}
if (sysctl(cpuspeed_mib, 2, &cpuspeed, &cpuspeed_sz, NULL, 0) < 0)
- syslog(LOG_INFO, "cannot read hw.cpuspeed");
+ logmsg(LOG_INFO, "cannot read hw.cpuspeed");
reply.cpuspeed = cpuspeed;
reply.perfmode = doperf;
reply.vno = APMD_VNO;
if (send(cli_fd, &reply, sizeof(reply), 0) != sizeof(reply))
- syslog(LOG_INFO, "reply to client botched");
+ logmsg(LOG_INFO, "reply to client botched");
close(cli_fd);
return reply.newstate;
@@ -315,7 +332,7 @@ handle_client(int sock_fd, int ctl_fd)
void
suspend(int ctl_fd)
{
- syslog(LOG_NOTICE, "system suspending");
+ logmsg(LOG_NOTICE, "system suspending");
power_status(ctl_fd, 1, NULL);
do_etc_file(_PATH_APM_ETC_SUSPEND);
sync();
@@ -326,7 +343,7 @@ suspend(int ctl_fd)
void
stand_by(int ctl_fd)
{
- syslog(LOG_NOTICE, "system entering standby");
+ logmsg(LOG_NOTICE, "system entering standby");
power_status(ctl_fd, 1, NULL);
do_etc_file(_PATH_APM_ETC_STANDBY);
sync();
@@ -337,7 +354,7 @@ stand_by(int ctl_fd)
void
hibernate(int ctl_fd)
{
- syslog(LOG_NOTICE, "system hibernating");
+ logmsg(LOG_NOTICE, "system hibernating");
power_status(ctl_fd, 1, NULL);
do_etc_file(_PATH_APM_ETC_HIBERNATE);
sync();
@@ -349,8 +366,7 @@ void
resumed(int ctl_fd)
{
do_etc_file(_PATH_APM_ETC_RESUME);
- syslog(LOG_NOTICE,
- "system resumed from sleep");
+ logmsg(LOG_NOTICE, "system resumed from sleep");
power_status(ctl_fd, 1, NULL);
}
@@ -447,9 +463,7 @@ main(int argc, char *argv[])
if (doperf == PERF_NONE)
doperf = PERF_MANUAL;
- if (debug)
- openlog(__progname, LOG_CONS, LOG_LOCAL1);
- else {
+ if (debug == 0) {
if (daemon(0, 0) < 0)
error("failed to daemonize", NULL);
openlog(__progname, LOG_CONS, LOG_DAEMON);
@@ -514,7 +528,7 @@ main(int argc, char *argv[])
if (!powerstatus && autoaction &&
autolimit > (int)pinfo.battery_life) {
- syslog(LOG_NOTICE,
+ logmsg(LOG_NOTICE,
"estimated battery life %d%%, "
"autoaction limit set to %d%% .",
pinfo.battery_life,
@@ -533,7 +547,7 @@ main(int argc, char *argv[])
if (ev->ident == ctl_fd) {
suspends = standbys = hibernates = resumes = 0;
- syslog(LOG_DEBUG, "apmevent %04x index %d",
+ logmsg(LOG_DEBUG, "apmevent %04x index %d",
(int)APM_EVENT_TYPE(ev->data),
(int)APM_EVENT_INDEX(ev->data));
@@ -579,7 +593,7 @@ main(int argc, char *argv[])
if ((standbys || suspends) && noacsleep &&
power_status(ctl_fd, 0, &pinfo))
- syslog(LOG_DEBUG, "no! sleep! till brooklyn!");
+ logmsg(LOG_DEBUG, "no! sleep! till brooklyn!");
else if (suspends)
suspend(ctl_fd);
else if (standbys)
@@ -632,7 +646,7 @@ setperfpolicy(char *policy)
}
if (sysctl(hw_perfpol_mib, 2, oldpolicy, &oldsz, policy, strlen(policy) + 1) < 0)
- syslog(LOG_INFO, "cannot set hw.perfpolicy");
+ logmsg(LOG_INFO, "cannot set hw.perfpolicy");
if (setlo == 1) {
int hw_perf_mib[] = {CTL_HW, HW_SETPERF};
@@ -640,7 +654,7 @@ setperfpolicy(char *policy)
int new_perf = 0;
size_t perf_sz = sizeof(perf);
if (sysctl(hw_perf_mib, 2, &perf, &perf_sz, &new_perf, perf_sz) < 0)
- syslog(LOG_INFO, "cannot set hw.setperf");
+ logmsg(LOG_INFO, "cannot set hw.setperf");
}
}
@@ -653,7 +667,7 @@ do_etc_file(const char *file)
/* If file doesn't exist, do nothing. */
if (access(file, X_OK|R_OK)) {
- syslog(LOG_DEBUG, "do_etc_file(): cannot access file %s", file);
+ logmsg(LOG_DEBUG, "do_etc_file(): cannot access file %s", file);
return;
}
@@ -666,21 +680,21 @@ do_etc_file(const char *file)
pid = fork();
switch (pid) {
case -1:
- syslog(LOG_ERR, "failed to fork(): %m");
+ logmsg(LOG_ERR, "failed to fork(): %s", strerror(errno));
return;
case 0:
/* We are the child. */
execl(file, prog, (char *)NULL);
- syslog(LOG_ERR, "failed to exec %s: %m", file);
+ logmsg(LOG_ERR, "failed to exec %s: %m", file, strerror(errno));
_exit(1);
/* NOTREACHED */
default:
/* We are the parent. */
wait4(pid, &status, 0, 0);
if (WIFEXITED(status))
- syslog(LOG_DEBUG, "%s exited with status %d", file,
+ logmsg(LOG_DEBUG, "%s exited with status %d", file,
WEXITSTATUS(status));
else
- syslog(LOG_ERR, "%s exited abnormally.", file);
+ logmsg(LOG_ERR, "%s exited abnormally.", file);
}
}