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
@@ -34,71 +34,87 @@ int resume ( int resuspend );
34int suspend ( int fix_rtc ); 34int suspend ( int fix_rtc );
35int main ( int argc, char **argv ); 35int main ( int argc, char **argv );
36int fork_with_pidfile ( void ); 36int fork_with_pidfile ( void );
37int kill_with_pidfile ( void ); 37int kill_with_pidfile ( void );
38void remove_pidfile ( void ); 38void remove_pidfile ( void );
39void usage ( void ); 39void 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 );
52 } 54 }
53} 55}
54 56
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" );
80 fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" ); 92 fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" );
81 exit ( 1 ); 93 exit ( 1 );
82} 94}
83 95
84int fork_with_pidfile ( void ) 96int 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 );
99 signal ( SIGINT, sig_handler ); 115 signal ( SIGINT, sig_handler );
100 116
101 // save pid 117 // save pid
102 if (( fp = fopen ( PIDFILE, "w" ))) { 118 if (( fp = fopen ( PIDFILE, "w" ))) {
103 fprintf ( fp, "%d", getpid ( )); 119 fprintf ( fp, "%d", getpid ( ));
104 fclose ( fp ); 120 fclose ( fp );
@@ -171,69 +187,70 @@ int main ( int argc, char **argv )
171 } 187 }
172 } 188 }
173 189
174 if ( geteuid ( ) != 0 ) { 190 if ( geteuid ( ) != 0 ) {
175 fprintf ( stderr, "You need root priviledges to run opiealarm." ); 191 fprintf ( stderr, "You need root priviledges to run opiealarm." );
176 return 2; 192 return 2;
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;
201 int fd; 219 int fd;
202 int rtc_sys_diff; 220 int rtc_sys_diff;
203 221
204 222
205 if ( !fork_with_pidfile ( )) 223 if ( !fork_with_pidfile ( ))
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
219 fclose ( fp ); 236 fclose ( fp );
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
234 alr = *gmtime ( &alrt ); 251 alr = *gmtime ( &alrt );
235 252
236 // get system time 253 // get system time
237 time ( &syst ); 254 time ( &syst );
238 sys = *localtime ( &syst ); 255 sys = *localtime ( &syst );
239 256
@@ -265,25 +282,27 @@ int suspend ( int fix_rtc )
265 282
266 if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 ) 283 if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 )
267 error_msg_and_die ( 1, "ioctl RTC_SET_TIME" ); 284 error_msg_and_die ( 1, "ioctl RTC_SET_TIME" );
268 } 285 }
269 286
270 // set alarm time 287 // set alarm time
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" );
284 303
285 // disable alarm irq 304 // disable alarm irq
286 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) 305 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 )
287 error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" ); 306 error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" );
288 307
289 close ( fd ); 308 close ( fd );
@@ -317,24 +336,25 @@ int resume ( int resuspend )
317{ 336{
318 FILE *fp; 337 FILE *fp;
319 338
320 // re-suspend when on AC (optional) when woken up via RTC 339 // re-suspend when on AC (optional) when woken up via RTC
321 340
322 if ( !opiealarm_was_running ) { // opiealarm -s got it's RTC signal -> wake up by RTC 341 if ( !opiealarm_was_running ) { // opiealarm -s got it's RTC signal -> wake up by RTC
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 );
335 } while (( now - start ) < resuspend ); 355 } while (( now - start ) < resuspend );
336 356
337 if ( onac ( )) { // still on ac 357 if ( onac ( )) { // still on ac
338 // system() without fork 358 // system() without fork
339 argv[0] = "qcop"; 359 argv[0] = "qcop";
340 argv[1] = "QPE/Desktop"; 360 argv[1] = "QPE/Desktop";