summaryrefslogtreecommitdiff
authorsandman <sandman>2002-12-10 22:21:58 (UTC)
committer sandman <sandman>2002-12-10 22:21:58 (UTC)
commit887fb36c5344574f04d19c7442f3db899920e04d (patch) (unidiff)
tree3a1ccbdb5489bf628979a2b712a779ae009383dc
parent2173e57fcbd5d460fceb34a111845389dca56506 (diff)
downloadopie-887fb36c5344574f04d19c7442f3db899920e04d.zip
opie-887fb36c5344574f04d19c7442f3db899920e04d.tar.gz
opie-887fb36c5344574f04d19c7442f3db899920e04d.tar.bz2
/etc/resumeat is now in /var/run/resumeat
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c15
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
@@ -1,60 +1,61 @@
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 /etc/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 APMFILE "/proc/apm" 30 #define APMFILE "/proc/apm"
30 31
31int resume ( int resuspend ); 32int resume ( int resuspend );
32int suspend ( int fix_rtc ); 33int suspend ( int fix_rtc );
33int main ( int argc, char **argv ); 34int main ( int argc, char **argv );
34int fork_with_pidfile ( void ); 35int fork_with_pidfile ( void );
35int kill_with_pidfile ( void ); 36int kill_with_pidfile ( void );
36void remove_pidfile ( void ); 37void remove_pidfile ( void );
37void usage ( void ); 38void usage ( void );
38void sig_handler_child ( int sig ); 39void sig_handler_child ( int sig );
39void sig_handler_parent ( int sig ); 40void sig_handler_parent ( int sig );
40int onac ( void ); 41int onac ( void );
41 42
42static int opiealarm_was_running; 43static int opiealarm_was_running;
43static pid_t parent_pid = 0; 44static pid_t parent_pid = 0;
44 45
45 46
46 47
47void sig_handler_child ( int sig ) 48void sig_handler_child ( int sig )
48{ 49{
49 // child got SIGUSR2 -> cleanup pidfile and exit 50 // child got SIGUSR2 -> cleanup pidfile and exit
50 51
51 remove_pidfile ( ); 52 remove_pidfile ( );
52 exit ( 0 ); 53 exit ( 0 );
53} 54}
54 55
55void sig_handler_parent ( int sig ) 56void sig_handler_parent ( int sig )
56{ 57{
57 // parent got SIGUSR1 -> safe to exit now 58 // parent got SIGUSR1 -> safe to exit now
58 59
59 parent_pid = 0; 60 parent_pid = 0;
60 exit ( 0 ); 61 exit ( 0 );
@@ -216,110 +217,110 @@ int suspend ( int fix_rtc )
216 if ( !fork_with_pidfile ( )) 217 if ( !fork_with_pidfile ( ))
217 return 3; 218 return 3;
218 219
219 // we are the child process from here on ... 220 // we are the child process from here on ...
220 221
221 tzset ( ); // not sure if it is really needed -- it probably doesn't hurt ... 222 tzset ( ); // not sure if it is really needed -- it probably doesn't hurt ...
222 223
223 time ( &syst );// get the UNIX system time 224 time ( &syst );// get the UNIX system time
224 sys = *localtime ( &syst ); 225 sys = *localtime ( &syst );
225 226
226 do { 227 do {
227 228
228 if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 ) 229 if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 )
229 if (( fd = open ( "/dev/rtc", O_RDWR )) < 0 ) 230 if (( fd = open ( "/dev/rtc", O_RDWR )) < 0 )
230 break; // ( 1, "rtc" ); 231 break; // ( 1, "rtc" );
231 232
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
308int onac ( void ) 309int onac ( void )
309{ 310{
310 FILE *fp; 311 FILE *fp;
311 int on = 0; 312 int on = 0;
312 313
313 // check the apm proc interface for AC status 314 // check the apm proc interface for AC status
314 315
315 if (( fp = fopen ( APMFILE, "r" ))) { 316 if (( fp = fopen ( APMFILE, "r" ))) {
316 int ac = 0; 317 int ac = 0;
317 318
318 if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) 319 if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 )
319 on = ( ac == 0x01 ) ? 1 : 0; 320 on = ( ac == 0x01 ) ? 1 : 0;
320 321
321 fclose ( fp ); 322 fclose ( fp );
322 } 323 }
323 return on; 324 return on;
324} 325}
325 326