From c186fadc8760f399c3784db2d33cd961a9e3a7e9 Mon Sep 17 00:00:00 2001 From: jca Date: Sun, 18 Apr 2021 23:51:47 +0000 Subject: [PATCH] Simpler error handling for suspend()/hibernate() Save errno when we get an error so we can pass it to the apm(8) client. ok kn@ --- usr.sbin/apmd/apmd.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/usr.sbin/apmd/apmd.c b/usr.sbin/apmd/apmd.c index a6a302978e2..b4fdf482e05 100644 --- a/usr.sbin/apmd/apmd.c +++ b/usr.sbin/apmd/apmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: apmd.c,v 1.104 2021/04/06 22:12:48 jca Exp $ */ +/* $OpenBSD: apmd.c,v 1.105 2021/04/18 23:51:47 jca Exp $ */ /* * Copyright (c) 1995, 1996 John T. Kohl @@ -271,18 +271,15 @@ handle_client(int sock_fd, int ctl_fd) switch (cmd.action) { case SUSPEND: reply.newstate = SUSPENDING; - if (suspend(ctl_fd) == -1) - reply.error = errno; + reply.error = suspend(ctl_fd); break; case STANDBY: reply.newstate = STANDING_BY; - if (stand_by(ctl_fd) == -1) - reply.error = errno; + reply.error = stand_by(ctl_fd); break; case HIBERNATE: reply.newstate = HIBERNATING; - if (hibernate(ctl_fd) == -1) - reply.error = errno; + reply.error = hibernate(ctl_fd); break; case SETPERF_LOW: reply.newstate = NORMAL; @@ -329,46 +326,58 @@ handle_client(int sock_fd, int ctl_fd) int suspend(int ctl_fd) { - int ret; + int error = 0; logmsg(LOG_NOTICE, "system suspending"); power_status(ctl_fd, 1, NULL); do_etc_file(_PATH_APM_ETC_SUSPEND); sync(); sleep(1); - if ((ret = ioctl(ctl_fd, APM_IOC_SUSPEND, 0)) == -1) + + if (ioctl(ctl_fd, APM_IOC_SUSPEND, 0) == -1) { + error = errno; logmsg(LOG_WARNING, "%s: %s", __func__, strerror(errno)); - return (ret); + } + + return error; } int stand_by(int ctl_fd) { - int ret; + int error = 0; logmsg(LOG_NOTICE, "system entering standby"); power_status(ctl_fd, 1, NULL); do_etc_file(_PATH_APM_ETC_STANDBY); sync(); sleep(1); - if ((ret = ioctl(ctl_fd, APM_IOC_STANDBY, 0)) == -1) + + if (ioctl(ctl_fd, APM_IOC_STANDBY, 0) == -1) { + error = errno; logmsg(LOG_WARNING, "%s: %s", __func__, strerror(errno)); - return (ret); + } + + return error; } int hibernate(int ctl_fd) { - int ret; + int error = 0; logmsg(LOG_NOTICE, "system hibernating"); power_status(ctl_fd, 1, NULL); do_etc_file(_PATH_APM_ETC_HIBERNATE); sync(); sleep(1); - if ((ret = ioctl(ctl_fd, APM_IOC_HIBERNATE, 0)) == -1) + + if (ioctl(ctl_fd, APM_IOC_HIBERNATE, 0) == -1) { + error = errno; logmsg(LOG_WARNING, "%s: %s", __func__, strerror(errno)); - return (ret); + } + + return error; } void -- 2.20.1