mirror of
https://github.com/troglobit/finit.git
synced 2026-10-01 05:22:48 +07:00
Replace reboot(RB_SW_SUSPEND) w/ internal /sys/power/state API
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
@@ -250,10 +250,10 @@ static int do_reboot(int cmd, char *buf, size_t len)
|
||||
case INIT_CMD_SUSPEND:
|
||||
_d("suspend");
|
||||
sync();
|
||||
rc = reboot(RB_SW_SUSPEND);
|
||||
rc = sys_suspend();
|
||||
if (rc) {
|
||||
if (errno == EINVAL)
|
||||
snprintf(buf, len, "Kernel does not support suspend.");
|
||||
snprintf(buf, len, "Kernel does not support suspend to RAM.");
|
||||
else
|
||||
snprintf(buf, len, "Failed: %s", strerror(errno));
|
||||
}
|
||||
|
||||
+4
-2
@@ -160,6 +160,8 @@ int reboot_main(int argc, char *argv[])
|
||||
transform(NULL);
|
||||
|
||||
if (force) {
|
||||
sync();
|
||||
|
||||
switch (cmd) {
|
||||
case CMD_REBOOT:
|
||||
c = reboot(RB_AUTOBOOT);
|
||||
@@ -174,9 +176,9 @@ int reboot_main(int argc, char *argv[])
|
||||
break;
|
||||
|
||||
case CMD_SUSPEND:
|
||||
c = reboot(RB_SW_SUSPEND);
|
||||
c = suspend();
|
||||
if (c && errno == EINVAL)
|
||||
errx(1, "Kernel does not support suspend.");
|
||||
errx(1, "Kernel does not support suspend to RAM.");
|
||||
break;
|
||||
|
||||
case CMD_UNKNOWN:
|
||||
|
||||
+34
@@ -235,6 +235,40 @@ int fngetint(char *path, int *val)
|
||||
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 mod = 0, bytes;
|
||||
|
||||
@@ -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 fngetint (char *path, int *val);
|
||||
|
||||
int suspend (void);
|
||||
|
||||
int strtobytes (char *arg);
|
||||
char *sig2str (int sig);
|
||||
int str2sig (char *sig);
|
||||
|
||||
Reference in New Issue
Block a user