summaryrefslogtreecommitdiff
path: root/core/opiealarm
authorsandman <sandman>2002-07-01 21:28:44 (UTC)
committer sandman <sandman>2002-07-01 21:28:44 (UTC)
commitcce43bef3e68b007f027f52328972b6b86af1098 (patch) (unidiff)
tree1bd915e0c5901f1b47e0bd66d696828413eaf0ce /core/opiealarm
parent3c6fd2159f4ff609408a2b5c645ee5a454d01325 (diff)
downloadopie-cce43bef3e68b007f027f52328972b6b86af1098.zip
opie-cce43bef3e68b007f027f52328972b6b86af1098.tar.gz
opie-cce43bef3e68b007f027f52328972b6b86af1098.tar.bz2
Fixed a race. Kernel could send iPAQ sleeping while opiealarm had not yet
set the RTC alarm.
Diffstat (limited to 'core/opiealarm') (more/less context) (ignore whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c
index ce0103e..d96ffaf 100644
--- a/core/opiealarm/opiealarm.c
+++ b/core/opiealarm/opiealarm.c
@@ -43,6 +43,8 @@ int onac ( void );
43 43
44static int opiealarm_was_running; 44static int opiealarm_was_running;
45 45
46static pid_t startpid = 0;
47
46 48
47void log_msg ( const char *msg ) 49void log_msg ( const char *msg )
48{ 50{
@@ -58,6 +60,9 @@ void error_msg_and_die ( int perr, const char *msg )
58 log_msg ( strerror ( errno )); 60 log_msg ( strerror ( errno ));
59 log_msg ( msg ); 61 log_msg ( msg );
60 62
63 if ( getpid ( ) != startpid )
64 kill ( startpid, SIGUSR1 );
65
61 while ( 1 ) // pretend we are waiting on RTC, so opiealarm -r can kill us 66 while ( 1 ) // pretend we are waiting on RTC, so opiealarm -r can kill us
62 sleep ( 1 ); 67 sleep ( 1 );
63} 68}
@@ -65,12 +70,19 @@ void error_msg_and_die ( int perr, const char *msg )
65 70
66void sig_handler ( int sig ) 71void sig_handler ( int sig )
67{ 72{
68 log_msg ( "GOT SIGNAL -> EXITING\n" ); 73 if ( log ) {
69 fclose ( log ); 74 log_msg ( "GOT SIGNAL -> EXITING\n" );
75 fclose ( log );
76 }
70 remove_pidfile ( ); 77 remove_pidfile ( );
71 exit ( 0 ); 78 exit ( 0 );
72} 79}
73 80
81void sig_handler_parent ( int sig )
82{
83 exit ( 0 );
84}
85
74void usage ( void ) 86void usage ( void )
75{ 87{
76 fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" ); 88 fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" );
@@ -88,8 +100,12 @@ int fork_with_pidfile ( void )
88 100
89 pid = fork ( ); 101 pid = fork ( );
90 102
91 if ( pid > 0 ) 103 if ( pid > 0 ) {
104 signal ( SIGUSR1, sig_handler_parent );
105 while ( 1 )
106 sleep ( 1000 );
92 exit ( 0 ); 107 exit ( 0 );
108 }
93 else if ( pid < 0 ) { 109 else if ( pid < 0 ) {
94 perror ( "forking failed" ); 110 perror ( "forking failed" );
95 return 0; 111 return 0;
@@ -180,6 +196,7 @@ int main ( int argc, char **argv )
180 usage ( ); 196 usage ( );
181 197
182 // kill running opiealarm 198 // kill running opiealarm
199 startpid = getpid ( );
183 opiealarm_was_running = kill_with_pidfile ( ); 200 opiealarm_was_running = kill_with_pidfile ( );
184 remove_pidfile ( ); 201 remove_pidfile ( );
185 202
@@ -192,6 +209,7 @@ int main ( int argc, char **argv )
192 } 209 }
193 210
194 211
212
195int suspend ( int fix_rtc ) 213int suspend ( int fix_rtc )
196{ 214{
197 FILE *fp; 215 FILE *fp;
@@ -209,7 +227,6 @@ int suspend ( int fix_rtc )
209 log_msg ( "STARTING\n" ); 227 log_msg ( "STARTING\n" );
210 228
211 229
212
213 if (!( fp = fopen ( "/etc/resumeat", "r" ))) 230 if (!( fp = fopen ( "/etc/resumeat", "r" )))
214 error_msg_and_die ( 1, "/etc/resumeat" ); 231 error_msg_and_die ( 1, "/etc/resumeat" );
215 232
@@ -223,8 +240,8 @@ int suspend ( int fix_rtc )
223 if ( alrt == 0 ) 240 if ( alrt == 0 )
224 error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" ); 241 error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" );
225 242
226 /* subtract 5 sec from event time... */ 243 alrt -= 5; // wake up 5 sec before the specified time
227 alrt -= 5; 244
228 245
229 if ( log ) 246 if ( log )
230 fprintf ( log, "Setting RTC alarm to %d\n", alrt ); 247 fprintf ( log, "Setting RTC alarm to %d\n", alrt );
@@ -274,7 +291,9 @@ int suspend ( int fix_rtc )
274 if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) 291 if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 )
275 error_msg_and_die ( 1, "ioctl RTC_AIE_ON" ); 292 error_msg_and_die ( 1, "ioctl RTC_AIE_ON" );
276 293
277 log_msg ( "SLEEPING\n" ); 294 if ( log )
295 fprintf( log, "SIGUSR: pid %d - SLEEPING: pid %d\n", startpid, getpid ( ));
296 kill ( startpid, SIGUSR1 );
278 297
279 // wait for alarm irq 298 // wait for alarm irq
280 if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) 299 if ( read ( fd, buf, sizeof( unsigned long )) < 0 )
@@ -326,6 +345,7 @@ int resume ( int resuspend )
326 345
327 if ( !fork_with_pidfile ( )) 346 if ( !fork_with_pidfile ( ))
328 return 4; 347 return 4;
348 kill ( startpid, SIGUSR1 );
329 349
330 // sleep <resuspend> sec (not less!) 350 // sleep <resuspend> sec (not less!)
331 time ( &start ); 351 time ( &start );