Re: [PATCH] rtcwake: test for available suspend modes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2011-02-09 at 18:46 +0100, Lawrence Rust wrote:
> This patch adds a test to rtcwake to verify that the requested suspend
> mode (standby|mem|disk) is available.  If not it exits with error.
> 
> This avoids the current situation where a request to suspend with an
> unsupported mode fails quietly and rtcwake returns immediately.
> 
> See linux-2.6.37/Documentation/power/interface.txt

Oops, looking at the kernel code some more:

linux-2.6.37/kernel/power/main.c function state_show()

It seems that reading /sys/power/state results in a space separated list
rather than the highest state supported as I had believed.  This makes
the code somewhat easier.  So here's the revised form

Signed-off-by: Lawrence Rust <lawrence(at)softsystem(dot)co(dot)uk>

--- a/sys-utils/rtcwake.c	2011-01-24 10:16:34.000000000 +0100
+++ b/sys-utils/rtcwake.c	2011-02-10 09:51:52.000000000 +0100
@@ -247,6 +247,24 @@ static int setup_alarm(int fd, time_t *w
 	return 0;
 }
 
+static int is_suspend_available(const char *suspend)
+{
+	int rc;
+	char buf[32];
+	FILE *f = fopen(SYS_POWER_STATE_PATH, "r");
+
+	if (!f)
+		return -1;
+
+	if (fgets(buf, sizeof buf, f) == NULL)
+		rc = -1;
+	else
+		rc = strstr(buf, suspend) != NULL;
+
+	fclose(f);
+	return rc;
+}
+
 static void suspend_system(const char *suspend)
 {
 	FILE	*f = fopen(SYS_POWER_STATE_PATH, "w");
@@ -501,6 +519,11 @@ int main(int argc, char **argv)
 				alarm, sys_time, rtc_time, seconds);
 
 	if (strcmp(suspend, "show") && strcmp(suspend, "disable")) {
+		if (strcmp(suspend, "no") && strcmp(suspend, "on") &&
+		    strcmp(suspend, "off") && is_suspend_available(suspend) <= 0) {
+			errx(EXIT_FAILURE, _("suspend to \"%s\" unavailable"), suspend);
+		}
+
 		/* care about alarm setup only if the show|disable
 		 * modes are not set
 		 */


--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux