summaryrefslogtreecommitdiff
Unidiff
Diffstat (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
@@ -40,12 +40,14 @@ void usage ( void );
40void sig_handler ( int sig ); 40void sig_handler ( int sig );
41void error_msg_and_die ( int perr, const char *msg ); 41void error_msg_and_die ( int perr, const char *msg );
42int onac ( void ); 42int 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{
49 if ( log ) { 51 if ( log ) {
50 fprintf ( log, msg ); 52 fprintf ( log, msg );
51 fflush ( log ); 53 fflush ( log );
@@ -55,25 +57,35 @@ void log_msg ( const char *msg )
55void error_msg_and_die ( int perr, const char *msg ) 57void error_msg_and_die ( int perr, const char *msg )
56{ 58{
57 if ( perr ) 59 if ( perr )
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}
64 69
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" );
77 fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" ); 89 fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" );
78 fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" ); 90 fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" );
79 fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" ); 91 fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" );
@@ -85,14 +97,18 @@ int fork_with_pidfile ( void )
85{ 97{
86 FILE *fp; 98 FILE *fp;
87 pid_t pid; 99 pid_t pid;
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;
96 } 112 }
97 113
98 signal ( SIGTERM, sig_handler ); 114 signal ( SIGTERM, sig_handler );
@@ -177,24 +193,26 @@ int main ( int argc, char **argv )
177 } 193 }
178 194
179 if ( !mode ) 195 if ( !mode )
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
186 switch ( mode ) { 203 switch ( mode ) {
187 case 'r': return resume ( ac_resusp ); 204 case 'r': return resume ( ac_resusp );
188 case 's': 205 case 's':
189 default : return suspend ( fix_rtc ); 206 default : return suspend ( fix_rtc );
190 } 207 }
191 return 0; 208 return 0;
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;
198 char buf [64]; 216 char buf [64];
199 time_t alrt, syst, rtct; 217 time_t alrt, syst, rtct;
200 struct tm alr, sys, rtc; 218 struct tm alr, sys, rtc;
@@ -206,13 +224,12 @@ int suspend ( int fix_rtc )
206 return 3; 224 return 3;
207 225
208 log = fopen ( "/tmp/opiealarm.log", "w" ); 226 log = fopen ( "/tmp/opiealarm.log", "w" );
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
216 if ( !fgets ( buf, sizeof( buf ) - 1, fp )) 233 if ( !fgets ( buf, sizeof( buf ) - 1, fp ))
217 error_msg_and_die ( 1, "/etc/resumeat" ); 234 error_msg_and_die ( 1, "/etc/resumeat" );
218 235
@@ -220,14 +237,14 @@ int suspend ( int fix_rtc )
220 237
221 alrt = atoi ( buf ); 238 alrt = atoi ( buf );
222 239
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 );
231 248
232 tzset ( ); 249 tzset ( );
233 250
@@ -271,13 +288,15 @@ int suspend ( int fix_rtc )
271 if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 ) 288 if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 )
272 error_msg_and_die ( 1, "ioctl RTC_ALM_SET" ); 289 error_msg_and_die ( 1, "ioctl RTC_ALM_SET" );
273 // enable alarm irq 290 // enable alarm irq
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 )
281 error_msg_and_die ( 1, "read rtc alarm" ); 300 error_msg_and_die ( 1, "read rtc alarm" );
282 301
283 log_msg ( "WAKEUP\n" ); 302 log_msg ( "WAKEUP\n" );
@@ -323,12 +342,13 @@ int resume ( int resuspend )
323 if ( resuspend && onac ( )) { 342 if ( resuspend && onac ( )) {
324 time_t start, now; 343 time_t start, now;
325 char *argv [4]; 344 char *argv [4];
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 );
332 do { 352 do {
333 sleep ( 1 ); 353 sleep ( 1 );
334 time ( &now ); 354 time ( &now );