xref: /netbsd-src/usr.sbin/powerd/powerd.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: powerd.c,v 1.6 2006/02/05 14:11:18 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
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.
24  *
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.
36  */
37 
38 /*
39  * Power management daemon for sysmon.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/event.h>
44 #include <sys/power.h>
45 #include <sys/wait.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <sysexits.h>
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <util.h>
54 
55 int	debug;
56 
57 static int kq;
58 
59 #define	A_CNT(x)		(sizeof((x)) / sizeof((x)[0]))
60 
61 #define	_PATH_DEV_POWER		"/dev/power"
62 #define	_PATH_POWERD_SCRIPTS	"/etc/powerd/scripts"
63 
64 static void usage(void) __attribute__((__noreturn__));
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_switch_state_change(power_event_t *);
70 
71 static const char *script_paths[] = {
72 	NULL,
73 	_PATH_POWERD_SCRIPTS
74 };
75 
76 int
77 main(int argc, char *argv[])
78 {
79 	struct kevent *ev, events[16];
80 	struct power_type power_type;
81 	char *cp;
82 	int ch, fd;
83 
84 	setprogname(*argv);
85 
86 	while ((ch = getopt(argc, argv, "d")) != -1) {
87 		switch (ch) {
88 		case 'd':
89 			debug = 1;
90 			break;
91 
92 		default:
93 			usage();
94 		}
95 	}
96 	argc -= optind;
97 	argv += optind;
98 
99 	if (argc)
100 		usage();
101 
102 	if (debug == 0)
103 		(void)daemon(0, 0);
104 
105 	openlog("powerd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
106 	(void)pidfile(NULL);
107 
108 	if ((kq = kqueue()) == -1) {
109 		syslog(LOG_ERR, "kqueue: %m");
110 		exit(EX_OSERR);
111 	}
112 
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);
116 	}
117 
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);
121 	}
122 
123 	if (ioctl(fd, POWER_IOC_GET_TYPE, &power_type) == -1) {
124 		syslog(LOG_ERR, "POWER_IOC_GET_TYPE: %m");
125 		exit(EX_OSERR);
126 	}
127 
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);
133 	}
134 	script_paths[0] = cp;
135 
136 	ev = allocchange();
137 	EV_SET(ev, fd, EVFILT_READ, EV_ADD | EV_ENABLE,
138 	    0, 0, (intptr_t) dispatch_dev_power);
139 
140 	for (;;) {
141 		void (*handler)(struct kevent *);
142 		int i, rv;
143 
144 		rv = wait_for_events(events, A_CNT(events));
145 		for (i = 0; i < rv; i++) {
146 			handler = (void *) events[i].udata;
147 			(*handler)(&events[i]);
148 		}
149 	}
150 }
151 
152 static void
153 usage(void)
154 {
155 
156 	(void)fprintf(stderr, "usage: %s [-d]\n", getprogname());
157 	exit(EX_USAGE);
158 }
159 
160 static void
161 run_script(const char *argv[])
162 {
163 	char path[MAXPATHLEN+1];
164 	size_t i, j;
165 
166 	for (i = 0; i < A_CNT(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;
172 
173 			argv[0] = path;
174 
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");
181 			}
182 
183 			switch ((pid = vfork())) {
184 			case -1:
185 				syslog(LOG_ERR, "fork to run script: %m");
186 				return;
187 
188 			case 0:
189 				/* Child. */
190 				(void) execv(path, __UNCONST(argv));
191 				_exit(1);
192 				/* NOTREACHED */
193 
194 			default:
195 				/* Parent. */
196 				if (waitpid(pid, &status, 0) == -1) {
197 					syslog(LOG_ERR, "waitpid for %s: %m",
198 					    path);
199 					break;
200 				}
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);
209 				}
210 				break;
211 			}
212 
213 			return;
214 		}
215 	}
216 
217 	syslog(LOG_ERR, "no script for %s", argv[0]);
218 	if (debug)
219 		(void)fprintf(stderr, "no script for %s\n", argv[0]);
220 }
221 
222 static struct kevent changebuf[8];
223 static size_t nchanges;
224 
225 static struct kevent *
226 allocchange(void)
227 {
228 
229 	if (nchanges == A_CNT(changebuf)) {
230 		(void)wait_for_events(NULL, 0);
231 		nchanges = 0;
232 	}
233 
234 	return &changebuf[nchanges++];
235 }
236 
237 static int
238 wait_for_events(struct kevent *events, size_t nevents)
239 {
240 	int rv;
241 
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);
248 		}
249 	}
250 
251 	return rv;
252 }
253 
254 static const char *
255 pswitch_type_name(int type)
256 {
257 
258 	switch (type) {
259 	case PSWITCH_TYPE_POWER:
260 		return "power_button";
261 
262 	case PSWITCH_TYPE_SLEEP:
263 		return "sleep_button";
264 
265 	case PSWITCH_TYPE_LID:
266 		return "lid_switch";
267 
268 	case PSWITCH_TYPE_RESET:
269 		return "reset_button";
270 
271 	case PSWITCH_TYPE_ACADAPTER:
272 		return "acadapter";
273 
274 	default:
275 		return "=unknown pswitch type=";
276 	}
277 }
278 
279 static const char *
280 pswitch_event_name(int type)
281 {
282 
283 	switch (type) {
284 	case PSWITCH_EVENT_PRESSED:
285 		return "pressed";
286 
287 	case PSWITCH_EVENT_RELEASED:
288 		return "released";
289 
290 	default:
291 		return "=unknown pswitch event=";
292 	}
293 }
294 
295 static void
296 dispatch_dev_power(struct kevent *ev)
297 {
298 	power_event_t pev;
299 	int fd = ev->ident;
300 
301 	if (debug)
302 		(void)fprintf(stderr, "dispatch_dev_power: %" PRId64
303 		    " events available\n", ev->data);
304 
305  again:
306 	if (read(fd, &pev, sizeof(pev)) != sizeof(pev)) {
307 		if (errno == EWOULDBLOCK)
308 			return;
309 		syslog(LOG_ERR, "read of %s: %m", _PATH_DEV_POWER);
310 		exit(EX_OSERR);
311 	}
312 
313 	if (debug) {
314 		(void)fprintf(stderr, "dispatch_dev_power: event type %d\n",
315 		    pev.pev_type);
316 	}
317 
318 	switch (pev.pev_type) {
319 	case POWER_EVENT_SWITCH_STATE_CHANGE:
320 		dispatch_power_event_switch_state_change(&pev);
321 		break;
322 
323 	default:
324 		syslog(LOG_INFO, "unknown %s event type: %d",
325 		    _PATH_DEV_POWER, pev.pev_type);
326 	}
327 
328 	goto again;
329 }
330 
331 static void
332 dispatch_power_event_switch_state_change(power_event_t *pev)
333 {
334 	const char *argv[4];
335 
336 	argv[0] = pswitch_type_name(pev->pev_switch.psws_type);
337 	argv[1] = pev->pev_switch.psws_name;
338 	argv[2] = pswitch_event_name(pev->pev_switch.psws_state);
339 	argv[3] = NULL;
340 
341 	if (debug)
342 		(void)fprintf(stderr, "%s on %s %s\n", argv[0], argv[1],
343 		    argv[2]);
344 
345 	run_script(argv);
346 }
347