summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/opiealarm/opiealarm.c246
-rwxr-xr-xcore/opiealarm/opieatd29
-rwxr-xr-xroot/etc/init.d/opieatd2
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
@@ -21,11 +21,27 @@
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>
25#include <string.h>
24 26
25 27
26 #define PIDFILE "/var/run/opiealarm.pid" 28 #define PIDFILE "/var/run/opiealarm.pid"
29 #define APMFILE "/proc/apm"
30
31FILE *log; // debug only
27 32
28FILE *log; 33int resume ( void );
34int suspend ( void );
35int main ( int argc, char **argv );
36int fork_with_pidfile ( void );
37int kill_with_pidfile ( void );
38void remove_pidfile ( void );
39void usage ( void );
40void sig_handler ( int sig );
41void error_msg_and_die ( int perr, const char *msg );
42int onac ( void );
43
44static int opiealarm_was_running;
29 45
30 46
31void log_msg ( const char *msg ) 47void log_msg ( const char *msg )
@@ -42,21 +58,140 @@ void error_msg_and_die ( int perr, const char *msg )
42 log_msg ( strerror ( errno )); 58 log_msg ( strerror ( errno ));
43 log_msg ( msg ); 59 log_msg ( 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
66void sig_handler ( int sig )
67{
68 log_msg ( "GOT SIGNAL -> EXITING\n" );
69 fclose ( log );
70 remove_pidfile ( );
71 exit ( 0 );
72}
73
74void usage ( void )
75{
76 fprintf ( stderr, "Usage: opiealarm -r|-s\n" );
46 exit ( 1 ); 77 exit ( 1 );
47} 78}
48 79
80int 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
117int 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
130void remove_pidfile ( void )
131{
132 unlink ( PIDFILE );
133
134 signal ( SIGTERM, SIG_DFL );
135 signal ( SIGINT, SIG_DFL );
136}
137
138
139int 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
51void suspend_on_rtc ( ) 178int suspend ( void )
52{ 179{
53 FILE *fp; 180 FILE *fp;
54 char buf [64]; 181 char buf [64];
55 time_t t; 182 time_t t;
56 struct tm *tm; 183 struct tm *tm;
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" )))
61 error_msg_and_die ( 1, "/etc/resumeat" ); 196 error_msg_and_die ( 1, "/etc/resumeat" );
62 197
@@ -78,8 +213,8 @@ void suspend_on_rtc ( )
78 213
79 tm = gmtime ( &t ); 214 tm = gmtime ( &t );
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" );
84 // set alarm time 219 // set alarm time
85 if ( ioctl ( fd, RTC_ALM_SET, tm ) < 0 ) 220 if ( ioctl ( fd, RTC_ALM_SET, tm ) < 0 )
@@ -101,67 +236,74 @@ void suspend_on_rtc ( )
101 error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" ); 236 error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" );
102 237
103 close ( fd ); 238 close ( fd );
104} 239
105 240 log_msg ( "EXITING\n" );
106 241
107void 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}
114 247
115 248
116int main ( ) 249static 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 265int 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;
167} 309}
diff --git a/core/opiealarm/opieatd b/core/opiealarm/opieatd
index 3b9dc0e..fc2d421 100755
--- a/core/opiealarm/opieatd
+++ b/core/opiealarm/opieatd
@@ -2,21 +2,36 @@
2 2
3timefile=/etc/resumeat 3timefile=/etc/resumeat
4 4
5trap exit 0 SIGTERM SIGINT SIGQUIT
6
7echo $$ >/var/run/opieatd.pid
8
5mkdir -p /var/spool/at 9mkdir -p /var/spool/at
6[ -p /var/spool/at/trigger ] || mkfifo /var/spool/at/trigger 10[ -p /var/spool/at/trigger ] || mkfifo /var/spool/at/trigger
7 11
8while true; do 12while 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"
14 echo "" >$timefile 18 echo "" >$timefile
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
20 done 34 done
21done 35done
22 36
37exit 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
@@ -8,7 +8,7 @@ case "$1" in
8 /opt/QtPalmtop/bin/opieatd & 8 /opt/QtPalmtop/bin/opieatd &
9 ;; 9 ;;
10 stop) 10 stop)
11 killall opieatd 11 kill `cat /var/run/opieatd.pid`
12 ;; 12 ;;
13esac 13esac
14 14