author | sandman <sandman> | 2002-06-02 01:42:05 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-06-02 01:42:05 (UTC) |
commit | 2205558adae76dc40a5c0f2dd83226a4d1c67eae (patch) (unidiff) | |
tree | e7e96aa694bab035c2c75f3c1b6dbceea07d4270 | |
parent | 385a3d8034003a412fa73840bf985582bf6b4547 (diff) | |
download | opie-2205558adae76dc40a5c0f2dd83226a4d1c67eae.zip opie-2205558adae76dc40a5c0f2dd83226a4d1c67eae.tar.gz opie-2205558adae76dc40a5c0f2dd83226a4d1c67eae.tar.bz2 |
Reworked the whole system and introduced re-suspend. Effect is that the
iPAQ is re-suspended when woken up by the RTC (as opposed to the button)
and on AC. Timeout is currently hardcoded to 120sec.
-rw-r--r-- | core/opiealarm/opiealarm.c | 246 | ||||
-rwxr-xr-x | core/opiealarm/opieatd | 29 | ||||
-rwxr-xr-x | root/etc/init.d/opieatd | 2 |
3 files changed, 217 insertions, 60 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c index 33d631d..3a06236 100644 --- a/core/opiealarm/opiealarm.c +++ b/core/opiealarm/opiealarm.c | |||
@@ -23,2 +23,4 @@ | |||
23 | #include <signal.h> | 23 | #include <signal.h> |
24 | #include <errno.h> | ||
25 | #include <string.h> | ||
24 | 26 | ||
@@ -26,4 +28,18 @@ | |||
26 | #define PIDFILE "/var/run/opiealarm.pid" | 28 | #define PIDFILE "/var/run/opiealarm.pid" |
29 | #define APMFILE "/proc/apm" | ||
30 | |||
31 | FILE *log; // debug only | ||
27 | 32 | ||
28 | FILE *log; | 33 | int resume ( void ); |
34 | int suspend ( void ); | ||
35 | int main ( int argc, char **argv ); | ||
36 | int fork_with_pidfile ( void ); | ||
37 | int kill_with_pidfile ( void ); | ||
38 | void remove_pidfile ( void ); | ||
39 | void usage ( void ); | ||
40 | void sig_handler ( int sig ); | ||
41 | void error_msg_and_die ( int perr, const char *msg ); | ||
42 | int onac ( void ); | ||
43 | |||
44 | static int opiealarm_was_running; | ||
29 | 45 | ||
@@ -44,3 +60,18 @@ void error_msg_and_die ( int perr, const char *msg ) | |||
44 | 60 | ||
45 | unlink ( PIDFILE ); | 61 | while ( 1 ) // pretend we are waiting on RTC, so opiealarm -r can kill us |
62 | sleep ( 1 ); | ||
63 | } | ||
64 | |||
65 | |||
66 | void sig_handler ( int sig ) | ||
67 | { | ||
68 | log_msg ( "GOT SIGNAL -> EXITING\n" ); | ||
69 | fclose ( log ); | ||
70 | remove_pidfile ( ); | ||
71 | exit ( 0 ); | ||
72 | } | ||
73 | |||
74 | void usage ( void ) | ||
75 | { | ||
76 | fprintf ( stderr, "Usage: opiealarm -r|-s\n" ); | ||
46 | exit ( 1 ); | 77 | exit ( 1 ); |
@@ -48,5 +79,101 @@ void error_msg_and_die ( int perr, const char *msg ) | |||
48 | 79 | ||
80 | int fork_with_pidfile ( void ) | ||
81 | { | ||
82 | FILE *fp; | ||
83 | pid_t pid; | ||
84 | |||
85 | pid = fork ( ); | ||
86 | |||
87 | if ( pid > 0 ) | ||
88 | exit ( 0 ); | ||
89 | else if ( pid < 0 ) { | ||
90 | perror ( "forking failed" ); | ||
91 | return 0; | ||
92 | } | ||
93 | |||
94 | signal ( SIGTERM, sig_handler ); | ||
95 | signal ( SIGINT, sig_handler ); | ||
96 | |||
97 | // save pid | ||
98 | if (( fp = fopen ( PIDFILE, "w" ))) { | ||
99 | fprintf ( fp, "%d", getpid ( )); | ||
100 | fclose ( fp ); | ||
101 | |||
102 | // detach | ||
103 | close ( 0 ); | ||
104 | close ( 1 ); | ||
105 | close ( 2 ); | ||
106 | |||
107 | setpgid ( 0, 0 ); | ||
108 | |||
109 | return 1; | ||
110 | } | ||
111 | else { | ||
112 | perror ( PIDFILE ); | ||
113 | return 0; | ||
114 | } | ||
115 | } | ||
116 | |||
117 | int kill_with_pidfile ( void ) | ||
118 | { | ||
119 | FILE *fp; | ||
120 | pid_t pid; | ||
121 | |||
122 | if (( fp = fopen ( PIDFILE, "r" ))) { | ||
123 | if ( fscanf ( fp, "%d", &pid ) == 1 ) | ||
124 | return ( kill ( pid, SIGTERM ) == 0 ) ? 1 : 0; | ||
125 | fclose ( fp ); | ||
126 | } | ||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | void remove_pidfile ( void ) | ||
131 | { | ||
132 | unlink ( PIDFILE ); | ||
133 | |||
134 | signal ( SIGTERM, SIG_DFL ); | ||
135 | signal ( SIGINT, SIG_DFL ); | ||
136 | } | ||
137 | |||
138 | |||
139 | int main ( int argc, char **argv ) | ||
140 | { | ||
141 | int mode = 0; | ||
142 | int opt; | ||
143 | |||
144 | while (( opt = getopt ( argc, argv, "rs" )) != EOF ) { | ||
145 | switch ( opt ) { | ||
146 | case 's': | ||
147 | mode = 's'; | ||
148 | break; | ||
149 | case 'r': | ||
150 | mode = 'r'; | ||
151 | break; | ||
152 | default: | ||
153 | usage ( ); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | if ( geteuid ( ) != 0 ) { | ||
158 | fprintf ( stderr, "You need root priviledges to run opiealarm." ); | ||
159 | return 2; | ||
160 | } | ||
161 | |||
162 | if ( !mode ) | ||
163 | usage ( ); | ||
164 | |||
165 | // kill running opiealarm | ||
166 | opiealarm_was_running = kill_with_pidfile ( ); | ||
167 | remove_pidfile ( ); | ||
168 | |||
169 | if ( mode == 'r' ) | ||
170 | return resume ( ); | ||
171 | else | ||
172 | return suspend ( ); | ||
49 | 173 | ||
174 | return 0; | ||
175 | } | ||
176 | |||
50 | 177 | ||
51 | void suspend_on_rtc ( ) | 178 | int suspend ( void ) |
52 | { | 179 | { |
@@ -57,4 +184,12 @@ void suspend_on_rtc ( ) | |||
57 | int fd; | 184 | int fd; |
185 | |||
186 | |||
187 | if ( !fork_with_pidfile ( )) | ||
188 | return 3; | ||
189 | |||
190 | log = fopen ( "/tmp/opiealarm.log", "w" ); | ||
191 | log_msg ( "STARTING\n" ); | ||
58 | 192 | ||
59 | 193 | ||
194 | |||
60 | if (!( fp = fopen ( "/etc/resumeat", "r" ))) | 195 | if (!( fp = fopen ( "/etc/resumeat", "r" ))) |
@@ -80,4 +215,4 @@ void suspend_on_rtc ( ) | |||
80 | 215 | ||
81 | // Write alarm time to RTC | 216 | // Write alarm time to RTC |
82 | if (( fd = open ( "/dev/misc/rtc", O_RDWR ) < 0 ); | 217 | if (( fd = open ( "/dev/misc/rtc", O_RDWR )) < 0 ) |
83 | error_msg_and_die ( 1, "/dev/misc/rtc" ); | 218 | error_msg_and_die ( 1, "/dev/misc/rtc" ); |
@@ -103,11 +238,9 @@ void suspend_on_rtc ( ) | |||
103 | close ( fd ); | 238 | close ( fd ); |
104 | } | 239 | |
105 | 240 | log_msg ( "EXITING\n" ); | |
106 | 241 | ||
107 | void sig_handler ( int sig ) | ||
108 | { | ||
109 | log_msg ( "GOT SIGNAL -> EXITING\n" ); | ||
110 | fclose ( log ); | 242 | fclose ( log ); |
111 | unlink ( PIDFILE ); | 243 | remove_pidfile ( ); |
112 | exit ( 0 ); | 244 | |
245 | return 0; | ||
113 | } | 246 | } |
@@ -115,52 +248,61 @@ void sig_handler ( int sig ) | |||
115 | 248 | ||
116 | int main ( ) | 249 | static int onac ( void ) |
117 | { | 250 | { |
118 | FILE *fp; | 251 | FILE *fp; |
119 | pid_t pid; | 252 | int on = 0; |
120 | 253 | ||
121 | if ( geteuid ( ) != 0 ) { | 254 | if (( fp = fopen ( APMFILE, "r" ))) { |
122 | fprintf ( stderr, "You need root priviledges to run opiealarm." ); | 255 | int ac = 0; |
123 | return 1; | ||
124 | } | ||
125 | 256 | ||
126 | pid = fork ( ); | 257 | if ( fscanf ( fp, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%*x 0x%*x %*d%% %*i %*c", &ac ) == 1 ) |
127 | 258 | on = ( ac == 0x01 ) ? 1 : 0; | |
128 | if ( pid > 0 ) | 259 | |
129 | return 0; | 260 | fclose ( fp ); |
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 | } | 261 | } |
140 | 262 | return on; | |
141 | fprintf ( fp, "%d", getpid ( )); | 263 | } |
142 | fclose ( fp ); | ||
143 | 264 | ||
144 | // detach | 265 | int resume ( void ) |
145 | close ( 0 ); | 266 | { |
146 | close ( 1 ); | 267 | FILE *fp; |
147 | close ( 2 ); | ||
148 | 268 | ||
149 | setpgid ( 0, 0 ); | 269 | // re-suspend when on AC (optional) when woken up via RTC |
150 | 270 | ||
151 | log = fopen ( "/tmp/opiealarm.log", "w" ); | 271 | if ( !opiealarm_was_running ) { // opiealarm -s got it's RTC signal -> wake up by RTC |
152 | log_msg ( "STARTING\n" ); | 272 | if ( onac ( )) { |
153 | 273 | time_t start, now; | |
154 | signal ( SIGTERM, sig_handler ); | 274 | char *argv [4]; |
155 | signal ( SIGINT, sig_handler ); | 275 | |
276 | if ( !fork_with_pidfile ( )) | ||
277 | return 4; | ||
156 | 278 | ||
157 | extractevent ( ); | 279 | // sleep 120sec (not less!) |
158 | 280 | time ( &start ); | |
159 | signal ( SIGTERM, SIG_DFL ); | 281 | do { |
160 | signal ( SIGINT, SIG_DFL ); | 282 | sleep ( 1 ); |
283 | time ( &now ); | ||
284 | } while (( now - start ) < 120 ); | ||
161 | 285 | ||
162 | log_msg ( "EXITING\n" ); | 286 | if ( onac ( )) { // still on ac |
287 | // system() without fork | ||
288 | argv[0] = "qcop"; | ||
289 | argv[1] = "QPE/Desktop"; | ||
290 | argv[2] = "suspend()"; | ||
291 | argv[3] = 0; | ||
163 | 292 | ||
164 | fclose ( log ); | 293 | // hard coded for now ...but needed |
165 | unlink ( PIDFILE ); | 294 | setenv ( "LOGNAME", "root", 1 ); |
295 | setenv ( "HOME", "/root", 1 ); | ||
296 | setenv ( "LD_LIBRARY_PATH", "/opt/QtPalmtop/lib", 1 ); | ||
297 | setenv ( "QTDIR", "/opt/QtPalmtop", 1 ); | ||
298 | |||
299 | remove_pidfile ( ); | ||
300 | |||
301 | execv ( "/opt/QtPalmtop/bin/qcop", argv ); | ||
302 | |||
303 | perror ( "exec for qcop failed" ); | ||
304 | return 5; | ||
305 | } | ||
306 | } | ||
307 | } | ||
166 | return 0; | 308 | return 0; |
diff --git a/core/opiealarm/opieatd b/core/opiealarm/opieatd index 3b9dc0e..fc2d421 100755 --- a/core/opiealarm/opieatd +++ b/core/opiealarm/opieatd | |||
@@ -4,2 +4,6 @@ timefile=/etc/resumeat | |||
4 | 4 | ||
5 | trap exit 0 SIGTERM SIGINT SIGQUIT | ||
6 | |||
7 | echo $$ >/var/run/opieatd.pid | ||
8 | |||
5 | mkdir -p /var/spool/at | 9 | mkdir -p /var/spool/at |
@@ -8,6 +12,6 @@ mkdir -p /var/spool/at | |||
8 | while true; do | 12 | while true; do |
9 | cat /var/spool/at/trigger | while read line; do | 13 | while read </var/spool/at/trigger dummy; do |
10 | FILE=`ls -1 /var/spool/at/[0-9]* | head -n1` | 14 | FILES=`ls /var/spool/at/[0-9]* 2>/dev/null` |
11 | echo "File = $FILE" | 15 | |
12 | if [ -z "$FILE" ]; then | 16 | if [ -z "$FILES" ]; then |
13 | echo "clear resume at" | 17 | echo "clear resume at" |
@@ -15,5 +19,15 @@ while true; do | |||
15 | else | 19 | else |
16 | unixtime=`basename $FILE | cut -c1-10` | 20 | for i in "$FILES"; do |
17 | echo "Datestring = $unixtime" | 21 | echo "File = $i" |
18 | echo "$unixtime" >$timefile | 22 | |
23 | unixtime=`basename $i | cut -c1-10` | ||
24 | pid=`basename $i | cut -c12-` | ||
25 | |||
26 | if [ -d /proc/$pid ]; then | ||
27 | echo "Datestring = $unixtime" | ||
28 | echo "$unixtime" >$timefile | ||
29 | else | ||
30 | rm -f $i | ||
31 | fi | ||
32 | done | ||
19 | fi | 33 | fi |
@@ -22 +36,2 @@ done | |||
22 | 36 | ||
37 | exit 0 | ||
diff --git a/root/etc/init.d/opieatd b/root/etc/init.d/opieatd index 83f6035..765426c 100755 --- a/root/etc/init.d/opieatd +++ b/root/etc/init.d/opieatd | |||
@@ -10,3 +10,3 @@ case "$1" in | |||
10 | stop) | 10 | stop) |
11 | killall opieatd | 11 | kill `cat /var/run/opieatd.pid` |
12 | ;; | 12 | ;; |