-rw-r--r-- | core/opiealarm/opiealarm.c | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c index 072dca7..465e633 100644 --- a/core/opiealarm/opiealarm.c +++ b/core/opiealarm/opiealarm.c | |||
@@ -1,381 +1,383 @@ | |||
1 | /* | 1 | /* |
2 | * opiealarm.c | 2 | * opiealarm.c |
3 | * | 3 | * |
4 | * This program is for extracting the event time/date out | 4 | * This program is for extracting the event time/date out |
5 | * of /etc/resumeat and setting the RTC alarm to that time/date. | 5 | * of /etc/resumeat and setting the RTC alarm to that time/date. |
6 | * It is designed to run via a script just before the iPAQ | 6 | * It is designed to run via a script just before the iPAQ |
7 | * is suspended and right after the iPAQ resumes operation. | 7 | * is suspended and right after the iPAQ resumes operation. |
8 | * | 8 | * |
9 | * written and copyrighted by Robert Griebl <sandman@handhelds.org> | 9 | * written and copyrighted 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 TIMEFILE "/var/run/resumeat" | 29 | #define TIMEFILE "/var/run/resumeat" |
30 | #define APMFILE "/proc/apm" | 30 | #define APMFILE "/proc/apm" |
31 | 31 | ||
32 | int resume ( int resuspend ); | 32 | int resume ( int resuspend ); |
33 | int suspend ( int fix_rtc ); | 33 | int suspend ( int fix_rtc ); |
34 | int main ( int argc, char **argv ); | 34 | int main ( int argc, char **argv ); |
35 | int fork_with_pidfile ( void ); | 35 | int fork_with_pidfile ( void ); |
36 | int kill_with_pidfile ( void ); | 36 | int kill_with_pidfile ( void ); |
37 | void remove_pidfile ( void ); | 37 | void remove_pidfile ( void ); |
38 | void usage ( void ); | 38 | void usage ( void ); |
39 | void sig_handler_child ( int sig ); | 39 | void sig_handler_child ( int sig ); |
40 | void sig_handler_parent ( int sig ); | 40 | void sig_handler_parent ( int sig ); |
41 | int onac ( void ); | 41 | int onac ( void ); |
42 | 42 | ||
43 | static int opiealarm_was_running; | 43 | static int opiealarm_was_running; |
44 | static pid_t parent_pid = 0; | 44 | static pid_t parent_pid = 0; |
45 | 45 | ||
46 | 46 | ||
47 | 47 | ||
48 | void sig_handler_child ( int sig ) | 48 | void sig_handler_child ( int sig ) |
49 | { | 49 | { |
50 | // child got SIGUSR2 -> cleanup pidfile and exit | 50 | // child got SIGUSR2 -> cleanup pidfile and exit |
51 | 51 | ||
52 | remove_pidfile ( ); | 52 | remove_pidfile ( ); |
53 | exit ( 0 ); | 53 | exit ( 0 ); |
54 | } | 54 | } |
55 | 55 | ||
56 | void sig_handler_parent ( int sig ) | 56 | void sig_handler_parent ( int sig ) |
57 | { | 57 | { |
58 | // parent got SIGUSR1 -> safe to exit now | 58 | // parent got SIGUSR1 -> safe to exit now |
59 | 59 | ||
60 | parent_pid = 0; | 60 | parent_pid = 0; |
61 | exit ( 0 ); | 61 | exit ( 0 ); |
62 | } | 62 | } |
63 | 63 | ||
64 | void usage ( void ) | 64 | void usage ( void ) |
65 | { | 65 | { |
66 | fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" ); | 66 | fprintf ( stderr, "Usage: opiealarm -s [-f] | -r [-a]\n\n" ); |
67 | fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" ); | 67 | fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" ); |
68 | fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" ); | 68 | fprintf ( stderr, "\t-f \tFix RTC, if RTC and system have more than 5sec difference (suspend mode)\n" ); |
69 | fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" ); | 69 | fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" ); |
70 | fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" ); | 70 | fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" ); |
71 | exit ( 1 ); | 71 | exit ( 1 ); |
72 | } | 72 | } |
73 | 73 | ||
74 | int fork_with_pidfile ( void ) | 74 | int fork_with_pidfile ( void ) |
75 | { | 75 | { |
76 | FILE *fp; | 76 | FILE *fp; |
77 | pid_t pid; | 77 | pid_t pid; |
78 | 78 | ||
79 | pid = fork ( ); | 79 | pid = fork ( ); |
80 | 80 | ||
81 | if ( pid > 0 ) { | 81 | if ( pid > 0 ) { |
82 | // We can not just exit now, because the kernel could suspend | 82 | // We can not just exit now, because the kernel could suspend |
83 | // the iPAQ just before the child process sets the RTC. | 83 | // the iPAQ just before the child process sets the RTC. |
84 | // Solution: just wait for SIGUSR1 - the child process will | 84 | // Solution: just wait for SIGUSR1 - the child process will |
85 | // signal this when it thinks it is safe to exit. | 85 | // signal this when it thinks it is safe to exit. |
86 | 86 | ||
87 | signal ( SIGUSR1, sig_handler_parent ); | 87 | signal ( SIGUSR1, sig_handler_parent ); |
88 | while ( 1 ) | 88 | while ( 1 ) |
89 | sleep ( 1000 ); | 89 | sleep ( 1000 ); |
90 | exit ( 0 ); | 90 | exit ( 0 ); |
91 | } | 91 | } |
92 | else if ( pid < 0 ) { | 92 | else if ( pid < 0 ) { |
93 | perror ( "forking failed" ); | 93 | perror ( "forking failed" ); |
94 | return 0; | 94 | return 0; |
95 | } | 95 | } |
96 | 96 | ||
97 | // child process needs to react to SIGUSR2. This is sent when | 97 | // child process needs to react to SIGUSR2. This is sent when |
98 | // a new opiealarm process is started. | 98 | // a new opiealarm process is started. |
99 | 99 | ||
100 | signal ( SIGUSR2, sig_handler_child ); | 100 | signal ( SIGUSR2, sig_handler_child ); |
101 | 101 | ||
102 | // save pid | 102 | // save pid |
103 | if (( fp = fopen ( PIDFILE, "w" ))) { | 103 | if (( fp = fopen ( PIDFILE, "w" ))) { |
104 | fprintf ( fp, "%d", getpid ( )); | 104 | fprintf ( fp, "%d", getpid ( )); |
105 | fclose ( fp ); | 105 | fclose ( fp ); |
106 | 106 | ||
107 | // detach | 107 | // detach |
108 | close ( 0 ); | 108 | close ( 0 ); |
109 | close ( 1 ); | 109 | close ( 1 ); |
110 | close ( 2 ); | 110 | close ( 2 ); |
111 | 111 | ||
112 | setpgid ( 0, 0 ); | 112 | setpgid ( 0, 0 ); |
113 | 113 | ||
114 | return 1; | 114 | return 1; |
115 | } | 115 | } |
116 | else { | 116 | else { |
117 | perror ( PIDFILE ); | 117 | perror ( PIDFILE ); |
118 | return 0; | 118 | return 0; |
119 | } | 119 | } |
120 | } | 120 | } |
121 | 121 | ||
122 | int kill_with_pidfile ( void ) | 122 | int kill_with_pidfile ( void ) |
123 | { | 123 | { |
124 | FILE *fp; | 124 | FILE *fp; |
125 | pid_t pid; | 125 | pid_t pid; |
126 | int res = 0; | 126 | int res = 0; |
127 | 127 | ||
128 | // terminate a running opiealarm child process | 128 | // terminate a running opiealarm child process |
129 | // return 1 if we really killed one | 129 | // return 1 if we really killed one |
130 | 130 | ||
131 | if (( fp = fopen ( PIDFILE, "r" ))) { | 131 | if (( fp = fopen ( PIDFILE, "r" ))) { |
132 | if ( fscanf ( fp, "%d", &pid ) == 1 ) | 132 | if ( fscanf ( fp, "%d", &pid ) == 1 ) |
133 | res = ( kill ( pid, SIGUSR2 ) == 0 ) ? 1 : 0; | 133 | res = ( kill ( pid, SIGUSR2 ) == 0 ) ? 1 : 0; |
134 | fclose ( fp ); | 134 | fclose ( fp ); |
135 | } | 135 | } |
136 | return res; | 136 | return res; |
137 | } | 137 | } |
138 | 138 | ||
139 | void remove_pidfile ( void ) | 139 | void remove_pidfile ( void ) |
140 | { | 140 | { |
141 | // child is about to exit - cleanup | 141 | // child is about to exit - cleanup |
142 | 142 | ||
143 | unlink ( PIDFILE ); | 143 | unlink ( PIDFILE ); |
144 | signal ( SIGUSR2, SIG_DFL ); | 144 | signal ( SIGUSR2, SIG_DFL ); |
145 | } | 145 | } |
146 | 146 | ||
147 | 147 | ||
148 | int main ( int argc, char **argv ) | 148 | int main ( int argc, char **argv ) |
149 | { | 149 | { |
150 | int mode = 0; | 150 | int mode = 0; |
151 | int ac_resusp = 0; | 151 | int ac_resusp = 0; |
152 | int fix_rtc = 0; | 152 | int fix_rtc = 0; |
153 | int opt; | 153 | int opt; |
154 | 154 | ||
155 | while (( opt = getopt ( argc, argv, "a:frs" )) != EOF ) { | 155 | while (( opt = getopt ( argc, argv, "a:frs" )) != EOF ) { |
156 | switch ( opt ) { | 156 | switch ( opt ) { |
157 | case 's': | 157 | case 's': |
158 | mode = 's'; | 158 | mode = 's'; |
159 | break; | 159 | break; |
160 | case 'r': | 160 | case 'r': |
161 | mode = 'r'; | 161 | mode = 'r'; |
162 | break; | 162 | break; |
163 | case 'a': | 163 | case 'a': |
164 | ac_resusp = atoi ( optarg ); | 164 | ac_resusp = atoi ( optarg ); |
165 | if ( ac_resusp < 30 ) { | 165 | if ( ac_resusp < 30 ) { |
166 | ac_resusp = 120; | 166 | ac_resusp = 120; |
167 | 167 | ||
168 | fprintf ( stderr, "Warning: resuspend timeout must be >= 30 sec. -- now set to 120 sec\n" ); | 168 | fprintf ( stderr, "Warning: resuspend timeout must be >= 30 sec. -- now set to 120 sec\n" ); |
169 | } | 169 | } |
170 | break; | 170 | break; |
171 | case 'f': | 171 | case 'f': |
172 | fix_rtc = 1; | 172 | fix_rtc = 1; |
173 | break; | 173 | break; |
174 | default: | 174 | default: |
175 | usage ( ); | 175 | usage ( ); |
176 | } | 176 | } |
177 | } | 177 | } |
178 | 178 | ||
179 | if ( geteuid ( ) != 0 ) { | 179 | if ( geteuid ( ) != 0 ) { |
180 | fprintf ( stderr, "You need root priviledges to run opiealarm." ); | 180 | fprintf ( stderr, "You need root priviledges to run opiealarm." ); |
181 | return 2; | 181 | return 2; |
182 | } | 182 | } |
183 | 183 | ||
184 | if ( !mode ) | 184 | if ( !mode ) |
185 | usage ( ); | 185 | usage ( ); |
186 | 186 | ||
187 | 187 | ||
188 | parent_pid = getpid ( ); | 188 | parent_pid = getpid ( ); |
189 | 189 | ||
190 | // kill running opiealarm | 190 | // kill running opiealarm |
191 | opiealarm_was_running = kill_with_pidfile ( ); | 191 | opiealarm_was_running = kill_with_pidfile ( ); |
192 | remove_pidfile ( ); | 192 | remove_pidfile ( ); |
193 | 193 | ||
194 | switch ( mode ) { | 194 | switch ( mode ) { |
195 | case 'r': opt = resume ( ac_resusp ); | 195 | case 'r': opt = resume ( ac_resusp ); |
196 | break; | 196 | break; |
197 | case 's': | 197 | case 's': |
198 | default : opt = suspend ( fix_rtc ); | 198 | default : opt = suspend ( fix_rtc ); |
199 | break; | 199 | break; |
200 | } | 200 | } |
201 | 201 | ||
202 | parent_pid = 0; | 202 | parent_pid = 0; |
203 | return opt; | 203 | return opt; |
204 | } | 204 | } |
205 | 205 | ||
206 | 206 | ||
207 | int suspend ( int fix_rtc ) | 207 | int suspend ( int fix_rtc ) |
208 | { | 208 | { |
209 | FILE *fp; | 209 | FILE *fp; |
210 | char buf [64]; | 210 | char buf [64]; |
211 | time_t alrt, syst, rtct; | 211 | time_t alrt, syst, rtct; |
212 | struct tm alr, sys, rtc; | 212 | struct tm alr, sys, rtc; |
213 | int fd; | 213 | int fd; |
214 | int rtc_sys_diff; | 214 | int rtc_sys_diff; |
215 | 215 | ||
216 | 216 | ||
217 | if ( !fork_with_pidfile ( )) | 217 | if ( !fork_with_pidfile ( )) |
218 | return 3; | 218 | return 3; |
219 | 219 | ||
220 | // we are the child process from here on ... | 220 | // we are the child process from here on ... |
221 | 221 | ||
222 | tzset ( ); // not sure if it is really needed -- it probably doesn't hurt ... | 222 | tzset ( ); // not sure if it is really needed -- it probably doesn't hurt ... |
223 | 223 | ||
224 | time ( &syst );// get the UNIX system time | 224 | time ( &syst );// get the UNIX system time |
225 | sys = *localtime ( &syst ); | 225 | sys = *localtime ( &syst ); |
226 | 226 | ||
227 | do { | 227 | do { |
228 | 228 | ||
229 | if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 ) | 229 | if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 ) |
230 | if (( fd = open ( "/dev/rtc", O_RDWR )) < 0 ) | 230 | if (( fd = open ( "/dev/rtc", O_RDWR )) < 0 ) |
231 | break; // ( 1, "rtc" ); | 231 | break; // ( 1, "rtc" ); |
232 | 232 | ||
233 | memset ( &rtc, 0, sizeof ( struct tm )); // get the RTC time | 233 | memset ( &rtc, 0, sizeof ( struct tm )); // get the RTC time |
234 | 234 | ||
235 | if ( ioctl ( fd, RTC_RD_TIME, &rtc ) < 0 ) | 235 | if ( ioctl ( fd, RTC_RD_TIME, &rtc ) < 0 ) |
236 | break; // ( 1, "ioctl RTC_RD_TIME" ); | 236 | break; // ( 1, "ioctl RTC_RD_TIME" ); |
237 | 237 | ||
238 | rtct = mktime ( &rtc ); | 238 | rtct = mktime ( &rtc ); |
239 | 239 | ||
240 | rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff; // calculate the difference between system and hardware time | 240 | rtc_sys_diff = ( syst - rtct ) - sys. tm_gmtoff; // calculate the difference between system and hardware time |
241 | 241 | ||
242 | if ( fix_rtc && (( rtc_sys_diff < -3 ) || ( rtc_sys_diff > 3 ))) { | 242 | if ( fix_rtc && (( rtc_sys_diff < -3 ) || ( rtc_sys_diff > 3 ))) { |
243 | struct tm set; | 243 | struct tm set; |
244 | set = *gmtime ( &syst ); | 244 | set = *gmtime ( &syst ); |
245 | 245 | ||
246 | // if the difference between system and hardware time is more than 3 seconds, | 246 | // if the difference between system and hardware time is more than 3 seconds, |
247 | // we have to set the RTC (hwclock --systohc), or alarms won't work reliably. | 247 | // we have to set the RTC (hwclock --systohc), or alarms won't work reliably. |
248 | 248 | ||
249 | if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 ) | 249 | if ( ioctl ( fd, RTC_SET_TIME, &set ) < 0 ) |
250 | break; // ( 1, "ioctl RTC_SET_TIME" ); | 250 | break; // ( 1, "ioctl RTC_SET_TIME" ); |
251 | } | 251 | } |
252 | 252 | ||
253 | // read the wakeup time from TIMEFILE | 253 | // read the wakeup time from TIMEFILE |
254 | if (!( fp = fopen ( TIMEFILE, "r" ))) | 254 | if (!( fp = fopen ( TIMEFILE, "r" ))) |
255 | break; // ( 1, TIMEFILE ); | 255 | break; // ( 1, TIMEFILE ); |
256 | 256 | ||
257 | if ( !fgets ( buf, sizeof( buf ) - 1, fp )) | 257 | if ( !fgets ( buf, sizeof( buf ) - 1, fp )) |
258 | break; // ( 1, TIMEFILE ); | 258 | break; // ( 1, TIMEFILE ); |
259 | 259 | ||
260 | fclose ( fp ); | 260 | fclose ( fp ); |
261 | fp = 0; | ||
261 | 262 | ||
262 | alrt = atoi ( buf ); // get the alarm time | 263 | alrt = atoi ( buf ); // get the alarm time |
263 | 264 | ||
264 | if ( alrt == 0 ) | 265 | if ( alrt == 0 ) |
265 | break; // ( 0, TIMEFILE " contains an invalid time description" ); | 266 | break; // ( 0, TIMEFILE " contains an invalid time description" ); |
266 | alrt -= 5; // wake up 5 sec before the specified time | 267 | alrt -= 5; // wake up 5 sec before the specified time |
267 | 268 | ||
268 | alr = *gmtime ( &alrt ); | 269 | alr = *gmtime ( &alrt ); |
269 | 270 | ||
270 | if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 ) // set RTC alarm time | 271 | if ( ioctl ( fd, RTC_ALM_SET, &alr ) < 0 ) // set RTC alarm time |
271 | break; // ( 1, "ioctl RTC_ALM_SET" ); | 272 | break; // ( 1, "ioctl RTC_ALM_SET" ); |
272 | 273 | ||
273 | if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) | 274 | if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) |
274 | break; // ( 1, "ioctl RTC_AIE_ON" ); // enable RTC alarm irq | 275 | break; // ( 1, "ioctl RTC_AIE_ON" ); // enable RTC alarm irq |
275 | 276 | ||
276 | // tell the parent it is safe to exit now .. we have set the RTC alarm | 277 | // tell the parent it is safe to exit now .. we have set the RTC alarm |
277 | kill ( parent_pid, SIGUSR1 ); | 278 | kill ( parent_pid, SIGUSR1 ); |
278 | 279 | ||
279 | if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) // wait for the RTC alarm irq | 280 | if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) // wait for the RTC alarm irq |
280 | break; // ( 1, "read rtc alarm" ); | 281 | break; // ( 1, "read rtc alarm" ); |
281 | 282 | ||
282 | // iPAQ woke up via RTC irq -- otherwise we would have received a SIGUSR2 | 283 | // iPAQ woke up via RTC irq -- otherwise we would have received a SIGUSR2 |
283 | // from the "resume instance" of opiealarm. | 284 | // from the "resume instance" of opiealarm. |
284 | 285 | ||
285 | if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) // disable RTC alarm irq | 286 | if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) // disable RTC alarm irq |
286 | break; // ( 1, "ioctl RTC_AIE_OFF" ); | 287 | break; // ( 1, "ioctl RTC_AIE_OFF" ); |
287 | 288 | ||
288 | close ( fd ); | 289 | close ( fd ); |
290 | fd = -1; | ||
289 | 291 | ||
290 | remove_pidfile ( ); | 292 | remove_pidfile ( ); |
291 | 293 | ||
292 | return 0; | 294 | return 0; |
293 | 295 | ||
294 | } while ( 0 ); | 296 | } while ( 0 ); |
295 | 297 | ||
296 | if ( fp != NULL ) | 298 | if ( fp != NULL ) |
297 | fclose ( fp ); | 299 | fclose ( fp ); |
298 | 300 | ||
299 | if ( fd != -1 ) | 301 | if ( fd != -1 ) |
300 | close ( fd ); | 302 | close ( fd ); |
301 | 303 | ||
302 | kill ( parent_pid, SIGUSR1 ); | 304 | kill ( parent_pid, SIGUSR1 ); |
303 | 305 | ||
304 | while ( 1 ) // pretend that we are waiting on RTC, so opiealarm -r can kill us | 306 | while ( 1 ) // pretend that we are waiting on RTC, so opiealarm -r can kill us |
305 | sleep ( 1000 ); // if we don't do this, the "resuspend on AC" would be triggerd | 307 | sleep ( 1000 ); // if we don't do this, the "resuspend on AC" would be triggerd |
306 | return 0; | 308 | return 0; |
307 | } | 309 | } |
308 | 310 | ||
309 | int onac ( void ) | 311 | int onac ( void ) |
310 | { | 312 | { |
311 | FILE *fp; | 313 | FILE *fp; |
312 | int on = 0; | 314 | int on = 0; |
313 | 315 | ||
314 | // check the apm proc interface for AC status | 316 | // check the apm proc interface for AC status |
315 | 317 | ||
316 | if (( fp = fopen ( APMFILE, "r" ))) { | 318 | if (( fp = fopen ( APMFILE, "r" ))) { |
317 | int ac = 0; | 319 | int ac = 0; |
318 | 320 | ||
319 | if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) | 321 | if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) |
320 | on = ( ac == 0x01 ) ? 1 : 0; | 322 | on = ( ac == 0x01 ) ? 1 : 0; |
321 | 323 | ||
322 | fclose ( fp ); | 324 | fclose ( fp ); |
323 | } | 325 | } |
324 | return on; | 326 | return on; |
325 | } | 327 | } |
326 | 328 | ||
327 | int resume ( int resuspend ) | 329 | int resume ( int resuspend ) |
328 | { | 330 | { |
329 | FILE *fp; | 331 | FILE *fp; |
330 | 332 | ||
331 | // re-suspend when on AC (optional) when woken up via RTC | 333 | // re-suspend when on AC (optional) when woken up via RTC |
332 | 334 | ||
333 | if ( !opiealarm_was_running ) { | 335 | if ( !opiealarm_was_running ) { |
334 | // if opiealarm -s didn't wake up via RTC, the old process gets killed | 336 | // if opiealarm -s didn't wake up via RTC, the old process gets killed |
335 | // by kill_by_pidfile(), which is recorded in opiealarm_was_running | 337 | // by kill_by_pidfile(), which is recorded in opiealarm_was_running |
336 | 338 | ||
337 | if ( resuspend && onac ( )) { | 339 | if ( resuspend && onac ( )) { |
338 | time_t start, now; | 340 | time_t start, now; |
339 | char *argv [4]; | 341 | char *argv [4]; |
340 | 342 | ||
341 | if ( !fork_with_pidfile ( )) | 343 | if ( !fork_with_pidfile ( )) |
342 | return 4; | 344 | return 4; |
343 | 345 | ||
344 | // we can't wait for the resuspend timeout in the parent process. | 346 | // we can't wait for the resuspend timeout in the parent process. |
345 | // so we fork and tell the parent it can exit immediatly | 347 | // so we fork and tell the parent it can exit immediatly |
346 | 348 | ||
347 | kill ( parent_pid, SIGUSR1 ); | 349 | kill ( parent_pid, SIGUSR1 ); |
348 | 350 | ||
349 | // sleep <resuspend> seconds - this method is much more precise than sleep() ! | 351 | // sleep <resuspend> seconds - this method is much more precise than sleep() ! |
350 | time ( &start ); | 352 | time ( &start ); |
351 | do { | 353 | do { |
352 | sleep ( 1 ); | 354 | sleep ( 1 ); |
353 | time ( &now ); | 355 | time ( &now ); |
354 | } while (( now - start ) < resuspend ); | 356 | } while (( now - start ) < resuspend ); |
355 | 357 | ||
356 | if ( onac ( )) { // still on ac ? | 358 | if ( onac ( )) { // still on ac ? |
357 | argv[0] = "qcop"; | 359 | argv[0] = "qcop"; |
358 | argv[1] = "QPE/Desktop"; | 360 | argv[1] = "QPE/Desktop"; |
359 | argv[2] = "suspend()"; | 361 | argv[2] = "suspend()"; |
360 | argv[3] = 0; | 362 | argv[3] = 0; |
361 | 363 | ||
362 | // hard coded for now ...but needed | 364 | // hard coded for now ...but needed |
363 | // another way would be to simulate a power-button press | 365 | // another way would be to simulate a power-button press |
364 | 366 | ||
365 | setenv ( "LOGNAME", "root", 1 ); | 367 | setenv ( "LOGNAME", "root", 1 ); |
366 | setenv ( "HOME", "/root", 1 ); | 368 | setenv ( "HOME", "/root", 1 ); |
367 | setenv ( "LD_LIBRARY_PATH", "/opt/QtPalmtop/lib", 1 ); | 369 | setenv ( "LD_LIBRARY_PATH", "/opt/QtPalmtop/lib", 1 ); |
368 | setenv ( "QTDIR", "/opt/QtPalmtop", 1 ); | 370 | setenv ( "QTDIR", "/opt/QtPalmtop", 1 ); |
369 | 371 | ||
370 | remove_pidfile ( ); | 372 | remove_pidfile ( ); |
371 | 373 | ||
372 | // no need for system() since this process is no longer usefull anyway | 374 | // no need for system() since this process is no longer usefull anyway |
373 | execv ( "/opt/QtPalmtop/bin/qcop", argv ); | 375 | execv ( "/opt/QtPalmtop/bin/qcop", argv ); |
374 | 376 | ||
375 | perror ( "exec for qcop failed" ); | 377 | perror ( "exec for qcop failed" ); |
376 | return 5; | 378 | return 5; |
377 | } | 379 | } |
378 | } | 380 | } |
379 | } | 381 | } |
380 | return 0; | 382 | return 0; |
381 | } | 383 | } |