-rw-r--r-- | core/opiealarm/opiealarm.c | 15 |
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 | |||
@@ -13,32 +13,33 @@ | |||
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 | ||
31 | int resume ( int resuspend ); | 32 | int resume ( int resuspend ); |
32 | int suspend ( int fix_rtc ); | 33 | int suspend ( int fix_rtc ); |
33 | int main ( int argc, char **argv ); | 34 | int main ( int argc, char **argv ); |
34 | int fork_with_pidfile ( void ); | 35 | int fork_with_pidfile ( void ); |
35 | int kill_with_pidfile ( void ); | 36 | int kill_with_pidfile ( void ); |
36 | void remove_pidfile ( void ); | 37 | void remove_pidfile ( void ); |
37 | void usage ( void ); | 38 | void usage ( void ); |
38 | void sig_handler_child ( int sig ); | 39 | void sig_handler_child ( int sig ); |
39 | void sig_handler_parent ( int sig ); | 40 | void sig_handler_parent ( int sig ); |
40 | int onac ( void ); | 41 | int onac ( void ); |
41 | 42 | ||
42 | static int opiealarm_was_running; | 43 | static int opiealarm_was_running; |
43 | static pid_t parent_pid = 0; | 44 | static pid_t parent_pid = 0; |
44 | 45 | ||
@@ -232,78 +233,78 @@ int suspend ( int fix_rtc ) | |||
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 | ||
308 | int onac ( void ) | 309 | int onac ( void ) |
309 | { | 310 | { |