Home | History | Annotate | Line # | Download | only in powerd
powerd.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: powerd.c,v 1.1 2003/04/18 04:54:50 thorpej 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.1  thorpej static void usage(void);
     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.1  thorpej 	while ((ch = getopt(argc, argv, "d")) != -1) {
     85  1.1  thorpej 		switch (ch) {
     86  1.1  thorpej 		case 'd':
     87  1.1  thorpej 			debug = 1;
     88  1.1  thorpej 			break;
     89  1.1  thorpej 
     90  1.1  thorpej 		default:
     91  1.1  thorpej 			usage();
     92  1.1  thorpej 		}
     93  1.1  thorpej 	}
     94  1.1  thorpej 	argc -= optind;
     95  1.1  thorpej 	argv += optind;
     96  1.1  thorpej 
     97  1.1  thorpej 	if (argc)
     98  1.1  thorpej 		usage();
     99  1.1  thorpej 
    100  1.1  thorpej 	if (debug == 0)
    101  1.1  thorpej 		daemon(0, 0);
    102  1.1  thorpej 
    103  1.1  thorpej 	openlog("powerd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
    104  1.1  thorpej 	pidfile(NULL);
    105  1.1  thorpej 
    106  1.1  thorpej 	kq = kqueue();
    107  1.1  thorpej 	if (kq < 0) {
    108  1.1  thorpej 		syslog(LOG_ERR, "kqueue: %m");
    109  1.1  thorpej 		exit(EX_OSERR);
    110  1.1  thorpej 	}
    111  1.1  thorpej 
    112  1.1  thorpej 	fd = open(_PATH_DEV_POWER, O_RDONLY|O_NONBLOCK, 0600);
    113  1.1  thorpej 	if (fd < 0) {
    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.1  thorpej 	if (ioctl(fd, POWER_IOC_GET_TYPE, &power_type) < 0) {
    119  1.1  thorpej 		syslog(LOG_ERR, "POWER_IOC_GET_TYPE: %m");
    120  1.1  thorpej 		exit(EX_OSERR);
    121  1.1  thorpej 	}
    122  1.1  thorpej 
    123  1.1  thorpej 	asprintf(&cp, "%s/%s", _PATH_POWERD_SCRIPTS, power_type.power_type);
    124  1.1  thorpej 	if (cp == NULL) {
    125  1.1  thorpej 		syslog(LOG_ERR, "allocating script path: %m");
    126  1.1  thorpej 		exit(EX_OSERR);
    127  1.1  thorpej 	}
    128  1.1  thorpej 	script_paths[0] = cp;
    129  1.1  thorpej 
    130  1.1  thorpej 	ev = allocchange();
    131  1.1  thorpej 	EV_SET(ev, fd, EVFILT_READ, EV_ADD | EV_ENABLE,
    132  1.1  thorpej 	    0, 0, (intptr_t) dispatch_dev_power);
    133  1.1  thorpej 
    134  1.1  thorpej 	for (;;) {
    135  1.1  thorpej 		void (*handler)(struct kevent *);
    136  1.1  thorpej 		int i, rv;
    137  1.1  thorpej 
    138  1.1  thorpej 		rv = wait_for_events(events, A_CNT(events));
    139  1.1  thorpej 		for (i = 0; i < rv; i++) {
    140  1.1  thorpej 			handler = (void *) events[i].udata;
    141  1.1  thorpej 			(*handler)(&events[i]);
    142  1.1  thorpej 		}
    143  1.1  thorpej 	}
    144  1.1  thorpej }
    145  1.1  thorpej 
    146  1.1  thorpej static void
    147  1.1  thorpej usage(void)
    148  1.1  thorpej {
    149  1.1  thorpej 
    150  1.1  thorpej 	fprintf(stderr, "usage: %s [-d]\n", getprogname());
    151  1.1  thorpej 	exit(EX_USAGE);
    152  1.1  thorpej }
    153  1.1  thorpej 
    154  1.1  thorpej static void
    155  1.1  thorpej run_script(const char *argv[])
    156  1.1  thorpej {
    157  1.1  thorpej 	char path[MAXPATHLEN+1];
    158  1.1  thorpej 	int i;
    159  1.1  thorpej 
    160  1.1  thorpej 	for (i = 0; i < A_CNT(script_paths); i++) {
    161  1.1  thorpej 		sprintf(path, "%s/%s", script_paths[i], argv[0]);
    162  1.1  thorpej 		if (access(path, R_OK|X_OK) == 0) {
    163  1.1  thorpej 			int status;
    164  1.1  thorpej 			pid_t pid;
    165  1.1  thorpej 
    166  1.1  thorpej 			argv[0] = path;
    167  1.1  thorpej 
    168  1.1  thorpej 			if (debug) {
    169  1.1  thorpej 				fprintf(stderr, "running script: %s", argv[0]);
    170  1.1  thorpej 				for (i = 1; argv[i] != NULL; i++)
    171  1.1  thorpej 					fprintf(stderr, " %s", argv[i]);
    172  1.1  thorpej 				fprintf(stderr, "\n");
    173  1.1  thorpej 			}
    174  1.1  thorpej 
    175  1.1  thorpej 			switch ((pid = vfork())) {
    176  1.1  thorpej 			case -1:
    177  1.1  thorpej 				syslog(LOG_ERR, "fork to run script: %m");
    178  1.1  thorpej 				return;
    179  1.1  thorpej 
    180  1.1  thorpej 			case 0:
    181  1.1  thorpej 				/* Child. */
    182  1.1  thorpej 				(void) execv(path, (char **)argv);
    183  1.1  thorpej 				_exit(1);
    184  1.1  thorpej 				/* NOTREACHED */
    185  1.1  thorpej 
    186  1.1  thorpej 			default:
    187  1.1  thorpej 				/* Parent. */
    188  1.1  thorpej 				(void) wait4(pid, &status, 0, 0);
    189  1.1  thorpej 				if (WIFEXITED(status) &&
    190  1.1  thorpej 				    WEXITSTATUS(status) != 0) {
    191  1.1  thorpej 					syslog(LOG_ERR,
    192  1.1  thorpej 					    "%s exited with status %d",
    193  1.1  thorpej 					    path, WEXITSTATUS(status));
    194  1.1  thorpej 				} else if (!WIFEXITED(status)) {
    195  1.1  thorpej 					syslog(LOG_ERR,
    196  1.1  thorpej 					    "%s terminated abnormally", path);
    197  1.1  thorpej 				}
    198  1.1  thorpej 				break;
    199  1.1  thorpej 			}
    200  1.1  thorpej 
    201  1.1  thorpej 			return;
    202  1.1  thorpej 		}
    203  1.1  thorpej 	}
    204  1.1  thorpej 
    205  1.1  thorpej 	syslog(LOG_ERR, "no script for %s", argv[0]);
    206  1.1  thorpej 	if (debug)
    207  1.1  thorpej 		fprintf(stderr, "no script for %s\n", argv[0]);
    208  1.1  thorpej }
    209  1.1  thorpej 
    210  1.1  thorpej static struct kevent changebuf[8];
    211  1.1  thorpej static int nchanges;
    212  1.1  thorpej 
    213  1.1  thorpej static struct kevent *
    214  1.1  thorpej allocchange(void)
    215  1.1  thorpej {
    216  1.1  thorpej 
    217  1.1  thorpej 	if (nchanges == A_CNT(changebuf)) {
    218  1.1  thorpej 		(void) wait_for_events(NULL, 0);
    219  1.1  thorpej 		nchanges = 0;
    220  1.1  thorpej 	}
    221  1.1  thorpej 
    222  1.1  thorpej 	return (&changebuf[nchanges++]);
    223  1.1  thorpej }
    224  1.1  thorpej 
    225  1.1  thorpej static int
    226  1.1  thorpej wait_for_events(struct kevent *events, size_t nevents)
    227  1.1  thorpej {
    228  1.1  thorpej 	int rv;
    229  1.1  thorpej 
    230  1.1  thorpej 	while ((rv = kevent(kq, nchanges ? changebuf : NULL, nchanges,
    231  1.1  thorpej 			    events, nevents, NULL)) < 0) {
    232  1.1  thorpej 		nchanges = 0;
    233  1.1  thorpej 		if (errno != EINTR) {
    234  1.1  thorpej 			syslog(LOG_ERR, "kevent: %m");
    235  1.1  thorpej 			exit(EX_OSERR);
    236  1.1  thorpej 		}
    237  1.1  thorpej 	}
    238  1.1  thorpej 
    239  1.1  thorpej 	return (rv);
    240  1.1  thorpej }
    241  1.1  thorpej 
    242  1.1  thorpej static const char *
    243  1.1  thorpej pswitch_type_name(int type)
    244  1.1  thorpej {
    245  1.1  thorpej 
    246  1.1  thorpej 	switch (type) {
    247  1.1  thorpej 	case PSWITCH_TYPE_POWER:
    248  1.1  thorpej 		return ("power_button");
    249  1.1  thorpej 
    250  1.1  thorpej 	case PSWITCH_TYPE_SLEEP:
    251  1.1  thorpej 		return ("sleep_button");
    252  1.1  thorpej 
    253  1.1  thorpej 	case PSWITCH_TYPE_LID:
    254  1.1  thorpej 		return ("lid_switch");
    255  1.1  thorpej 
    256  1.1  thorpej 	default:
    257  1.1  thorpej 		return ("=unknown pswitch type=");
    258  1.1  thorpej 	}
    259  1.1  thorpej }
    260  1.1  thorpej 
    261  1.1  thorpej static const char *
    262  1.1  thorpej pswitch_event_name(int type)
    263  1.1  thorpej {
    264  1.1  thorpej 
    265  1.1  thorpej 	switch (type) {
    266  1.1  thorpej 	case PSWITCH_EVENT_PRESSED:
    267  1.1  thorpej 		return ("pressed");
    268  1.1  thorpej 
    269  1.1  thorpej 	case PSWITCH_EVENT_RELEASED:
    270  1.1  thorpej 		return ("released");
    271  1.1  thorpej 
    272  1.1  thorpej 	default:
    273  1.1  thorpej 		return ("=unknown pswitch event=");
    274  1.1  thorpej 	}
    275  1.1  thorpej }
    276  1.1  thorpej 
    277  1.1  thorpej static void
    278  1.1  thorpej dispatch_dev_power(struct kevent *ev)
    279  1.1  thorpej {
    280  1.1  thorpej 	power_event_t pev;
    281  1.1  thorpej 	int fd = ev->ident;
    282  1.1  thorpej 
    283  1.1  thorpej 	if (debug)
    284  1.1  thorpej 		fprintf(stderr, "dispatch_dev_power: %" PRId64
    285  1.1  thorpej 		    " events available\n", ev->data);
    286  1.1  thorpej 
    287  1.1  thorpej  again:
    288  1.1  thorpej 	if (read(fd, &pev, sizeof(pev)) != sizeof(pev)) {
    289  1.1  thorpej 		if (errno == EWOULDBLOCK)
    290  1.1  thorpej 			return;
    291  1.1  thorpej 		syslog(LOG_ERR, "read of %s: %m", _PATH_DEV_POWER);
    292  1.1  thorpej 		exit(EX_OSERR);
    293  1.1  thorpej 	}
    294  1.1  thorpej 
    295  1.1  thorpej 	if (debug) {
    296  1.1  thorpej 		fprintf(stderr, "dispatch_dev_power: event type %d\n",
    297  1.1  thorpej 		    pev.pev_type);
    298  1.1  thorpej 	}
    299  1.1  thorpej 
    300  1.1  thorpej 	switch (pev.pev_type) {
    301  1.1  thorpej 	case POWER_EVENT_SWITCH_STATE_CHANGE:
    302  1.1  thorpej 		dispatch_power_event_switch_state_change(&pev);
    303  1.1  thorpej 		break;
    304  1.1  thorpej 
    305  1.1  thorpej 	default:
    306  1.1  thorpej 		syslog(LOG_INFO, "unknown %s event type: %d",
    307  1.1  thorpej 		    _PATH_DEV_POWER, pev.pev_type);
    308  1.1  thorpej 	}
    309  1.1  thorpej 
    310  1.1  thorpej 	goto again;
    311  1.1  thorpej }
    312  1.1  thorpej 
    313  1.1  thorpej static void
    314  1.1  thorpej dispatch_power_event_switch_state_change(power_event_t *pev)
    315  1.1  thorpej {
    316  1.1  thorpej 	const char *argv[4];
    317  1.1  thorpej 
    318  1.1  thorpej 	argv[0] = pswitch_type_name(pev->pev_switch.psws_type);
    319  1.1  thorpej 	argv[1] = pev->pev_switch.psws_name;
    320  1.1  thorpej 	argv[2] = pswitch_event_name(pev->pev_switch.psws_state);
    321  1.1  thorpej 	argv[3] = NULL;
    322  1.1  thorpej 
    323  1.1  thorpej 	if (debug)
    324  1.1  thorpej 		fprintf(stderr, "%s on %s %s\n", argv[0], argv[1], argv[2]);
    325  1.1  thorpej 
    326  1.1  thorpej 	run_script(argv);
    327  1.1  thorpej }
    328