summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c
index 465e633..998cabd 100644
--- a/core/opiealarm/opiealarm.c
+++ b/core/opiealarm/opiealarm.c
@@ -1,101 +1,101 @@
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 /var/run/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 TIMEFILE "/var/run/resumeat"
30 #define APMFILE "/proc/apm" 30 #define APMFILE "/proc/apm"
31 31
32int resume ( int resuspend ); 32int resume ( int resuspend );
33int suspend ( int fix_rtc ); 33int suspend ( int fix_rtc );
34int main ( int argc, char **argv ); 34int main ( int argc, char **argv );
35int fork_with_pidfile ( void ); 35int fork_with_pidfile ( void );
36int kill_with_pidfile ( void ); 36int kill_with_pidfile ( void );
37void remove_pidfile ( void ); 37void remove_pidfile ( void );
38void usage ( void ); 38void usage ( void );
39void sig_handler_child ( int sig ); 39void sig_handler_child ( int sig );
40void sig_handler_parent ( int sig ); 40void sig_handler_parent ( int sig );
41int onac ( void ); 41int onac ( void );
42 42
43static int opiealarm_was_running; 43static int opiealarm_was_running;
44static pid_t parent_pid = 0; 44static pid_t parent_pid = 0;
45 45
46 46
47 47
48void sig_handler_child ( int sig ) 48void sig_handler_child ( int sig )
49{ 49{
50 // child got SIGUSR2 -> cleanup pidfile and exit 50 // child got SIGUSR2 -> cleanup pidfile and exit
51 51
52 remove_pidfile ( ); 52 remove_pidfile ( );
53 exit ( 0 ); 53 exit ( 0 );
54} 54}
55 55
56void sig_handler_parent ( int sig ) 56void sig_handler_parent ( int sig )
57{ 57{
58 // parent got SIGUSR1 -> safe to exit now 58 // parent got SIGUSR1 -> safe to exit now
59 59
60 parent_pid = 0; 60 parent_pid = 0;
61 exit ( 0 ); 61 exit ( 0 );
62} 62}
63 63
64void usage ( void ) 64void usage ( void )
65{ 65{
66 fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" ); 66 fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" );
67 fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" ); 67 fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" );
68 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" );
69 fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" ); 69 fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" );
70 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" );
71 exit ( 1 ); 71 exit ( 1 );
72} 72}
73 73
74int fork_with_pidfile ( void ) 74int fork_with_pidfile ( void )
75{ 75{
76 FILE *fp; 76 FILE *fp;
77 pid_t pid; 77 pid_t pid;
78 78
79 pid = fork ( ); 79 pid = fork ( );
80 80
81 if ( pid > 0 ) { 81 if ( pid > 0 ) {
82 // We can not just exit now, because the kernel could suspend 82 // We can not just exit now, because the kernel could suspend
83 // the iPAQ just before the child process sets the RTC. 83 // the iPAQ just before the child process sets the RTC.
84 // Solution: just wait for SIGUSR1 - the child process will 84 // Solution: just wait for SIGUSR1 - the child process will
85 // signal this when it thinks it is safe to exit. 85 // signal this when it thinks it is safe to exit.
86 86
87 signal ( SIGUSR1, sig_handler_parent ); 87 signal ( SIGUSR1, sig_handler_parent );
88 while ( 1 ) 88 while ( 1 )
89 sleep ( 1000 ); 89 sleep ( 1000 );
90 exit ( 0 ); 90 exit ( 0 );
91 } 91 }
92 else if ( pid < 0 ) { 92 else if ( pid < 0 ) {
93 perror ( "forking failed" ); 93 perror ( "forking failed" );
94 return 0; 94 return 0;
95 } 95 }
96 96
97 // child process needs to react to SIGUSR2. This is sent when 97 // child process needs to react to SIGUSR2. This is sent when
98 // a new opiealarm process is started. 98 // a new opiealarm process is started.
99 99
100 signal ( SIGUSR2, sig_handler_child ); 100 signal ( SIGUSR2, sig_handler_child );
101 101
@@ -278,106 +278,106 @@ int suspend ( int fix_rtc )
278 kill ( parent_pid, SIGUSR1 ); 278 kill ( parent_pid, SIGUSR1 );
279 279
280 if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) // wait for the RTC alarm irq 280 if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) // wait for the RTC alarm irq
281 break; // ( 1, "read rtc alarm" ); 281 break; // ( 1, "read rtc alarm" );
282 282
283 // iPAQ woke up via RTC irq -- otherwise we would have received a SIGUSR2 283 // iPAQ woke up via RTC irq -- otherwise we would have received a SIGUSR2
284 // from the "resume instance" of opiealarm. 284 // from the "resume instance" of opiealarm.
285 285
286 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) // disable RTC alarm irq 286 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) // disable RTC alarm irq
287 break; // ( 1, "ioctl RTC_AIE_OFF" ); 287 break; // ( 1, "ioctl RTC_AIE_OFF" );
288 288
289 close ( fd ); 289 close ( fd );
290 fd = -1; 290 fd = -1;
291 291
292 remove_pidfile ( ); 292 remove_pidfile ( );
293 293
294 return 0; 294 return 0;
295 295
296 } while ( 0 ); 296 } while ( 0 );
297 297
298 if ( fp != NULL ) 298 if ( fp != NULL )
299 fclose ( fp ); 299 fclose ( fp );
300 300
301 if ( fd != -1 ) 301 if ( fd != -1 )
302 close ( fd ); 302 close ( fd );
303 303
304 kill ( parent_pid, SIGUSR1 ); 304 kill ( parent_pid, SIGUSR1 );
305 305
306 while ( 1 ) // pretend that we are waiting on RTC, so opiealarm -r can kill us 306 while ( 1 ) // pretend that we are waiting on RTC, so opiealarm -r can kill us
307 sleep ( 1000 ); // if we don't do this, the "resuspend on AC" would be triggerd 307 sleep ( 1000 ); // if we don't do this, the "resuspend on AC" would be triggerd
308 return 0; 308 return 0;
309} 309}
310 310
311int onac ( void ) 311int onac ( void )
312{ 312{
313 FILE *fp; 313 FILE *fp;
314 int on = 0; 314 int on = 0;
315 315
316 // check the apm proc interface for AC status 316 // check the apm proc interface for AC status
317 317
318 if (( fp = fopen ( APMFILE, "r" ))) { 318 if (( fp = fopen ( APMFILE, "r" ))) {
319 int ac = 0; 319 int ac = 0;
320 320
321 if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) 321 if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 )
322 on = ( ac == 0x01 ) ? 1 : 0; 322 on = ( ac == 0x01 ) ? 1 : 0;
323 323
324 fclose ( fp ); 324 fclose ( fp );
325 } 325 }
326 return on; 326 return on;
327} 327}
328 328
329int resume ( int resuspend ) 329int resume ( int resuspend )
330{ 330{
331 FILE *fp; 331 FILE *fp;
332 332
333 // re-suspend when on AC (optional) when woken up via RTC 333 // re-suspend when on AC (optional) when woken up via RTC
334 334
335 if ( !opiealarm_was_running ) { 335 if ( !opiealarm_was_running ) {
336 // if opiealarm -s didn't wake up via RTC, the old process gets killed 336 // if opiealarm -s didn't wake up via RTC, the old process gets killed
337 // by kill_by_pidfile(), which is recorded in opiealarm_was_running 337 // by kill_by_pidfile(), which is recorded in opiealarm_was_running
338 338
339 if ( resuspend && onac ( )) { 339 if ( resuspend && onac ( )) {
340 time_t start, now; 340 time_t start, now;
341 char *argv [4]; 341 char *argv [4];
342 342
343 if ( !fork_with_pidfile ( )) 343 if ( !fork_with_pidfile ( ))
344 return 4; 344 return 4;
345 345
346 // we can't wait for the resuspend timeout in the parent process. 346 // we can't wait for the resuspend timeout in the parent process.
347 // so we fork and tell the parent it can exit immediatly 347 // so we fork and tell the parent it can exit immediatly
348 348
349 kill ( parent_pid, SIGUSR1 ); 349 kill ( parent_pid, SIGUSR1 );
350 350
351 // sleep <resuspend> seconds - this method is much more precise than sleep() ! 351 // sleep <resuspend> seconds - this method is much more precise than sleep() !
352 time ( &start ); 352 time ( &start );
353 do { 353 do {
354 sleep ( 1 ); 354 sleep ( 1 );
355 time ( &now ); 355 time ( &now );
356 } while (( now - start ) < resuspend ); 356 } while (( now - start ) < resuspend );
357 357
358 if ( onac ( )) { // still on ac ? 358 if ( onac ( )) { // still on ac ?
359 argv[0] = "qcop"; 359 argv[0] = "qcop";
360 argv[1] = "QPE/Desktop"; 360 argv[1] = "QPE/Desktop";
361 argv[2] = "suspend()"; 361 argv[2] = "suspend()";
362 argv[3] = 0; 362 argv[3] = 0;
363 363
364 // hard coded for now ...but needed 364 // hard coded for now ...but needed
365 // another way would be to simulate a power-button press 365 // another way would be to simulate a power-button press
366 366
367 setenv ( "LOGNAME", "root", 1 ); 367 setenv ( "LOGNAME", "root", 1 );
368 setenv ( "HOME", "/root", 1 ); 368 setenv ( "HOME", "/root", 1 );
369 setenv ( "LD_LIBRARY_PATH", "/opt/QtPalmtop/lib", 1 ); 369 setenv ( "LD_LIBRARY_PATH", "/opt/QtPalmtop/lib", 1 );
370 setenv ( "QTDIR", "/opt/QtPalmtop", 1 ); 370 setenv ( "QTDIR", "/opt/QtPalmtop", 1 );
371 371
372 remove_pidfile ( ); 372 remove_pidfile ( );
373 373
374 // no need for system() since this process is no longer usefull anyway 374 // no need for system() since this process is no longer usefull anyway
375 execv ( "/opt/QtPalmtop/bin/qcop", argv ); 375 execv ( "/opt/QtPalmtop/bin/qcop", argv );
376 376
377 perror ( "exec for qcop failed" ); 377 perror ( "exec for qcop failed" );
378 return 5; 378 return 5;
379 } 379 }
380 } 380 }
381 } 381 }
382 return 0; 382 return 0;
383} 383}