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