Re: wakeup tool

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

 



* Karel Zak <kzak@xxxxxxxxxx> [2007-07-12 00:08]:
> On Wed, Jul 11, 2007 at 09:34:59PM +0200, Bernhard Walle wrote:
> > * Karel Zak <kzak@xxxxxxxxxx> [2007-07-05 23:59]:
> > > On Thu, Jul 05, 2007 at 04:44:25PM +0200, Bernhard Walle wrote:
> > > > Hi,
> > > > 
> > > > wouldn't it make sense to include the wakeup tool from kernel commit
> > > > k87ac84f42a7a580d0dd72ae31d6a5eb4bfe04c6d [1] to util-linux?
> > > 
> > >  Send patch ;-)
> > 
> > See below. I can write a manual page and adjust the program to read
> > utc/localtime from /etc/adjtime,
> 
>  Cool, go ahead.

Thanks for your input. Here's my improved version:


Signed-off-by: Bernhard Walle <bwalle@xxxxxxx>

---
 misc-utils/Makefile.am |    2 
 misc-utils/rtcwake.1   |   94 ++++++++++
 misc-utils/rtcwake.c   |  437 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 532 insertions(+), 1 deletion(-)

--- a/misc-utils/Makefile.am
+++ b/misc-utils/Makefile.am
@@ -5,7 +5,7 @@ EXTRA_DIST = README.flushb
 bin_PROGRAMS =
 
 usrbinexec_PROGRAMS = cal ddate logger look mcookie \
-	namei script whereis
+	namei script whereis rtcwake
 EXTRA_DIST += README.cal README.ddate README.namei README.namei2
 
 mcookie_SOURCES = mcookie.c ../lib/md5.c
