1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/* apmsleep.c:
* Suspend computer using APM and wake-up at given time using
* the Real Time Clock (RTC) device.
*
* see man page apmsleep.1 for further details
*
* Possible applications:
* - Run certain internet lookup programs at certain times
* e.g. download email, news, etc.
* - Send Faxes at night, but do not run laptop all night
* - Start up in the morning to catch up old cron jobs
*
* Hint:
* use 'date' to calculate suspend time including daylight saving
* time correction.
*
* Possible future extensions (anyone?):
* - correct handling daylight saving time jumps
* - Set/restore '/dev/nvram' to set 'suspend to dram' option
* - apmcron job scheduler to add flexibility
*
* Based on Paul Gortmacher's RTC Test/Example Program
* Copyright (C) 1999, Peter Englmaier <ppe@pa.uky.edu>
*
* Released under the GNU General Public License, version 2,
* included herein by reference.
*
* $Log: apmsleep.c,v $
* Revision 1.1 1999/08/02 20:08:08 apenwarr
*
*
* Integrated apmsleep, and added xbattery (but without Makefile support).
*
* Revision 0.5 1999/08/01 01:29:29 ppe
* Now using the apm library
*
* Revision 0.4 1999/07/19 16:45:29 ppe
* *** empty log message ***
*
* Revision 0.3 1999/07/19 16:10:38 ppe
* Bug in wakeup time calculation corrected.
*
*/
#include <stdio.h>
#include <linux/mc146818rtc.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <sys/wait.h>
#include <apm.h>
static char rcsid[]="$Id: apmsleep.c,v 1.1 1999/08/02 20:08:08 apenwarr Exp $";
static char rcsrev[] = "$Revision: 1.1 $";
void version() {
printf("Version: %s\n",rcsrev);
}
void usage() {
printf("Usage:\n\n"
"apmsleep [+]hh:mm\n\n"
"Example:\n"
" apmsleep +1:15 will suspend for one hour and 15 minutes\n"
" apmsleep 8:00 will suspend until 8:00 am\n\n"
"Bugs: Daylight saving jumps are not taken into account.\n"
" Modem ring detection may trigger early wake-up.\n"
" Does not work with Suspend to Disk.\n");
}
int main(int argc, char **argv)
{
int i, fd, retval;
struct rtc_time rtc_tm;
struct tm kernel_tm;
time_t kernel_time, new_time, old_time;
char *p, c;
int apmfd; /* APM device file descriptor */
int isoffset; /* setting time offset */
int hour, minute; /* alarm time requested */
int flag;
static int suspend_mode=0, wait_mode=0, debug_mode=0;
while (1) {
/* int this_option_optind = optind ? optind : 1; */
int option_index = 0;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"debug", 0, 0, 'd'},
{"version", 0, 0, 'V'},
{"suspend", 0, 0, 's'},
{"standby", 0, 0, 'S'},
{"wait", 0, 0, 'w'},
{"noapm", 0, 0, 'n'},
{0,0,0,0}
};
c = getopt_long(argc, argv, "hdVsSwn", long_options, &option_index);
if (c == -1) break;
switch(c)
{
case 'h':
usage();
exit(0);
case 'V':
version();
exit(0);
case 'd':
debug_mode=1;
break;
case 's': /*suspend*/
suspend_mode=0;
break;
case 'S': /*standby*/
suspend_mode=1;
break;
case 'n': /*noapm*/
suspend_mode=2;
wait_mode=1;
break;
case 'w':
wait_mode=1;
break;
case '?': /*unknown option*/
exit(1);
default:
fprintf(stderr,"unknown option return %d\n", c);
exit(1);
}
} /* end while (1) */
if (optind < argc) {
if (optind+1 != argc) {
fprintf(stderr,"To many arguments.\n"); usage();
exit(1);
}
p=argv[optind];
isoffset=0;
if (p[0]=='+') { p++; isoffset=1;}
if (2!=sscanf(p, "%d:%d", &hour, &minute)) {
fprintf(stderr,"apmsleep: Bad argument(s)\n");
exit(1);
}
} else {
fprintf(stderr,"apmsleep: missing argument.\n"); usage();
exit(1);
}
/* argument processing finished */
if (geteuid()!=0) {
fprintf(stderr, "apmsleep: warning: This program must be run as root "
"or have the SUID attribute set\n");
}
/* check if APM is supported */
if (apm_exists()) {
fprintf(stderr, "apmsleep: Your kernel does not support APM.\n");
fprintf(stderr, "apmsleep: Recompile kernel with APM and "
"/dev/rtc support\n");
exit(1);
}
apmfd=apm_open();
if (apmfd == -1) {
fprintf(stderr, "apmsleep: Cannot open APM device.\n");
exit(1);
}
fd = open ("/dev/rtc", O_RDONLY);
if (errno == EACCES) {
fprintf(stderr, "apmsleep: You don't have permission to "
"access /dev/rtc\n");
fprintf(stderr, "apmsleep: The program must be run as root "
"or have the SUID attribute set\n");
exit(errno);
}
if (fd == -1) {
perror("/dev/rtc");
fprintf(stderr, "apmsleep: Your kernel does not support "
"the real time clock device\n");
fprintf(stderr, "apmsleep: Recompile kernel with APM and "
"/dev/rtc support\n");
exit(errno);
}
/* Read the RTC time/date */
retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
if (retval == -1) {
perror("ioctl");
exit(errno);
}
/* Read Kernel time/date */
time(&kernel_time);
kernel_tm=*localtime(&kernel_time);
if (debug_mode) {
printf("Current RTC time is %02d:%02d:%02d.\n",
rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
printf("Current local time is %02d:%02d:%02d.\n",
kernel_tm.tm_hour, kernel_tm.tm_min, kernel_tm.tm_sec);
}
/* set alarm time in RTC */
if (isoffset) {
hour+=(int)rtc_tm.tm_hour;
minute+=(int)rtc_tm.tm_min;
} else {
rtc_tm.tm_sec=0;
/* here we take difference between RTC and localtime into account */
minute+=(int)rtc_tm.tm_min - (int)kernel_tm.tm_min;
hour+=(int)rtc_tm.tm_hour - (int)kernel_tm.tm_hour;
}
while (minute<0) {minute +=60; hour--; }
while (minute>60) {minute -=60; hour++; }
hour %= 24;
rtc_tm.tm_min = minute;
rtc_tm.tm_hour = hour;
if (debug_mode)
printf("Setting RTC alarm time to %02d:%02d:%02d.\n",
hour, minute, rtc_tm.tm_sec);
retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
if (retval == -1) {
perror("ioctl");
exit(errno);
}
/* Read back the current alarm settings */
retval = ioctl(fd, RTC_ALM_READ, &rtc_tm);
if (retval == -1) {
perror("ioctl");
exit(errno);
}
if (debug_mode)
printf("RTC alarm time now set to %02d:%02d:%02d.\n",
rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
/* Enable alarm interrupts */
retval = ioctl(fd, RTC_AIE_ON, 0);
if (retval == -1) {
perror("ioctl");
exit(errno);
}
if (fork()==0) {
/* suspend system -- called from within a child, to make
* sure we don't miss the time leap
*/
switch(suspend_mode) {
case 0:
apm_suspend(apmfd);
break;
case 1:
apm_standby(apmfd);
break;
}
exit(0);
}
if (debug_mode) {
printf("Waiting until clock jumps\n");
fflush(stdout);
}
time(&new_time);
kernel_time=new_time;
flag=0;
do {
old_time=new_time;
sleep(2);
time(&new_time);
flag=difftime(new_time,old_time)<10.0;
if (flag && wait_mode==0 && difftime(new_time,kernel_time)>60.0)
break; /* timeout, flag is 1 */
} while (flag);
if (flag)
fprintf(stderr,"Time out -- no time leap happened\n");
else if (debug_mode)
printf("Time leap detected\n");
/* Waiting for child to finish */
wait(&i);
/* Disable alarm interrupts */
retval = ioctl(fd, RTC_AIE_OFF, 0);
if (retval == -1) {
perror("ioctl");
exit(errno);
}
close(fd);
close(apmfd);
return flag;
}
|