-rw-r--r-- | core/opiealarm/opiealarm.c | 36 | ||||
-rwxr-xr-x | root/etc/resume-scripts/R46opiealarm | 2 |
2 files changed, 25 insertions, 13 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c index 3a06236..ac98832 100644 --- a/core/opiealarm/opiealarm.c +++ b/core/opiealarm/opiealarm.c | |||
@@ -25,17 +25,17 @@ | |||
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 ( void ); | 33 | int resume ( int resuspend ); |
34 | int suspend ( void ); | 34 | int suspend ( void ); |
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 ); |
@@ -68,17 +68,20 @@ void sig_handler ( int sig ) | |||
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 | ||
74 | void usage ( void ) | 74 | void usage ( void ) |
75 | { | 75 | { |
76 | fprintf ( stderr, "Usage: opiealarm -r|-s\n" ); | 76 | fprintf ( stderr, "Usage: opiealarm -r|-s [-a]\n\n" ); |
77 | fprintf ( stderr, "\t-s\tSuspend mode: set RTC alarm\n" ); | ||
78 | fprintf ( stderr, "\t-r\tResume mode: kill running opiealarm\n" ); | ||
79 | fprintf ( stderr, "\t-a <x>\tResuspend in <x> seconds (resume mode)\n\n" ); | ||
77 | exit ( 1 ); | 80 | exit ( 1 ); |
78 | } | 81 | } |
79 | 82 | ||
80 | int fork_with_pidfile ( void ) | 83 | int fork_with_pidfile ( void ) |
81 | { | 84 | { |
82 | FILE *fp; | 85 | FILE *fp; |
83 | pid_t pid; | 86 | pid_t pid; |
84 | 87 | ||
@@ -134,26 +137,35 @@ void remove_pidfile ( void ) | |||
134 | signal ( SIGTERM, SIG_DFL ); | 137 | signal ( SIGTERM, SIG_DFL ); |
135 | signal ( SIGINT, SIG_DFL ); | 138 | signal ( SIGINT, SIG_DFL ); |
136 | } | 139 | } |
137 | 140 | ||
138 | 141 | ||
139 | int main ( int argc, char **argv ) | 142 | int main ( int argc, char **argv ) |
140 | { | 143 | { |
141 | int mode = 0; | 144 | int mode = 0; |
145 | int ac_resusp = 0; | ||
142 | int opt; | 146 | int opt; |
143 | 147 | ||
144 | while (( opt = getopt ( argc, argv, "rs" )) != EOF ) { | 148 | while (( opt = getopt ( argc, argv, "a:rs" )) != EOF ) { |
145 | switch ( opt ) { | 149 | switch ( opt ) { |
146 | case 's': | 150 | case 's': |
147 | mode = 's'; | 151 | mode = 's'; |
148 | break; | 152 | break; |
149 | case 'r': | 153 | case 'r': |
150 | mode = 'r'; | 154 | mode = 'r'; |
151 | break; | 155 | break; |
156 | case 'a': | ||
157 | ac_resusp = atoi ( optarg ); | ||
158 | if ( ac_resusp < 30 ) { | ||
159 | ac_resusp = 120; | ||
160 | |||
161 | fprintf ( stderr, "Warning: resuspend timeout must be >= 30 sec. -- now set to 120 sec\n" ); | ||
162 | } | ||
163 | break; | ||
152 | default: | 164 | default: |
153 | usage ( ); | 165 | usage ( ); |
154 | } | 166 | } |
155 | } | 167 | } |
156 | 168 | ||
157 | if ( geteuid ( ) != 0 ) { | 169 | if ( geteuid ( ) != 0 ) { |
158 | fprintf ( stderr, "You need root priviledges to run opiealarm." ); | 170 | fprintf ( stderr, "You need root priviledges to run opiealarm." ); |
159 | return 2; | 171 | return 2; |
@@ -161,21 +173,21 @@ int main ( int argc, char **argv ) | |||
161 | 173 | ||
162 | if ( !mode ) | 174 | if ( !mode ) |
163 | usage ( ); | 175 | usage ( ); |
164 | 176 | ||
165 | // kill running opiealarm | 177 | // kill running opiealarm |
166 | opiealarm_was_running = kill_with_pidfile ( ); | 178 | opiealarm_was_running = kill_with_pidfile ( ); |
167 | remove_pidfile ( ); | 179 | remove_pidfile ( ); |
168 | 180 | ||
169 | if ( mode == 'r' ) | 181 | switch ( mode ) { |
170 | return resume ( ); | 182 | case 'r': return resume ( ac_resusp ); |
171 | else | 183 | case 's': |
172 | return suspend ( ); | 184 | default : return suspend ( ); |
173 | 185 | } | |
174 | return 0; | 186 | return 0; |
175 | } | 187 | } |
176 | 188 | ||
177 | 189 | ||
178 | int suspend ( void ) | 190 | int suspend ( void ) |
179 | { | 191 | { |
180 | FILE *fp; | 192 | FILE *fp; |
181 | char buf [64]; | 193 | char buf [64]; |
@@ -257,36 +269,36 @@ static int onac ( void ) | |||
257 | if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) | 269 | if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) |
258 | on = ( ac == 0x01 ) ? 1 : 0; | 270 | on = ( ac == 0x01 ) ? 1 : 0; |
259 | 271 | ||
260 | fclose ( fp ); | 272 | fclose ( fp ); |
261 | } | 273 | } |
262 | return on; | 274 | return on; |
263 | } | 275 | } |
264 | 276 | ||
265 | int resume ( void ) | 277 | int resume ( int resuspend ) |
266 | { | 278 | { |
267 | FILE *fp; | 279 | FILE *fp; |
268 | 280 | ||
269 | // re-suspend when on AC (optional) when woken up via RTC | 281 | // re-suspend when on AC (optional) when woken up via RTC |
270 | 282 | ||
271 | if ( !opiealarm_was_running ) { // opiealarm -s got it's RTC signal -> wake up by RTC | 283 | if ( !opiealarm_was_running ) { // opiealarm -s got it's RTC signal -> wake up by RTC |
272 | if ( onac ( )) { | 284 | if ( resuspend && onac ( )) { |
273 | time_t start, now; | 285 | time_t start, now; |
274 | char *argv [4]; | 286 | char *argv [4]; |
275 | 287 | ||
276 | if ( !fork_with_pidfile ( )) | 288 | if ( !fork_with_pidfile ( )) |
277 | return 4; | 289 | return 4; |
278 | 290 | ||
279 | // sleep 120sec (not less!) | 291 | // sleep <resuspend> sec (not less!) |
280 | time ( &start ); | 292 | time ( &start ); |
281 | do { | 293 | do { |
282 | sleep ( 1 ); | 294 | sleep ( 1 ); |
283 | time ( &now ); | 295 | time ( &now ); |
284 | } while (( now - start ) < 120 ); | 296 | } while (( now - start ) < resuspend ); |
285 | 297 | ||
286 | if ( onac ( )) { // still on ac | 298 | if ( onac ( )) { // still on ac |
287 | // system() without fork | 299 | // system() without fork |
288 | argv[0] = "qcop"; | 300 | argv[0] = "qcop"; |
289 | argv[1] = "QPE/Desktop"; | 301 | argv[1] = "QPE/Desktop"; |
290 | argv[2] = "suspend()"; | 302 | argv[2] = "suspend()"; |
291 | argv[3] = 0; | 303 | argv[3] = 0; |
292 | 304 | ||
diff --git a/root/etc/resume-scripts/R46opiealarm b/root/etc/resume-scripts/R46opiealarm index c258047..169dc30 100755 --- a/root/etc/resume-scripts/R46opiealarm +++ b/root/etc/resume-scripts/R46opiealarm | |||
@@ -1,5 +1,5 @@ | |||
1 | #!/bin/sh | 1 | #!/bin/sh |
2 | # Starts opiealarm, which only runs while the iPaq sleeps | 2 | # Starts opiealarm, which only runs while the iPaq sleeps |
3 | # and wakes it up when the RTC alarm goes off. | 3 | # and wakes it up when the RTC alarm goes off. |
4 | 4 | ||
5 | /opt/QtPalmtop/bin/opiealarm -r | 5 | /opt/QtPalmtop/bin/opiealarm -r -a 120 |