--- /dev/null
+++ b/misc-utils/rtcwake.1
@@ -0,0 +1,94 @@
+.\" Copyright (c) 2007, SUSE LINUX Products GmbH
+.\"                     Bernhard Walle <bwalle@xxxxxxx>
+.\"
+.\" This program is free software; you can redistribute it and/or
+.\" modify it under the terms of the GNU General Public License
+.\" as published by the Free Software Foundation; either version 2
+.\" of the License, or (at your option) any later version.
+.\"
+.\" This program is distributed in the hope that it will be useful,
+.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
+.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+.\" GNU General Public License for more details.
+.\"
+.\" You should have received a copy of the GNU General Public License
+.\" along with this program; if not, write to the Free Software
+.\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+.\" 02110-1301, USA.
+.\"
+.TH rtcwake 1 "2007-07-13" "" "Linux Programmer's Manual"
+.SH NAME
+rtcwake - enter a system sleep state until specified wakeup time
+.SH SYNOPSIS
+\fBrtcwake\fP [-hvVlua] [-d \fIdevice\fP] [-m \fIstandby_mode\fP] -t \fItime_t\fP | -s \fIseconds\fP
+.SH DESCRIPTION
+This program is used to enter a system sleep state until specified wakeup time.
+.PP
+This uses cross-platform Linux interfaces to enter a system sleep state, and
+leave it no later than a specified time.  It uses any RTC framework driver that
+supports standard driver model wakeup flags.
+.PP
+This is normally used like the old \fBapmsleep\fP utility, to wake from a suspend
+state like ACPI S1 (standby) or S3 (suspend-to-RAM).  Most platforms can
+implement those without analogues of BIOS, APM, or ACPI.
+.P
+On some systems, this can also be used like \fBnvram-wakeup\fP, waking from states
+like ACPI S4 (suspend to disk).  Not all systems have persistent media that are
+appropriate for such suspend modes.
+.SS Options
+.TP
+\fB-v\fP | \fB--verbose\fP
+Be verbose.
+.TP
+\fB-h\fP | \fB--help\fP
+Display a short help message that shows how to use the program.
+.TP
+\fB-V\fP | \fB--version\fP
+Displays version information and exists.
+.TP
+\fB-a\fP | \fB--auto\fP
+Reads the clock mode (whether the hardware clock is set to UTC or local time)
+from \fI/etc/adjtime\fP. That's the location where the \fBhwclock\fP stores
+that information.
+.TP
+\fB-l\fP | \fB--local\fP
+Assumes that the hardware clock is set to local time, regardless of the
+contents of \fI/etc/adjtime\fP.
+.TP
+\fB-u\fP | \fB--utc\fP
+Assumes that the hardware clock is set to UTC (Universal Time Coordinated),
+regardless of the contents of \fI/etc/adjtime\fP.
+.TP
+\fB-d\fP \fIdevice\fP | \fB--device\fP \fIdevice\fP
+Uses \fIdevice\fP instead of \fIrtc0\fP as realtime clock. This option
+is only relevant if your system has more than one RTC. You may specify
+\fIrtc1\fP, \fIrtc2\fP, ... here.
+.TP
+\fB-s\fP \fIseconds\fP | \fB--seconds\fP \fIseconds\fP
+Sets the wakeup time to \fIseconds\fP in future from now.
+.TP
+\fB-t\fP \fItime_t\fP | \fB--time\fP \fItime_t\fP
+Sets the wakeup time to the absolute time \fItime_t\fP. \fItime_t\fP
+is the time in seconds since 1970-01-01, 00:00 UTC. Use the
+\fBdate\fP tool to convert between human-readable time and \fItime_t\fP.
+.TP
+\fB-m\fP \fImode\fP | \fB--mode\fP \fImode\fP
+Use standby state \fImode\fP. Valid values are \fIstandby\fP,
+\fImem\fP, \fIdisk\fP and \fIon\fP (no suspend). The default is
+\fIstandby\fP.
+.SH HISTORY
+The program first appeared as kernel commit message for Linux 2.6 in the GIT
+commit 87ac84f42a7a580d0dd72ae31d6a5eb4bfe04c6d.
+.SH AVAILABILITY
+The cal command is part of the util-linux-ng package and is available from
+ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.
+.SH AUTHOR
+The program was written by David Brownell <david-b@xxxxxxxxxxx> and improved by
+Bernhard Walle <bwalle@xxxxxxx>.
+.SH COPYRIGHT
+This is free software.  You may redistribute copies of it  under  the  terms
+of  the  GNU General  Public  License <http://www.gnu.org/licenses/gpl.html>.
+There is NO WARRANTY, to the extent permitted by law.
+.SH "SEE ALSO"
+.BR hwclock (8),
+.BR date (1)
--- /dev/null
+++ b/misc-utils/rtcwake.c
@@ -0,0 +1,437 @@
+/*
+ * rtcwake -- enter a system sleep state until specified wakeup time.
+ *
+ * This uses cross-platform Linux interfaces to enter a system sleep state,
+ * and leave it no later than a specified time.  It uses any RTC framework
+ * driver that supports standard driver model wakeup flags.
+ *
+ * This is normally used like the old "apmsleep" utility, to wake from a
+ * suspend state like ACPI S1 (standby) or S3 (suspend-to-RAM).  Most
+ * platforms can implement those without analogues of BIOS, APM, or ACPI.
+ *
+ * On some systems, this can also be used like "nvram-wakeup", waking
+ * from states like ACPI S4 (suspend to disk).  Not all systems have
+ * persistent media that are appropriate for such suspend modes.
+ *
+ * The best way to set the system's RTC is so that it holds the current
+ * time in UTC.  Use the "-l" flag to tell this program that the system
+ * RTC uses a local timezone instead (maybe you dual-boot MS-Windows).
+ */
+
+#include <stdio.h>
+#include <getopt.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <time.h>
+
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+
+#include <linux/rtc.h>
+
+/* constants from legacy PC/AT hardware */
+#define	RTC_PF	0x40
+#define	RTC_AF	0x20
+#define	RTC_UF	0x10
+
+#define MAX_LINE		1024
+
+static char		*progname;
+
+#define VERSION_STRING 		"rtcwake from " PACKAGE_STRING
+#define RTC_PATH         	"/sys/class/rtc/%s/device/power/wakeup"
+#define SYS_POWER_SATE_PATH	"/sys/power/state"
+#define ADJTIME_PATH		"/etc/adjtime"
+
+enum ClockMode {
+	CM_AUTO,
+	CM_UTC,
+	CM_LOCAL
+};
+
+static unsigned		verbose;
+enum ClockMode		clock_mode = CM_AUTO;
+
+static struct option long_options[] = {
+	{"auto",	no_argument,		0, 'a'},
+	{"local", 	no_argument,   		0, 'l'},
+	{"utc",		no_argument,		0, 'u'},
+	{"verbose",	no_argument, 		0, 'v'},
+	{"version",	no_argument,		0, 'V'},
+	{"help", 	no_argument,		0, 'h'},
+	{"mode",	required_argument,	0, 'm'},
+	{"device",	required_argument,	0, 'd'},
+	{"seconds",	required_argument,	0, 's'},
+	{"time",	required_argument,	0, 't'},
+	{0,		0, 			0, 0  }
+};
+
+static int may_wakeup(const char *devname)
+{
+	char	buf[128], *s;
+	FILE	*f;
+
+	snprintf(buf, sizeof buf, RTC_PATH, devname);
+	f = fopen(buf, "r");
+	if (!f) {
+		perror(buf);
+		return 0;
+	}
+	fgets(buf, sizeof buf, f);
+	fclose(f);
+
+	s = strchr(buf, '\n');
+	if (!s)
+		return 0;
+	*s = 0;
+
+	/* wakeup events could be disabled or not supported */
+	return strcmp(buf, "enabled") == 0;
+}
+
+/* all times should be in UTC */
+static time_t	sys_time;
+static time_t	rtc_time;
+
+static int get_basetimes(int fd)
+{
+	struct tm	tm;
+	struct rtc_time	rtc;
+
+	/* this process works in RTC time, except when working
+	 * with the system clock (which always uses UTC).
+	 */
+	if (clock_mode == CM_UTC)
+		setenv("TZ", "UTC", 1);
+	tzset();
+
+	/* read rtc and system clocks "at the same time", or as
+	 * precisely (+/- a second) as we can read them.
+	 */
+	if (ioctl(fd, RTC_RD_TIME, &rtc) < 0) {
+		perror("read rtc time");
+		return 0;
+	}
+	sys_time = time(0);
+	if (sys_time == (time_t)-1) {
+		perror("read system time");
+		return 0;
+	}
+
+	/* convert rtc_time to normal arithmetic-friendly form,
+	 * updating tm.tm_wday as used by asctime().
+	 */
+	memset(&tm, 0, sizeof tm);
+	tm.tm_sec = rtc.tm_sec;
+	tm.tm_min = rtc.tm_min;
+	tm.tm_hour = rtc.tm_hour;
+	tm.tm_mday = rtc.tm_mday;
+	tm.tm_mon = rtc.tm_mon;
+	tm.tm_year = rtc.tm_year;
+	tm.tm_isdst = rtc.tm_isdst;	/* stays unspecified? */
+	rtc_time = mktime(&tm);
+
+	if (rtc_time == (time_t)-1) {
+		perror("convert rtc time");
+		return 0;
+	}
+
+	if (verbose) {
+		if (clock_mode == CM_LOCAL) {
+			printf("\ttzone   = %ld\n", timezone);
+			printf("\ttzname  = %s\n", tzname[daylight]);
+			gmtime_r(&rtc_time, &tm);
+		}
+		printf("\tsystime = %ld, (UTC) %s",
+				(long) sys_time, asctime(gmtime(&sys_time)));
+		printf("\trtctime = %ld, (UTC) %s",
+				(long) rtc_time, asctime(&tm));
+	}
+
+	return 1;
+}
+
+static int setup_alarm(int fd, time_t *wakeup)
+{
+	struct tm		*tm;
+	struct rtc_wkalrm	wake;
+
+	tm = gmtime(wakeup);
+
+	wake.time.tm_sec = tm->tm_sec;
+	wake.time.tm_min = tm->tm_min;
+	wake.time.tm_hour = tm->tm_hour;
+	wake.time.tm_mday = tm->tm_mday;
+	wake.time.tm_mon = tm->tm_mon;
+	wake.time.tm_year = tm->tm_year;
+	wake.time.tm_wday = tm->tm_wday;
+	wake.time.tm_yday = tm->tm_yday;
+	wake.time.tm_isdst = tm->tm_isdst;
+
+	/* many rtc alarms only support up to 24 hours from 'now' ... */
+	if ((rtc_time + (24 * 60 * 60)) > *wakeup) {
+		if (ioctl(fd, RTC_ALM_SET, &wake.time) < 0) {
+			perror("set rtc alarm");
+			return 0;
+		}
+		if (ioctl(fd, RTC_AIE_ON, 0) < 0) {
+			perror("enable rtc alarm");
+			return 0;
+		}
+
+		/* ... so use the "more than 24 hours" request only if we must */
+	} else {
+		/* avoid an extra AIE_ON call */
+		wake.enabled = 1;
+
+		if (ioctl(fd, RTC_WKALM_SET, &wake) < 0) {
+			perror("set rtc wake alarm");
+			return 0;
+		}
+	}
+
+	return 1;
+}
+
+static void suspend_system(const char *suspend)
+{
+	FILE	*f = fopen(SYS_POWER_SATE_PATH, "w");
+
+	if (!f) {
+		perror(SYS_POWER_SATE_PATH);
+		return;
+	}
+
+	fprintf(f, "%s\n", suspend);
+	fflush(f);
+
+	/* this executes after wake from suspend */
+	fclose(f);
+}
+
+static void usage(void)
+{
+	printf("usage: %s [options]\n"
+			"\t-d rtc0|rtc1|...\t(select rtc)\n"
+			"\t-l\t\t\t(RTC uses local timezone)\n"
+			"\t-m standby|mem|...\t(sleep mode)\n"
+			"\t-s seconds\t\t(seconds to sleep)\n"
+			"\t-t time_t\t\t(time to wake)\n"
+			"\t-u\t\t\t(RTC uses UTC)\n"
+			"\t-v\t\t\t(verbose messages)\n"
+			"\t-V\t\t\t(show version)\n", progname);
+}
+
+static int read_clock_mode(void)
+{
+	FILE *fp;
+	char linebuf[MAX_LINE];
+
+	fp = fopen(ADJTIME_PATH, "r");
+	if (!fp)
+		return 0;
+
+	/* skip first line */
+	if (!fgets(linebuf, MAX_LINE, fp)) {
+		fclose(fp);
+		return 0;
+	}
+
+	/* skip second line */
+	if (!fgets(linebuf, MAX_LINE, fp)) {
+		fclose(fp);
+		return 0;
+	}
+
+	/* read third line */
+	if (!fgets(linebuf, MAX_LINE, fp)) {
+		fclose(fp);
+		return 0;
+	}
+
+	if (strncmp(linebuf, "UTC", 3) == 0)
+		clock_mode = CM_UTC;
+	else if (strncmp(linebuf, "LOCAL", 5) == 0)
+		clock_mode = CM_LOCAL;
+
+	return 1;
+}
+
+int main(int argc, char **argv)
+{
+	static char		*devname = "rtc0";
+	static unsigned		seconds = 0;
+	static char		*suspend = "standby";
+
+	int		t;
+	int		fd;
+	time_t		alarm = 0;
+
+	progname = strrchr(argv[0], '/');
+	if (progname)
+		progname++;
+	else
+		progname = argv[0];
+	if (chdir("/dev/") < 0) {
+		perror("chdir /dev");
+		return 1;
+	}
+
+	while ((t = getopt_long(argc, argv, "ahd:lm:s:t:uVv",
+					long_options, NULL)) != EOF) {
+		switch (t) {
+
+			case 'a':
+				/* CM_AUTO is default */
+				break;
+
+			case 'd':
+				devname = strdup(optarg);
+				break;
+
+			case 'l':
+				clock_mode = CM_LOCAL;
+				break;
+
+				/* what system power mode to use?  for now handle only
+				 * standardized mode names; eventually when systems define
+				 * their own state names, parse /sys/power/state.
+				 *
+				 * "on" is used just to test the RTC alarm mechanism,
+				 * bypassing all the wakeup-from-sleep infrastructure.
+				 */
+			case 'm':
+				if (strcmp(optarg, "standby") == 0
+						|| strcmp(optarg, "mem") == 0
+						|| strcmp(optarg, "disk") == 0
+						|| strcmp(optarg, "on") == 0
+				   ) {
+					suspend = strdup(optarg);
+					break;
+				}
+				printf("%s: unrecognized suspend state '%s'\n",
+						progname, optarg);
+				usage();
+				return 1;
+
+				/* alarm time, seconds-to-sleep (relative) */
+			case 's':
+				t = atoi(optarg);
+				if (t < 0) {
+					printf("%s: illegal interval %s seconds\n",
+							progname, optarg);
+					usage();
+					return 1;
+				}
+				seconds = t;
+				break;
+
+				/* alarm time, time_t (absolute, seconds since 1/1 1970 UTC) */
+			case 't':
+				t = atoi(optarg);
+				if (t < 0) {
+					printf("%s: illegal time_t value %s\n",
+							progname, optarg);
+					usage();
+					return 1;
+				}
+				alarm = t;
+				break;
+
+			case 'u':
+				clock_mode = CM_UTC;
+				break;
+
+			case 'v':
+				verbose++;
+				break;
+
+			case 'V':
+				printf("%s: version %s\n", progname, VERSION_STRING);
+				return 0;
+
+			default:
+				usage();
+				return 1;
+		}
+	}
+
+	if (clock_mode == CM_AUTO) {
+		if (!read_clock_mode()) {
+			printf("%s: assuming RTC uses UTC ...\n", progname);
+			clock_mode = CM_UTC;
+		}
+		if (verbose)
+			fprintf(stderr, "Using %s time\n",
+					clock_mode == CM_UTC ? "UTC" : "local");
+	}
+
+	if (!alarm && !seconds) {
+		printf("%s: must provide wake time\n", progname);
+		usage();
+		return 1;
+	}
+
+	/* this RTC must exist and (if we'll sleep) be wakeup-enabled */
+	fd = open(devname, O_RDONLY);
+	if (fd < 0) {
+		perror(devname);
+		return 1;
+	}
+	if (strcmp(suspend, "on") != 0 && !may_wakeup(devname)) {
+		printf("%s: %s not enabled for wakeup events\n",
+				progname, devname);
+		return 1;
+	}
+
+	/* relative or absolute alarm time, normalized to time_t */
+	if (!get_basetimes(fd))
+		return 1;
+	if (verbose)
+		printf("alarm %ld, sys_time %ld, rtc_time %ld, seconds %u\n",
+				alarm, sys_time, rtc_time, seconds);
+	if (alarm) {
+		if (alarm < sys_time) {
+			printf("%s: time doesn't go backward to %s",
+					progname, ctime(&alarm));
+			return 1;
+		}
+		alarm += sys_time - rtc_time;
+	} else
+		alarm = rtc_time + seconds + 1;
+	if (setup_alarm(fd, &alarm) < 0)
+		return 1;
+
+	sync();
+	printf("%s: wakeup from \"%s\" using %s at %s",
+			progname, suspend, devname,
+			ctime(&alarm));
+	fflush(stdout);
+	usleep(10 * 1000);
+
+	if (strcmp(suspend, "on") != 0)
+		suspend_system(suspend);
+	else {
+		unsigned long data;
+
+		do {
+			t = read(fd, &data, sizeof data);
+			if (t < 0) {
+				perror("rtc read");
+				break;
+			}
+			if (verbose)
+				printf("... %s: %03lx\n", devname, data);
+		} while (!(data & RTC_AF));
+	}
+
+	if (ioctl(fd, RTC_AIE_OFF, 0) < 0)
+		perror("disable rtc alarm interrupt");
+
+	close(fd);
+	return 0;
+}
-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" 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