Home | History | Annotate | Line # | Download | only in powerd
powerd.c revision 1.17.2.1
      1  1.17.2.1       tls /*	$NetBSD: powerd.c,v 1.17.2.1 2013/02/25 00:30:47 tls 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.1   thorpej 
    193       1.1   thorpej 			argv[0] = path;
    194       1.1   thorpej 
    195       1.1   thorpej 			if (debug) {
    196      1.15  pgoyette 				(void)fprintf(stderr, "%srunning script: %s",
    197      1.15  pgoyette 				    no_scripts?"not ":"", argv[0]);
    198       1.5  christos 				for (j = 1; argv[j] != NULL; j++)
    199       1.5  christos 					(void)fprintf(stderr, " %s", argv[j]);
    200       1.5  christos 				(void)fprintf(stderr, "\n");
    201       1.1   thorpej 			}
    202      1.15  pgoyette 			if (no_scripts != 0)
    203      1.15  pgoyette 				return;
    204       1.1   thorpej 
    205       1.1   thorpej 			switch ((pid = vfork())) {
    206       1.1   thorpej 			case -1:
    207      1.15  pgoyette 				powerd_log(LOG_ERR, "fork to run script: %s",
    208      1.15  pgoyette 				    strerror(errno));
    209       1.1   thorpej 				return;
    210       1.1   thorpej 
    211       1.1   thorpej 			case 0:
    212       1.1   thorpej 				/* Child. */
    213       1.6  christos 				(void) execv(path, __UNCONST(argv));
    214       1.1   thorpej 				_exit(1);
    215       1.1   thorpej 				/* NOTREACHED */
    216       1.1   thorpej 
    217       1.1   thorpej 			default:
    218       1.1   thorpej 				/* Parent. */
    219       1.5  christos 				if (waitpid(pid, &status, 0) == -1) {
    220      1.15  pgoyette 					powerd_log(LOG_ERR,
    221      1.15  pgoyette 					    "waitpid for %s: %s", path,
    222      1.15  pgoyette 					    strerror(errno));
    223       1.5  christos 					break;
    224       1.5  christos 				}
    225       1.1   thorpej 				if (WIFEXITED(status) &&
    226       1.1   thorpej 				    WEXITSTATUS(status) != 0) {
    227      1.15  pgoyette 					powerd_log(LOG_ERR,
    228       1.1   thorpej 					    "%s exited with status %d",
    229       1.1   thorpej 					    path, WEXITSTATUS(status));
    230       1.1   thorpej 				} else if (!WIFEXITED(status)) {
    231      1.15  pgoyette 					powerd_log(LOG_ERR,
    232       1.1   thorpej 					    "%s terminated abnormally", path);
    233       1.1   thorpej 				}
    234       1.1   thorpej 				break;
    235       1.1   thorpej 			}
    236       1.1   thorpej 
    237       1.1   thorpej 			return;
    238       1.1   thorpej 		}
    239       1.1   thorpej 	}
    240       1.1   thorpej 
    241      1.15  pgoyette 	powerd_log(LOG_ERR, "no script for %s", argv[0]);
    242       1.1   thorpej }
    243       1.1   thorpej 
    244       1.1   thorpej static struct kevent changebuf[8];
    245       1.5  christos static size_t nchanges;
    246       1.1   thorpej 
    247       1.1   thorpej static struct kevent *
    248       1.1   thorpej allocchange(void)
    249       1.1   thorpej {
    250       1.1   thorpej 
    251       1.7   xtraeme 	if (nchanges == __arraycount(changebuf)) {
    252       1.5  christos 		(void)wait_for_events(NULL, 0);
    253       1.1   thorpej 		nchanges = 0;
    254       1.1   thorpej 	}
    255       1.1   thorpej 
    256       1.5  christos 	return &changebuf[nchanges++];
    257       1.1   thorpej }
    258       1.1   thorpej 
    259       1.1   thorpej static int
    260       1.1   thorpej wait_for_events(struct kevent *events, size_t nevents)
    261       1.1   thorpej {
    262       1.1   thorpej 	int rv;
    263       1.1   thorpej 
    264      1.16  pgoyette 	while ((rv = prog_kevent(kq, nchanges ? changebuf : NULL, nchanges,
    265       1.5  christos 	    events, nevents, NULL)) < 0) {
    266       1.1   thorpej 		nchanges = 0;
    267       1.1   thorpej 		if (errno != EINTR) {
    268      1.15  pgoyette 			powerd_log(LOG_ERR, "kevent: %s", strerror(errno));
    269       1.1   thorpej 			exit(EX_OSERR);
    270       1.1   thorpej 		}
    271       1.1   thorpej 	}
    272       1.1   thorpej 
    273       1.5  christos 	return rv;
    274       1.1   thorpej }
    275       1.1   thorpej 
    276       1.1   thorpej static void
    277       1.1   thorpej dispatch_dev_power(struct kevent *ev)
    278       1.1   thorpej {
    279       1.1   thorpej 	power_event_t pev;
    280       1.1   thorpej 	int fd = ev->ident;
    281       1.1   thorpej 
    282       1.1   thorpej 	if (debug)
    283       1.8   xtraeme 		(void)fprintf(stderr, "%s: %" PRId64
    284       1.8   xtraeme 		    " event%s available\n", __func__,
    285       1.8   xtraeme 		    ev->data, ev->data > 1 ? "s" : "");
    286       1.1   thorpej 
    287       1.1   thorpej  again:
    288      1.16  pgoyette 	if (prog_read(fd, &pev, sizeof(pev)) != sizeof(pev)) {
    289       1.1   thorpej 		if (errno == EWOULDBLOCK)
    290       1.1   thorpej 			return;
    291      1.15  pgoyette 		powerd_log(LOG_ERR, "read of %s: %s", _PATH_POWER,
    292      1.15  pgoyette 		    strerror(errno));
    293       1.1   thorpej 		exit(EX_OSERR);
    294       1.1   thorpej 	}
    295       1.1   thorpej 
    296       1.8   xtraeme 	if (debug)
    297       1.8   xtraeme 		(void)fprintf(stderr, "%s: event type %d\n",
    298       1.8   xtraeme 		    __func__, pev.pev_type);
    299       1.1   thorpej 
    300       1.1   thorpej 	switch (pev.pev_type) {
    301       1.8   xtraeme 	case POWER_EVENT_ENVSYS_STATE_CHANGE:
    302       1.1   thorpej 	case POWER_EVENT_SWITCH_STATE_CHANGE:
    303       1.8   xtraeme 		dispatch_power_event_state_change(fd, &pev);
    304       1.1   thorpej 		break;
    305       1.1   thorpej 	default:
    306      1.15  pgoyette 		powerd_log(LOG_INFO, "unknown %s event type: %d",
    307      1.14    jruoho 		    _PATH_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.8   xtraeme dispatch_power_event_state_change(int fd, power_event_t *pev)
    315       1.1   thorpej {
    316       1.8   xtraeme 	prop_dictionary_t dict;
    317       1.8   xtraeme 	prop_object_t obj;
    318       1.8   xtraeme 	const char *argv[6];
    319      1.10   xtraeme 	char *buf = NULL;
    320       1.8   xtraeme 	int error;
    321       1.8   xtraeme 
    322       1.8   xtraeme 	error = prop_dictionary_recv_ioctl(fd, POWER_EVENT_RECVDICT, &dict);
    323       1.8   xtraeme 	if (error) {
    324       1.8   xtraeme 		if (debug)
    325       1.8   xtraeme 			printf("%s: prop_dictionary_recv_ioctl error=%d\n",
    326       1.8   xtraeme 			    __func__, error);
    327       1.8   xtraeme 		return;
    328       1.8   xtraeme 	}
    329       1.8   xtraeme 
    330      1.10   xtraeme 	if (debug) {
    331      1.10   xtraeme 		buf = prop_dictionary_externalize(dict);
    332      1.10   xtraeme 		printf("%s", buf);
    333      1.11   xtraeme 		free(buf);
    334      1.10   xtraeme 	}
    335       1.8   xtraeme 
    336       1.8   xtraeme 	obj = prop_dictionary_get(dict, "powerd-script-name");
    337       1.8   xtraeme 	argv[0] = prop_string_cstring_nocopy(obj);
    338       1.8   xtraeme 
    339       1.8   xtraeme 	obj = prop_dictionary_get(dict, "driver-name");
    340       1.8   xtraeme 	argv[1] = prop_string_cstring_nocopy(obj);
    341       1.8   xtraeme 
    342       1.8   xtraeme 	obj = prop_dictionary_get(dict, "powerd-event-name");
    343       1.8   xtraeme 	argv[2] = prop_string_cstring_nocopy(obj);
    344       1.1   thorpej 
    345       1.8   xtraeme 	obj = prop_dictionary_get(dict, "sensor-name");
    346       1.8   xtraeme 	argv[3] = prop_string_cstring_nocopy(obj);
    347       1.1   thorpej 
    348       1.9   xtraeme 	obj = prop_dictionary_get(dict, "state-description");
    349       1.8   xtraeme 	argv[4] = prop_string_cstring_nocopy(obj);
    350       1.8   xtraeme 
    351       1.8   xtraeme 	argv[5] = NULL;
    352       1.8   xtraeme 
    353       1.1   thorpej 	run_script(argv);
    354       1.1   thorpej }
    355      1.15  pgoyette 
    356      1.15  pgoyette static void
    357      1.15  pgoyette powerd_log(int pri, const char *msg, ...)
    358      1.15  pgoyette {
    359      1.15  pgoyette 	va_list arglist;
    360      1.15  pgoyette 	unsigned int i;
    361      1.15  pgoyette 
    362      1.15  pgoyette 	va_start(arglist, msg);
    363      1.15  pgoyette 	if (debug == 0)
    364      1.15  pgoyette 		vsyslog(pri, msg, arglist);
    365      1.15  pgoyette 	else {
    366      1.15  pgoyette 		for (i = 0; i < __arraycount(prioritynames); i++)
    367      1.15  pgoyette 			if (prioritynames[i].c_val == pri)
    368      1.15  pgoyette 				break;
    369      1.15  pgoyette 		fprintf(stderr, "%s: ",
    370      1.15  pgoyette 		    (prioritynames[i].c_val == -1) ?
    371      1.15  pgoyette 			    "UNKNOWN" : prioritynames[i].c_name);
    372      1.15  pgoyette 		vfprintf(stderr, msg, arglist);
    373      1.15  pgoyette 	}
    374  1.17.2.1       tls 	va_end(arglist);
    375      1.15  pgoyette }
    376