summaryrefslogtreecommitdiff
authorsandman <sandman>2002-06-01 19:01:22 (UTC)
committer sandman <sandman>2002-06-01 19:01:22 (UTC)
commit9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1 (patch) (side-by-side diff)
tree16bc3af93db4a6677f324a48efe9663361e576b9
parentbc167b7ef02ccd5f6127d9f9640f4b7af6c5a496 (diff)
downloadopie-9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1.zip
opie-9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1.tar.gz
opie-9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1.tar.bz2
Changed opiealarm into a real daemon (fork/detach)
Logging to /tmp/opiealarm.log is intentionally active at the moment.
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c98
1 files changed, 87 insertions, 11 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c
index 071cb24..33d631d 100644
--- a/core/opiealarm/opiealarm.c
+++ b/core/opiealarm/opiealarm.c
@@ -21,4 +21,19 @@
#include <stdlib.h>
+#include <syslog.h>
+#include <signal.h>
+#define PIDFILE "/var/run/opiealarm.pid"
+
+FILE *log;
+
+
+void log_msg ( const char *msg )
+{
+ if ( log ) {
+ fprintf ( log, msg );
+ fflush ( log );
+ }
+}
+
void error_msg_and_die ( int perr, const char *msg )
@@ -26,5 +41,6 @@ void error_msg_and_die ( int perr, const char *msg )
if ( perr )
- perror ( msg );
- else
- fprintf ( stderr, "%s\n", msg );
+ log_msg ( strerror ( errno ));
+ log_msg ( msg );
+
+ unlink ( PIDFILE );
exit ( 1 );
@@ -33,3 +49,4 @@ void error_msg_and_die ( int perr, const char *msg )
-void extractevent ( )
+
+void suspend_on_rtc ( )
{
@@ -41,2 +58,3 @@ void extractevent ( )
+
if (!( fp = fopen ( "/etc/resumeat", "r" )))
@@ -56,9 +74,11 @@ void extractevent ( )
t -= 5;
+
+ if ( log )
+ fprintf ( log, "Setting RTC alarm to %d\n", t );
+
tm = gmtime ( &t );
- /* Write alarm time to RTC */
- fd = open ( "/dev/misc/rtc", O_RDWR );
- if ( fd < 0 )
+ // Write alarm time to RTC
+ if (( fd = open ( "/dev/misc/rtc", O_RDWR ) < 0 );
error_msg_and_die ( 1, "/dev/misc/rtc" );
-
// set alarm time
@@ -66,3 +86,2 @@ void extractevent ( )
error_msg_and_die ( 1, "ioctl RTC_ALM_SET" );
-
// enable alarm irq
@@ -71,2 +90,4 @@ void extractevent ( )
+ log_msg ( "SLEEPING\n" );
+
// wait for alarm irq
@@ -75,2 +96,4 @@ void extractevent ( )
+ log_msg ( "WAKEUP\n" );
+
// disable alarm irq
@@ -83,8 +106,61 @@ void extractevent ( )
+void sig_handler ( int sig )
+{
+ log_msg ( "GOT SIGNAL -> EXITING\n" );
+ fclose ( log );
+ unlink ( PIDFILE );
+ exit ( 0 );
+}
+
+
int main ( )
{
- if ( geteuid ( ) != 0 )
- error_msg_and_die ( 0, "You need root priviledges to run opiealarm." );
+ FILE *fp;
+ pid_t pid;
+
+ if ( geteuid ( ) != 0 ) {
+ fprintf ( stderr, "You need root priviledges to run opiealarm." );
+ return 1;
+ }
+
+ pid = fork ( );
+
+ if ( pid > 0 )
+ return 0;
+ else if ( pid < 0 ) {
+ perror ( "Could not fork." );
+ return 2;
+ }
+
+ // save pid
+ if (!( fp = fopen ( PIDFILE, "w" ))) {
+ perror ( PIDFILE );
+ return 3;
+ }
+
+ fprintf ( fp, "%d", getpid ( ));
+ fclose ( fp );
+
+ // detach
+ close ( 0 );
+ close ( 1 );
+ close ( 2 );
+
+ setpgid ( 0, 0 );
+
+ log = fopen ( "/tmp/opiealarm.log", "w" );
+ log_msg ( "STARTING\n" );
+
+ signal ( SIGTERM, sig_handler );
+ signal ( SIGINT, sig_handler );
extractevent ( );
+
+ signal ( SIGTERM, SIG_DFL );
+ signal ( SIGINT, SIG_DFL );
+
+ log_msg ( "EXITING\n" );
+
+ fclose ( log );
+ unlink ( PIDFILE );
return 0;