summaryrefslogtreecommitdiff
authorsandman <sandman>2002-06-18 14:11:42 (UTC)
committer sandman <sandman>2002-06-18 14:11:42 (UTC)
commit5538834e726c3b5d3ba7998c56845ea652d184dd (patch) (unidiff)
treef2c78c7116b2d695cd9c939f39f38c1a59506a17
parent99cf287037a0d9f99e4fc035d1b9f6db08bb6583 (diff)
downloadopie-5538834e726c3b5d3ba7998c56845ea652d184dd.zip
opie-5538834e726c3b5d3ba7998c56845ea652d184dd.tar.gz
opie-5538834e726c3b5d3ba7998c56845ea652d184dd.tar.bz2
Fix a compiler warning
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c
index 128929e..a4d46ed 100644
--- a/core/opiealarm/opiealarm.c
+++ b/core/opiealarm/opiealarm.c
@@ -1,350 +1,350 @@
1/* opiealarm.c 1/* opiealarm.c
2* This program is for extracting the event time/date out 2* This program is for extracting the event time/date out
3* of /etc/resumeat and setting the RTC alarm to that time/date. 3* of /etc/resumeat and setting the RTC alarm to that time/date.
4* It is designed to run via a script just before the iPaq 4* It is designed to run via a script just before the iPaq
5* is suspended. 5* is suspended.
6* 6*
7* Roughly based on ipaqalarm from Benjamin Long 7* Roughly based on ipaqalarm from Benjamin Long
8* 8*
9* written by Robert Griebl <sandman@handhelds.org> 9* written 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 APMFILE "/proc/apm" 29 #define APMFILE "/proc/apm"
30 30
31FILE *log; // debug only 31FILE *log; // debug only
32 32
33int resume ( int resuspend ); 33int resume ( int resuspend );
34int suspend ( int fix_rtc ); 34int suspend ( int fix_rtc );
35int main ( int argc, char **argv ); 35int main ( int argc, char **argv );
36int fork_with_pidfile ( void ); 36int fork_with_pidfile ( void );
37int kill_with_pidfile ( void ); 37int kill_with_pidfile ( void );
38void remove_pidfile ( void ); 38void remove_pidfile ( void );
39void usage ( void ); 39void usage ( void );
40void sig_handler ( int sig ); 40void sig_handler ( int sig );
41void error_msg_and_die ( int perr, const char *msg ); 41void error_msg_and_die ( int perr, const char *msg );
42int onac ( void ); 42int onac ( void );
43 43
44static int opiealarm_was_running; 44static int opiealarm_was_running;
45 45
46 46
47void log_msg ( const char *msg ) 47void log_msg ( const char *msg )
48{ 48{
49 if ( log ) { 49 if ( log ) {
50 fprintf ( log, msg ); 50 fprintf ( log, msg );
51 fflush ( log ); 51 fflush ( log );
52 } 52 }
53} 53}
54 54
55void error_msg_and_die ( int perr, const char *msg ) 55void error_msg_and_die ( int perr, const char *msg )
56{ 56{
57 if ( perr ) 57 if ( perr )
58 log_msg ( strerror ( errno )); 58 log_msg ( strerror ( errno ));
59 log_msg ( msg ); 59 log_msg ( msg );
60 60
61 while ( 1 ) // pretend we are waiting on RTC, so opiealarm -r can kill us 61 while ( 1 ) // pretend we are waiting on RTC, so opiealarm -r can kill us
62 sleep ( 1 ); 62 sleep ( 1 );
63} 63}
64 64
65 65
66void sig_handler ( int sig ) 66void sig_handler ( int sig )
67{ 67{
68 log_msg ( "GOT SIGNAL -> EXITING\n" ); 68 log_msg ( "GOT SIGNAL -> EXITING\n" );
69 fclose ( log ); 69 fclose ( log );
70 remove_pidfile ( ); 70 remove_pidfile ( );
71 exit ( 0 ); 71 exit ( 0 );
72} 72}
73 73
74void usage ( void ) 74void usage ( void )
75{ 75{
76 fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" ); 76 fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" );
77 fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" ); 77 fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" );
78 fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" ); 78 fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" );
79 fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" ); 79 fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" );
80 fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" ); 80 fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" );
81 exit ( 1 ); 81 exit ( 1 );
82} 82}
83 83
84int fork_with_pidfile ( void ) 84int fork_with_pidfile ( void )
85{ 85{
86 FILE *fp; 86 FILE *fp;
87 pid_t pid; 87 pid_t pid;
88 88
89 pid = fork ( ); 89 pid = fork ( );
90 90
91 if ( pid > 0 ) 91 if ( pid > 0 )
92 exit ( 0 ); 92 exit ( 0 );
93 else if ( pid < 0 ) { 93 else if ( pid < 0 ) {
94 perror ( "forking failed" ); 94 perror ( "forking failed" );
95 return 0; 95 return 0;
96 } 96 }
97 97
98 signal ( SIGTERM, sig_handler ); 98 signal ( SIGTERM, sig_handler );
99 signal ( SIGINT, sig_handler ); 99 signal ( SIGINT, sig_handler );
100 100
101 // save pid 101 // save pid
102 if (( fp = fopen ( PIDFILE, "w" ))) { 102 if (( fp = fopen ( PIDFILE, "w" ))) {
103 fprintf ( fp, "%d", getpid ( )); 103 fprintf ( fp, "%d", getpid ( ));
104 fclose ( fp ); 104 fclose ( fp );
105 105
106 // detach 106 // detach
107 close ( 0 ); 107 close ( 0 );
108 close ( 1 ); 108 close ( 1 );
109 close ( 2 ); 109 close ( 2 );
110 110
111 setpgid ( 0, 0 ); 111 setpgid ( 0, 0 );
112 112
113 return 1; 113 return 1;
114 } 114 }
115 else { 115 else {
116 perror ( PIDFILE ); 116 perror ( PIDFILE );
117 return 0; 117 return 0;
118 } 118 }
119} 119}
120 120
121int kill_with_pidfile ( void ) 121int kill_with_pidfile ( void )
122{ 122{
123 FILE *fp; 123 FILE *fp;
124 pid_t pid; 124 pid_t pid;
125 125
126 if (( fp = fopen ( PIDFILE, "r" ))) { 126 if (( fp = fopen ( PIDFILE, "r" ))) {
127 if ( fscanf ( fp, "%d", &pid ) == 1 ) 127 if ( fscanf ( fp, "%d", &pid ) == 1 )
128 return ( kill ( pid, SIGTERM ) == 0 ) ? 1 : 0; 128 return ( kill ( pid, SIGTERM ) == 0 ) ? 1 : 0;
129 fclose ( fp ); 129 fclose ( fp );
130 } 130 }
131 return 0; 131 return 0;
132} 132}
133 133
134void remove_pidfile ( void ) 134void remove_pidfile ( void )
135{ 135{
136 unlink ( PIDFILE ); 136 unlink ( PIDFILE );
137 137
138 signal ( SIGTERM, SIG_DFL ); 138 signal ( SIGTERM, SIG_DFL );
139 signal ( SIGINT, SIG_DFL ); 139 signal ( SIGINT, SIG_DFL );
140} 140}
141 141
142 142
143int main ( int argc, char **argv ) 143int main ( int argc, char **argv )
144{ 144{
145 int mode = 0; 145 int mode = 0;
146 int ac_resusp = 0; 146 int ac_resusp = 0;
147 int fix_rtc = 0; 147 int fix_rtc = 0;
148 int opt; 148 int opt;
149 149
150 while (( opt = getopt ( argc, argv, "a:frs" )) != EOF ) { 150 while (( opt = getopt ( argc, argv, "a:frs" )) != EOF ) {
151 switch ( opt ) { 151 switch ( opt ) {
152 case 's': 152 case 's':
153 mode = 's'; 153 mode = 's';
154 break; 154 break;
155 case 'r': 155 case 'r':
156 mode = 'r'; 156 mode = 'r';
157 break; 157 break;
158 case 'a': 158 case 'a':
159 ac_resusp = atoi ( optarg ); 159 ac_resusp = atoi ( optarg );
160 if ( ac_resusp < 30 ) { 160 if ( ac_resusp < 30 ) {
161 ac_resusp = 120; 161 ac_resusp = 120;
162 162
163 fprintf ( stderr, "Warning: resuspend timeout must be >= 30 sec. -- now set to 120 sec\n" ); 163 fprintf ( stderr, "Warning: resuspend timeout must be >= 30 sec. -- now set to 120 sec\n" );
164 } 164 }
165 break; 165 break;
166 case 'f': 166 case 'f':
167 fix_rtc = 1; 167 fix_rtc = 1;
168 break; 168 break;
169 default: 169 default:
170 usage ( ); 170 usage ( );
171 } 171 }
172 } 172 }
173 173
174 if ( geteuid ( ) != 0 ) { 174 if ( geteuid ( ) != 0 ) {
175 fprintf ( stderr, "You need root priviledges to run opiealarm." ); 175 fprintf ( stderr, "You need root priviledges to run opiealarm." );
176 return 2; 176 return 2;
177 } 177 }
178 178
179 if ( !mode ) 179 if ( !mode )
180 usage ( ); 180 usage ( );
181 181
182 // kill running opiealarm 182 // kill running opiealarm
183 opiealarm_was_running = kill_with_pidfile ( ); 183 opiealarm_was_running = kill_with_pidfile ( );
184 remove_pidfile ( ); 184 remove_pidfile ( );
185 185
186 switch ( mode ) { 186 switch ( mode ) {
187 case 'r': return resume ( ac_resusp ); 187 case 'r': return resume ( ac_resusp );
188 case 's': 188 case 's':
189 default : return suspend ( fix_rtc ); 189 default : return suspend ( fix_rtc );
190 } 190 }
191 return 0; 191 return 0;
192 } 192 }
193 193
194 194
195int suspend ( int fix_rtc ) 195int suspend ( int fix_rtc )
196{ 196{
197 FILE *fp; 197 FILE *fp;
198 char buf [64]; 198 char buf [64];
199 time_t alrt, syst, rtct; 199 time_t alrt, syst, rtct;
200 struct tm alr, sys, rtc; 200 struct tm alr, sys, rtc;
201 int fd; 201 int fd;
202 int rtc_sys_diff; 202 int rtc_sys_diff;
203 203
204 204
205 if ( !fork_with_pidfile ( )) 205 if ( !fork_with_pidfile ( ))
206 return 3; 206 return 3;
207 207
208 log = fopen ( "/tmp/opiealarm.log", "w" ); 208 log = fopen ( "/tmp/opiealarm.log", "w" );
209 log_msg ( "STARTING\n" ); 209 log_msg ( "STARTING\n" );
210 210
211 211
212 212
213 if (!( fp = fopen ( "/etc/resumeat", "r" ))) 213 if (!( fp = fopen ( "/etc/resumeat", "r" )))
214 error_msg_and_die ( 1, "/etc/resumeat" ); 214 error_msg_and_die ( 1, "/etc/resumeat" );
215 215
216 if ( !fgets ( buf, sizeof( buf ) - 1, fp )) 216 if ( !fgets ( buf, sizeof( buf ) - 1, fp ))
217 error_msg_and_die ( 1, "/etc/resumeat" ); 217 error_msg_and_die ( 1, "/etc/resumeat" );
218 218
219 fclose ( fp ); 219 fclose ( fp );
220 220
221 alrt = atoi ( buf ); 221 alrt = atoi ( buf );
222 222
223 if ( alrt == 0 ) 223 if ( alrt == 0 )
224 error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" ); 224 error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" );
225 225
226 /* subtract 5 sec from event time... */ 226 /* subtract 5 sec from event time... */
227 alrt -= 5; 227 alrt -= 5;
228 228
229 if ( log ) 229 if ( log )
230 fprintf ( log, "Setting RTC alarm to %d\n", alrt ); 230 fprintf ( log, "Setting RTC alarm to %d\n", alrt );
231 231
232 alr = *gmtime ( &alrt ); 232 alr = *gmtime ( &alrt );
233 233
234 // get system time 234 // get system time
235 time ( &syst ); 235 time ( &syst );
236 sys = *localtime ( &syst ); 236 sys = *localtime ( &syst );
237 237
238 // Write alarm time to RTC 238 // Write alarm time to RTC
239 if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 ) 239 if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 )
240 error_msg_and_die ( 1, "/dev/misc/rtc" ); 240 error_msg_and_die ( 1, "/dev/misc/rtc" );
241 241
242 // get RTC time 242 // get RTC time
243 if ( ioctl ( fd, RTC_ALM_SET, &rtc ) < 0 ) 243 if ( ioctl ( fd, RTC_ALM_SET, &rtc ) < 0 )
244 error_msg_and_die ( 1, "ioctl RTC_RD_TIME" ); 244 error_msg_and_die ( 1, "ioctl RTC_RD_TIME" );
245 rtct = mktime ( &rtc ); 245 rtct = mktime ( &rtc );
246 246
247 rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff; 247 rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff;
248 248
249 if ( fix_rtc && (( rtc_sys_diff < -4 ) || ( rtc_sys_diff > 4 ))) { 249 if ( fix_rtc && (( rtc_sys_diff < -4 ) || ( rtc_sys_diff > 4 ))) {
250 struct tm set; 250 struct tm set;
251 251
252 set = *gmtime ( &syst ); 252 set = *gmtime ( &syst );
253 253
254 fprintf ( log, "Correcting RTC: %d seconds\n", rtc_sys_diff ); 254 fprintf ( log, "Correcting RTC: %d seconds\n", rtc_sys_diff );
255 255
256 if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 ) 256 if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 )
257 error_msg_and_die ( 1, "ioctl RTC_SET_TIME" ); 257 error_msg_and_die ( 1, "ioctl RTC_SET_TIME" );
258 } 258 }
259 259
260 // set alarm time 260 // set alarm time
261 if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 ) 261 if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 )
262 error_msg_and_die ( 1, "ioctl RTC_ALM_SET" ); 262 error_msg_and_die ( 1, "ioctl RTC_ALM_SET" );
263 // enable alarm irq 263 // enable alarm irq
264 if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) 264 if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 )
265 error_msg_and_die ( 1, "ioctl RTC_AIE_ON" ); 265 error_msg_and_die ( 1, "ioctl RTC_AIE_ON" );
266 266
267 log_msg ( "SLEEPING\n" ); 267 log_msg ( "SLEEPING\n" );
268 268
269 // wait for alarm irq 269 // wait for alarm irq
270 if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) 270 if ( read ( fd, buf, sizeof( unsigned long )) < 0 )
271 error_msg_and_die ( 1, "read rtc alarm" ); 271 error_msg_and_die ( 1, "read rtc alarm" );
272 272
273 log_msg ( "WAKEUP\n" ); 273 log_msg ( "WAKEUP\n" );
274 274
275 // disable alarm irq 275 // disable alarm irq
276 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) 276 if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 )
277 error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" ); 277 error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" );
278 278
279 close ( fd ); 279 close ( fd );
280 280
281 log_msg ( "EXITING\n" ); 281 log_msg ( "EXITING\n" );
282 282
283 fclose ( log ); 283 fclose ( log );
284 remove_pidfile ( ); 284 remove_pidfile ( );
285 285
286 return 0; 286 return 0;
287} 287}
288 288
289 289
290static int onac ( void ) 290int onac ( void )
291{ 291{
292 FILE *fp; 292 FILE *fp;
293 int on = 0; 293 int on = 0;
294 294
295 if (( fp = fopen ( APMFILE, "r" ))) { 295 if (( fp = fopen ( APMFILE, "r" ))) {
296 int ac = 0; 296 int ac = 0;
297 297
298 if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) 298 if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 )
299 on = ( ac == 0x01 ) ? 1 : 0; 299 on = ( ac == 0x01 ) ? 1 : 0;
300 300
301 fclose ( fp ); 301 fclose ( fp );
302 } 302 }
303 return on; 303 return on;
304} 304}
305 305
306int resume ( int resuspend ) 306int resume ( int resuspend )
307{ 307{
308 FILE *fp; 308 FILE *fp;
309 309
310 // re-suspend when on AC (optional) when woken up via RTC 310 // re-suspend when on AC (optional) when woken up via RTC
311 311
312 if ( !opiealarm_was_running ) { // opiealarm -s got it's RTC signal -> wake up by RTC 312 if ( !opiealarm_was_running ) { // opiealarm -s got it's RTC signal -> wake up by RTC
313 if ( resuspend && onac ( )) { 313 if ( resuspend && onac ( )) {
314 time_t start, now; 314 time_t start, now;
315 char *argv [4]; 315 char *argv [4];
316 316
317 if ( !fork_with_pidfile ( )) 317 if ( !fork_with_pidfile ( ))
318 return 4; 318 return 4;
319 319
320 // sleep <resuspend> sec (not less!) 320 // sleep <resuspend> sec (not less!)
321 time ( &start ); 321 time ( &start );
322 do { 322 do {
323 sleep ( 1 ); 323 sleep ( 1 );
324 time ( &now ); 324 time ( &now );
325 } while (( now - start ) < resuspend ); 325 } while (( now - start ) < resuspend );
326 326
327 if ( onac ( )) { // still on ac 327 if ( onac ( )) { // still on ac
328 // system() without fork 328 // system() without fork
329 argv[0] = "qcop"; 329 argv[0] = "qcop";
330 argv[1] = "QPE/Desktop"; 330 argv[1] = "QPE/Desktop";
331 argv[2] = "suspend()"; 331 argv[2] = "suspend()";
332 argv[3] = 0; 332 argv[3] = 0;
333 333
334 // hard coded for now ...but needed 334 // hard coded for now ...but needed
335 setenv ( "LOGNAME", "root", 1 ); 335 setenv ( "LOGNAME", "root", 1 );
336 setenv ( "HOME", "/root", 1 ); 336 setenv ( "HOME", "/root", 1 );
337 setenv ( "LD_LIBRARY_PATH", "/opt/QtPalmtop/lib", 1 ); 337 setenv ( "LD_LIBRARY_PATH", "/opt/QtPalmtop/lib", 1 );
338 setenv ( "QTDIR", "/opt/QtPalmtop", 1 ); 338 setenv ( "QTDIR", "/opt/QtPalmtop", 1 );
339 339
340 remove_pidfile ( ); 340 remove_pidfile ( );
341 341
342 execv ( "/opt/QtPalmtop/bin/qcop", argv ); 342 execv ( "/opt/QtPalmtop/bin/qcop", argv );
343 343
344 perror ( "exec for qcop failed" ); 344 perror ( "exec for qcop failed" );
345 return 5; 345 return 5;
346 } 346 }
347 } 347 }
348 } 348 }
349 return 0; 349 return 0;
350} 350}