Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / powerd / powerd.c
blob12eb9e78fde4a5b2cd61b177adac60b4ab6d7563
1 /* $NetBSD: powerd.c,v 1.12 2007/10/06 22:12:04 xtraeme Exp $ */
3 /*
4 * Copyright (c) 2003 Wasabi Systems, Inc.
5 * All rights reserved.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Power management daemon for sysmon.
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/event.h>
45 #include <sys/power.h>
46 #include <sys/wait.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <sysexits.h>
52 #include <syslog.h>
53 #include <unistd.h>
54 #include <util.h>
55 #include <prop/proplib.h>
57 int debug;
59 static int kq;
61 #define _PATH_DEV_POWER "/dev/power"
62 #define _PATH_POWERD_SCRIPTS "/etc/powerd/scripts"
64 static void usage(void) __dead;
65 static void run_script(const char *[]);
66 static struct kevent *allocchange(void);
67 static int wait_for_events(struct kevent *, size_t);
68 static void dispatch_dev_power(struct kevent *);
69 static void dispatch_power_event_state_change(int, power_event_t *);
71 static const char *script_paths[] = {
72 NULL,
73 _PATH_POWERD_SCRIPTS
76 int
77 main(int argc, char *argv[])
79 struct kevent *ev, events[16];
80 struct power_type power_type;
81 char *cp;
82 int ch, fd;
84 setprogname(*argv);
86 while ((ch = getopt(argc, argv, "d")) != -1) {
87 switch (ch) {
88 case 'd':
89 debug = 1;
90 break;
92 default:
93 usage();
96 argc -= optind;
97 argv += optind;
99 if (argc)
100 usage();
102 if (debug == 0)
103 (void)daemon(0, 0);
105 openlog("powerd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
106 (void)pidfile(NULL);
108 if ((kq = kqueue()) == -1) {
109 syslog(LOG_ERR, "kqueue: %m");
110 exit(EX_OSERR);
113 if ((fd = open(_PATH_DEV_POWER, O_RDONLY|O_NONBLOCK, 0600)) == -1) {
114 syslog(LOG_ERR, "open %s: %m", _PATH_DEV_POWER);
115 exit(EX_OSERR);
118 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
119 syslog(LOG_ERR, "Cannot set close on exec in power fd: %m");
120 exit(EX_OSERR);
123 if (ioctl(fd, POWER_IOC_GET_TYPE, &power_type) == -1) {
124 syslog(LOG_ERR, "POWER_IOC_GET_TYPE: %m");
125 exit(EX_OSERR);
128 (void)asprintf(&cp, "%s/%s", _PATH_POWERD_SCRIPTS,
129 power_type.power_type);
130 if (cp == NULL) {
131 syslog(LOG_ERR, "allocating script path: %m");
132 exit(EX_OSERR);
134 script_paths[0] = cp;
136 ev = allocchange();
137 EV_SET(ev, fd, EVFILT_READ, EV_ADD | EV_ENABLE,
138 0, 0, (intptr_t) dispatch_dev_power);
140 for (;;) {
141 void (*handler)(struct kevent *);
142 int i, rv;
144 rv = wait_for_events(events, __arraycount(events));
145 for (i = 0; i < rv; i++) {
146 handler = (void *) events[i].udata;
147 (*handler)(&events[i]);
152 static void
153 usage(void)
156 (void)fprintf(stderr, "usage: %s [-d]\n", getprogname());
157 exit(EX_USAGE);
160 static void
161 run_script(const char *argv[])
163 char path[MAXPATHLEN+1];
164 size_t i, j;
166 for (i = 0; i < __arraycount(script_paths); i++) {
167 (void)snprintf(path, sizeof(path), "%s/%s", script_paths[i],
168 argv[0]);
169 if (access(path, R_OK|X_OK) == 0) {
170 int status;
171 pid_t pid;
173 argv[0] = path;
175 if (debug) {
176 (void)fprintf(stderr, "running script: %s",
177 argv[0]);
178 for (j = 1; argv[j] != NULL; j++)
179 (void)fprintf(stderr, " %s", argv[j]);
180 (void)fprintf(stderr, "\n");
183 switch ((pid = vfork())) {
184 case -1:
185 syslog(LOG_ERR, "fork to run script: %m");
186 return;
188 case 0:
189 /* Child. */
190 (void) execv(path, __UNCONST(argv));
191 _exit(1);
192 /* NOTREACHED */
194 default:
195 /* Parent. */
196 if (waitpid(pid, &status, 0) == -1) {
197 syslog(LOG_ERR, "waitpid for %s: %m",
198 path);
199 break;
201 if (WIFEXITED(status) &&
202 WEXITSTATUS(status) != 0) {
203 syslog(LOG_ERR,
204 "%s exited with status %d",
205 path, WEXITSTATUS(status));
206 } else if (!WIFEXITED(status)) {
207 syslog(LOG_ERR,
208 "%s terminated abnormally", path);
210 break;
213 return;
217 syslog(LOG_ERR, "no script for %s", argv[0]);
218 if (debug)
219 (void)fprintf(stderr, "no script for %s\n", argv[0]);
222 static struct kevent changebuf[8];
223 static size_t nchanges;
225 static struct kevent *
226 allocchange(void)
229 if (nchanges == __arraycount(changebuf)) {
230 (void)wait_for_events(NULL, 0);
231 nchanges = 0;
234 return &changebuf[nchanges++];
237 static int
238 wait_for_events(struct kevent *events, size_t nevents)
240 int rv;
242 while ((rv = kevent(kq, nchanges ? changebuf : NULL, nchanges,
243 events, nevents, NULL)) < 0) {
244 nchanges = 0;
245 if (errno != EINTR) {
246 syslog(LOG_ERR, "kevent: %m");
247 exit(EX_OSERR);
251 return rv;
254 static void
255 dispatch_dev_power(struct kevent *ev)
257 power_event_t pev;
258 int fd = ev->ident;
260 if (debug)
261 (void)fprintf(stderr, "%s: %" PRId64
262 " event%s available\n", __func__,
263 ev->data, ev->data > 1 ? "s" : "");
265 again:
266 if (read(fd, &pev, sizeof(pev)) != sizeof(pev)) {
267 if (errno == EWOULDBLOCK)
268 return;
269 syslog(LOG_ERR, "read of %s: %m", _PATH_DEV_POWER);
270 exit(EX_OSERR);
273 if (debug)
274 (void)fprintf(stderr, "%s: event type %d\n",
275 __func__, pev.pev_type);
277 switch (pev.pev_type) {
278 case POWER_EVENT_ENVSYS_STATE_CHANGE:
279 case POWER_EVENT_SWITCH_STATE_CHANGE:
280 dispatch_power_event_state_change(fd, &pev);
281 break;
282 default:
283 syslog(LOG_INFO, "unknown %s event type: %d",
284 _PATH_DEV_POWER, pev.pev_type);
287 goto again;
290 static void
291 dispatch_power_event_state_change(int fd, power_event_t *pev)
293 prop_dictionary_t dict;
294 prop_object_t obj;
295 const char *argv[6];
296 char *buf = NULL;
297 int error;
299 error = prop_dictionary_recv_ioctl(fd, POWER_EVENT_RECVDICT, &dict);
300 if (error) {
301 if (debug)
302 printf("%s: prop_dictionary_recv_ioctl error=%d\n",
303 __func__, error);
304 return;
307 if (debug) {
308 buf = prop_dictionary_externalize(dict);
309 printf("%s", buf);
310 free(buf);
313 obj = prop_dictionary_get(dict, "powerd-script-name");
314 argv[0] = prop_string_cstring_nocopy(obj);
316 obj = prop_dictionary_get(dict, "driver-name");
317 argv[1] = prop_string_cstring_nocopy(obj);
319 obj = prop_dictionary_get(dict, "powerd-event-name");
320 argv[2] = prop_string_cstring_nocopy(obj);
322 obj = prop_dictionary_get(dict, "sensor-name");
323 argv[3] = prop_string_cstring_nocopy(obj);
325 obj = prop_dictionary_get(dict, "state-description");
326 argv[4] = prop_string_cstring_nocopy(obj);
328 argv[5] = NULL;
330 run_script(argv);