if (kq <= 0) { syslog(LOG_ERR, "kqueue: %m"); exit(1); }
if (kq <= 0) error("kqueue", NULL);
main(int argc, char *argv[]){	const char *fname = apmdev;	int ctl_fd, sock_fd, ch, suspends, standbys, resumes;	int statonly = 0;	int enableonly = 0;	int pctonly = 0;	int powerstatus = 0, powerbak = 0, powerchange = 0;	int messages = 0;	int noacsleep = 0;	struct timespec ts = {TIMO, 0}, sts = {0, 0};	const char *sockname = sockfile;	int kq;	struct kevent ev[2];	while ((ch = getopt(argc, argv, "qadsepmf:t:S:")) != -1)		switch(ch) {		case 'q':			speaker_ok = FALSE;			break;		case 'a':			noacsleep = 1;			break;		case 'd':			debug = 1;			break;		case 'f':			fname = optarg;			break;		case 'S':			sockname = optarg;			break;		case 't':			ts.tv_sec = strtoul(optarg, NULL, 0);			if (ts.tv_sec == 0)				usage();			break;		case 's':	/* status only */			statonly = 1;			break;		case 'e':			enableonly = 1;			break;		case 'p':			pctonly = 1;			break;		case 'm':			messages = 1;			break;		case '?':		default:			usage();		}	argc -= optind;	argv += optind;	if ((ctl_fd = open(fname, O_RDWR)) == -1)		err(1, "cannot open device file `%s'", fname);	sock_fd = bind_socket(sockname);	if (debug)		openlog(__progname, LOG_CONS, LOG_LOCAL1);	else {		openlog(__progname, LOG_CONS, LOG_DAEMON);		setlogmask(LOG_UPTO(LOG_NOTICE));		daemon(0, 0);	}	(void) signal(SIGTERM, sigexit);	(void) signal(SIGHUP, sigexit);	(void) signal(SIGINT, sigexit);	power_status(ctl_fd, 1, 0);	if (statonly)		exit(0);	if (enableonly) {		set_driver_messages(ctl_fd, APM_PRINT_ON);		exit(0);	}	if (pctonly) {		set_driver_messages(ctl_fd, APM_PRINT_PCT);		exit(0);	}	if (!messages)		set_driver_messages(ctl_fd, APM_PRINT_OFF);	kq = kqueue();	if (kq <= 0) {		syslog(LOG_ERR, "kqueue: %m");		exit(1);	}	EV_SET(&ev[0], ctl_fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,	    0, 0, NULL);	EV_SET(&ev[1], sock_fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,	    0, 0, NULL);	if (kevent(kq, ev, 2, NULL, 0, &sts) < 0) {		syslog(LOG_ERR, "kevent: %m");		exit(1);	}	for (;;) {		int rv;		sts = ts;		if ((rv = kevent(kq, NULL, 0, ev, 1, &sts)) < 0)			break;		/* wakeup for timeout: take status */		powerbak = power_status(ctl_fd, 0, 0);		if (powerstatus != powerbak) {			powerstatus = powerbak;			powerchange = 1;		}		if (!rv)			continue;		if (ev->ident == ctl_fd) {			suspends = standbys = resumes = 0;			syslog(LOG_DEBUG, "apmevent %04x index %d",			    APM_EVENT_TYPE(ev->data),			    APM_EVENT_INDEX(ev->data));			switch (APM_EVENT_TYPE(ev->data)) {			case APM_SUSPEND_REQ:			case APM_USER_SUSPEND_REQ:			case APM_CRIT_SUSPEND_REQ:			case APM_BATTERY_LOW:				suspends++;				break;			case APM_USER_STANDBY_REQ:			case APM_STANDBY_REQ:				standbys++;				break;#if 0			case APM_CANCEL:				suspends = standbys = 0;				break;#endif			case APM_NORMAL_RESUME:			case APM_CRIT_RESUME:			case APM_SYS_STANDBY_RESUME:				powerbak = power_status(ctl_fd, 0, 0);				if (powerstatus != powerbak) {					powerstatus = powerbak;					powerchange = 1;				}				resumes++;				break;			case APM_POWER_CHANGE:				powerbak = power_status(ctl_fd, 0, 0);				if (powerstatus != powerbak) {					powerstatus = powerbak;					powerchange = 1;				}				break;			default:			}			if ((standbys || suspends) && noacsleep &&			    power_status(ctl_fd, 0, 0))				syslog(LOG_DEBUG, "no! sleep! till brooklyn!");			else if (suspends)				suspend(ctl_fd);			else if (standbys)				stand_by(ctl_fd);			else if (resumes) {				do_etc_file(_PATH_APM_ETC_RESUME);				syslog(LOG_NOTICE,				    "system resumed from APM sleep");			}			if (powerchange) {				if (powerstatus)					do_etc_file(_PATH_APM_ETC_POWERUP);				else					do_etc_file(_PATH_APM_ETC_POWERDOWN);				powerchange = 0;			}		} else if (ev->ident == sock_fd)			switch (handle_client(sock_fd, ctl_fd)) {			case NORMAL:				break;			case SUSPENDING:				suspend(ctl_fd);				break;			case STANDING_BY:				stand_by(ctl_fd);				break;			}	}	syslog(LOG_ERR, "kevent: %m");	return 1;}