author | sandman <sandman> | 2002-06-01 19:01:22 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-06-01 19:01:22 (UTC) |
commit | 9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1 (patch) (unidiff) | |
tree | 16bc3af93db4a6677f324a48efe9663361e576b9 | |
parent | bc167b7ef02ccd5f6127d9f9640f4b7af6c5a496 (diff) | |
download | opie-9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1.zip opie-9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1.tar.gz opie-9c2cdd8890f0508dd00e2bde8e1cfa85d1a814d1.tar.bz2 |
Changed opiealarm into a real daemon (fork/detach)
Logging to /tmp/opiealarm.log is intentionally active at the moment.
-rw-r--r-- | core/opiealarm/opiealarm.c | 102 |
1 files changed, 89 insertions, 13 deletions
diff --git a/core/opiealarm/opiealarm.c b/core/opiealarm/opiealarm.c index 071cb24..33d631d 100644 --- a/core/opiealarm/opiealarm.c +++ b/core/opiealarm/opiealarm.c | |||
@@ -18,28 +18,46 @@ | |||
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> | ||
23 | #include <signal.h> | ||
22 | 24 | ||
23 | 25 | ||
26 | #define PIDFILE "/var/run/opiealarm.pid" | ||
27 | |||
28 | FILE *log; | ||
29 | |||
30 | |||
31 | void log_msg ( const char *msg ) | ||
32 | { | ||
33 | if ( log ) { | ||
34 | fprintf ( log, msg ); | ||
35 | fflush ( log ); | ||
36 | } | ||
37 | } | ||
38 | |||
24 | void error_msg_and_die ( int perr, const char *msg ) | 39 | void error_msg_and_die ( int perr, const char *msg ) |
25 | { | 40 | { |
26 | if ( perr ) | 41 | if ( perr ) |
27 | perror ( msg ); | 42 | log_msg ( strerror ( errno )); |
28 | else | 43 | log_msg ( msg ); |
29 | fprintf ( stderr, "%s\n", msg ); | 44 | |
45 | unlink ( PIDFILE ); | ||
30 | exit ( 1 ); | 46 | exit ( 1 ); |
31 | } | 47 | } |
32 | 48 | ||
33 | 49 | ||
34 | void extractevent ( ) | 50 | |
51 | void suspend_on_rtc ( ) | ||
35 | { | 52 | { |
36 | FILE *fp; | 53 | FILE *fp; |
37 | char buf [64]; | 54 | char buf [64]; |
38 | time_t t; | 55 | time_t t; |
39 | struct tm *tm; | 56 | struct tm *tm; |
40 | int fd; | 57 | int fd; |
41 | 58 | ||
59 | |||
42 | if (!( fp = fopen ( "/etc/resumeat", "r" ))) | 60 | if (!( fp = fopen ( "/etc/resumeat", "r" ))) |
43 | error_msg_and_die ( 1, "/etc/resumeat" ); | 61 | error_msg_and_die ( 1, "/etc/resumeat" ); |
44 | 62 | ||
45 | if ( !fgets ( buf, sizeof( buf ) - 1, fp )) | 63 | if ( !fgets ( buf, sizeof( buf ) - 1, fp )) |
@@ -53,39 +71,97 @@ void extractevent ( ) | |||
53 | error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" ); | 71 | error_msg_and_die ( 0, "/etc/resumeat contains an invalid time description" ); |
54 | 72 | ||
55 | /* subtract 5 sec from event time... */ | 73 | /* subtract 5 sec from event time... */ |
56 | t -= 5; | 74 | t -= 5; |
57 | tm = gmtime ( &t ); | ||
58 | 75 | ||
59 | /* Write alarm time to RTC */ | 76 | if ( log ) |
60 | fd = open ( "/dev/misc/rtc", O_RDWR ); | 77 | fprintf ( log, "Setting RTC alarm to %d\n", t ); |
61 | if ( fd < 0 ) | ||
62 | error_msg_and_die ( 1, "/dev/misc/rtc" ); | ||
63 | 78 | ||
79 | tm = gmtime ( &t ); | ||
80 | |||
81 | // Write alarm time to RTC | ||
82 | if (( fd = open ( "/dev/misc/rtc", O_RDWR ) < 0 ); | ||
83 | error_msg_and_die ( 1, "/dev/misc/rtc" ); | ||
64 | // set alarm time | 84 | // set alarm time |
65 | if ( ioctl ( fd, RTC_ALM_SET, tm ) < 0 ) | 85 | if ( ioctl ( fd, RTC_ALM_SET, tm ) < 0 ) |
66 | error_msg_and_die ( 1, "ioctl RTC_ALM_SET" ); | 86 | error_msg_and_die ( 1, "ioctl RTC_ALM_SET" ); |
67 | |||
68 | // enable alarm irq | 87 | // enable alarm irq |
69 | if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) | 88 | if ( ioctl ( fd, RTC_AIE_ON, 0 ) < 0 ) |
70 | error_msg_and_die ( 1, "ioctl RTC_AIE_ON" ); | 89 | error_msg_and_die ( 1, "ioctl RTC_AIE_ON" ); |
90 | |||
91 | log_msg ( "SLEEPING\n" ); | ||
71 | 92 | ||
72 | // wait for alarm irq | 93 | // wait for alarm irq |
73 | if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) | 94 | if ( read ( fd, buf, sizeof( unsigned long )) < 0 ) |
74 | error_msg_and_die ( 1, "read rtc alarm" ); | 95 | error_msg_and_die ( 1, "read rtc alarm" ); |
75 | 96 | ||
97 | log_msg ( "WAKEUP\n" ); | ||
98 | |||
76 | // disable alarm irq | 99 | // disable alarm irq |
77 | if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) | 100 | if ( ioctl ( fd, RTC_AIE_OFF, 0 ) < 0 ) |
78 | error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" ); | 101 | error_msg_and_die ( 1, "ioctl RTC_AIE_OFF" ); |
79 | 102 | ||
80 | close ( fd ); | 103 | close ( fd ); |
81 | } | 104 | } |
82 | 105 | ||
83 | 106 | ||
107 | void sig_handler ( int sig ) | ||
108 | { | ||
109 | log_msg ( "GOT SIGNAL -> EXITING\n" ); | ||
110 | fclose ( log ); | ||
111 | unlink ( PIDFILE ); | ||
112 | exit ( 0 ); | ||
113 | } | ||
114 | |||
115 | |||
84 | int main ( ) | 116 | int main ( ) |
85 | { | 117 | { |
86 | if ( geteuid ( ) != 0 ) | 118 | FILE *fp; |
87 | error_msg_and_die ( 0, "You need root priviledges to run opiealarm." ); | 119 | pid_t pid; |
120 | |||
121 | if ( geteuid ( ) != 0 ) { | ||
122 | fprintf ( stderr, "You need root priviledges to run opiealarm." ); | ||
123 | return 1; | ||
124 | } | ||
125 | |||
126 | pid = fork ( ); | ||
127 | |||
128 | if ( pid > 0 ) | ||
129 | return 0; | ||
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 | } | ||
140 | |||
141 | fprintf ( fp, "%d", getpid ( )); | ||
142 | fclose ( fp ); | ||
143 | |||
144 | // detach | ||
145 | close ( 0 ); | ||
146 | close ( 1 ); | ||
147 | close ( 2 ); | ||
148 | |||
149 | setpgid ( 0, 0 ); | ||
150 | |||
151 | log = fopen ( "/tmp/opiealarm.log", "w" ); | ||
152 | log_msg ( "STARTING\n" ); | ||
153 | |||
154 | signal ( SIGTERM, sig_handler ); | ||
155 | signal ( SIGINT, sig_handler ); | ||
88 | 156 | ||
89 | extractevent ( ); | 157 | extractevent ( ); |
158 | |||
159 | signal ( SIGTERM, SIG_DFL ); | ||
160 | signal ( SIGINT, SIG_DFL ); | ||
161 | |||
162 | log_msg ( "EXITING\n" ); | ||
163 | |||
164 | fclose ( log ); | ||
165 | unlink ( PIDFILE ); | ||
90 | return 0; | 166 | return 0; |
91 | } | 167 | } |