summaryrefslogtreecommitdiff
authorsandman <sandman>2002-12-10 22:21:58 (UTC)
committer sandman <sandman>2002-12-10 22:21:58 (UTC)
commit887fb36c5344574f04d19c7442f3db899920e04d (patch) (unidiff)
tree3a1ccbdb5489bf628979a2b712a779ae009383dc
parent2173e57fcbd5d460fceb34a111845389dca56506 (diff)
downloadopie-887fb36c5344574f04d19c7442f3db899920e04d.zip
opie-887fb36c5344574f04d19c7442f3db899920e04d.tar.gz
opie-887fb36c5344574f04d19c7442f3db899920e04d.tar.bz2
/etc/resumeat is now in /var/run/resumeat
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c
index 5ea24b7..072dca7 100644
--- a/core/opiealarm/opiealarm.c
+++ b/core/opiealarm/opiealarm.c
@@ -1,92 +1,93 @@
1/* 1/*
2 * opiealarm.c 2 * opiealarm.c
3 * 3 *
4 * This program is for extracting the event time/date out 4 * This program is for extracting the event time/date out
5 * of /etc/resumeat and setting the RTC alarm to that time/date. 5 * of /etc/resumeat and setting the RTC alarm to that time/date.
6 * It is designed to run via a script just before the iPAQ 6 * It is designed to run via a script just before the iPAQ
7 * is suspended and right after the iPAQ resumes operation. 7 * is suspended and right after the iPAQ resumes operation.
8 * 8 *
9 * written and copyrighted by Robert Griebl <sandman@handhelds.org> 9 * written and copyrighted by Robert Griebl <sandman@handhelds.org>
10 */ 10 */
11 11
12#include <stdio.h> 12#include <stdio.h>
13#include <linux/rtc.h> 13#include <linux/rtc.h>
14#include <sys/ioctl.h> 14#include <sys/ioctl.h>
15#include <sys/time.h> 15#include <sys/time.h>
16#include <sys/types.h> 16#include <sys/types.h>
17#include <fcntl.h> 17#include <fcntl.h>
18#include <unistd.h> 18#include <unistd.h>
19#include <errno.h> 19#include <errno.h>
20#include <time.h> 20#include <time.h>
21#include <stdlib.h> 21#include <stdlib.h>
22#include <syslog.h> 22#include <syslog.h>
23#include <signal.h> 23#include <signal.h>
24#include <errno.h> 24#include <errno.h>
25#include <string.h> 25#include <string.h>
26 26
27 27
28 #define PIDFILE "/var/run/opiealarm.pid" 28 #define PIDFILE "/var/run/opiealarm.pid"
29#define TIMEFILE "/var/run/resumeat"
29 #define APMFILE "/proc/apm" 30 #define APMFILE "/proc/apm"
30 31
31int resume ( int resuspend ); 32int resume ( int resuspend );
32int suspend ( int fix_rtc ); 33int suspend ( int fix_rtc );
33int main ( int argc, char **argv ); 34int main ( int argc, char **argv );
34int fork_with_pidfile ( void ); 35int fork_with_pidfile ( void );
35int kill_with_pidfile ( void ); 36int kill_with_pidfile ( void );
36void remove_pidfile ( void ); 37void remove_pidfile ( void );
37void usage ( void ); 38void usage ( void );
38void sig_handler_child ( int sig ); 39void sig_handler_child ( int sig );
39void sig_handler_parent ( int sig ); 40void sig_handler_parent ( int sig );
40int onac ( void ); 41int onac ( void );
41 42
42static int opiealarm_was_running; 43static int opiealarm_was_running;
43static pid_t parent_pid = 0; 44static pid_t parent_pid = 0;
44 45
45 46
46 47
47void sig_handler_child ( int sig ) 48void sig_handler_child ( int sig )
48{ 49{
49 // child got SIGUSR2 -> cleanup pidfile and exit 50 // child got SIGUSR2 -> cleanup pidfile and exit
50 51
51 remove_pidfile ( ); 52 remove_pidfile ( );
52 exit ( 0 ); 53 exit ( 0 );
53} 54}
54 55
55void sig_handler_parent ( int sig ) 56void sig_handler_parent ( int sig )
56{ 57{
57 // parent got SIGUSR1 -> safe to exit now 58 // parent got SIGUSR1 -> safe to exit now
58 59
59 parent_pid = 0; 60 parent_pid = 0;
60 exit ( 0 ); 61 exit ( 0 );
61} 62}
62 63
63void usage ( void ) 64void usage ( void )
64{ 65{
65 fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" ); 66 fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" );
66 fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" ); 67 fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" );
67 fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" ); 68 fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" );
68 fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" ); 69 fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" );
69 fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" ); 70 fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" );
70 exit ( 1 ); 71 exit ( 1 );
71} 72}
72 73
73int fork_with_pidfile ( void ) 74int fork_with_pidfile ( void )
74{ 75{
75 FILE *fp; 76 FILE *fp;
76 pid_t pid; 77 pid_t pid;
77 78
78 pid = fork ( ); 79 pid = fork ( );
79 80
80 if ( pid > 0 ) { 81 if ( pid > 0 ) {
81 // We can not just exit now, because the kernel could suspend 82 // We can not just exit now, because the kernel could suspend
82 // the iPAQ just before the child process sets the RTC. 83 // the iPAQ just before the child process sets the RTC.
83 // Solution: just wait for SIGUSR1 - the child process will 84 // Solution: just wait for SIGUSR1 - the child process will
84 // signal this when it thinks it is safe to exit. 85 // signal this when it thinks it is safe to exit.
85 86
86 signal ( SIGUSR1, sig_handler_parent ); 87 signal ( SIGUSR1, sig_handler_parent );
87 while ( 1 ) 88 while ( 1 )
88 sleep ( 1000 ); 89 sleep ( 1000 );
89 exit ( 0 ); 90 exit ( 0 );
90 } 91 }
91 else if ( pid < 0 ) { 92 else if ( pid < 0 ) {
92 perror ( "forking failed" ); 93 perror ( "forking failed" );
@@ -184,174 +185,174 @@ int main ( int argc, char **argv )
184 usage ( ); 185 usage ( );
185 186
186 187
187 parent_pid = getpid ( ); 188 parent_pid = getpid ( );
188 189
189 // kill running opiealarm 190 // kill running opiealarm
190 opiealarm_was_running = kill_with_pidfile ( ); 191 opiealarm_was_running = kill_with_pidfile ( );
191 remove_pidfile ( ); 192 remove_pidfile ( );
192 193
193 switch ( mode ) { 194 switch ( mode ) {
194 case 'r': opt = resume ( ac_resusp ); 195 case 'r': opt = resume ( ac_resusp );
195 break; 196 break;
196 case 's': 197 case 's':
197 default : opt = suspend ( fix_rtc ); 198 default : opt = suspend ( fix_rtc );
198 break; 199 break;
199 } 200 }
200 201
201 parent_pid = 0; 202 parent_pid = 0;
202 return opt; 203 return opt;
203 } 204 }
204 205
205 206
206int suspend ( int fix_rtc ) 207int suspend ( int fix_rtc )
207{ 208{
208 FILE *fp; 209 FILE *fp;
209 char buf [64]; 210 char buf [64];
210 time_t alrt, syst, rtct; 211 time_t alrt, syst, rtct;
211 struct tm alr, sys, rtc; 212 struct tm alr, sys, rtc;
212 int fd; 213 int fd;
213 int rtc_sys_diff; 214 int rtc_sys_diff;
214 215
215 216
216 if ( !fork_with_pidfile ( )) 217 if ( !fork_with_pidfile ( ))
217 return 3; 218 return 3;
218 219
219 // we are the child process from here on ... 220 // we are the child process from here on ...
220 221
221 tzset ( ); // not sure if it is really needed -- it probably doesn't hurt ... 222 tzset ( ); // not sure if it is really needed -- it probably doesn't hurt ...
222 223
223 time ( &syst );// get the UNIX system time 224 time ( &syst );// get the UNIX system time
224 sys = *localtime ( &syst ); 225 sys = *localtime ( &syst );
225 226
226 do { 227 do {
227 228
228 if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 ) 229 if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 )
229 if (( fd = open ( "/dev/rtc", O_RDWR )) < 0 ) 230 if (( fd = open ( "/dev/rtc", O_RDWR )) < 0 )
230 break; // ( 1, "rtc" ); 231 break; // ( 1, "rtc" );
231 232
232 memset ( &rtc, 0, sizeof ( struct tm )); // get the RTC time 233 memset ( &rtc, 0, sizeof ( struct tm )); // get the RTC time
233 234
234 if ( ioctl ( fd, RTC_RD_TIME, &rtc ) < 0 ) 235 if ( ioctl ( fd, RTC_RD_TIME, &rtc ) < 0 )
235 break; // ( 1, "ioctl RTC_RD_TIME" ); 236 break; // ( 1, "ioctl RTC_RD_TIME" );
236 237
237 rtct = mktime ( &rtc ); 238 rtct = mktime ( &rtc );
238 239
239 rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff; // calculate the difference between system and hardware time 240 rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff; // calculate the difference between system and hardware time
240 241
241 if ( fix_rtc && (( rtc_sys_diff < -3 ) || ( rtc_sys_diff > 3 ))) { 242 if ( fix_rtc && (( rtc_sys_diff < -3 ) || ( rtc_sys_diff > 3 ))) {
242 struct tm set; 243 struct tm set;
243 set = *gmtime ( &syst ); 244 set = *gmtime ( &syst );
244 245
245 // if the difference between system and hardware time is more than 3 seconds, 246 // if the difference between system and hardware time is more than 3 seconds,
246 // we have to set the RTC (hwclock --systohc), or alarms won't work reliably. 247 // we have to set the RTC (hwclock --systohc), or alarms won't work reliably.
247 248
248 if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 ) 249 if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 )
249 break; // ( 1, "ioctl RTC_SET_TIME" ); 250 break; // ( 1, "ioctl RTC_SET_TIME" );
250 } 251 }
251 252
252 // read the wakeup time from /etc/resumeat 253 // read the wakeup time from TIMEFILE
253 if (!( fp = fopen ( "/etc/resumeat", "r" ))) 254 if (!( fp = fopen ( TIMEFILE, "r" )))
254 break; // ( 1, "/etc/resumeat" ); 255 break; // ( 1, TIMEFILE );
255 256
256 if ( !fgets ( buf, sizeof( buf ) - 1, fp )) 257 if ( !fgets ( buf, sizeof( buf ) - 1, fp ))
257 break; // ( 1, "/etc/resumeat" ); 258 break; // ( 1, TIMEFILE );
258 259
259 fclose ( fp ); 260 fclose ( fp );
260 261
261 alrt = atoi ( buf ); // get the alarm time 262 alrt = atoi ( buf ); // get the alarm time
262 263
263 if ( alrt == 0 ) 264 if ( alrt == 0 )
264 break; // ( 0, "/etc/resumeat contains an invalid time description" ); 265 break; // ( 0, TIMEFILE " contains an invalid time description" );
265 alrt -= 5; // wake up 5 sec before the specified time 266 alrt -= 5; // wake up 5 sec before the specified time
266 267
267 alr = *gmtime ( &alrt ); 268 alr = *gmtime ( &alrt );
268 269
269 if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 ) // set RTC alarm time 270 if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 ) // set RTC alarm time
270 break; // ( 1, "ioctl RTC_ALM_SET" ); 271 break; // ( 1, "ioctl RTC_ALM_SET" );
271 272
272 if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) 273 if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 )
273 break; // ( 1, "ioctl RTC_AIE_ON" ); // enable RTC alarm irq 274 break; // ( 1, "ioctl RTC_AIE_ON" ); // enable RTC alarm irq
274 275
275 // tell the parent it is safe to exit now .. we have set the RTC alarm 276 // tell the parent it is safe to exit now .. we have set the RTC alarm
276 kill ( parent_pid, SIGUSR1 ); 277 kill ( parent_pid, SIGUSR1 );
277 278
278 if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) // wait for the RTC alarm irq 279 if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) // wait for the RTC alarm irq
279 break; // ( 1, "read rtc alarm" ); 280 break; // ( 1, "read rtc alarm" );
280 281
281 // iPAQ woke up via RTC irq -- otherwise we would have received a SIGUSR2 282 // iPAQ woke up via RTC irq -- otherwise we would have received a SIGUSR2
282 // from the "resume instance" of opiealarm. 283 // from the "resume instance" of opiealarm.
283 284
284 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) // disable RTC alarm irq 285 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) // disable RTC alarm irq
285 break; // ( 1, "ioctl RTC_AIE_OFF" ); 286 break; // ( 1, "ioctl RTC_AIE_OFF" );
286 287
287 close ( fd ); 288 close ( fd );
288 289
289 remove_pidfile ( ); 290 remove_pidfile ( );
290 291
291 return 0; 292 return 0;
292 293
293 } while ( 0 ) 294 } while ( 0 );
294 295
295 if ( fp != NULL ) 296 if ( fp != NULL )
296 fclose ( fp ); 297 fclose ( fp );
297 298
298 if ( fd != -1 ) 299 if ( fd != -1 )
299 close ( fd ); 300 close ( fd );
300 301
301 kill ( parent_pid, SIGUSR1 ); 302 kill ( parent_pid, SIGUSR1 );
302 303
303 while ( 1 ) // pretend that we are waiting on RTC, so opiealarm -r can kill us 304 while ( 1 ) // pretend that we are waiting on RTC, so opiealarm -r can kill us
304 sleep ( 1000 ); // if we don't do this, the "resuspend on AC" would be triggerd 305 sleep ( 1000 ); // if we don't do this, the "resuspend on AC" would be triggerd
305 return 0; 306 return 0;
306} 307}
307 308
308int onac ( void ) 309int onac ( void )
309{ 310{
310 FILE *fp; 311 FILE *fp;
311 int on = 0; 312 int on = 0;
312 313
313 // check the apm proc interface for AC status 314 // check the apm proc interface for AC status
314 315
315 if (( fp = fopen ( APMFILE, "r" ))) { 316 if (( fp = fopen ( APMFILE, "r" ))) {
316 int ac = 0; 317 int ac = 0;
317 318
318 if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) 319 if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 )
319 on = ( ac == 0x01 ) ? 1 : 0; 320 on = ( ac == 0x01 ) ? 1 : 0;
320 321
321 fclose ( fp ); 322 fclose ( fp );
322 } 323 }
323 return on; 324 return on;
324} 325}
325 326
326int resume ( int resuspend ) 327int resume ( int resuspend )
327{ 328{
328 FILE *fp; 329 FILE *fp;
329 330
330 // re-suspend when on AC (optional) when woken up via RTC 331 // re-suspend when on AC (optional) when woken up via RTC
331 332
332 if ( !opiealarm_was_running ) { 333 if ( !opiealarm_was_running ) {
333 // if opiealarm -s didn't wake up via RTC, the old process gets killed 334 // if opiealarm -s didn't wake up via RTC, the old process gets killed
334 // by kill_by_pidfile(), which is recorded in opiealarm_was_running 335 // by kill_by_pidfile(), which is recorded in opiealarm_was_running
335 336
336 if ( resuspend && onac ( )) { 337 if ( resuspend && onac ( )) {
337 time_t start, now; 338 time_t start, now;
338 char *argv [4]; 339 char *argv [4];
339 340
340 if ( !fork_with_pidfile ( )) 341 if ( !fork_with_pidfile ( ))
341 return 4; 342 return 4;
342 343
343 // we can't wait for the resuspend timeout in the parent process. 344 // we can't wait for the resuspend timeout in the parent process.
344 // so we fork and tell the parent it can exit immediatly 345 // so we fork and tell the parent it can exit immediatly
345 346
346 kill ( parent_pid, SIGUSR1 ); 347 kill ( parent_pid, SIGUSR1 );
347 348
348 // sleep <resuspend> seconds - this method is much more precise than sleep() ! 349 // sleep <resuspend> seconds - this method is much more precise than sleep() !
349 time ( &start ); 350 time ( &start );
350 do { 351 do {
351 sleep ( 1 ); 352 sleep ( 1 );
352 time ( &now ); 353 time ( &now );
353 } while (( now - start ) < resuspend ); 354 } while (( now - start ) < resuspend );
354 355
355 if ( onac ( )) { // still on ac ? 356 if ( onac ( )) { // still on ac ?
356 argv[0] = "qcop"; 357 argv[0] = "qcop";
357 argv[1] = "QPE/Desktop"; 358 argv[1] = "QPE/Desktop";