Home | History | Annotate | Line # | Download | only in powerd
powerd.c revision 1.5
      1  1.5  christos /*	$NetBSD: powerd.c,v 1.5 2006/02/05 14:03:46 christos 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.1   thorpej #include <sys/param.h>
     43  1.1   thorpej #include <sys/event.h>
     44  1.1   thorpej #include <sys/power.h>
     45  1.1   thorpej #include <sys/wait.h>
     46  1.1   thorpej #include <errno.h>
     47  1.1   thorpej #include <fcntl.h>
     48  1.1   thorpej #include <stdio.h>
     49  1.1   thorpej #include <stdlib.h>
     50  1.1   thorpej #include <sysexits.h>
     51  1.1   thorpej #include <syslog.h>
     52  1.1   thorpej #include <unistd.h>
     53  1.1   thorpej #include <util.h>
     54  1.1   thorpej 
     55  1.1   thorpej int	debug;
     56  1.1   thorpej 
     57  1.1   thorpej static int kq;
     58  1.1   thorpej 
     59  1.1   thorpej #define	A_CNT(x)		(sizeof((x)) / sizeof((x)[0]))
     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.1   thorpej static void dispatch_power_event_switch_state_change(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.1   thorpej 		rv = wait_for_events(events, A_CNT(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.1   thorpej 	for (i = 0; i < A_CNT(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.1   thorpej 				(void) execv(path, (char **)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.1   thorpej 	if (nchanges == A_CNT(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 const char *
    255  1.1   thorpej pswitch_type_name(int type)
    256  1.1   thorpej {
    257  1.1   thorpej 
    258  1.1   thorpej 	switch (type) {
    259  1.1   thorpej 	case PSWITCH_TYPE_POWER:
    260  1.5  christos 		return "power_button";
    261  1.1   thorpej 
    262  1.1   thorpej 	case PSWITCH_TYPE_SLEEP:
    263  1.5  christos 		return "sleep_button";
    264  1.1   thorpej 
    265  1.1   thorpej 	case PSWITCH_TYPE_LID:
    266  1.5  christos 		return "lid_switch";
    267  1.2   thorpej 
    268  1.2   thorpej 	case PSWITCH_TYPE_RESET:
    269  1.5  christos 		return "reset_button";
    270  1.1   thorpej 
    271  1.4     kochi 	case PSWITCH_TYPE_ACADAPTER:
    272  1.5  christos 		return "acadapter";
    273  1.4     kochi 
    274  1.1   thorpej 	default:
    275  1.5  christos 		return "=unknown pswitch type=";
    276  1.1   thorpej 	}
    277  1.1   thorpej }
    278  1.1   thorpej 
    279  1.1   thorpej static const char *
    280  1.1   thorpej pswitch_event_name(int type)
    281  1.1   thorpej {
    282  1.1   thorpej 
    283  1.1   thorpej 	switch (type) {
    284  1.1   thorpej 	case PSWITCH_EVENT_PRESSED:
    285  1.5  christos 		return "pressed";
    286  1.1   thorpej 
    287  1.1   thorpej 	case PSWITCH_EVENT_RELEASED:
    288  1.5  christos 		return "released";
    289  1.1   thorpej 
    290  1.1   thorpej 	default:
    291  1.5  christos 		return "=unknown pswitch event=";
    292  1.1   thorpej 	}
    293  1.1   thorpej }
    294  1.1   thorpej 
    295  1.1   thorpej static void
    296  1.1   thorpej dispatch_dev_power(struct kevent *ev)
    297  1.1   thorpej {
    298  1.1   thorpej 	power_event_t pev;
    299  1.1   thorpej 	int fd = ev->ident;
    300  1.1   thorpej 
    301  1.1   thorpej 	if (debug)
    302  1.5  christos 		(void)fprintf(stderr, "dispatch_dev_power: %" PRId64
    303  1.1   thorpej 		    " events available\n", ev->data);
    304  1.1   thorpej 
    305  1.1   thorpej  again:
    306  1.1   thorpej 	if (read(fd, &pev, sizeof(pev)) != sizeof(pev)) {
    307  1.1   thorpej 		if (errno == EWOULDBLOCK)
    308  1.1   thorpej 			return;
    309  1.1   thorpej 		syslog(LOG_ERR, "read of %s: %m", _PATH_DEV_POWER);
    310  1.1   thorpej 		exit(EX_OSERR);
    311  1.1   thorpej 	}
    312  1.1   thorpej 
    313  1.1   thorpej 	if (debug) {
    314  1.5  christos 		(void)fprintf(stderr, "dispatch_dev_power: event type %d\n",
    315  1.1   thorpej 		    pev.pev_type);
    316  1.1   thorpej 	}
    317  1.1   thorpej 
    318  1.1   thorpej 	switch (pev.pev_type) {
    319  1.1   thorpej 	case POWER_EVENT_SWITCH_STATE_CHANGE:
    320  1.1   thorpej 		dispatch_power_event_switch_state_change(&pev);
    321  1.1   thorpej 		break;
    322  1.1   thorpej 
    323  1.1   thorpej 	default:
    324  1.1   thorpej 		syslog(LOG_INFO, "unknown %s event type: %d",
    325  1.1   thorpej 		    _PATH_DEV_POWER, pev.pev_type);
    326  1.1   thorpej 	}
    327  1.1   thorpej 
    328  1.1   thorpej 	goto again;
    329  1.1   thorpej }
    330  1.1   thorpej 
    331  1.1   thorpej static void
    332  1.1   thorpej dispatch_power_event_switch_state_change(power_event_t *pev)
    333  1.1   thorpej {
    334  1.1   thorpej 	const char *argv[4];
    335  1.1   thorpej 
    336  1.1   thorpej 	argv[0] = pswitch_type_name(pev->pev_switch.psws_type);
    337  1.1   thorpej 	argv[1] = pev->pev_switch.psws_name;
    338  1.1   thorpej 	argv[2] = pswitch_event_name(pev->pev_switch.psws_state);
    339  1.1   thorpej 	argv[3] = NULL;
    340  1.1   thorpej 
    341  1.1   thorpej 	if (debug)
    342  1.5  christos 		(void)fprintf(stderr, "%s on %s %s\n", argv[0], argv[1],
    343  1.5  christos 		    argv[2]);
    344  1.1   thorpej 
    345  1.1   thorpej 	run_script(argv);
    346  1.1   thorpej }
    347