powerd.c revision 1.7 1 1.7 xtraeme /* $NetBSD: powerd.c,v 1.7 2007/05/07 02:33:35 xtraeme Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2003 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.1 thorpej /*
39 1.1 thorpej * Power management daemon for sysmon.
40 1.1 thorpej */
41 1.1 thorpej
42 1.7 xtraeme #include <sys/cdefs.h>
43 1.1 thorpej #include <sys/param.h>
44 1.1 thorpej #include <sys/event.h>
45 1.1 thorpej #include <sys/power.h>
46 1.1 thorpej #include <sys/wait.h>
47 1.1 thorpej #include <errno.h>
48 1.1 thorpej #include <fcntl.h>
49 1.1 thorpej #include <stdio.h>
50 1.1 thorpej #include <stdlib.h>
51 1.1 thorpej #include <sysexits.h>
52 1.1 thorpej #include <syslog.h>
53 1.1 thorpej #include <unistd.h>
54 1.1 thorpej #include <util.h>
55 1.1 thorpej
56 1.1 thorpej int debug;
57 1.1 thorpej
58 1.1 thorpej static int kq;
59 1.1 thorpej
60 1.1 thorpej #define _PATH_DEV_POWER "/dev/power"
61 1.1 thorpej #define _PATH_POWERD_SCRIPTS "/etc/powerd/scripts"
62 1.1 thorpej
63 1.5 christos static void usage(void) __attribute__((__noreturn__));
64 1.1 thorpej static void run_script(const char *[]);
65 1.1 thorpej static struct kevent *allocchange(void);
66 1.1 thorpej static int wait_for_events(struct kevent *, size_t);
67 1.1 thorpej static void dispatch_dev_power(struct kevent *);
68 1.1 thorpej static void dispatch_power_event_switch_state_change(power_event_t *);
69 1.1 thorpej
70 1.1 thorpej static const char *script_paths[] = {
71 1.1 thorpej NULL,
72 1.1 thorpej _PATH_POWERD_SCRIPTS
73 1.1 thorpej };
74 1.1 thorpej
75 1.1 thorpej int
76 1.1 thorpej main(int argc, char *argv[])
77 1.1 thorpej {
78 1.1 thorpej struct kevent *ev, events[16];
79 1.1 thorpej struct power_type power_type;
80 1.1 thorpej char *cp;
81 1.1 thorpej int ch, fd;
82 1.1 thorpej
83 1.5 christos setprogname(*argv);
84 1.5 christos
85 1.1 thorpej while ((ch = getopt(argc, argv, "d")) != -1) {
86 1.1 thorpej switch (ch) {
87 1.1 thorpej case 'd':
88 1.1 thorpej debug = 1;
89 1.1 thorpej break;
90 1.1 thorpej
91 1.1 thorpej default:
92 1.1 thorpej usage();
93 1.1 thorpej }
94 1.1 thorpej }
95 1.1 thorpej argc -= optind;
96 1.1 thorpej argv += optind;
97 1.1 thorpej
98 1.1 thorpej if (argc)
99 1.1 thorpej usage();
100 1.1 thorpej
101 1.1 thorpej if (debug == 0)
102 1.5 christos (void)daemon(0, 0);
103 1.1 thorpej
104 1.1 thorpej openlog("powerd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
105 1.5 christos (void)pidfile(NULL);
106 1.1 thorpej
107 1.5 christos if ((kq = kqueue()) == -1) {
108 1.1 thorpej syslog(LOG_ERR, "kqueue: %m");
109 1.1 thorpej exit(EX_OSERR);
110 1.1 thorpej }
111 1.1 thorpej
112 1.5 christos if ((fd = open(_PATH_DEV_POWER, O_RDONLY|O_NONBLOCK, 0600)) == -1) {
113 1.1 thorpej syslog(LOG_ERR, "open %s: %m", _PATH_DEV_POWER);
114 1.1 thorpej exit(EX_OSERR);
115 1.1 thorpej }
116 1.1 thorpej
117 1.5 christos if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
118 1.5 christos syslog(LOG_ERR, "Cannot set close on exec in power fd: %m");
119 1.5 christos exit(EX_OSERR);
120 1.5 christos }
121 1.5 christos
122 1.5 christos if (ioctl(fd, POWER_IOC_GET_TYPE, &power_type) == -1) {
123 1.1 thorpej syslog(LOG_ERR, "POWER_IOC_GET_TYPE: %m");
124 1.1 thorpej exit(EX_OSERR);
125 1.1 thorpej }
126 1.1 thorpej
127 1.5 christos (void)asprintf(&cp, "%s/%s", _PATH_POWERD_SCRIPTS,
128 1.5 christos power_type.power_type);
129 1.1 thorpej if (cp == NULL) {
130 1.1 thorpej syslog(LOG_ERR, "allocating script path: %m");
131 1.1 thorpej exit(EX_OSERR);
132 1.1 thorpej }
133 1.1 thorpej script_paths[0] = cp;
134 1.1 thorpej
135 1.1 thorpej ev = allocchange();
136 1.1 thorpej EV_SET(ev, fd, EVFILT_READ, EV_ADD | EV_ENABLE,
137 1.1 thorpej 0, 0, (intptr_t) dispatch_dev_power);
138 1.1 thorpej
139 1.1 thorpej for (;;) {
140 1.1 thorpej void (*handler)(struct kevent *);
141 1.1 thorpej int i, rv;
142 1.1 thorpej
143 1.7 xtraeme rv = wait_for_events(events, __arraycount(events));
144 1.1 thorpej for (i = 0; i < rv; i++) {
145 1.1 thorpej handler = (void *) events[i].udata;
146 1.1 thorpej (*handler)(&events[i]);
147 1.1 thorpej }
148 1.1 thorpej }
149 1.1 thorpej }
150 1.1 thorpej
151 1.1 thorpej static void
152 1.1 thorpej usage(void)
153 1.1 thorpej {
154 1.1 thorpej
155 1.5 christos (void)fprintf(stderr, "usage: %s [-d]\n", getprogname());
156 1.1 thorpej exit(EX_USAGE);
157 1.1 thorpej }
158 1.1 thorpej
159 1.1 thorpej static void
160 1.1 thorpej run_script(const char *argv[])
161 1.1 thorpej {
162 1.1 thorpej char path[MAXPATHLEN+1];
163 1.5 christos size_t i, j;
164 1.1 thorpej
165 1.7 xtraeme for (i = 0; i < __arraycount(script_paths); i++) {
166 1.5 christos (void)snprintf(path, sizeof(path), "%s/%s", script_paths[i],
167 1.5 christos argv[0]);
168 1.1 thorpej if (access(path, R_OK|X_OK) == 0) {
169 1.1 thorpej int status;
170 1.1 thorpej pid_t pid;
171 1.1 thorpej
172 1.1 thorpej argv[0] = path;
173 1.1 thorpej
174 1.1 thorpej if (debug) {
175 1.5 christos (void)fprintf(stderr, "running script: %s",
176 1.5 christos argv[0]);
177 1.5 christos for (j = 1; argv[j] != NULL; j++)
178 1.5 christos (void)fprintf(stderr, " %s", argv[j]);
179 1.5 christos (void)fprintf(stderr, "\n");
180 1.1 thorpej }
181 1.1 thorpej
182 1.1 thorpej switch ((pid = vfork())) {
183 1.1 thorpej case -1:
184 1.1 thorpej syslog(LOG_ERR, "fork to run script: %m");
185 1.1 thorpej return;
186 1.1 thorpej
187 1.1 thorpej case 0:
188 1.1 thorpej /* Child. */
189 1.6 christos (void) execv(path, __UNCONST(argv));
190 1.1 thorpej _exit(1);
191 1.1 thorpej /* NOTREACHED */
192 1.1 thorpej
193 1.1 thorpej default:
194 1.1 thorpej /* Parent. */
195 1.5 christos if (waitpid(pid, &status, 0) == -1) {
196 1.5 christos syslog(LOG_ERR, "waitpid for %s: %m",
197 1.5 christos path);
198 1.5 christos break;
199 1.5 christos }
200 1.1 thorpej if (WIFEXITED(status) &&
201 1.1 thorpej WEXITSTATUS(status) != 0) {
202 1.1 thorpej syslog(LOG_ERR,
203 1.1 thorpej "%s exited with status %d",
204 1.1 thorpej path, WEXITSTATUS(status));
205 1.1 thorpej } else if (!WIFEXITED(status)) {
206 1.1 thorpej syslog(LOG_ERR,
207 1.1 thorpej "%s terminated abnormally", path);
208 1.1 thorpej }
209 1.1 thorpej break;
210 1.1 thorpej }
211 1.1 thorpej
212 1.1 thorpej return;
213 1.1 thorpej }
214 1.1 thorpej }
215 1.1 thorpej
216 1.1 thorpej syslog(LOG_ERR, "no script for %s", argv[0]);
217 1.1 thorpej if (debug)
218 1.5 christos (void)fprintf(stderr, "no script for %s\n", argv[0]);
219 1.1 thorpej }
220 1.1 thorpej
221 1.1 thorpej static struct kevent changebuf[8];
222 1.5 christos static size_t nchanges;
223 1.1 thorpej
224 1.1 thorpej static struct kevent *
225 1.1 thorpej allocchange(void)
226 1.1 thorpej {
227 1.1 thorpej
228 1.7 xtraeme if (nchanges == __arraycount(changebuf)) {
229 1.5 christos (void)wait_for_events(NULL, 0);
230 1.1 thorpej nchanges = 0;
231 1.1 thorpej }
232 1.1 thorpej
233 1.5 christos return &changebuf[nchanges++];
234 1.1 thorpej }
235 1.1 thorpej
236 1.1 thorpej static int
237 1.1 thorpej wait_for_events(struct kevent *events, size_t nevents)
238 1.1 thorpej {
239 1.1 thorpej int rv;
240 1.1 thorpej
241 1.1 thorpej while ((rv = kevent(kq, nchanges ? changebuf : NULL, nchanges,
242 1.5 christos events, nevents, NULL)) < 0) {
243 1.1 thorpej nchanges = 0;
244 1.1 thorpej if (errno != EINTR) {
245 1.1 thorpej syslog(LOG_ERR, "kevent: %m");
246 1.1 thorpej exit(EX_OSERR);
247 1.1 thorpej }
248 1.1 thorpej }
249 1.1 thorpej
250 1.5 christos return rv;
251 1.1 thorpej }
252 1.1 thorpej
253 1.1 thorpej static const char *
254 1.1 thorpej pswitch_type_name(int type)
255 1.1 thorpej {
256 1.1 thorpej
257 1.1 thorpej switch (type) {
258 1.1 thorpej case PSWITCH_TYPE_POWER:
259 1.5 christos return "power_button";
260 1.1 thorpej
261 1.1 thorpej case PSWITCH_TYPE_SLEEP:
262 1.5 christos return "sleep_button";
263 1.1 thorpej
264 1.1 thorpej case PSWITCH_TYPE_LID:
265 1.5 christos return "lid_switch";
266 1.2 thorpej
267 1.2 thorpej case PSWITCH_TYPE_RESET:
268 1.5 christos return "reset_button";
269 1.1 thorpej
270 1.4 kochi case PSWITCH_TYPE_ACADAPTER:
271 1.5 christos return "acadapter";
272 1.4 kochi
273 1.1 thorpej default:
274 1.5 christos return "=unknown pswitch type=";
275 1.1 thorpej }
276 1.1 thorpej }
277 1.1 thorpej
278 1.1 thorpej static const char *
279 1.1 thorpej pswitch_event_name(int type)
280 1.1 thorpej {
281 1.1 thorpej
282 1.1 thorpej switch (type) {
283 1.1 thorpej case PSWITCH_EVENT_PRESSED:
284 1.5 christos return "pressed";
285 1.1 thorpej
286 1.1 thorpej case PSWITCH_EVENT_RELEASED:
287 1.5 christos return "released";
288 1.1 thorpej
289 1.1 thorpej default:
290 1.5 christos return "=unknown pswitch event=";
291 1.1 thorpej }
292 1.1 thorpej }
293 1.1 thorpej
294 1.1 thorpej static void
295 1.1 thorpej dispatch_dev_power(struct kevent *ev)
296 1.1 thorpej {
297 1.1 thorpej power_event_t pev;
298 1.1 thorpej int fd = ev->ident;
299 1.1 thorpej
300 1.1 thorpej if (debug)
301 1.5 christos (void)fprintf(stderr, "dispatch_dev_power: %" PRId64
302 1.1 thorpej " events available\n", ev->data);
303 1.1 thorpej
304 1.1 thorpej again:
305 1.1 thorpej if (read(fd, &pev, sizeof(pev)) != sizeof(pev)) {
306 1.1 thorpej if (errno == EWOULDBLOCK)
307 1.1 thorpej return;
308 1.1 thorpej syslog(LOG_ERR, "read of %s: %m", _PATH_DEV_POWER);
309 1.1 thorpej exit(EX_OSERR);
310 1.1 thorpej }
311 1.1 thorpej
312 1.1 thorpej if (debug) {
313 1.5 christos (void)fprintf(stderr, "dispatch_dev_power: event type %d\n",
314 1.1 thorpej pev.pev_type);
315 1.1 thorpej }
316 1.1 thorpej
317 1.1 thorpej switch (pev.pev_type) {
318 1.1 thorpej case POWER_EVENT_SWITCH_STATE_CHANGE:
319 1.1 thorpej dispatch_power_event_switch_state_change(&pev);
320 1.1 thorpej break;
321 1.1 thorpej
322 1.1 thorpej default:
323 1.1 thorpej syslog(LOG_INFO, "unknown %s event type: %d",
324 1.1 thorpej _PATH_DEV_POWER, pev.pev_type);
325 1.1 thorpej }
326 1.1 thorpej
327 1.1 thorpej goto again;
328 1.1 thorpej }
329 1.1 thorpej
330 1.1 thorpej static void
331 1.1 thorpej dispatch_power_event_switch_state_change(power_event_t *pev)
332 1.1 thorpej {
333 1.1 thorpej const char *argv[4];
334 1.1 thorpej
335 1.1 thorpej argv[0] = pswitch_type_name(pev->pev_switch.psws_type);
336 1.1 thorpej argv[1] = pev->pev_switch.psws_name;
337 1.1 thorpej argv[2] = pswitch_event_name(pev->pev_switch.psws_state);
338 1.1 thorpej argv[3] = NULL;
339 1.1 thorpej
340 1.1 thorpej if (debug)
341 1.5 christos (void)fprintf(stderr, "%s on %s %s\n", argv[0], argv[1],
342 1.5 christos argv[2]);
343 1.1 thorpej
344 1.1 thorpej run_script(argv);
345 1.1 thorpej }
346