-/* $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
struct apm_command command;
struct apm_reply reply;
char *msg;
+ int ret;
switch (action) {
case NONE:
}
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
default:
break;
}
+ if (reply.error)
+ errx(1, "%s: %s", apm_state(reply.newstate), strerror(reply.error));
return (0);
}
-/* $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
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);
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;
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
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;