powerd.c revision 1.21 1 1.21 mrg /* $NetBSD: powerd.c,v 1.21 2023/08/03 08:03:19 mrg 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.15 pgoyette #define SYSLOG_NAMES
43 1.15 pgoyette
44 1.7 xtraeme #include <sys/cdefs.h>
45 1.16 pgoyette #include <sys/ioctl.h>
46 1.1 thorpej #include <sys/param.h>
47 1.1 thorpej #include <sys/event.h>
48 1.1 thorpej #include <sys/power.h>
49 1.1 thorpej #include <sys/wait.h>
50 1.16 pgoyette #include <err.h>
51 1.1 thorpej #include <errno.h>
52 1.1 thorpej #include <fcntl.h>
53 1.14 jruoho #include <paths.h>
54 1.1 thorpej #include <stdio.h>
55 1.1 thorpej #include <stdlib.h>
56 1.1 thorpej #include <sysexits.h>
57 1.1 thorpej #include <syslog.h>
58 1.1 thorpej #include <unistd.h>
59 1.1 thorpej #include <util.h>
60 1.8 xtraeme #include <prop/proplib.h>
61 1.15 pgoyette #include <stdarg.h>
62 1.15 pgoyette #include <string.h>
63 1.1 thorpej
64 1.16 pgoyette #include "prog_ops.h"
65 1.16 pgoyette
66 1.15 pgoyette int debug, no_scripts;
67 1.1 thorpej
68 1.1 thorpej static int kq;
69 1.1 thorpej
70 1.1 thorpej #define _PATH_POWERD_SCRIPTS "/etc/powerd/scripts"
71 1.1 thorpej
72 1.13 perry static void usage(void) __dead;
73 1.1 thorpej static void run_script(const char *[]);
74 1.1 thorpej static struct kevent *allocchange(void);
75 1.1 thorpej static int wait_for_events(struct kevent *, size_t);
76 1.1 thorpej static void dispatch_dev_power(struct kevent *);
77 1.8 xtraeme static void dispatch_power_event_state_change(int, power_event_t *);
78 1.17 joerg static void powerd_log(int, const char *, ...) __printflike(2, 3);
79 1.1 thorpej
80 1.1 thorpej static const char *script_paths[] = {
81 1.1 thorpej NULL,
82 1.1 thorpej _PATH_POWERD_SCRIPTS
83 1.1 thorpej };
84 1.1 thorpej
85 1.1 thorpej int
86 1.1 thorpej main(int argc, char *argv[])
87 1.1 thorpej {
88 1.1 thorpej struct kevent *ev, events[16];
89 1.1 thorpej struct power_type power_type;
90 1.1 thorpej char *cp;
91 1.1 thorpej int ch, fd;
92 1.1 thorpej
93 1.5 christos setprogname(*argv);
94 1.5 christos
95 1.16 pgoyette if (prog_init && prog_init() == -1)
96 1.16 pgoyette err(1, "init failed");
97 1.16 pgoyette
98 1.15 pgoyette while ((ch = getopt(argc, argv, "dn")) != -1) {
99 1.1 thorpej switch (ch) {
100 1.1 thorpej case 'd':
101 1.1 thorpej debug = 1;
102 1.1 thorpej break;
103 1.1 thorpej
104 1.15 pgoyette case 'n':
105 1.15 pgoyette no_scripts = 1;
106 1.15 pgoyette break;
107 1.15 pgoyette
108 1.1 thorpej default:
109 1.1 thorpej usage();
110 1.1 thorpej }
111 1.1 thorpej }
112 1.1 thorpej argc -= optind;
113 1.1 thorpej argv += optind;
114 1.1 thorpej
115 1.1 thorpej if (argc)
116 1.1 thorpej usage();
117 1.1 thorpej
118 1.15 pgoyette if (debug == 0) {
119 1.5 christos (void)daemon(0, 0);
120 1.1 thorpej
121 1.15 pgoyette openlog("powerd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
122 1.15 pgoyette (void)pidfile(NULL);
123 1.15 pgoyette }
124 1.1 thorpej
125 1.16 pgoyette if ((kq = prog_kqueue()) == -1) {
126 1.15 pgoyette powerd_log(LOG_ERR, "kqueue: %s", strerror(errno));
127 1.1 thorpej exit(EX_OSERR);
128 1.1 thorpej }
129 1.1 thorpej
130 1.16 pgoyette if ((fd = prog_open(_PATH_POWER, O_RDONLY|O_NONBLOCK, 0600)) == -1) {
131 1.15 pgoyette powerd_log(LOG_ERR, "open %s: %s", _PATH_POWER,
132 1.15 pgoyette strerror(errno));
133 1.1 thorpej exit(EX_OSERR);
134 1.1 thorpej }
135 1.1 thorpej
136 1.16 pgoyette if (prog_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
137 1.15 pgoyette powerd_log(LOG_ERR, "Cannot set close on exec in power fd: %s",
138 1.15 pgoyette strerror(errno));
139 1.5 christos exit(EX_OSERR);
140 1.5 christos }
141 1.5 christos
142 1.16 pgoyette if (prog_ioctl(fd, POWER_IOC_GET_TYPE, &power_type) == -1) {
143 1.15 pgoyette powerd_log(LOG_ERR, "POWER_IOC_GET_TYPE: %s", strerror(errno));
144 1.1 thorpej exit(EX_OSERR);
145 1.1 thorpej }
146 1.1 thorpej
147 1.5 christos (void)asprintf(&cp, "%s/%s", _PATH_POWERD_SCRIPTS,
148 1.5 christos power_type.power_type);
149 1.1 thorpej if (cp == NULL) {
150 1.15 pgoyette powerd_log(LOG_ERR, "allocating script path: %s",
151 1.15 pgoyette strerror(errno));
152 1.1 thorpej exit(EX_OSERR);
153 1.1 thorpej }
154 1.1 thorpej script_paths[0] = cp;
155 1.1 thorpej
156 1.1 thorpej ev = allocchange();
157 1.1 thorpej EV_SET(ev, fd, EVFILT_READ, EV_ADD | EV_ENABLE,
158 1.1 thorpej 0, 0, (intptr_t) dispatch_dev_power);
159 1.1 thorpej
160 1.1 thorpej for (;;) {
161 1.1 thorpej void (*handler)(struct kevent *);
162 1.1 thorpej int i, rv;
163 1.1 thorpej
164 1.7 xtraeme rv = wait_for_events(events, __arraycount(events));
165 1.1 thorpej for (i = 0; i < rv; i++) {
166 1.1 thorpej handler = (void *) events[i].udata;
167 1.1 thorpej (*handler)(&events[i]);
168 1.1 thorpej }
169 1.1 thorpej }
170 1.1 thorpej }
171 1.1 thorpej
172 1.1 thorpej static void
173 1.1 thorpej usage(void)
174 1.1 thorpej {
175 1.1 thorpej
176 1.15 pgoyette (void)fprintf(stderr, "usage: %s [-dn]\n", getprogname());
177 1.1 thorpej exit(EX_USAGE);
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej static void
181 1.1 thorpej run_script(const char *argv[])
182 1.1 thorpej {
183 1.1 thorpej char path[MAXPATHLEN+1];
184 1.5 christos size_t i, j;
185 1.1 thorpej
186 1.7 xtraeme for (i = 0; i < __arraycount(script_paths); i++) {
187 1.5 christos (void)snprintf(path, sizeof(path), "%s/%s", script_paths[i],
188 1.5 christos argv[0]);
189 1.1 thorpej if (access(path, R_OK|X_OK) == 0) {
190 1.1 thorpej int status;
191 1.1 thorpej pid_t pid;
192 1.21 mrg const char *orig_argv0 = argv[0];
193 1.1 thorpej
194 1.1 thorpej argv[0] = path;
195 1.1 thorpej
196 1.1 thorpej if (debug) {
197 1.15 pgoyette (void)fprintf(stderr, "%srunning script: %s",
198 1.15 pgoyette no_scripts?"not ":"", argv[0]);
199 1.5 christos for (j = 1; argv[j] != NULL; j++)
200 1.5 christos (void)fprintf(stderr, " %s", argv[j]);
201 1.5 christos (void)fprintf(stderr, "\n");
202 1.1 thorpej }
203 1.21 mrg if (no_scripts != 0) {
204 1.21 mrg argv[0] = orig_argv0;
205 1.15 pgoyette return;
206 1.21 mrg }
207 1.1 thorpej
208 1.1 thorpej switch ((pid = vfork())) {
209 1.1 thorpej case -1:
210 1.15 pgoyette powerd_log(LOG_ERR, "fork to run script: %s",
211 1.15 pgoyette strerror(errno));
212 1.21 mrg argv[0] = orig_argv0;
213 1.1 thorpej return;
214 1.1 thorpej
215 1.1 thorpej case 0:
216 1.1 thorpej /* Child. */
217 1.6 christos (void) execv(path, __UNCONST(argv));
218 1.1 thorpej _exit(1);
219 1.1 thorpej /* NOTREACHED */
220 1.1 thorpej
221 1.1 thorpej default:
222 1.1 thorpej /* Parent. */
223 1.5 christos if (waitpid(pid, &status, 0) == -1) {
224 1.15 pgoyette powerd_log(LOG_ERR,
225 1.15 pgoyette "waitpid for %s: %s", path,
226 1.15 pgoyette strerror(errno));
227 1.5 christos break;
228 1.5 christos }
229 1.1 thorpej if (WIFEXITED(status) &&
230 1.1 thorpej WEXITSTATUS(status) != 0) {
231 1.15 pgoyette powerd_log(LOG_ERR,
232 1.1 thorpej "%s exited with status %d",
233 1.1 thorpej path, WEXITSTATUS(status));
234 1.1 thorpej } else if (!WIFEXITED(status)) {
235 1.15 pgoyette powerd_log(LOG_ERR,
236 1.1 thorpej "%s terminated abnormally", path);
237 1.1 thorpej }
238 1.1 thorpej break;
239 1.1 thorpej }
240 1.21 mrg argv[0] = orig_argv0;
241 1.1 thorpej
242 1.1 thorpej return;
243 1.1 thorpej }
244 1.1 thorpej }
245 1.1 thorpej
246 1.15 pgoyette powerd_log(LOG_ERR, "no script for %s", argv[0]);
247 1.1 thorpej }
248 1.1 thorpej
249 1.1 thorpej static struct kevent changebuf[8];
250 1.5 christos static size_t nchanges;
251 1.1 thorpej
252 1.1 thorpej static struct kevent *
253 1.1 thorpej allocchange(void)
254 1.1 thorpej {
255 1.1 thorpej
256 1.7 xtraeme if (nchanges == __arraycount(changebuf)) {
257 1.5 christos (void)wait_for_events(NULL, 0);
258 1.1 thorpej nchanges = 0;
259 1.1 thorpej }
260 1.1 thorpej
261 1.5 christos return &changebuf[nchanges++];
262 1.1 thorpej }
263 1.1 thorpej
264 1.1 thorpej static int
265 1.1 thorpej wait_for_events(struct kevent *events, size_t nevents)
266 1.1 thorpej {
267 1.1 thorpej int rv;
268 1.1 thorpej
269 1.16 pgoyette while ((rv = prog_kevent(kq, nchanges ? changebuf : NULL, nchanges,
270 1.5 christos events, nevents, NULL)) < 0) {
271 1.1 thorpej nchanges = 0;
272 1.1 thorpej if (errno != EINTR) {
273 1.15 pgoyette powerd_log(LOG_ERR, "kevent: %s", strerror(errno));
274 1.1 thorpej exit(EX_OSERR);
275 1.1 thorpej }
276 1.1 thorpej }
277 1.1 thorpej
278 1.5 christos return rv;
279 1.1 thorpej }
280 1.1 thorpej
281 1.1 thorpej static void
282 1.1 thorpej dispatch_dev_power(struct kevent *ev)
283 1.1 thorpej {
284 1.1 thorpej power_event_t pev;
285 1.1 thorpej int fd = ev->ident;
286 1.1 thorpej
287 1.1 thorpej if (debug)
288 1.8 xtraeme (void)fprintf(stderr, "%s: %" PRId64
289 1.8 xtraeme " event%s available\n", __func__,
290 1.8 xtraeme ev->data, ev->data > 1 ? "s" : "");
291 1.1 thorpej
292 1.1 thorpej again:
293 1.16 pgoyette if (prog_read(fd, &pev, sizeof(pev)) != sizeof(pev)) {
294 1.1 thorpej if (errno == EWOULDBLOCK)
295 1.1 thorpej return;
296 1.15 pgoyette powerd_log(LOG_ERR, "read of %s: %s", _PATH_POWER,
297 1.15 pgoyette strerror(errno));
298 1.1 thorpej exit(EX_OSERR);
299 1.1 thorpej }
300 1.1 thorpej
301 1.8 xtraeme if (debug)
302 1.8 xtraeme (void)fprintf(stderr, "%s: event type %d\n",
303 1.8 xtraeme __func__, pev.pev_type);
304 1.1 thorpej
305 1.1 thorpej switch (pev.pev_type) {
306 1.8 xtraeme case POWER_EVENT_ENVSYS_STATE_CHANGE:
307 1.1 thorpej case POWER_EVENT_SWITCH_STATE_CHANGE:
308 1.8 xtraeme dispatch_power_event_state_change(fd, &pev);
309 1.1 thorpej break;
310 1.1 thorpej default:
311 1.15 pgoyette powerd_log(LOG_INFO, "unknown %s event type: %d",
312 1.14 jruoho _PATH_POWER, pev.pev_type);
313 1.1 thorpej }
314 1.1 thorpej
315 1.1 thorpej goto again;
316 1.1 thorpej }
317 1.1 thorpej
318 1.1 thorpej static void
319 1.8 xtraeme dispatch_power_event_state_change(int fd, power_event_t *pev)
320 1.1 thorpej {
321 1.8 xtraeme prop_dictionary_t dict;
322 1.8 xtraeme const char *argv[6];
323 1.10 xtraeme char *buf = NULL;
324 1.8 xtraeme int error;
325 1.8 xtraeme
326 1.8 xtraeme error = prop_dictionary_recv_ioctl(fd, POWER_EVENT_RECVDICT, &dict);
327 1.8 xtraeme if (error) {
328 1.8 xtraeme if (debug)
329 1.8 xtraeme printf("%s: prop_dictionary_recv_ioctl error=%d\n",
330 1.8 xtraeme __func__, error);
331 1.8 xtraeme return;
332 1.8 xtraeme }
333 1.20 roy
334 1.10 xtraeme if (debug) {
335 1.10 xtraeme buf = prop_dictionary_externalize(dict);
336 1.10 xtraeme printf("%s", buf);
337 1.11 xtraeme free(buf);
338 1.10 xtraeme }
339 1.8 xtraeme
340 1.20 roy /* First three arguments are not optional. */
341 1.20 roy if (!prop_dictionary_get_string(dict, "powerd-script-name", &argv[0])) {
342 1.20 roy powerd_log(LOG_ERR, "dict does not have powerd-script-name");
343 1.20 roy return;
344 1.20 roy }
345 1.20 roy if (!prop_dictionary_get_string(dict, "driver-name", &argv[1])) {
346 1.20 roy powerd_log(LOG_ERR, "dict does not have driver-name");
347 1.20 roy return;
348 1.20 roy }
349 1.20 roy if (!prop_dictionary_get_string(dict, "powerd-event-name", &argv[2])) {
350 1.20 roy powerd_log(LOG_ERR, "dict does not have powerd-event-name");
351 1.20 roy return;
352 1.20 roy }
353 1.20 roy
354 1.20 roy /* These arguments are optional. */
355 1.20 roy if (!prop_dictionary_get_string(dict, "sensor-name", &argv[3]))
356 1.20 roy argv[3] = NULL;
357 1.20 roy if (!prop_dictionary_get_string(dict, "state-description", &argv[4]))
358 1.20 roy argv[4] = NULL;
359 1.20 roy
360 1.8 xtraeme argv[5] = NULL;
361 1.8 xtraeme
362 1.1 thorpej run_script(argv);
363 1.1 thorpej }
364 1.15 pgoyette
365 1.15 pgoyette static void
366 1.15 pgoyette powerd_log(int pri, const char *msg, ...)
367 1.15 pgoyette {
368 1.15 pgoyette va_list arglist;
369 1.15 pgoyette unsigned int i;
370 1.15 pgoyette
371 1.15 pgoyette va_start(arglist, msg);
372 1.15 pgoyette if (debug == 0)
373 1.15 pgoyette vsyslog(pri, msg, arglist);
374 1.15 pgoyette else {
375 1.15 pgoyette for (i = 0; i < __arraycount(prioritynames); i++)
376 1.15 pgoyette if (prioritynames[i].c_val == pri)
377 1.15 pgoyette break;
378 1.15 pgoyette fprintf(stderr, "%s: ",
379 1.15 pgoyette (prioritynames[i].c_val == -1) ?
380 1.15 pgoyette "UNKNOWN" : prioritynames[i].c_name);
381 1.15 pgoyette vfprintf(stderr, msg, arglist);
382 1.15 pgoyette }
383 1.18 christos va_end(arglist);
384 1.15 pgoyette }
385