Replace reboot(RB_SW_SUSPEND) w/ internal /sys/power/state API

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2022-04-16 23:59:32 +02:00
parent 01cb088e51
commit bd9bb92ca0
4 changed files with 42 additions and 4 deletions
+2 -2
View File
@@ -250,10 +250,10 @@ static int do_reboot(int cmd, char *buf, size_t len)
case INIT_CMD_SUSPEND: case INIT_CMD_SUSPEND:
_d("suspend"); _d("suspend");
sync(); sync();
rc = reboot(RB_SW_SUSPEND); rc = sys_suspend();
if (rc) { if (rc) {
if (errno == EINVAL) if (errno == EINVAL)
snprintf(buf, len, "Kernel does not support suspend."); snprintf(buf, len, "Kernel does not support suspend to RAM.");
else else
snprintf(buf, len, "Failed: %s", strerror(errno)); snprintf(buf, len, "Failed: %s", strerror(errno));
} }
+4 -2
View File
@@ -160,6 +160,8 @@ int reboot_main(int argc, char *argv[])
transform(NULL); transform(NULL);
if (force) { if (force) {
sync();
switch (cmd) { switch (cmd) {
case CMD_REBOOT: case CMD_REBOOT:
c = reboot(RB_AUTOBOOT); c = reboot(RB_AUTOBOOT);
@@ -174,9 +176,9 @@ int reboot_main(int argc, char *argv[])
break; break;
case CMD_SUSPEND: case CMD_SUSPEND:
c = reboot(RB_SW_SUSPEND); c = suspend();
if (c && errno == EINVAL) if (c && errno == EINVAL)
errx(1, "Kernel does not support suspend."); errx(1, "Kernel does not support suspend to RAM.");
break; break;
case CMD_UNKNOWN: case CMD_UNKNOWN:
+34
View File
@@ -235,6 +235,40 @@ int fngetint(char *path, int *val)
return 0; return 0;
} }
/*
* This is a replacement for the non-working reboot(RB_SW_SUSPEND). It
* checks for suspend to RAM support, assuming `mem_sleep` has a sane
* default, e.g. 'deep' or 's2idle'. The latter should be possible to
* select from finit.conf, but for now we go with the kernel default.
* For more information on kernel sleep states, see:
* https://www.kernel.org/doc/html/latest/admin-guide/pm/sleep-states.html
*/
int suspend(void)
{
char buf[80];
char *ptr;
if (fnread(buf, sizeof(buf), "/sys/power/state") <= 0) {
opnotsup:
errno = EINVAL;
return 1;
}
chomp(buf);
ptr = strtok(buf, " ");
while (ptr) {
if (!strcmp(ptr, "mem"))
break;
ptr = strtok(NULL, " ");
}
if (!ptr)
goto opnotsup;
return fnwrite("mem", "/sys/power/state");
}
int strtobytes(char *arg) int strtobytes(char *arg)
{ {
int mod = 0, bytes; int mod = 0, bytes;
+2
View File
@@ -59,6 +59,8 @@ int fnread (char *buf, size_t len, char *fmt, ...) __attribute__ ((forma
int fnwrite (char *value, char *fmt, ...) __attribute__ ((format (printf, 2, 3))); int fnwrite (char *value, char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
int fngetint (char *path, int *val); int fngetint (char *path, int *val);
int suspend (void);
int strtobytes (char *arg); int strtobytes (char *arg);
char *sig2str (int sig); char *sig2str (int sig);
int str2sig (char *sig); int str2sig (char *sig);