Developers who shall remain anonymous were confused by the behavior of apmd -d
because the behavior of apmd -d is confusing. It doesn't do anything like any
other daemon in the system when running with -d.

This introduces a logmsg function to replace syslog, which will either syslog
or printf depending on debug. I think this makes debugging much easier.


Index: apmd.c
===================================================================
RCS file: /cvs/src/usr.sbin/apmd/apmd.c,v
retrieving revision 1.82
diff -u -p -r1.82 apmd.c
--- apmd.c      30 Nov 2018 18:05:31 -0000      1.82
+++ apmd.c      30 Nov 2018 18:21:02 -0000
@@ -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 a
 #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 a
                                    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 a
                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);
 }
 
@@ -446,9 +462,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);
@@ -513,7 +527,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,
@@ -532,7 +546,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));
 
@@ -578,7 +592,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)
@@ -631,7 +645,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};
@@ -639,7 +653,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");
        }
 }
 
@@ -652,7 +666,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;
        }
 
@@ -665,21 +679,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);
        }
 }

Reply via email to