Home | History | Annotate | Line # | Download | only in powerd
powerd.c revision 1.9
      1  1.9   xtraeme /*	$NetBSD: powerd.c,v 1.9 2007/09/04 16:54:37 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.8   xtraeme #include <prop/proplib.h>
     56  1.1   thorpej 
     57  1.1   thorpej int	debug;
     58  1.1   thorpej 
     59  1.1   thorpej static int kq;
     60  1.1   thorpej 
     61  1.1   thorpej #define	_PATH_DEV_POWER		"/dev/power"
     62  1.1   thorpej #define	_PATH_POWERD_SCRIPTS	"/etc/powerd/scripts"
     63  1.1   thorpej 
     64  1.5  christos static void usage(void) __attribute__((__noreturn__));
     65  1.1   thorpej static void run_script(const char *[]);
     66  1.1   thorpej static struct kevent *allocchange(void);
     67  1.1   thorpej static int wait_for_events(struct kevent *, size_t);
     68  1.1   thorpej static void dispatch_dev_power(struct kevent *);
     69  1.8   xtraeme static void dispatch_power_event_state_change(int, power_event_t *);
     70  1.1   thorpej 
     71  1.1   thorpej static const char *script_paths[] = {
     72  1.1   thorpej 	NULL,
     73  1.1   thorpej 	_PATH_POWERD_SCRIPTS
     74  1.1   thorpej };
     75  1.1   thorpej 
     76  1.1   thorpej int
     77  1.1   thorpej main(int argc, char *argv[])
     78  1.1   thorpej {
     79  1.1   thorpej 	struct kevent *ev, events[16];
     80  1.1   thorpej 	struct power_type power_type;
     81  1.1   thorpej 	char *cp;
     82  1.1   thorpej 	int ch, fd;
     83  1.1   thorpej 
     84  1.5  christos 	setprogname(*argv);
     85  1.5  christos 
     86  1.1   thorpej 	while ((ch = getopt(argc, argv, "d")) != -1) {
     87  1.1   thorpej 		switch (ch) {
     88  1.1   thorpej 		case 'd':
     89  1.1   thorpej 			debug = 1;
     90  1.1   thorpej 			break;
     91  1.1   thorpej 
     92  1.1   thorpej 		default:
     93  1.1   thorpej 			usage();
     94  1.1   thorpej 		}
     95  1.1   thorpej 	}
     96  1.1   thorpej 	argc -= optind;
     97  1.1   thorpej 	argv += optind;
     98  1.1   thorpej 
     99  1.1   thorpej 	if (argc)
    100  1.1   thorpej 		usage();
    101  1.1   thorpej 
    102  1.1   thorpej 	if (debug == 0)
    103  1.5  christos 		(void)daemon(0, 0);
    104  1.1   thorpej 
    105  1.1   thorpej 	openlog("powerd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
    106  1.5  christos 	(void)pidfile(NULL);
    107  1.1   thorpej 
    108  1.5  christos 	if ((kq = kqueue()) == -1) {
    109  1.1   thorpej 		syslog(LOG_ERR, "kqueue: %m");
    110  1.1   thorpej 		exit(EX_OSERR);
    111  1.1   thorpej 	}
    112  1.1   thorpej 
    113  1.5  christos 	if ((fd = open(_PATH_DEV_POWER, O_RDONLY|O_NONBLOCK, 0600)) == -1) {
    114  1.1   thorpej 		syslog(LOG_ERR, "open %s: %m", _PATH_DEV_POWER);
    115  1.1   thorpej 		exit(EX_OSERR);
    116  1.1   thorpej 	}
    117  1.1   thorpej 
    118  1.5  christos 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
    119  1.5  christos 		syslog(LOG_ERR, "Cannot set close on exec in power fd: %m");
    120  1.5  christos 		exit(EX_OSERR);
    121  1.5  christos 	}
    122  1.5  christos 
    123  1.5  christos 	if (ioctl(fd, POWER_IOC_GET_TYPE, &power_type) == -1) {
    124  1.1   thorpej 		syslog(LOG_ERR, "POWER_IOC_GET_TYPE: %m");
    125  1.1   thorpej 		exit(EX_OSERR);
    126  1.1   thorpej 	}
    127  1.1   thorpej 
    128  1.5  christos 	(void)asprintf(&cp, "%s/%s", _PATH_POWERD_SCRIPTS,
    129  1.5  christos 	    power_type.power_type);
    130  1.1   thorpej 	if (cp == NULL) {
    131  1.1   thorpej 		syslog(LOG_ERR, "allocating script path: %m");
    132  1.1   thorpej 		exit(EX_OSERR);
    133  1.1   thorpej 	}
    134  1.1   thorpej 	script_paths[0] = cp;
    135  1.1   thorpej 
    136  1.1   thorpej 	ev = allocchange();
    137  1.1   thorpej 	EV_SET(ev, fd, EVFILT_READ, EV_ADD | EV_ENABLE,
    138  1.1   thorpej 	    0, 0, (intptr_t) dispatch_dev_power);
    139  1.1   thorpej 
    140  1.1   thorpej 	for (;;) {
    141  1.1   thorpej 		void (*handler)(struct kevent *);
    142  1.1   thorpej 		int i, rv;
    143  1.1   thorpej 
    144  1.7   xtraeme 		rv = wait_for_events(events, __arraycount(events));
    145  1.1   thorpej 		for (i = 0; i < rv; i++) {
    146  1.1   thorpej 			handler = (void *) events[i].udata;
    147  1.1   thorpej 			(*handler)(&events[i]);
    148  1.1   thorpej 		}
    149  1.1   thorpej 	}
    150  1.1   thorpej }
    151  1.1   thorpej 
    152  1.1   thorpej static void
    153  1.1   thorpej usage(void)
    154  1.1   thorpej {
    155  1.1   thorpej 
    156  1.5  christos 	(void)fprintf(stderr, "usage: %s [-d]\n", getprogname());
    157  1.1   thorpej 	exit(EX_USAGE);
    158  1.1   thorpej }
    159  1.1   thorpej 
    160  1.1   thorpej static void
    161  1.1   thorpej run_script(const char *argv[])
    162  1.1   thorpej {
    163  1.1   thorpej 	char path[MAXPATHLEN+1];
    164  1.5  christos 	size_t i, j;
    165  1.1   thorpej 
    166  1.7   xtraeme 	for (i = 0; i < __arraycount(script_paths); i++) {
    167  1.5  christos 		(void)snprintf(path, sizeof(path), "%s/%s", script_paths[i],
    168  1.5  christos 		    argv[0]);
    169  1.1   thorpej 		if (access(path, R_OK|X_OK) == 0) {
    170  1.1   thorpej 			int status;
    171  1.1   thorpej 			pid_t pid;
    172  1.1   thorpej 
    173  1.1   thorpej 			argv[0] = path;
    174  1.1   thorpej 
    175  1.1   thorpej 			if (debug) {
    176  1.5  christos 				(void)fprintf(stderr, "running script: %s",
    177  1.5  christos 				    argv[0]);
    178  1.5  christos 				for (j = 1; argv[j] != NULL; j++)
    179  1.5  christos 					(void)fprintf(stderr, " %s", argv[j]);
    180  1.5  christos 				(void)fprintf(stderr, "\n");
    181  1.1   thorpej 			}
    182  1.1   thorpej 
    183  1.1   thorpej 			switch ((pid = vfork())) {
    184  1.1   thorpej 			case -1:
    185  1.1   thorpej 				syslog(LOG_ERR, "fork to run script: %m");
    186  1.1   thorpej 				return;
    187  1.1   thorpej 
    188  1.1   thorpej 			case 0:
    189  1.1   thorpej 				/* Child. */
    190  1.6  christos 				(void) execv(path, __UNCONST(argv));
    191  1.1   thorpej 				_exit(1);
    192  1.1   thorpej 				/* NOTREACHED */
    193  1.1   thorpej 
    194  1.1   thorpej 			default:
    195  1.1   thorpej 				/* Parent. */
    196  1.5  christos 				if (waitpid(pid, &status, 0) == -1) {
    197  1.5  christos 					syslog(LOG_ERR, "waitpid for %s: %m",
    198  1.5  christos 					    path);
    199  1.5  christos 					break;
    200  1.5  christos 				}
    201  1.1   thorpej 				if (WIFEXITED(status) &&
    202  1.1   thorpej 				    WEXITSTATUS(status) != 0) {
    203  1.1   thorpej 					syslog(LOG_ERR,
    204  1.1   thorpej 					    "%s exited with status %d",
    205  1.1   thorpej 					    path, WEXITSTATUS(status));
    206  1.1   thorpej 				} else if (!WIFEXITED(status)) {
    207  1.1   thorpej 					syslog(LOG_ERR,
    208  1.1   thorpej 					    "%s terminated abnormally", path);
    209  1.1   thorpej 				}
    210  1.1   thorpej 				break;
    211  1.1   thorpej 			}
    212  1.1   thorpej 
    213  1.1   thorpej 			return;
    214  1.1   thorpej 		}
    215  1.1   thorpej 	}
    216  1.1   thorpej 
    217  1.1   thorpej 	syslog(LOG_ERR, "no script for %s", argv[0]);
    218  1.1   thorpej 	if (debug)
    219  1.5  christos 		(void)fprintf(stderr, "no script for %s\n", argv[0]);
    220  1.1   thorpej }
    221  1.1   thorpej 
    222  1.1   thorpej static struct kevent changebuf[8];
    223  1.5  christos static size_t nchanges;
    224  1.1   thorpej 
    225  1.1   thorpej static struct kevent *
    226  1.1   thorpej allocchange(void)
    227  1.1   thorpej {
    228  1.1   thorpej 
    229  1.7   xtraeme 	if (nchanges == __arraycount(changebuf)) {
    230  1.5  christos 		(void)wait_for_events(NULL, 0);
    231  1.1   thorpej 		nchanges = 0;
    232  1.1   thorpej 	}
    233  1.1   thorpej 
    234  1.5  christos 	return &changebuf[nchanges++];
    235  1.1   thorpej }
    236  1.1   thorpej 
    237  1.1   thorpej static int
    238  1.1   thorpej wait_for_events(struct kevent *events, size_t nevents)
    239  1.1   thorpej {
    240  1.1   thorpej 	int rv;
    241  1.1   thorpej 
    242  1.1   thorpej 	while ((rv = kevent(kq, nchanges ? changebuf : NULL, nchanges,
    243  1.5  christos 	    events, nevents, NULL)) < 0) {
    244  1.1   thorpej 		nchanges = 0;
    245  1.1   thorpej 		if (errno != EINTR) {
    246  1.1   thorpej 			syslog(LOG_ERR, "kevent: %m");
    247  1.1   thorpej 			exit(EX_OSERR);
    248  1.1   thorpej 		}
    249  1.1   thorpej 	}
    250  1.1   thorpej 
    251  1.5  christos 	return rv;
    252  1.1   thorpej }
    253  1.1   thorpej 
    254  1.1   thorpej static void
    255  1.1   thorpej dispatch_dev_power(struct kevent *ev)
    256  1.1   thorpej {
    257  1.1   thorpej 	power_event_t pev;
    258  1.1   thorpej 	int fd = ev->ident;
    259  1.1   thorpej 
    260  1.1   thorpej 	if (debug)
    261  1.8   xtraeme 		(void)fprintf(stderr, "%s: %" PRId64
    262  1.8   xtraeme 		    " event%s available\n", __func__,
    263  1.8   xtraeme 		    ev->data, ev->data > 1 ? "s" : "");
    264  1.1   thorpej 
    265  1.1   thorpej  again:
    266  1.1   thorpej 	if (read(fd, &pev, sizeof(pev)) != sizeof(pev)) {
    267  1.1   thorpej 		if (errno == EWOULDBLOCK)
    268  1.1   thorpej 			return;
    269  1.1   thorpej 		syslog(LOG_ERR, "read of %s: %m", _PATH_DEV_POWER);
    270  1.1   thorpej 		exit(EX_OSERR);
    271  1.1   thorpej 	}
    272  1.1   thorpej 
    273  1.8   xtraeme 	if (debug)
    274  1.8   xtraeme 		(void)fprintf(stderr, "%s: event type %d\n",
    275  1.8   xtraeme 		    __func__, pev.pev_type);
    276  1.1   thorpej 
    277  1.1   thorpej 	switch (pev.pev_type) {
    278  1.8   xtraeme 	case POWER_EVENT_ENVSYS_STATE_CHANGE:
    279  1.1   thorpej 	case POWER_EVENT_SWITCH_STATE_CHANGE:
    280  1.8   xtraeme 		dispatch_power_event_state_change(fd, &pev);
    281  1.1   thorpej 		break;
    282  1.1   thorpej 	default:
    283  1.1   thorpej 		syslog(LOG_INFO, "unknown %s event type: %d",
    284  1.1   thorpej 		    _PATH_DEV_POWER, pev.pev_type);
    285  1.1   thorpej 	}
    286  1.1   thorpej 
    287  1.1   thorpej 	goto again;
    288  1.1   thorpej }
    289  1.1   thorpej 
    290  1.1   thorpej static void
    291  1.8   xtraeme dispatch_power_event_state_change(int fd, power_event_t *pev)
    292  1.1   thorpej {
    293  1.8   xtraeme 	prop_dictionary_t dict;
    294  1.8   xtraeme 	prop_object_t obj;
    295  1.8   xtraeme 	const char *argv[6];
    296  1.8   xtraeme 	int error;
    297  1.8   xtraeme 
    298  1.8   xtraeme 	error = prop_dictionary_recv_ioctl(fd, POWER_EVENT_RECVDICT, &dict);
    299  1.8   xtraeme 	if (error) {
    300  1.8   xtraeme 		if (debug)
    301  1.8   xtraeme 			printf("%s: prop_dictionary_recv_ioctl error=%d\n",
    302  1.8   xtraeme 			    __func__, error);
    303  1.8   xtraeme 		return;
    304  1.8   xtraeme 	}
    305  1.8   xtraeme 
    306  1.8   xtraeme 	if (debug)
    307  1.8   xtraeme 		printf("%s", prop_dictionary_externalize(dict));
    308  1.8   xtraeme 
    309  1.8   xtraeme 	obj = prop_dictionary_get(dict, "powerd-script-name");
    310  1.8   xtraeme 	argv[0] = prop_string_cstring_nocopy(obj);
    311  1.8   xtraeme 
    312  1.8   xtraeme 	obj = prop_dictionary_get(dict, "driver-name");
    313  1.8   xtraeme 	argv[1] = prop_string_cstring_nocopy(obj);
    314  1.8   xtraeme 
    315  1.8   xtraeme 	obj = prop_dictionary_get(dict, "powerd-event-name");
    316  1.8   xtraeme 	argv[2] = prop_string_cstring_nocopy(obj);
    317  1.1   thorpej 
    318  1.8   xtraeme 	obj = prop_dictionary_get(dict, "sensor-name");
    319  1.8   xtraeme 	argv[3] = prop_string_cstring_nocopy(obj);
    320  1.1   thorpej 
    321  1.9   xtraeme 	obj = prop_dictionary_get(dict, "state-description");
    322  1.8   xtraeme 	argv[4] = prop_string_cstring_nocopy(obj);
    323  1.8   xtraeme 
    324  1.8   xtraeme 	argv[5] = NULL;
    325  1.8   xtraeme 
    326  1.8   xtraeme 	prop_object_release(dict);
    327  1.1   thorpej 
    328  1.1   thorpej 	run_script(argv);
    329  1.1   thorpej }
    330