Re: [PATCH] util-linux-ng/sys-utils/rtcwake : add show and disable actions/modes for rtc alarm

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

 



 Hi Marek,

 (sorry for delay, but I'm ill in last days...)

On Thu, Jan 07, 2010 at 08:26:58PM +0100, Marek Otahal wrote:
> Changelog: 
> 
> Support for 'show' and 'disable' actions/modes (rtcwake -m <mode>)
> * show : print current alarm settings (from /proc/driver/rtc)

 this is nice idea, but the implementation is poor. It'd be nice to
 have more user friendly output (convert to the local time, print
 important information only, ...), for example:

        # rtcwake --show
        PENDING, alarm time: 11:22:33

 maybe we can also use RTC_WKALM_RD ioctl instead /proc.

> * disable: disable alarm on [optionally specified -d] rtc device

 Good idea.

> --- sys-utils/rtcwake.c	2010-01-07 19:08:21.673672059 +0100
> +++ ../../util-linux-ng/util-linux-ng-2.17-rc3/sys-utils/rtcwake.c	2010-01-04 20:16:51.444954038 +0100
> @@ -54,7 +54,7 @@
>  #define ADJTIME_PATH		"/etc/adjtime"
>  #define DEFAULT_DEVICE		"/dev/rtc0"
>  #define DEFAULT_MODE		"standby"
> -
> +#define RTC_INFO		"/proc/driver/rtc"
>  enum ClockMode {
>  	CM_AUTO,
>  	CM_UTC,
> @@ -291,6 +291,35 @@
>  	return 0;
>  }
>  
> +static int disable_alarm(int fd) {
> +	if (ioctl(fd, RTC_AIE_OFF, 0) < 0) {
> +        	perror(_("disable rtc alarm interrupt"));
> +		return -1;
> +	}
> +	return 0;
> +}
> +
> +static int print_alarm(void) {
> +	int f = 0; 
> +	f = open(RTC_INFO, O_RDONLY);
> +	if(f<0) {
> +		fprintf(stderr, "Cannot open %s for reading .\n", RTC_INFO);
> +		return 1;
> +	}
> +
> +	char buf;
> +	int i = 0;
> +	
> +	do {
> +		i = read(f, &buf, sizeof buf);
> +		fprintf(stdout, "%c", buf);
> +		buf = ' ';
> +	}while( i > 0);
> +	
> +	close(f);
> +	return 0;
> +}
> +
>  int main(int argc, char **argv)
>  {
>  	char		*devname = DEFAULT_DEVICE;
> @@ -338,6 +367,8 @@
>  					|| strcmp(optarg, "on") == 0
>  					|| strcmp(optarg, "no") == 0
>  					|| strcmp(optarg, "off") == 0
> +					|| strcmp(optarg, "show") == 0
> +					|| strcmp(optarg, "disable") == 0
>  			   ) {
>  				suspend = strdup(optarg);
>  				break;
> @@ -403,7 +434,7 @@
>  		printf(clock_mode == CM_UTC ? _("Using UTC time.\n") :
>  				_("Using local time.\n"));
>  
> -	if (!alarm && !seconds) {
> +	if ( (!alarm && !seconds) && strcmp(suspend, "show") != 0 && strcmp(suspend, "disable") != 0 ) {
>  		fprintf(stderr, _("%s: must provide wake time\n"), progname);
>  		usage(EXIT_FAILURE);
>  	}
> @@ -468,9 +499,11 @@
>  	fflush(stdout);
>  	usleep(10 * 1000);
>  
> -	if (strcmp(suspend, "no") == 0)
> +	if (strcmp(suspend, "no") == 0) {
>  		exit(EXIT_SUCCESS);
> -	else if (strcmp(suspend, "on") != 0) {
> +	}
> +	// determine supported suspend action (nowadays only "mem" or "disk") & do the suspend
> +	else if ( (strcmp(suspend, "mem") == 0) || (strcmp(suspend, "disk") == 0) ) {
>  		sync();
>  		suspend_system(suspend);
>  	} else if (strcmp(suspend, "off") == 0) {
> @@ -487,7 +520,19 @@
>  		fprintf(stderr, _("%s: unable to execute %s: %s\n"),
>  				progname, _PATH_SHUTDOWN, strerror(errno));
>  		rc = EXIT_FAILURE;
> -	} else {
> +	} else if(strcmp(suspend, "show") == 0) {
> +		// print alarm info
> +		fprintf(stdout, "Current alarm settings by rtc: \n");
> +		print_alarm();

 We shouldn't ignore return value from print_alarm(), the same problem
 I see with suspend_system() call in the rtcwake code.

> +		close(fd);
> +		return rc;
> +		
> +	} else if(strcmp(suspend, "disable") == 0) {
> +		// disable the alarm
> +		fprintf(stdout, "Disabling alarm on device %s \n", devname);
> +		// alarm is always disabled at the end, so just print info and skip
> +
> +	} else { // mode "on", "standby"
>  		unsigned long data;
>  
>  		do {
> @@ -501,9 +546,7 @@
>  		} while (!(data & RTC_AF));
>  	}
>  
> -	if (ioctl(fd, RTC_AIE_OFF, 0) < 0)
> -		perror(_("disable rtc alarm interrupt"));
> -
> +	disable_alarm(fd);
>  	close(fd);
>  
>  	return rc;

 Please, use CC: to util-linux-ng mailing list. Thanks.

    Karel

-- 
 Karel Zak  <kzak@xxxxxxxxxx>
--
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