From c7cff24ee1708a5e760cedcf0bc20937627414d9 Mon Sep 17 00:00:00 2001 From: kn Date: Tue, 6 Apr 2021 20:30:32 +0000 Subject: [PATCH] Make apm(8) report apmd(8) failure apm(8) never got the result of the requested power action carried out by apmd(8), so apm(4) errors got silently discarded; for example, zzz(8) would merely print "Suspending system..." and exit zero on platforms lacking suspend/resume support. Enrich reply messages from apmd to apm with an error field containing the failed ioctl(2)'s errno if need be. Hoist apmd's power action dispatch into handle_client() so it can write the error in the first place before replying. OK dv --- usr.sbin/apm/apm.c | 10 +++++-- usr.sbin/apmd/apm-proto.h | 4 ++- usr.sbin/apmd/apmd.c | 56 ++++++++++++++++++++++----------------- usr.sbin/apmd/apmsubr.c | 19 ++++++++++++- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/usr.sbin/apm/apm.c b/usr.sbin/apm/apm.c index c91b8da9ba9..f8962969795 100644 --- a/usr.sbin/apm/apm.c +++ b/usr.sbin/apm/apm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: apm.c,v 1.37 2020/09/23 05:50:26 jca Exp $ */ +/* $OpenBSD: apm.c,v 1.38 2021/04/06 20:30:32 kn Exp $ */ /* * Copyright (c) 1996 John T. Kohl @@ -97,6 +97,7 @@ do_zzz(int fd, enum apm_action action) struct apm_command command; struct apm_reply reply; char *msg; + int ret; switch (action) { case NONE: @@ -117,7 +118,10 @@ do_zzz(int fd, enum apm_action action) } printf("%s...\n", msg); - exit(send_command(fd, &command, &reply)); + ret = send_command(fd, &command, &reply); + if (reply.error) + errx(1, "%s: %s", apm_state(reply.newstate), strerror(reply.error)); + exit(ret); } static int @@ -418,5 +422,7 @@ balony: default: break; } + if (reply.error) + errx(1, "%s: %s", apm_state(reply.newstate), strerror(reply.error)); return (0); } diff --git a/usr.sbin/apmd/apm-proto.h b/usr.sbin/apmd/apm-proto.h index 4d54947fe85..dd8cb56991a 100644 --- a/usr.sbin/apmd/apm-proto.h +++ b/usr.sbin/apmd/apm-proto.h @@ -1,4 +1,4 @@ -/* $OpenBSD: apm-proto.h,v 1.10 2020/09/23 05:50:26 jca Exp $ */ +/* $OpenBSD: apm-proto.h,v 1.11 2021/04/06 20:30:32 kn Exp $ */ /* * Copyright (c) 1996 John T. Kohl @@ -64,6 +64,7 @@ struct apm_reply { enum apm_perfmode perfmode; int cpuspeed; struct apm_power_info batterystate; + int error; }; #define APMD_VNO 3 @@ -71,3 +72,4 @@ struct apm_reply { extern const char *battstate(int state); extern const char *ac_state(int state); extern const char *perf_mode(int mode); +extern const char *apm_state(int apm_state); diff --git a/usr.sbin/apmd/apmd.c b/usr.sbin/apmd/apmd.c index 58026f7a72b..a17dc717dd4 100644 --- a/usr.sbin/apmd/apmd.c +++ b/usr.sbin/apmd/apmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: apmd.c,v 1.102 2021/03/25 20:46:55 kn Exp $ */ +/* $OpenBSD: apmd.c,v 1.103 2021/04/06 20:30:32 kn Exp $ */ /* * Copyright (c) 1995, 1996 John T. Kohl @@ -65,9 +65,9 @@ void usage(void); int power_status(int fd, int force, struct apm_power_info *pinfo); int bind_socket(const char *sn); enum apm_state handle_client(int sock_fd, int ctl_fd); -void suspend(int ctl_fd); -void stand_by(int ctl_fd); -void hibernate(int ctl_fd); +int suspend(int ctl_fd); +int stand_by(int ctl_fd); +int hibernate(int ctl_fd); void resumed(int ctl_fd); void setperfpolicy(char *policy); void sigexit(int signo); @@ -266,16 +266,23 @@ handle_client(int sock_fd, int ctl_fd) return NORMAL; } + bzero(&reply, sizeof(reply)); power_status(ctl_fd, 0, &reply.batterystate); switch (cmd.action) { case SUSPEND: reply.newstate = SUSPENDING; + if (suspend(ctl_fd) == -1) + reply.error = errno; break; case STANDBY: reply.newstate = STANDING_BY; + if (stand_by(ctl_fd) == -1) + reply.error = errno; break; case HIBERNATE: reply.newstate = HIBERNATING; + if (hibernate(ctl_fd) == -1) + reply.error = errno; break; case SETPERF_LOW: reply.newstate = NORMAL; @@ -321,40 +328,49 @@ handle_client(int sock_fd, int ctl_fd) return reply.newstate; } -void +int suspend(int ctl_fd) { + int ret; + logmsg(LOG_NOTICE, "system suspending"); power_status(ctl_fd, 1, NULL); do_etc_file(_PATH_APM_ETC_SUSPEND); sync(); sleep(1); - if (ioctl(ctl_fd, APM_IOC_SUSPEND, 0) == -1) + if ((ret = ioctl(ctl_fd, APM_IOC_SUSPEND, 0)) == -1) logmsg(LOG_WARNING, "%s: %s", __func__, strerror(errno)); + return (ret); } -void +int stand_by(int ctl_fd) { + int ret; + logmsg(LOG_NOTICE, "system entering standby"); power_status(ctl_fd, 1, NULL); do_etc_file(_PATH_APM_ETC_STANDBY); sync(); sleep(1); - if (ioctl(ctl_fd, APM_IOC_STANDBY, 0) == -1) + if ((ret = ioctl(ctl_fd, APM_IOC_STANDBY, 0)) == -1) logmsg(LOG_WARNING, "%s: %s", __func__, strerror(errno)); + return (ret); } -void +int hibernate(int ctl_fd) { + int ret; + logmsg(LOG_NOTICE, "system hibernating"); power_status(ctl_fd, 1, NULL); do_etc_file(_PATH_APM_ETC_HIBERNATE); sync(); sleep(1); - if (ioctl(ctl_fd, APM_IOC_HIBERNATE, 0) == -1) + if ((ret = ioctl(ctl_fd, APM_IOC_HIBERNATE, 0)) == -1) logmsg(LOG_WARNING, "%s: %s", __func__, strerror(errno)); + return (ret); } void @@ -512,20 +528,12 @@ main(int argc, char *argv[]) break; if (rv == 1 && ev->ident == sock_fd) { - switch (handle_client(sock_fd, ctl_fd)) { - case NORMAL: - break; - case SUSPENDING: - suspend(ctl_fd); - break; - case STANDING_BY: - stand_by(ctl_fd); - break; - case HIBERNATING: - hibernate(ctl_fd); - break; - } - continue; + int state; + + if ((state = handle_client(sock_fd, ctl_fd)) == -1) + logmsg(LOG_WARNING, "%s: %s", apm_state(state), strerror(errno)); + else + continue; } suspends = standbys = hibernates = resumes = 0; diff --git a/usr.sbin/apmd/apmsubr.c b/usr.sbin/apmd/apmsubr.c index 9a8232e498f..63b72da1406 100644 --- a/usr.sbin/apmd/apmsubr.c +++ b/usr.sbin/apmd/apmsubr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: apmsubr.c,v 1.9 2020/09/23 05:50:26 jca Exp $ */ +/* $OpenBSD: apmsubr.c,v 1.10 2021/04/06 20:30:32 kn Exp $ */ /* * Copyright (c) 1995,1996 John T. Kohl @@ -83,3 +83,20 @@ perf_mode(int mode) return "invalid"; } } + +const char * +apm_state(int apm_state) +{ + switch (apm_state) { + case NORMAL: + return "normal"; + case SUSPENDING: + return "suspend"; + case STANDING_BY: + return "standby"; + case HIBERNATING: + return "hibenate"; + default: + return "unknown"; +} +} -- 2.20.1