apmd.c revision 1.9 1 1.9 kenh /* $NetBSD: apmd.c,v 1.9 1998/11/19 19:39:14 kenh Exp $ */
2 1.4 jtc
3 1.1 jtk /*-
4 1.4 jtc * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.4 jtc * All rights reserved.
6 1.4 jtc *
7 1.4 jtc * This code is derived from software contributed to The NetBSD Foundation
8 1.4 jtc * by John Kohl.
9 1.1 jtk *
10 1.1 jtk * Redistribution and use in source and binary forms, with or without
11 1.1 jtk * modification, are permitted provided that the following conditions
12 1.1 jtk * are met:
13 1.1 jtk * 1. Redistributions of source code must retain the above copyright
14 1.1 jtk * notice, this list of conditions and the following disclaimer.
15 1.1 jtk * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jtk * notice, this list of conditions and the following disclaimer in the
17 1.1 jtk * documentation and/or other materials provided with the distribution.
18 1.1 jtk * 3. All advertising materials mentioning features or use of this software
19 1.1 jtk * must display the following acknowledgement:
20 1.4 jtc * This product includes software developed by the NetBSD
21 1.4 jtc * Foundation, Inc. and its contributors.
22 1.4 jtc * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.4 jtc * contributors may be used to endorse or promote products derived
24 1.4 jtc * from this software without specific prior written permission.
25 1.1 jtk *
26 1.4 jtc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.4 jtc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.4 jtc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.6 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.6 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.4 jtc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.4 jtc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.4 jtc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.4 jtc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.4 jtc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 jtk * POSSIBILITY OF SUCH DAMAGE.
37 1.1 jtk */
38 1.1 jtk
39 1.1 jtk #include <stdio.h>
40 1.1 jtk #include <errno.h>
41 1.1 jtk #include <syslog.h>
42 1.1 jtk #include <fcntl.h>
43 1.1 jtk #include <unistd.h>
44 1.1 jtk #include <stdlib.h>
45 1.1 jtk #include <string.h>
46 1.1 jtk #include <signal.h>
47 1.1 jtk #include <sys/types.h>
48 1.2 jtk #include <pwd.h>
49 1.2 jtk #include <grp.h>
50 1.1 jtk #include <sys/stat.h>
51 1.1 jtk #include <sys/ioctl.h>
52 1.1 jtk #include <sys/time.h>
53 1.1 jtk #include <sys/socket.h>
54 1.1 jtk #include <sys/un.h>
55 1.1 jtk #include <sys/wait.h>
56 1.1 jtk #include <machine/apmvar.h>
57 1.1 jtk #include <err.h>
58 1.1 jtk #include "pathnames.h"
59 1.1 jtk #include "apm-proto.h"
60 1.1 jtk
61 1.1 jtk #define MAX(a,b) (a > b ? a : b)
62 1.1 jtk #define TRUE 1
63 1.1 jtk #define FALSE 0
64 1.1 jtk
65 1.1 jtk const char apmdev[] = _PATH_APM_CTLDEV;
66 1.1 jtk const char sockfile[] = _PATH_APM_SOCKET;
67 1.1 jtk
68 1.1 jtk static int debug = 0;
69 1.1 jtk
70 1.1 jtk extern char *__progname;
71 1.1 jtk
72 1.1 jtk void usage (void);
73 1.1 jtk int power_status (int fd, int force, struct apm_power_info *pinfo);
74 1.2 jtk int bind_socket (const char *sn, mode_t mode, uid_t uid, gid_t gid);
75 1.1 jtk enum apm_state handle_client(int sock_fd, int ctl_fd);
76 1.1 jtk void suspend(int ctl_fd);
77 1.1 jtk void stand_by(int ctl_fd);
78 1.1 jtk void resume(int ctl_fd);
79 1.1 jtk void sigexit(int signo);
80 1.1 jtk void make_noise(int howmany);
81 1.1 jtk void do_etc_file(const char *file);
82 1.9 kenh void do_ac_state(int state);
83 1.1 jtk
84 1.1 jtk void
85 1.1 jtk sigexit(int signo)
86 1.1 jtk {
87 1.1 jtk exit(1);
88 1.1 jtk }
89 1.1 jtk
90 1.1 jtk void
91 1.1 jtk usage(void)
92 1.1 jtk {
93 1.2 jtk fprintf(stderr,"usage: %s [-d] [-t timo] [-s] [-a] [-f devfile] [-S sockfile]\n\t[-m sockmode] [-o sockowner]\n", __progname);
94 1.1 jtk exit(1);
95 1.1 jtk }
96 1.1 jtk
97 1.1 jtk
98 1.1 jtk int
99 1.1 jtk power_status(int fd, int force, struct apm_power_info *pinfo)
100 1.1 jtk {
101 1.1 jtk struct apm_power_info bstate;
102 1.1 jtk static struct apm_power_info last;
103 1.1 jtk int acon = 0;
104 1.1 jtk
105 1.1 jtk if (ioctl(fd, APM_IOC_GETPOWER, &bstate) == 0) {
106 1.1 jtk /* various conditions under which we report status: something changed
107 1.1 jtk enough since last report, or asked to force a print */
108 1.1 jtk if (bstate.ac_state == APM_AC_ON)
109 1.1 jtk acon = 1;
110 1.1 jtk if (force ||
111 1.1 jtk bstate.ac_state != last.ac_state ||
112 1.1 jtk bstate.battery_state != last.battery_state ||
113 1.1 jtk (bstate.minutes_left && bstate.minutes_left < 15) ||
114 1.1 jtk abs(bstate.battery_life - last.battery_life) > 20) {
115 1.1 jtk if (bstate.minutes_left)
116 1.1 jtk syslog(LOG_NOTICE,
117 1.1 jtk "battery status: %s. external power status: %s. "
118 1.1 jtk "estimated battery life %d%% (%d minutes)",
119 1.1 jtk battstate(bstate.battery_state),
120 1.1 jtk ac_state(bstate.ac_state), bstate.battery_life,
121 1.1 jtk bstate.minutes_left);
122 1.1 jtk else
123 1.1 jtk syslog(LOG_NOTICE,
124 1.1 jtk "battery status: %s. external power status: %s. "
125 1.1 jtk "estimated battery life %d%%",
126 1.1 jtk battstate(bstate.battery_state),
127 1.1 jtk ac_state(bstate.ac_state), bstate.battery_life);
128 1.1 jtk last = bstate;
129 1.1 jtk }
130 1.1 jtk if (pinfo)
131 1.1 jtk *pinfo = bstate;
132 1.1 jtk } else
133 1.1 jtk syslog(LOG_ERR, "cannot fetch power status: %m");
134 1.1 jtk return acon;
135 1.1 jtk }
136 1.1 jtk
137 1.1 jtk static char *socketname;
138 1.1 jtk
139 1.1 jtk static void sockunlink(void);
140 1.1 jtk
141 1.1 jtk static void
142 1.1 jtk sockunlink(void)
143 1.1 jtk {
144 1.1 jtk if (socketname)
145 1.1 jtk (void) remove(socketname);
146 1.1 jtk }
147 1.1 jtk
148 1.1 jtk int
149 1.2 jtk bind_socket(const char *sockname, mode_t mode, uid_t uid, gid_t gid)
150 1.1 jtk {
151 1.1 jtk int sock;
152 1.1 jtk struct sockaddr_un s_un;
153 1.1 jtk
154 1.8 lukem sock = socket(AF_LOCAL, SOCK_STREAM, 0);
155 1.1 jtk if (sock == -1)
156 1.1 jtk err(1, "cannot create local socket");
157 1.1 jtk
158 1.8 lukem s_un.sun_family = AF_LOCAL;
159 1.1 jtk strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
160 1.1 jtk s_un.sun_len = SUN_LEN(&s_un);
161 1.1 jtk /* remove it if present, we're moving in */
162 1.1 jtk (void) remove(sockname);
163 1.1 jtk if (bind(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1)
164 1.2 jtk err(1, "cannot create APM socket");
165 1.2 jtk if (chmod(sockname, mode) == -1 || chown(sockname, uid, gid) == -1)
166 1.2 jtk err(1, "cannot set socket mode/owner/group to %o/%d/%d",
167 1.2 jtk mode, uid, gid);
168 1.1 jtk listen(sock, 1);
169 1.1 jtk socketname = strdup(sockname);
170 1.1 jtk atexit(sockunlink);
171 1.1 jtk return sock;
172 1.1 jtk }
173 1.1 jtk
174 1.1 jtk enum apm_state
175 1.1 jtk handle_client(int sock_fd, int ctl_fd)
176 1.1 jtk {
177 1.1 jtk /* accept a handle from the client, process it, then clean up */
178 1.1 jtk int cli_fd;
179 1.1 jtk struct sockaddr_un from;
180 1.3 jtk int fromlen = sizeof(from);
181 1.1 jtk struct apm_command cmd;
182 1.1 jtk struct apm_reply reply;
183 1.1 jtk
184 1.1 jtk cli_fd = accept(sock_fd, (struct sockaddr *)&from, &fromlen);
185 1.1 jtk if (cli_fd == -1) {
186 1.1 jtk syslog(LOG_INFO, "client accept failure: %m");
187 1.1 jtk return NORMAL;
188 1.1 jtk }
189 1.1 jtk if (recv(cli_fd, &cmd, sizeof(cmd), 0) != sizeof(cmd)) {
190 1.1 jtk (void) close(cli_fd);
191 1.1 jtk syslog(LOG_INFO, "client size botch");
192 1.1 jtk return NORMAL;
193 1.1 jtk }
194 1.1 jtk if (cmd.vno != APMD_VNO) {
195 1.1 jtk close(cli_fd); /* terminate client */
196 1.1 jtk /* no error message, just drop it. */
197 1.1 jtk return NORMAL;
198 1.1 jtk }
199 1.1 jtk power_status(ctl_fd, 0, &reply.batterystate);
200 1.1 jtk switch (cmd.action) {
201 1.1 jtk default:
202 1.1 jtk reply.newstate = NORMAL;
203 1.1 jtk break;
204 1.1 jtk case SUSPEND:
205 1.1 jtk reply.newstate = SUSPENDING;
206 1.1 jtk break;
207 1.1 jtk case STANDBY:
208 1.1 jtk reply.newstate = STANDING_BY;
209 1.1 jtk break;
210 1.1 jtk }
211 1.1 jtk reply.vno = APMD_VNO;
212 1.1 jtk if (send(cli_fd, &reply, sizeof(reply), 0) != sizeof(reply)) {
213 1.1 jtk syslog(LOG_INFO, "client reply botch");
214 1.1 jtk }
215 1.1 jtk close(cli_fd);
216 1.1 jtk return reply.newstate;
217 1.1 jtk }
218 1.1 jtk
219 1.1 jtk static int speaker_ok = TRUE;
220 1.1 jtk
221 1.1 jtk void
222 1.1 jtk make_noise(howmany)
223 1.1 jtk int howmany;
224 1.1 jtk {
225 1.1 jtk int spkrfd;
226 1.1 jtk int trycnt;
227 1.1 jtk
228 1.1 jtk if (!speaker_ok) /* don't bother after sticky errors */
229 1.1 jtk return;
230 1.1 jtk
231 1.1 jtk for (trycnt = 0; trycnt < 3; trycnt++) {
232 1.1 jtk spkrfd = open(_PATH_DEV_SPEAKER, O_WRONLY);
233 1.1 jtk if (spkrfd == -1) {
234 1.1 jtk switch (errno) {
235 1.1 jtk case EBUSY:
236 1.1 jtk usleep(500000);
237 1.1 jtk errno = EBUSY;
238 1.1 jtk continue;
239 1.1 jtk case ENOENT:
240 1.1 jtk case ENODEV:
241 1.1 jtk case ENXIO:
242 1.1 jtk case EPERM:
243 1.1 jtk case EACCES:
244 1.1 jtk syslog(LOG_INFO,
245 1.1 jtk "speaker device " _PATH_DEV_SPEAKER " unavailable: %m");
246 1.1 jtk speaker_ok = FALSE;
247 1.1 jtk return;
248 1.1 jtk }
249 1.1 jtk } else
250 1.1 jtk break;
251 1.1 jtk }
252 1.1 jtk if (spkrfd == -1) {
253 1.1 jtk syslog(LOG_WARNING, "cannot open " _PATH_DEV_SPEAKER ": %m");
254 1.1 jtk return;
255 1.1 jtk }
256 1.1 jtk syslog(LOG_DEBUG, "sending %d tones to speaker\n", howmany);
257 1.1 jtk write (spkrfd, "o4cc", 2 + howmany);
258 1.1 jtk close(spkrfd);
259 1.1 jtk return;
260 1.1 jtk }
261 1.1 jtk
262 1.1 jtk
263 1.1 jtk void
264 1.1 jtk suspend(int ctl_fd)
265 1.1 jtk {
266 1.1 jtk do_etc_file(_PATH_APM_ETC_SUSPEND);
267 1.1 jtk sync();
268 1.1 jtk make_noise(2);
269 1.1 jtk sync();
270 1.1 jtk sync();
271 1.1 jtk sleep(1);
272 1.1 jtk ioctl(ctl_fd, APM_IOC_SUSPEND, 0);
273 1.1 jtk }
274 1.1 jtk
275 1.1 jtk void
276 1.1 jtk stand_by(int ctl_fd)
277 1.1 jtk {
278 1.1 jtk do_etc_file(_PATH_APM_ETC_STANDBY);
279 1.1 jtk sync();
280 1.1 jtk make_noise(1);
281 1.1 jtk sync();
282 1.1 jtk sync();
283 1.1 jtk sleep(1);
284 1.1 jtk ioctl(ctl_fd, APM_IOC_STANDBY, 0);
285 1.1 jtk }
286 1.1 jtk
287 1.1 jtk #define TIMO (10*60) /* 10 minutes */
288 1.1 jtk
289 1.1 jtk void
290 1.1 jtk resume(int ctl_fd)
291 1.1 jtk {
292 1.1 jtk do_etc_file(_PATH_APM_ETC_RESUME);
293 1.1 jtk }
294 1.1 jtk
295 1.7 lukem int
296 1.1 jtk main(int argc, char *argv[])
297 1.1 jtk {
298 1.1 jtk const char *fname = apmdev;
299 1.1 jtk int ctl_fd, sock_fd, ch, ready;
300 1.1 jtk int statonly = 0;
301 1.1 jtk fd_set devfds;
302 1.1 jtk fd_set selcopy;
303 1.1 jtk struct apm_event_info apmevent;
304 1.1 jtk int suspends, standbys, resumes;
305 1.1 jtk int noacsleep = 0;
306 1.2 jtk mode_t mode = 0660;
307 1.1 jtk struct timeval tv = {TIMO, 0}, stv;
308 1.1 jtk const char *sockname = sockfile;
309 1.2 jtk char *user, *group;
310 1.2 jtk char *scratch;
311 1.2 jtk uid_t uid = 0;
312 1.2 jtk gid_t gid = 0;
313 1.2 jtk struct passwd *pw;
314 1.2 jtk struct group *gr;
315 1.1 jtk
316 1.2 jtk while ((ch = getopt(argc, argv, "qadsf:t:S:m:o:")) != -1)
317 1.1 jtk switch(ch) {
318 1.1 jtk case 'q':
319 1.1 jtk speaker_ok = FALSE;
320 1.1 jtk break;
321 1.1 jtk case 'a':
322 1.1 jtk noacsleep = 1;
323 1.1 jtk break;
324 1.1 jtk case 'd':
325 1.1 jtk debug = 1;
326 1.1 jtk break;
327 1.1 jtk case 'f':
328 1.1 jtk fname = optarg;
329 1.1 jtk break;
330 1.1 jtk case 'S':
331 1.1 jtk sockname = optarg;
332 1.1 jtk break;
333 1.1 jtk case 't':
334 1.1 jtk tv.tv_sec = strtoul(optarg, 0, 0);
335 1.1 jtk if (tv.tv_sec == 0)
336 1.1 jtk usage();
337 1.1 jtk break;
338 1.2 jtk case 'm':
339 1.2 jtk mode = strtoul(optarg, 0, 8);
340 1.2 jtk if (mode == 0)
341 1.2 jtk usage();
342 1.2 jtk break;
343 1.2 jtk case 'o':
344 1.2 jtk /* (user):(group) */
345 1.2 jtk user = optarg;
346 1.2 jtk group = strchr(user, ':');
347 1.2 jtk if (group)
348 1.2 jtk *group++ = '\0';
349 1.2 jtk if (*user) {
350 1.2 jtk uid = strtoul(user, &scratch, 0);
351 1.2 jtk if (*scratch != '\0') {
352 1.2 jtk pw = getpwnam(user);
353 1.2 jtk if (pw)
354 1.2 jtk uid = pw->pw_uid;
355 1.2 jtk else
356 1.2 jtk errx(1, "user name `%s' unknown", user);
357 1.2 jtk }
358 1.2 jtk }
359 1.2 jtk if (group && *group) {
360 1.2 jtk gid = strtoul(group, &scratch, 0);
361 1.2 jtk if (*scratch != '\0') {
362 1.2 jtk gr = getgrnam(group);
363 1.2 jtk if (gr)
364 1.2 jtk gid = gr->gr_gid;
365 1.2 jtk else
366 1.2 jtk errx(1, "group name `%s' unknown", group);
367 1.2 jtk }
368 1.2 jtk }
369 1.2 jtk break;
370 1.1 jtk case 's': /* status only */
371 1.1 jtk statonly = 1;
372 1.1 jtk break;
373 1.1 jtk case '?':
374 1.1 jtk
375 1.1 jtk default:
376 1.1 jtk usage();
377 1.1 jtk }
378 1.1 jtk argc -= optind;
379 1.1 jtk argv += optind;
380 1.1 jtk if ((ctl_fd = open(fname, O_RDWR)) == -1) {
381 1.1 jtk (void)err(1, "cannot open device file `%s'", fname);
382 1.1 jtk }
383 1.1 jtk if (debug) {
384 1.1 jtk openlog(__progname, LOG_CONS, LOG_LOCAL1);
385 1.1 jtk } else {
386 1.1 jtk openlog(__progname, LOG_CONS, LOG_DAEMON);
387 1.1 jtk setlogmask(LOG_UPTO(LOG_NOTICE));
388 1.1 jtk daemon(0, 0);
389 1.1 jtk }
390 1.9 kenh if (statonly) {
391 1.9 kenh power_status(ctl_fd, 1, 0);
392 1.1 jtk exit(0);
393 1.9 kenh } else {
394 1.9 kenh struct apm_power_info pinfo;
395 1.9 kenh power_status(ctl_fd, 1, &pinfo);
396 1.9 kenh do_ac_state(pinfo.ac_state);
397 1.9 kenh }
398 1.9 kenh
399 1.1 jtk (void) signal(SIGTERM, sigexit);
400 1.1 jtk (void) signal(SIGHUP, sigexit);
401 1.1 jtk (void) signal(SIGINT, sigexit);
402 1.5 jtk (void) signal(SIGPIPE, SIG_IGN);
403 1.1 jtk
404 1.9 kenh
405 1.2 jtk sock_fd = bind_socket(sockname, mode, uid, gid);
406 1.1 jtk
407 1.1 jtk FD_ZERO(&devfds);
408 1.1 jtk FD_SET(ctl_fd, &devfds);
409 1.1 jtk FD_SET(sock_fd, &devfds);
410 1.9 kenh
411 1.9 kenh
412 1.1 jtk
413 1.1 jtk for (selcopy = devfds, errno = 0, stv = tv;
414 1.1 jtk (ready = select(MAX(ctl_fd,sock_fd)+1, &selcopy, 0, 0, &stv)) >= 0 ||
415 1.1 jtk errno == EINTR;
416 1.1 jtk selcopy = devfds, errno = 0, stv = tv) {
417 1.1 jtk if (errno == EINTR)
418 1.1 jtk continue;
419 1.1 jtk if (ready == 0) {
420 1.1 jtk /* wakeup for timeout: take status */
421 1.1 jtk power_status(ctl_fd, 0, 0);
422 1.1 jtk }
423 1.1 jtk if (FD_ISSET(ctl_fd, &selcopy)) {
424 1.1 jtk suspends = standbys = resumes = 0;
425 1.1 jtk while (ioctl(ctl_fd, APM_IOC_NEXTEVENT, &apmevent) == 0) {
426 1.1 jtk syslog(LOG_DEBUG, "apmevent %04x index %d", apmevent.type,
427 1.1 jtk apmevent.index);
428 1.1 jtk switch (apmevent.type) {
429 1.1 jtk case APM_SUSPEND_REQ:
430 1.1 jtk case APM_USER_SUSPEND_REQ:
431 1.1 jtk case APM_CRIT_SUSPEND_REQ:
432 1.1 jtk case APM_BATTERY_LOW:
433 1.1 jtk suspends++;
434 1.1 jtk break;
435 1.1 jtk case APM_USER_STANDBY_REQ:
436 1.1 jtk case APM_STANDBY_REQ:
437 1.1 jtk standbys++;
438 1.1 jtk break;
439 1.1 jtk #if 0
440 1.1 jtk case APM_CANCEL:
441 1.1 jtk suspends = standbys = 0;
442 1.1 jtk break;
443 1.1 jtk #endif
444 1.1 jtk case APM_NORMAL_RESUME:
445 1.1 jtk case APM_CRIT_RESUME:
446 1.1 jtk case APM_SYS_STANDBY_RESUME:
447 1.1 jtk resumes++;
448 1.1 jtk break;
449 1.1 jtk case APM_POWER_CHANGE:
450 1.9 kenh {
451 1.9 kenh struct apm_power_info pinfo;
452 1.9 kenh power_status(ctl_fd, 1, &pinfo);
453 1.9 kenh do_ac_state(pinfo.ac_state);
454 1.1 jtk break;
455 1.9 kenh }
456 1.1 jtk default:
457 1.1 jtk break;
458 1.1 jtk }
459 1.1 jtk }
460 1.1 jtk if ((standbys || suspends) && noacsleep &&
461 1.1 jtk power_status(ctl_fd, 0, 0)) {
462 1.1 jtk syslog(LOG_DEBUG, "not sleeping cuz AC is connected");
463 1.1 jtk } else if (suspends) {
464 1.1 jtk suspend(ctl_fd);
465 1.1 jtk } else if (standbys) {
466 1.1 jtk stand_by(ctl_fd);
467 1.1 jtk } else if (resumes) {
468 1.1 jtk resume(ctl_fd);
469 1.1 jtk syslog(LOG_NOTICE, "system resumed from APM sleep");
470 1.1 jtk }
471 1.1 jtk ready--;
472 1.1 jtk }
473 1.1 jtk if (ready == 0)
474 1.1 jtk continue;
475 1.1 jtk if (FD_ISSET(sock_fd, &selcopy)) {
476 1.1 jtk switch (handle_client(sock_fd, ctl_fd)) {
477 1.1 jtk case NORMAL:
478 1.1 jtk break;
479 1.1 jtk case SUSPENDING:
480 1.1 jtk suspend(ctl_fd);
481 1.1 jtk break;
482 1.1 jtk case STANDING_BY:
483 1.1 jtk stand_by(ctl_fd);
484 1.1 jtk break;
485 1.1 jtk }
486 1.1 jtk }
487 1.1 jtk }
488 1.1 jtk syslog(LOG_ERR, "select failed: %m");
489 1.1 jtk exit(1);
490 1.1 jtk }
491 1.1 jtk
492 1.1 jtk void
493 1.1 jtk do_etc_file(const char *file)
494 1.1 jtk {
495 1.1 jtk pid_t pid;
496 1.1 jtk int status;
497 1.1 jtk const char *prog;
498 1.1 jtk
499 1.1 jtk /* If file doesn't exist, do nothing. */
500 1.1 jtk if (access(file, X_OK|R_OK)) {
501 1.1 jtk syslog(LOG_DEBUG, "do_etc_file(): cannot access file %s", file);
502 1.1 jtk return;
503 1.1 jtk }
504 1.1 jtk
505 1.1 jtk prog = strrchr(file, '/');
506 1.1 jtk if (prog)
507 1.1 jtk prog++;
508 1.1 jtk else
509 1.1 jtk prog = file;
510 1.1 jtk
511 1.1 jtk pid = fork();
512 1.1 jtk switch (pid) {
513 1.1 jtk case -1:
514 1.1 jtk syslog(LOG_ERR, "failed to fork(): %m");
515 1.1 jtk return;
516 1.1 jtk case 0:
517 1.1 jtk /* We are the child. */
518 1.1 jtk execl(file, prog, NULL);
519 1.1 jtk _exit(-1);
520 1.1 jtk /* NOTREACHED */
521 1.1 jtk default:
522 1.1 jtk /* We are the parent. */
523 1.1 jtk wait4(pid, &status, 0, 0);
524 1.1 jtk if (WIFEXITED(status))
525 1.1 jtk syslog(LOG_DEBUG, "%s exited with status %d", file,
526 1.1 jtk WEXITSTATUS(status));
527 1.1 jtk else {
528 1.1 jtk syslog(LOG_ERR, "%s exited abnormally.", file);
529 1.1 jtk }
530 1.1 jtk break;
531 1.1 jtk }
532 1.9 kenh }
533 1.9 kenh
534 1.9 kenh void do_ac_state(int state)
535 1.9 kenh {
536 1.9 kenh switch (state) {
537 1.9 kenh case APM_AC_OFF:
538 1.9 kenh do_etc_file(_PATH_APM_ETC_BATTERY);
539 1.9 kenh break;
540 1.9 kenh case APM_AC_ON:
541 1.9 kenh case APM_AC_BACKUP:
542 1.9 kenh do_etc_file(_PATH_APM_ETC_LINE);
543 1.9 kenh break;
544 1.9 kenh default:
545 1.9 kenh /* Silently ignore */
546 1.9 kenh }
547 1.1 jtk }
548