summaryrefslogtreecommitdiff
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c246
1 files changed, 117 insertions, 129 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c
index d96ffaf..70e5415 100644
--- a/core/opiealarm/opiealarm.c
+++ b/core/opiealarm/opiealarm.c
@@ -1,11 +1,11 @@
-/* opiealarm.c
-* This program is for extracting the event time/date out
-* of /etc/resumeat and setting the RTC alarm to that time/date.
-* It is designed to run via a script just before the iPaq
-* is suspended.
-*
-* Roughly based on ipaqalarm from Benjamin Long
-*
-* written by Robert Griebl <sandman@handhelds.org>
-*/
+/*
+ * opiealarm.c
+ *
+ * This program is for extracting the event time/date out
+ * of /etc/resumeat and setting the RTC alarm to that time/date.
+ * It is designed to run via a script just before the iPAQ
+ * is suspended and right after the iPAQ resumes operation.
+ *
+ * written and copyrighted by Robert Griebl <sandman@handhelds.org>
+ */
@@ -30,4 +30,2 @@
-FILE *log; // debug only
-
int resume ( int resuspend );
@@ -39,4 +37,4 @@ void remove_pidfile ( void );
void usage ( void );
-void sig_handler ( int sig );
-void error_msg_and_die ( int perr, const char *msg );
+void sig_handler_child ( int sig );
+void sig_handler_parent ( int sig );
int onac ( void );
@@ -44,34 +42,10 @@ int onac ( void );
static int opiealarm_was_running;
-
-static pid_t startpid = 0;
+static pid_t parent_pid = 0;
-void log_msg ( const char *msg )
-{
- if ( log ) {
- fprintf ( log, msg );
- fflush ( log );
- }
-}
-void error_msg_and_die ( int perr, const char *msg )
+void sig_handler_child ( int sig )
{
- if ( perr )
- log_msg ( strerror ( errno ));
- log_msg ( msg );
-
- if ( getpid ( ) != startpid )
- kill ( startpid, SIGUSR1 );
+ // child got SIGUSR2 -> cleanup pidfile and exit
- while ( 1 ) // pretend we are waiting on RTC, so opiealarm -r can kill us
- sleep ( 1 );
-}
-
-
-void sig_handler ( int sig )
-{
- if ( log ) {
- log_msg ( "GOT SIGNAL -> EXITING\n" );
- fclose ( log );
- }
remove_pidfile ( );
@@ -82,2 +56,5 @@ void sig_handler_parent ( int sig )
{
+ // parent got SIGUSR1 -> safe to exit now
+
+ parent_pid = 0;
exit ( 0 );
@@ -103,2 +80,7 @@ int fork_with_pidfile ( void )
if ( pid > 0 ) {
+ // We can not just exit now, because the kernel could suspend
+ // the iPAQ just before the child process sets the RTC.
+ // Solution: just wait for SIGUSR1 - the child process will
+ // signal this when it thinks it is safe to exit.
+
signal ( SIGUSR1, sig_handler_parent );
@@ -113,4 +95,6 @@ int fork_with_pidfile ( void )
- signal ( SIGTERM, sig_handler );
- signal ( SIGINT, sig_handler );
+ // child process needs to react to SIGUSR2. This is sent when
+ // a new opiealarm process is started.
+
+ signal ( SIGUSR2, sig_handler_child );
@@ -140,2 +124,6 @@ int kill_with_pidfile ( void )
pid_t pid;
+ int res = 0;
+
+ // terminate a running opiealarm child process
+ // return 1 if we really killed one
@@ -143,6 +131,6 @@ int kill_with_pidfile ( void )
if ( fscanf ( fp, "%d", &pid ) == 1 )
- return ( kill ( pid, SIGTERM ) == 0 ) ? 1 : 0;
+ res = ( kill ( pid, SIGUSR2 ) == 0 ) ? 1 : 0;
fclose ( fp );
}
- return 0;
+ return res;
}
@@ -151,6 +139,6 @@ void remove_pidfile ( void )
{
- unlink ( PIDFILE );
-
- signal ( SIGTERM, SIG_DFL );
- signal ( SIGINT, SIG_DFL );
+ // child is about to exit - cleanup
+
+ unlink ( PIDFILE );
+ signal ( SIGUSR2, SIG_DFL );
}
@@ -197,4 +185,6 @@ int main ( int argc, char **argv )
+
+ parent_pid = getpid ( );
+
// kill running opiealarm
- startpid = getpid ( );
opiealarm_was_running = kill_with_pidfile ( );
@@ -203,7 +193,11 @@ int main ( int argc, char **argv )
switch ( mode ) {
- case 'r': return resume ( ac_resusp );
+ case 'r': opt = resume ( ac_resusp );
+ break;
case 's':
- default : return suspend ( fix_rtc );
+ default : opt = suspend ( fix_rtc );
+ break;
}
- return 0;
+
+ parent_pid = 0;
+ return opt;
}
@@ -211,3 +205,2 @@ int main ( int argc, char **argv )
-
int suspend ( int fix_rtc )
@@ -224,92 +217,76 @@ int suspend ( int fix_rtc )
return 3;
-
- log = fopen ( "/tmp/opiealarm.log", "w" );
- log_msg ( "STARTING\n" );
-
- if (!( fp = fopen ( "/etc/resumeat", "r" )))
- error_msg_and_die ( 1, "/etc/resumeat" );
-
- if ( !fgets ( buf, sizeof( buf ) - 1, fp ))
- error_msg_and_die ( 1, "/etc/resumeat" );
+ // we are the child process from here on ...
- fclose ( fp );
+ tzset ( ); // not sure if it is really needed -- it probably doesn't hurt ...
- alrt = atoi ( buf );
+ do { // try/catch simulation
- if ( alrt == 0 )
- error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" );
+ // read the wakeup time from /etc/resumeat
+ if (!( fp = fopen ( "/etc/resumeat", "r" )))
+ break; // ( 1, "/etc/resumeat" );
- alrt -= 5; // wake up 5 sec before the specified time
-
+ if ( !fgets ( buf, sizeof( buf ) - 1, fp ))
+ break; // ( 1, "/etc/resumeat" );
- if ( log )
- fprintf ( log, "Setting RTC alarm to %d\n", alrt );
-
- tzset ( );
+ fclose ( fp );
- alr = *gmtime ( &alrt );
-
- // get system time
- time ( &syst );
- sys = *localtime ( &syst );
+ alrt = atoi ( buf ); // get the alarm time
+ if ( alrt == 0 )
+ break; // ( 0, "/etc/resumeat contains an invalid time description" );
+ alrt -= 5; // wake up 5 sec before the specified time
+ alr = *gmtime ( &alrt );
+
+ time ( &syst );// get the UNIX system time
+ sys = *localtime ( &syst );
- // Write alarm time to RTC
- if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 )
- error_msg_and_die ( 1, "/dev/misc/rtc" );
+ if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 ) // open the RTC device
+ break; // ( 1, "/dev/misc/rtc" );
- // get RTC time
- memset ( &rtc, 0, sizeof ( struct tm ));
- if ( ioctl ( fd, RTC_RD_TIME, &rtc ) < 0 )
- error_msg_and_die ( 1, "ioctl RTC_RD_TIME" );
- rtct = mktime ( &rtc );
+ memset ( &rtc, 0, sizeof ( struct tm )); // get the RTC time
+ if ( ioctl ( fd, RTC_RD_TIME, &rtc ) < 0 )
+ break; // ( 1, "ioctl RTC_RD_TIME" );
+ rtct = mktime ( &rtc );
- fprintf ( log, "System time: %02d.%02d.%04d %02d:%02d:%02d DST: %d (TZ: %s, offset: %d)\n", sys. tm_mday, sys. tm_mon + 1, sys. tm_year + 1900, sys. tm_hour, sys. tm_min, sys. tm_sec, sys. tm_isdst, sys. tm_zone, sys. tm_gmtoff );
- fprintf ( log, "RTC time: %02d.%02d.%04d %02d:%02d:%02d DST: %d (TZ: %s, offset: %d)\n", rtc. tm_mday, rtc. tm_mon + 1, rtc. tm_year + 1900, rtc. tm_hour, rtc. tm_min, rtc. tm_sec, rtc. tm_isdst, rtc. tm_zone, rtc. tm_gmtoff );
- fprintf ( log, "Wakeup time: %02d.%02d.%04d %02d:%02d:%02d DST: %d (TZ: %s, offset: %d)\n", alr. tm_mday, alr. tm_mon + 1, alr. tm_year + 1900, alr. tm_hour, alr. tm_min, alr. tm_sec, alr. tm_isdst, alr. tm_zone, alr. tm_gmtoff );
-
- fprintf ( log, "System/RTC diff: %d seconds\n", ( syst - rtct ) - sys. tm_gmtoff );
-
-
- rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff;
+ rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff; // calculate the difference between system and hardware time
- if ( fix_rtc && (( rtc_sys_diff < -4 ) || ( rtc_sys_diff > 4 ))) {
- struct tm set;
-
- set = *gmtime ( &syst );
+ if ( fix_rtc && (( rtc_sys_diff < -3 ) || ( rtc_sys_diff > 3 ))) {
+ struct tm set;
+ set = *gmtime ( &syst );
+
+ // if the difference between system and hardware time is more than 3 seconds,
+ // we have to set the RTC (hwclock --systohc), or alarms won't work reliably.
- fprintf ( log, "Correcting RTC: %d seconds\n", rtc_sys_diff );
+ if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 )
+ break; // ( 1, "ioctl RTC_SET_TIME" );
+ }
- if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 )
- error_msg_and_die ( 1, "ioctl RTC_SET_TIME" );
- }
+ if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 ) // set RTC alarm time
+ break; // ( 1, "ioctl RTC_ALM_SET" );
+ if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 )
+ break; // ( 1, "ioctl RTC_AIE_ON" ); // enable RTC alarm irq
- // set alarm time
- if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 )
- error_msg_and_die ( 1, "ioctl RTC_ALM_SET" );
- // enable alarm irq
- if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 )
- error_msg_and_die ( 1, "ioctl RTC_AIE_ON" );
-
- if ( log )
- fprintf( log, "SIGUSR: pid %d - SLEEPING: pid %d\n", startpid, getpid ( ));
- kill ( startpid, SIGUSR1 );
+ // tell the parent it is safe to exit now .. we have set the RTC alarm
+ kill ( parent_pid, SIGUSR1 );
- // wait for alarm irq
- if ( read ( fd, buf, sizeof( unsigned long )) < 0 )
- error_msg_and_die ( 1, "read rtc alarm" );
+ if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) // wait for the RTC alarm irq
+ break; // ( 1, "read rtc alarm" );
- log_msg ( "WAKEUP\n" );
+ // iPAQ woke up via RTC irq -- otherwise we would have received a SIGUSR2
+ // from the "resume instance" of opiealarm.
- // disable alarm irq
- if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 )
- error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" );
+ if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) // disable RTC alarm irq
+ break; // ( 1, "ioctl RTC_AIE_OFF" );
- close ( fd );
-
- log_msg ( "EXITING\n" );
+ close ( fd );
- fclose ( log );
- remove_pidfile ( );
+ remove_pidfile ( ); // normal exit
+ return 0;
+
+ } while ( 0 );
+ kill ( parent_pid, SIGUSR1 ); // parent is still running - it can exit now
+
+ while ( 1 ) // pretend that we are waiting on RTC, so opiealarm -r can kill us
+ sleep ( 1000 ); // if we don't do this, the "resuspend on AC" would be triggerd
return 0;
@@ -323,2 +300,4 @@ int onac ( void )
+ // check the apm proc interface for AC status
+
if (( fp = fopen ( APMFILE, "r" ))) {
@@ -340,3 +319,6 @@ int resume ( int resuspend )
- if ( !opiealarm_was_running ) { // opiealarm -s got it's RTC signal -> wake up by RTC
+ if ( !opiealarm_was_running ) {
+ // if opiealarm -s didn't wake up via RTC, the old process gets killed
+ // by kill_by_pidfile(), which is recorded in opiealarm_was_running
+
if ( resuspend && onac ( )) {
@@ -347,5 +329,9 @@ int resume ( int resuspend )
return 4;
- kill ( startpid, SIGUSR1 );
+
+ // we can't wait for the resuspend timeout in the parent process.
+ // so we fork and tell the parent it can exit immediatly
+
+ kill ( parent_pid, SIGUSR1 );
- // sleep <resuspend> sec (not less!)
+ // sleep <resuspend> seconds - this method is much more precise than sleep() !
time ( &start );
@@ -356,4 +342,3 @@ int resume ( int resuspend )
- if ( onac ( )) { // still on ac
- // system() without fork
+ if ( onac ( )) { // still on ac ?
argv[0] = "qcop";
@@ -364,2 +349,4 @@ int resume ( int resuspend )
// hard coded for now ...but needed
+ // another way would be to simulate a power-button press
+
setenv ( "LOGNAME", "root", 1 );
@@ -371,2 +358,3 @@ int resume ( int resuspend )
+ // no need for system() since this process is no longer usefull anyway
execv ( "/opt/QtPalmtop/bin/qcop", argv );