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