summaryrefslogtreecommitdiff
path: root/core
authorsandman <sandman>2002-06-01 19:01:22 (UTC)
committer sandman <sandman>2002-06-01 19:01:22 (UTC)
commit9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1 (patch) (unidiff)
tree16bc3af93db4a6677f324a48efe9663361e576b9 /core
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 (limited to 'core') (more/less context) (ignore whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c102
1 files changed, 89 insertions, 13 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 @@
21#include <stdlib.h> 21#include <stdlib.h>
22#include <syslog.h>
23#include <signal.h>
22 24
23 25
26 #define PIDFILE "/var/run/opiealarm.pid"
27
28FILE *log;
29
30
31void log_msg ( const char *msg )
32{
33 if ( log ) {
34 fprintf ( log, msg );
35 fflush ( log );
36 }
37}
38
24void error_msg_and_die ( int perr, const char *msg ) 39void error_msg_and_die ( int perr, const char *msg )
@@ -26,5 +41,6 @@ void error_msg_and_die ( int perr, const char *msg )
26 if ( perr ) 41 if ( perr )
27 perror ( msg ); 42 log_msg ( strerror ( errno ));
28 else 43 log_msg ( msg );
29 fprintf ( stderr, "%s\n", msg ); 44
45 unlink ( PIDFILE );
30 exit ( 1 ); 46 exit ( 1 );
@@ -33,3 +49,4 @@ void error_msg_and_die ( int perr, const char *msg )
33 49
34void extractevent ( ) 50
51void suspend_on_rtc ( )
35{ 52{
@@ -41,2 +58,3 @@ void extractevent ( )
41 58
59
42 if (!( fp = fopen ( "/etc/resumeat", "r" ))) 60 if (!( fp = fopen ( "/etc/resumeat", "r" )))
@@ -56,13 +74,14 @@ void extractevent ( )
56 t -= 5; 74 t -= 5;
57 tm = gmtime ( &t );
58 75
59 /* Write alarm time to RTC */ 76 if ( log )
60 fd = open ( "/dev/misc/rtc", O_RDWR ); 77 fprintf ( log, "Setting RTC alarm to %d\n", t );
61 if ( fd < 0 )
62 error_msg_and_die ( 1, "/dev/misc/rtc" );
63 78
79 tm = gmtime ( &t );
80
81 // Write alarm time to RTC
82 if (( fd = open ( "/dev/misc/rtc", O_RDWR ) < 0 );
83 error_msg_and_die ( 1, "/dev/misc/rtc" );
64 // set alarm time 84 // set alarm time
65 if ( ioctl ( fd, RTC_ALM_SET, tm ) < 0 ) 85 if ( ioctl ( fd, RTC_ALM_SET, tm ) < 0 )
66 error_msg_and_die ( 1, "ioctl RTC_ALM_SET" ); 86 error_msg_and_die ( 1, "ioctl RTC_ALM_SET" );
67
68 // enable alarm irq 87 // enable alarm irq
@@ -70,2 +89,4 @@ void extractevent ( )
70 error_msg_and_die ( 1, "ioctl RTC_AIE_ON" ); 89 error_msg_and_die ( 1, "ioctl RTC_AIE_ON" );
90
91 log_msg ( "SLEEPING\n" );
71 92
@@ -75,2 +96,4 @@ void extractevent ( )
75 96
97 log_msg ( "WAKEUP\n" );
98
76 // disable alarm irq 99 // disable alarm irq
@@ -83,8 +106,61 @@ void extractevent ( )
83 106
107void sig_handler ( int sig )
108{
109 log_msg ( "GOT SIGNAL -> EXITING\n" );
110 fclose ( log );
111 unlink ( PIDFILE );
112 exit ( 0 );
113}
114
115
84int main ( ) 116int main ( )
85{ 117{
86 if ( geteuid ( ) != 0 ) 118 FILE *fp;
87 error_msg_and_die ( 0, "You need root priviledges to run opiealarm." ); 119 pid_t pid;
120
121 if ( geteuid ( ) != 0 ) {
122 fprintf ( stderr, "You need root priviledges to run opiealarm." );
123 return 1;
124 }
125
126 pid = fork ( );
127
128 if ( pid > 0 )
129 return 0;
130 else if ( pid < 0 ) {
131 perror ( "Could not fork." );
132 return 2;
133 }
134
135 // save pid
136 if (!( fp = fopen ( PIDFILE, "w" ))) {
137 perror ( PIDFILE );
138 return 3;
139 }
140
141 fprintf ( fp, "%d", getpid ( ));
142 fclose ( fp );
143
144 // detach
145 close ( 0 );
146 close ( 1 );
147 close ( 2 );
148
149 setpgid ( 0, 0 );
150
151 log = fopen ( "/tmp/opiealarm.log", "w" );
152 log_msg ( "STARTING\n" );
153
154 signal ( SIGTERM, sig_handler );
155 signal ( SIGINT, sig_handler );
88 156
89 extractevent ( ); 157 extractevent ( );
158
159 signal ( SIGTERM, SIG_DFL );
160 signal ( SIGINT, SIG_DFL );
161
162 log_msg ( "EXITING\n" );
163
164 fclose ( log );
165 unlink ( PIDFILE );
90 return 0; 166 return 0;