summaryrefslogtreecommitdiff
Unidiff
Diffstat (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
@@ -20,17 +20,34 @@
20#include <time.h> 20#include <time.h>
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 )
25{ 40{
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 );
31} 47}
32 48
33 49
34void extractevent ( ) 50
51void suspend_on_rtc ( )
35{ 52{
36 FILE *fp; 53 FILE *fp;
@@ -40,4 +57,5 @@ void extractevent ( )
40 int fd; 57 int fd;
41 58
59
42 if (!( fp = fopen ( "/etc/resumeat", "r" ))) 60 if (!( fp = fopen ( "/etc/resumeat", "r" )))
43 error_msg_and_die ( 1, "/etc/resumeat" ); 61 error_msg_and_die ( 1, "/etc/resumeat" );
@@ -55,18 +73,21 @@ void extractevent ( )
55 /* subtract 5 sec from event time... */ 73 /* subtract 5 sec from event time... */
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
69 if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) 88 if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 )
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
72 // wait for alarm irq 93 // wait for alarm irq
@@ -74,4 +95,6 @@ void extractevent ( )
74 error_msg_and_die ( 1, "read rtc alarm" ); 95 error_msg_and_die ( 1, "read rtc alarm" );
75 96
97 log_msg ( "WAKEUP\n" );
98
76 // disable alarm irq 99 // disable alarm irq
77 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) 100 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 )
@@ -82,10 +105,63 @@ void extractevent ( )
82 105
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;
91} 167}