xref: /freebsd-14.2/sbin/devd/devd.cc (revision 8334958a)
13054f218SWarner Losh /*-
2b1ee04b1SWarner Losh  * Copyright (c) 2002-2003 M. Warner Losh.
33054f218SWarner Losh  * All rights reserved.
43054f218SWarner Losh  *
53054f218SWarner Losh  * Redistribution and use in source and binary forms, with or without
63054f218SWarner Losh  * modification, are permitted provided that the following conditions
73054f218SWarner Losh  * are met:
83054f218SWarner Losh  * 1. Redistributions of source code must retain the above copyright
93054f218SWarner Losh  *    notice, this list of conditions and the following disclaimer.
103054f218SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
113054f218SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
123054f218SWarner Losh  *    documentation and/or other materials provided with the distribution.
133054f218SWarner Losh  *
143054f218SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
153054f218SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163054f218SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
173054f218SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
183054f218SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
193054f218SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203054f218SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
213054f218SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223054f218SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233054f218SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243054f218SWarner Losh  * SUCH DAMAGE.
253054f218SWarner Losh  */
263054f218SWarner Losh 
273054f218SWarner Losh /*
283054f218SWarner Losh  * DEVD control daemon.
293054f218SWarner Losh  */
303054f218SWarner Losh 
313054f218SWarner Losh // TODO list:
323054f218SWarner Losh //	o devd.conf and devd man pages need a lot of help:
33251bedd1SWarner Losh //	  - devd needs to document the unix domain socket
343054f218SWarner Losh //	  - devd.conf needs more details on the supported statements.
353054f218SWarner Losh 
363054f218SWarner Losh #include <sys/cdefs.h>
373054f218SWarner Losh __FBSDID("$FreeBSD$");
383054f218SWarner Losh 
393054f218SWarner Losh #include <sys/param.h>
40251bedd1SWarner Losh #include <sys/socket.h>
41251bedd1SWarner Losh #include <sys/stat.h>
429c180898SWarner Losh #include <sys/sysctl.h>
43251bedd1SWarner Losh #include <sys/types.h>
44251bedd1SWarner Losh #include <sys/un.h>
453054f218SWarner Losh 
465102ef84SWarner Losh #include <ctype.h>
473054f218SWarner Losh #include <dirent.h>
483054f218SWarner Losh #include <errno.h>
493054f218SWarner Losh #include <err.h>
503054f218SWarner Losh #include <fcntl.h>
515102ef84SWarner Losh #include <regex.h>
52f32a7686SWarner Losh #include <signal.h>
533054f218SWarner Losh #include <stdlib.h>
543054f218SWarner Losh #include <stdio.h>
553054f218SWarner Losh #include <string.h>
563054f218SWarner Losh #include <unistd.h>
573054f218SWarner Losh 
588b823408SWarner Losh #include <algorithm>
593054f218SWarner Losh #include <map>
603054f218SWarner Losh #include <string>
61251bedd1SWarner Losh #include <list>
623054f218SWarner Losh #include <vector>
633054f218SWarner Losh 
646aeeca8eSWarner Losh #include "devd.h"		/* C compatible definitions */
656aeeca8eSWarner Losh #include "devd.hh"		/* C++ class definitions */
663054f218SWarner Losh 
67251bedd1SWarner Losh #define PIPE "/var/run/devd.pipe"
683054f218SWarner Losh #define CF "/etc/devd.conf"
699c180898SWarner Losh #define SYSCTL "hw.bus.devctl_disable"
703054f218SWarner Losh 
713054f218SWarner Losh using namespace std;
723054f218SWarner Losh 
733054f218SWarner Losh extern FILE *yyin;
743054f218SWarner Losh extern int lineno;
753054f218SWarner Losh 
76842ccec5SWarner Losh static const char notify = '!';
778b823408SWarner Losh static const char nomatch = '?';
788b823408SWarner Losh static const char attach = '+';
798b823408SWarner Losh static const char detach = '-';
808b823408SWarner Losh 
815a882d77SWarner Losh int Dflag;
823054f218SWarner Losh int dflag;
83812f53a7SWarner Losh int nflag;
843054f218SWarner Losh int romeo_must_die = 0;
853054f218SWarner Losh 
86*8334958aSJoseph Koshy static const char *configfile = CF;
87*8334958aSJoseph Koshy 
883054f218SWarner Losh static void event_loop(void);
893054f218SWarner Losh static void usage(void);
903054f218SWarner Losh 
918b823408SWarner Losh template <class T> void
928b823408SWarner Losh delete_and_clear(vector<T *> &v)
938b823408SWarner Losh {
948b823408SWarner Losh 	typename vector<T *>::const_iterator i;
953054f218SWarner Losh 
968b823408SWarner Losh 	for (i = v.begin(); i != v.end(); i++)
978b823408SWarner Losh 		delete *i;
988b823408SWarner Losh 	v.clear();
998b823408SWarner Losh }
1008b823408SWarner Losh 
1013054f218SWarner Losh config cfg;
1023054f218SWarner Losh 
1033054f218SWarner Losh event_proc::event_proc() : _prio(-1)
1043054f218SWarner Losh {
1053054f218SWarner Losh 	// nothing
1063054f218SWarner Losh }
1073054f218SWarner Losh 
1083054f218SWarner Losh event_proc::~event_proc()
1093054f218SWarner Losh {
11013f8bb71SAlexander Nedotsukov 	delete_and_clear(_epsvec);
1113054f218SWarner Losh }
1123054f218SWarner Losh 
1133054f218SWarner Losh void
1143054f218SWarner Losh event_proc::add(eps *eps)
1153054f218SWarner Losh {
1163054f218SWarner Losh 	_epsvec.push_back(eps);
1173054f218SWarner Losh }
1183054f218SWarner Losh 
1193054f218SWarner Losh bool
1203054f218SWarner Losh event_proc::matches(config &c)
1213054f218SWarner Losh {
1223054f218SWarner Losh 	vector<eps *>::const_iterator i;
1233054f218SWarner Losh 
1243054f218SWarner Losh 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
1253054f218SWarner Losh 		if (!(*i)->do_match(c))
1263054f218SWarner Losh 			return (false);
1273054f218SWarner Losh 	return (true);
1283054f218SWarner Losh }
1293054f218SWarner Losh 
1303054f218SWarner Losh bool
1313054f218SWarner Losh event_proc::run(config &c)
1323054f218SWarner Losh {
1333054f218SWarner Losh 	vector<eps *>::const_iterator i;
1343054f218SWarner Losh 
1353054f218SWarner Losh 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
1363054f218SWarner Losh 		if (!(*i)->do_action(c))
1373054f218SWarner Losh 			return (false);
1383054f218SWarner Losh 	return (true);
1393054f218SWarner Losh }
1403054f218SWarner Losh 
1413054f218SWarner Losh action::action(const char *cmd)
1423054f218SWarner Losh 	: _cmd(cmd)
1433054f218SWarner Losh {
1443054f218SWarner Losh 	// nothing
1453054f218SWarner Losh }
1463054f218SWarner Losh 
1473054f218SWarner Losh action::~action()
1483054f218SWarner Losh {
1493054f218SWarner Losh 	// nothing
1503054f218SWarner Losh }
1513054f218SWarner Losh 
1523054f218SWarner Losh bool
1535102ef84SWarner Losh action::do_action(config &c)
1543054f218SWarner Losh {
1558b823408SWarner Losh 	string s = c.expand_string(_cmd);
1565a882d77SWarner Losh 	if (Dflag)
1578b823408SWarner Losh 		fprintf(stderr, "Executing '%s'\n", s.c_str());
1588b823408SWarner Losh 	::system(s.c_str());
1593054f218SWarner Losh 	return (true);
1603054f218SWarner Losh }
1613054f218SWarner Losh 
1625102ef84SWarner Losh match::match(config &c, const char *var, const char *re)
1638b823408SWarner Losh 	: _var(var)
1643054f218SWarner Losh {
1658b823408SWarner Losh 	string pattern = re;
1668b823408SWarner Losh 	_re = "^";
1678b823408SWarner Losh 	_re.append(c.expand_string(string(re)));
1688b823408SWarner Losh 	_re.append("$");
1698b823408SWarner Losh 	regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB);
1703054f218SWarner Losh }
1713054f218SWarner Losh 
1723054f218SWarner Losh match::~match()
1733054f218SWarner Losh {
1745102ef84SWarner Losh 	regfree(&_regex);
1753054f218SWarner Losh }
1763054f218SWarner Losh 
1773054f218SWarner Losh bool
1785102ef84SWarner Losh match::do_match(config &c)
1793054f218SWarner Losh {
1805102ef84SWarner Losh 	string value = c.get_variable(_var);
1815102ef84SWarner Losh 	bool retval;
1825102ef84SWarner Losh 
1835a882d77SWarner Losh 	if (Dflag)
1848b823408SWarner Losh 		fprintf(stderr, "Testing %s=%s against %s\n", _var.c_str(),
1858b823408SWarner Losh 		    value.c_str(), _re.c_str());
1868b823408SWarner Losh 
1875102ef84SWarner Losh 	retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
1885102ef84SWarner Losh 	return retval;
1893054f218SWarner Losh }
1903054f218SWarner Losh 
191cd70782bSWarner Losh #include <sys/sockio.h>
192cd70782bSWarner Losh #include <net/if.h>
193cd70782bSWarner Losh #include <net/if_media.h>
194cd70782bSWarner Losh 
19594d2d4ebSBrooks Davis media::media(config &, const char *var, const char *type)
196cd70782bSWarner Losh 	: _var(var), _type(-1)
197cd70782bSWarner Losh {
198cd70782bSWarner Losh 	static struct ifmedia_description media_types[] = {
199cd70782bSWarner Losh 		{ IFM_ETHER,		"Ethernet" },
200cd70782bSWarner Losh 		{ IFM_TOKEN,		"Tokenring" },
201cd70782bSWarner Losh 		{ IFM_FDDI,		"FDDI" },
202cd70782bSWarner Losh 		{ IFM_IEEE80211,	"802.11" },
203cd70782bSWarner Losh 		{ IFM_ATM,		"ATM" },
204cd70782bSWarner Losh 		{ IFM_CARP,		"CARP" },
205cd70782bSWarner Losh 		{ -1,			"unknown" },
206cd70782bSWarner Losh 		{ 0, NULL },
207cd70782bSWarner Losh 	};
208cd70782bSWarner Losh 	for (int i = 0; media_types[i].ifmt_string != NULL; i++)
209cd70782bSWarner Losh 		if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
210cd70782bSWarner Losh 			_type = media_types[i].ifmt_word;
211cd70782bSWarner Losh 			break;
212cd70782bSWarner Losh 		}
213cd70782bSWarner Losh }
214cd70782bSWarner Losh 
215cd70782bSWarner Losh media::~media()
216cd70782bSWarner Losh {
217cd70782bSWarner Losh }
218cd70782bSWarner Losh 
219cd70782bSWarner Losh bool
220cd70782bSWarner Losh media::do_match(config &c)
221cd70782bSWarner Losh {
22209c47e76SWarner Losh 	string value;
223cd70782bSWarner Losh 	struct ifmediareq ifmr;
224cd70782bSWarner Losh 	bool retval;
225cd70782bSWarner Losh 	int s;
226cd70782bSWarner Losh 
22709c47e76SWarner Losh 	// Since we can be called from both a device attach/detach
22809c47e76SWarner Losh 	// context where device-name is defined and what we want,
22909c47e76SWarner Losh 	// as well as from a link status context, where subsystem is
23009c47e76SWarner Losh 	// the name of interest, first try device-name and fall back
23109c47e76SWarner Losh 	// to subsystem if none exists.
23209c47e76SWarner Losh 	value = c.get_variable("device-name");
23309c47e76SWarner Losh 	if (value.length() == 0)
234de02eec8SWarner Losh 		value = c.get_variable("subsystem");
235cd70782bSWarner Losh 	if (Dflag)
236cd70782bSWarner Losh 		fprintf(stderr, "Testing media type of %s against 0x%x\n",
237cd70782bSWarner Losh 		    value.c_str(), _type);
238cd70782bSWarner Losh 
239cd70782bSWarner Losh 	retval = false;
240cd70782bSWarner Losh 
241cd70782bSWarner Losh 	s = socket(PF_INET, SOCK_DGRAM, 0);
242cd70782bSWarner Losh 	if (s >= 0) {
243cd70782bSWarner Losh 		memset(&ifmr, 0, sizeof(ifmr));
244cd70782bSWarner Losh 		strncpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
245cd70782bSWarner Losh 
246cd70782bSWarner Losh 		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
247cd70782bSWarner Losh 		    ifmr.ifm_status & IFM_AVALID) {
248cd70782bSWarner Losh 			if (Dflag)
249cd70782bSWarner Losh 				fprintf(stderr, "%s has media type 0x%x\n",
250cd70782bSWarner Losh 				    value.c_str(), IFM_TYPE(ifmr.ifm_active));
251cd70782bSWarner Losh 			retval = (IFM_TYPE(ifmr.ifm_active) == _type);
252cd70782bSWarner Losh 		} else if (_type == -1) {
253cd70782bSWarner Losh 			if (Dflag)
254cd70782bSWarner Losh 				fprintf(stderr, "%s has unknown media type\n",
255cd70782bSWarner Losh 				    value.c_str());
256cd70782bSWarner Losh 			retval = true;
257cd70782bSWarner Losh 		}
258cd70782bSWarner Losh 		close(s);
259cd70782bSWarner Losh 	}
260cd70782bSWarner Losh 
261cd70782bSWarner Losh 	return retval;
262cd70782bSWarner Losh }
263cd70782bSWarner Losh 
2643054f218SWarner Losh const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
2653054f218SWarner Losh const string var_list::nothing = "";
2663054f218SWarner Losh 
2673054f218SWarner Losh const string &
2683054f218SWarner Losh var_list::get_variable(const string &var) const
2693054f218SWarner Losh {
2703054f218SWarner Losh 	map<string, string>::const_iterator i;
2713054f218SWarner Losh 
2723054f218SWarner Losh 	i = _vars.find(var);
2733054f218SWarner Losh 	if (i == _vars.end())
2748b823408SWarner Losh 		return (var_list::bogus);
2753054f218SWarner Losh 	return (i->second);
2763054f218SWarner Losh }
2773054f218SWarner Losh 
2783054f218SWarner Losh bool
2793054f218SWarner Losh var_list::is_set(const string &var) const
2803054f218SWarner Losh {
2813054f218SWarner Losh 	return (_vars.find(var) != _vars.end());
2823054f218SWarner Losh }
2833054f218SWarner Losh 
2843054f218SWarner Losh void
2853054f218SWarner Losh var_list::set_variable(const string &var, const string &val)
2863054f218SWarner Losh {
2875a882d77SWarner Losh 	if (Dflag)
288528d3f79SWarner Losh 		fprintf(stderr, "setting %s=%s\n", var.c_str(), val.c_str());
2893054f218SWarner Losh 	_vars[var] = val;
2903054f218SWarner Losh }
2913054f218SWarner Losh 
2923054f218SWarner Losh void
2933054f218SWarner Losh config::reset(void)
2943054f218SWarner Losh {
2953054f218SWarner Losh 	_dir_list.clear();
2968b823408SWarner Losh 	delete_and_clear(_var_list_table);
2978b823408SWarner Losh 	delete_and_clear(_attach_list);
2988b823408SWarner Losh 	delete_and_clear(_detach_list);
2998b823408SWarner Losh 	delete_and_clear(_nomatch_list);
300842ccec5SWarner Losh 	delete_and_clear(_notify_list);
3013054f218SWarner Losh }
3023054f218SWarner Losh 
3033054f218SWarner Losh void
3043054f218SWarner Losh config::parse_one_file(const char *fn)
3053054f218SWarner Losh {
3065a882d77SWarner Losh 	if (Dflag)
3073054f218SWarner Losh 		printf("Parsing %s\n", fn);
3083054f218SWarner Losh 	yyin = fopen(fn, "r");
3093054f218SWarner Losh 	if (yyin == NULL)
3103054f218SWarner Losh 		err(1, "Cannot open config file %s", fn);
3113054f218SWarner Losh 	if (yyparse() != 0)
3123054f218SWarner Losh 		errx(1, "Cannot parse %s at line %d", fn, lineno);
3133054f218SWarner Losh 	fclose(yyin);
3143054f218SWarner Losh }
3153054f218SWarner Losh 
3163054f218SWarner Losh void
3173054f218SWarner Losh config::parse_files_in_dir(const char *dirname)
3183054f218SWarner Losh {
3193054f218SWarner Losh 	DIR *dirp;
3203054f218SWarner Losh 	struct dirent *dp;
3213054f218SWarner Losh 	char path[PATH_MAX];
3223054f218SWarner Losh 
3235a882d77SWarner Losh 	if (Dflag)
3243054f218SWarner Losh 		printf("Parsing files in %s\n", dirname);
3253054f218SWarner Losh 	dirp = opendir(dirname);
3263054f218SWarner Losh 	if (dirp == NULL)
3273054f218SWarner Losh 		return;
3283054f218SWarner Losh 	readdir(dirp);		/* Skip . */
3293054f218SWarner Losh 	readdir(dirp);		/* Skip .. */
3303054f218SWarner Losh 	while ((dp = readdir(dirp)) != NULL) {
3313054f218SWarner Losh 		if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
3323054f218SWarner Losh 			snprintf(path, sizeof(path), "%s/%s",
3333054f218SWarner Losh 			    dirname, dp->d_name);
3343054f218SWarner Losh 			parse_one_file(path);
3353054f218SWarner Losh 		}
3363054f218SWarner Losh 	}
3373054f218SWarner Losh }
3383054f218SWarner Losh 
3398b823408SWarner Losh class epv_greater {
3408b823408SWarner Losh public:
3418b823408SWarner Losh 	int operator()(event_proc *const&l1, event_proc *const&l2)
3428b823408SWarner Losh 	{
3438b823408SWarner Losh 		return (l1->get_priority() > l2->get_priority());
3448b823408SWarner Losh 	}
3458b823408SWarner Losh };
3468b823408SWarner Losh 
3478b823408SWarner Losh void
3488b823408SWarner Losh config::sort_vector(vector<event_proc *> &v)
3498b823408SWarner Losh {
3508b823408SWarner Losh 	sort(v.begin(), v.end(), epv_greater());
3518b823408SWarner Losh }
3528b823408SWarner Losh 
3533054f218SWarner Losh void
3543054f218SWarner Losh config::parse(void)
3553054f218SWarner Losh {
3563054f218SWarner Losh 	vector<string>::const_iterator i;
3573054f218SWarner Losh 
358*8334958aSJoseph Koshy 	parse_one_file(configfile);
3593054f218SWarner Losh 	for (i = _dir_list.begin(); i != _dir_list.end(); i++)
3603054f218SWarner Losh 		parse_files_in_dir((*i).c_str());
3618b823408SWarner Losh 	sort_vector(_attach_list);
3628b823408SWarner Losh 	sort_vector(_detach_list);
3638b823408SWarner Losh 	sort_vector(_nomatch_list);
364842ccec5SWarner Losh 	sort_vector(_notify_list);
3653054f218SWarner Losh }
3663054f218SWarner Losh 
3673054f218SWarner Losh void
3683054f218SWarner Losh config::drop_pidfile()
3693054f218SWarner Losh {
3703054f218SWarner Losh 	FILE *fp;
3713054f218SWarner Losh 
3723054f218SWarner Losh 	if (_pidfile == "")
3733054f218SWarner Losh 		return;
3743054f218SWarner Losh 	fp = fopen(_pidfile.c_str(), "w");
3753054f218SWarner Losh 	if (fp == NULL)
3763054f218SWarner Losh 		return;
3773054f218SWarner Losh 	fprintf(fp, "%d\n", getpid());
3783054f218SWarner Losh 	fclose(fp);
3793054f218SWarner Losh }
3803054f218SWarner Losh 
3813054f218SWarner Losh void
3823054f218SWarner Losh config::add_attach(int prio, event_proc *p)
3833054f218SWarner Losh {
3843054f218SWarner Losh 	p->set_priority(prio);
3853054f218SWarner Losh 	_attach_list.push_back(p);
3863054f218SWarner Losh }
3873054f218SWarner Losh 
3883054f218SWarner Losh void
3893054f218SWarner Losh config::add_detach(int prio, event_proc *p)
3903054f218SWarner Losh {
3913054f218SWarner Losh 	p->set_priority(prio);
3923054f218SWarner Losh 	_detach_list.push_back(p);
3933054f218SWarner Losh }
3943054f218SWarner Losh 
3953054f218SWarner Losh void
3963054f218SWarner Losh config::add_directory(const char *dir)
3973054f218SWarner Losh {
3983054f218SWarner Losh 	_dir_list.push_back(string(dir));
3993054f218SWarner Losh }
4003054f218SWarner Losh 
4013054f218SWarner Losh void
4023054f218SWarner Losh config::add_nomatch(int prio, event_proc *p)
4033054f218SWarner Losh {
4043054f218SWarner Losh 	p->set_priority(prio);
4053054f218SWarner Losh 	_nomatch_list.push_back(p);
4063054f218SWarner Losh }
4073054f218SWarner Losh 
4083054f218SWarner Losh void
409842ccec5SWarner Losh config::add_notify(int prio, event_proc *p)
410842ccec5SWarner Losh {
411842ccec5SWarner Losh 	p->set_priority(prio);
412842ccec5SWarner Losh 	_notify_list.push_back(p);
413842ccec5SWarner Losh }
414842ccec5SWarner Losh 
415842ccec5SWarner Losh void
4163054f218SWarner Losh config::set_pidfile(const char *fn)
4173054f218SWarner Losh {
4183054f218SWarner Losh 	_pidfile = string(fn);
4193054f218SWarner Losh }
4203054f218SWarner Losh 
4213054f218SWarner Losh void
4223054f218SWarner Losh config::push_var_table()
4233054f218SWarner Losh {
4243054f218SWarner Losh 	var_list *vl;
4253054f218SWarner Losh 
4263054f218SWarner Losh 	vl = new var_list();
4273054f218SWarner Losh 	_var_list_table.push_back(vl);
4285a882d77SWarner Losh 	if (Dflag)
4298b823408SWarner Losh 		fprintf(stderr, "Pushing table\n");
4303054f218SWarner Losh }
4313054f218SWarner Losh 
4323054f218SWarner Losh void
4333054f218SWarner Losh config::pop_var_table()
4343054f218SWarner Losh {
4353054f218SWarner Losh 	delete _var_list_table.back();
4363054f218SWarner Losh 	_var_list_table.pop_back();
4375a882d77SWarner Losh 	if (Dflag)
4388b823408SWarner Losh 		fprintf(stderr, "Popping table\n");
4393054f218SWarner Losh }
4403054f218SWarner Losh 
4413054f218SWarner Losh void
4423054f218SWarner Losh config::set_variable(const char *var, const char *val)
4433054f218SWarner Losh {
4443054f218SWarner Losh 	_var_list_table.back()->set_variable(var, val);
4453054f218SWarner Losh }
4463054f218SWarner Losh 
4473054f218SWarner Losh const string &
4483054f218SWarner Losh config::get_variable(const string &var)
4493054f218SWarner Losh {
4503054f218SWarner Losh 	vector<var_list *>::reverse_iterator i;
4513054f218SWarner Losh 
4523054f218SWarner Losh 	for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); i++) {
4533054f218SWarner Losh 		if ((*i)->is_set(var))
4548b823408SWarner Losh 			return ((*i)->get_variable(var));
4553054f218SWarner Losh 	}
4563054f218SWarner Losh 	return (var_list::nothing);
4573054f218SWarner Losh }
4583054f218SWarner Losh 
4598b823408SWarner Losh bool
4608b823408SWarner Losh config::is_id_char(char ch)
4618b823408SWarner Losh {
4628b823408SWarner Losh 	return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
4638b823408SWarner Losh 	    ch == '-'));
4648b823408SWarner Losh }
4658b823408SWarner Losh 
4665102ef84SWarner Losh void
4676cabd675SWarner Losh config::expand_one(const char *&src, string &dst)
4683054f218SWarner Losh {
4695102ef84SWarner Losh 	int count;
4706cabd675SWarner Losh 	string buffer, varstr;
4715102ef84SWarner Losh 
4728b823408SWarner Losh 	src++;
4735102ef84SWarner Losh 	// $$ -> $
4745102ef84SWarner Losh 	if (*src == '$') {
4756cabd675SWarner Losh 		dst.append(src++, 1);
4765102ef84SWarner Losh 		return;
4775102ef84SWarner Losh 	}
4785102ef84SWarner Losh 
4795102ef84SWarner Losh 	// $(foo) -> $(foo)
4808b823408SWarner Losh 	// Not sure if I want to support this or not, so for now we just pass
4818b823408SWarner Losh 	// it through.
4825102ef84SWarner Losh 	if (*src == '(') {
4836cabd675SWarner Losh 		dst.append("$");
4845102ef84SWarner Losh 		count = 1;
4856cabd675SWarner Losh 		/* If the string ends before ) is matched , return. */
4866cabd675SWarner Losh 		while (count > 0 && *src) {
4875102ef84SWarner Losh 			if (*src == ')')
4885102ef84SWarner Losh 				count--;
4895102ef84SWarner Losh 			else if (*src == '(')
4905102ef84SWarner Losh 				count++;
4916cabd675SWarner Losh 			dst.append(src++, 1);
4925102ef84SWarner Losh 		}
4935102ef84SWarner Losh 		return;
4945102ef84SWarner Losh 	}
4955102ef84SWarner Losh 
4965102ef84SWarner Losh 	// ${^A-Za-z] -> $\1
4975102ef84SWarner Losh 	if (!isalpha(*src)) {
4986cabd675SWarner Losh 		dst.append("$");
4996cabd675SWarner Losh 		dst.append(src++, 1);
5005102ef84SWarner Losh 		return;
5015102ef84SWarner Losh 	}
5025102ef84SWarner Losh 
5035102ef84SWarner Losh 	// $var -> replace with value
5046cabd675SWarner Losh 	do {
5056cabd675SWarner Losh 		buffer.append(src++, 1);
50666ff6821SWarner Losh 	} while (is_id_char(*src));
5076cabd675SWarner Losh 	buffer.append("", 1);
5086cabd675SWarner Losh 	varstr = get_variable(buffer.c_str());
5096cabd675SWarner Losh 	dst.append(varstr);
5105102ef84SWarner Losh }
5115102ef84SWarner Losh 
5125102ef84SWarner Losh const string
5135102ef84SWarner Losh config::expand_string(const string &s)
5145102ef84SWarner Losh {
5155102ef84SWarner Losh 	const char *src;
5166cabd675SWarner Losh 	string dst;
5175102ef84SWarner Losh 
5185102ef84SWarner Losh 	src = s.c_str();
5195102ef84SWarner Losh 	while (*src) {
5205102ef84SWarner Losh 		if (*src == '$')
5216cabd675SWarner Losh 			expand_one(src, dst);
5225102ef84SWarner Losh 		else
5236cabd675SWarner Losh 			dst.append(src++, 1);
5245102ef84SWarner Losh 	}
5256cabd675SWarner Losh 	dst.append("", 1);
5265102ef84SWarner Losh 
5276cabd675SWarner Losh 	return (dst);
5283054f218SWarner Losh }
5293054f218SWarner Losh 
5308b823408SWarner Losh bool
5318b823408SWarner Losh config::chop_var(char *&buffer, char *&lhs, char *&rhs)
5328b823408SWarner Losh {
5338b823408SWarner Losh 	char *walker;
5348b823408SWarner Losh 
5358b823408SWarner Losh 	if (*buffer == '\0')
5368b823408SWarner Losh 		return (false);
5378b823408SWarner Losh 	walker = lhs = buffer;
5388b823408SWarner Losh 	while (is_id_char(*walker))
5398b823408SWarner Losh 		walker++;
5408b823408SWarner Losh 	if (*walker != '=')
5418b823408SWarner Losh 		return (false);
5428b823408SWarner Losh 	walker++;		// skip =
5438b823408SWarner Losh 	if (*walker == '"') {
5448b823408SWarner Losh 		walker++;	// skip "
5458b823408SWarner Losh 		rhs = walker;
5468b823408SWarner Losh 		while (*walker && *walker != '"')
5478b823408SWarner Losh 			walker++;
5488b823408SWarner Losh 		if (*walker != '"')
5498b823408SWarner Losh 			return (false);
5508b823408SWarner Losh 		rhs[-2] = '\0';
5518b823408SWarner Losh 		*walker++ = '\0';
5528b823408SWarner Losh 	} else {
5538b823408SWarner Losh 		rhs = walker;
5548b823408SWarner Losh 		while (*walker && !isspace(*walker))
5558b823408SWarner Losh 			walker++;
5568b823408SWarner Losh 		if (*walker != '\0')
5578b823408SWarner Losh 			*walker++ = '\0';
5588b823408SWarner Losh 		rhs[-1] = '\0';
5598b823408SWarner Losh 	}
560b1ee04b1SWarner Losh 	while (isspace(*walker))
561b1ee04b1SWarner Losh 		walker++;
5628b823408SWarner Losh 	buffer = walker;
5638b823408SWarner Losh 	return (true);
5648b823408SWarner Losh }
5658b823408SWarner Losh 
5668b823408SWarner Losh 
5678b823408SWarner Losh char *
5688b823408SWarner Losh config::set_vars(char *buffer)
5698b823408SWarner Losh {
5708b823408SWarner Losh 	char *lhs;
5718b823408SWarner Losh 	char *rhs;
5728b823408SWarner Losh 
5738b823408SWarner Losh 	while (1) {
5748b823408SWarner Losh 		if (!chop_var(buffer, lhs, rhs))
5758b823408SWarner Losh 			break;
5768b823408SWarner Losh 		set_variable(lhs, rhs);
5778b823408SWarner Losh 	}
5788b823408SWarner Losh 	return (buffer);
5798b823408SWarner Losh }
5808b823408SWarner Losh 
5818b823408SWarner Losh void
5828b823408SWarner Losh config::find_and_execute(char type)
5838b823408SWarner Losh {
5848b823408SWarner Losh 	vector<event_proc *> *l;
5858b823408SWarner Losh 	vector<event_proc *>::const_iterator i;
58694d2d4ebSBrooks Davis 	const char *s;
5878b823408SWarner Losh 
5888b823408SWarner Losh 	switch (type) {
5898b823408SWarner Losh 	default:
5908b823408SWarner Losh 		return;
591842ccec5SWarner Losh 	case notify:
592842ccec5SWarner Losh 		l = &_notify_list;
593842ccec5SWarner Losh 		s = "notify";
594842ccec5SWarner Losh 		break;
5958b823408SWarner Losh 	case nomatch:
5968b823408SWarner Losh 		l = &_nomatch_list;
5978b823408SWarner Losh 		s = "nomatch";
5988b823408SWarner Losh 		break;
5998b823408SWarner Losh 	case attach:
6008b823408SWarner Losh 		l = &_attach_list;
6018b823408SWarner Losh 		s = "attach";
6028b823408SWarner Losh 		break;
6038b823408SWarner Losh 	case detach:
6048b823408SWarner Losh 		l = &_detach_list;
6058b823408SWarner Losh 		s = "detach";
6068b823408SWarner Losh 		break;
6078b823408SWarner Losh 	}
6085a882d77SWarner Losh 	if (Dflag)
6098b823408SWarner Losh 		fprintf(stderr, "Processing %s event\n", s);
6108b823408SWarner Losh 	for (i = l->begin(); i != l->end(); i++) {
6118b823408SWarner Losh 		if ((*i)->matches(*this)) {
6128b823408SWarner Losh 			(*i)->run(*this);
6138b823408SWarner Losh 			break;
6148b823408SWarner Losh 		}
6158b823408SWarner Losh 	}
6168b823408SWarner Losh 
6178b823408SWarner Losh }
6188b823408SWarner Losh 
6193054f218SWarner Losh 
6203054f218SWarner Losh static void
6218b823408SWarner Losh process_event(char *buffer)
6223054f218SWarner Losh {
6233054f218SWarner Losh 	char type;
6243054f218SWarner Losh 	char *sp;
6253054f218SWarner Losh 
6268b823408SWarner Losh 	sp = buffer + 1;
6275a882d77SWarner Losh 	if (Dflag)
6288b823408SWarner Losh 		fprintf(stderr, "Processing event '%s'\n", buffer);
6293054f218SWarner Losh 	type = *buffer++;
6308b823408SWarner Losh 	cfg.push_var_table();
6318b823408SWarner Losh 	// No match doesn't have a device, and the format is a little
6328b823408SWarner Losh 	// different, so handle it separately.
633842ccec5SWarner Losh 	switch (type) {
634842ccec5SWarner Losh 	case notify:
635842ccec5SWarner Losh 		sp = cfg.set_vars(sp);
636842ccec5SWarner Losh 		break;
637842ccec5SWarner Losh 	case nomatch:
638528d3f79SWarner Losh 		//? at location pnp-info on bus
639528d3f79SWarner Losh 		sp = strchr(sp, ' ');
640528d3f79SWarner Losh 		if (sp == NULL)
641528d3f79SWarner Losh 			return;	/* Can't happen? */
642528d3f79SWarner Losh 		*sp++ = '\0';
643842ccec5SWarner Losh 		if (strncmp(sp, "at ", 3) == 0)
644842ccec5SWarner Losh 			sp += 3;
645842ccec5SWarner Losh 		sp = cfg.set_vars(sp);
646842ccec5SWarner Losh 		if (strncmp(sp, "on ", 3) == 0)
647842ccec5SWarner Losh 			cfg.set_variable("bus", sp + 3);
648842ccec5SWarner Losh 		break;
649842ccec5SWarner Losh 	case attach:	/*FALLTHROUGH*/
650842ccec5SWarner Losh 	case detach:
6518b823408SWarner Losh 		sp = strchr(sp, ' ');
6523054f218SWarner Losh 		if (sp == NULL)
6533054f218SWarner Losh 			return;	/* Can't happen? */
6548b823408SWarner Losh 		*sp++ = '\0';
6558b823408SWarner Losh 		cfg.set_variable("device-name", buffer);
6568b823408SWarner Losh 		if (strncmp(sp, "at ", 3) == 0)
6578b823408SWarner Losh 			sp += 3;
6588b823408SWarner Losh 		sp = cfg.set_vars(sp);
6598b823408SWarner Losh 		if (strncmp(sp, "on ", 3) == 0)
6608b823408SWarner Losh 			cfg.set_variable("bus", sp + 3);
661842ccec5SWarner Losh 		break;
662b1ee04b1SWarner Losh 	}
663b1ee04b1SWarner Losh 
6648b823408SWarner Losh 	cfg.find_and_execute(type);
6658b823408SWarner Losh 	cfg.pop_var_table();
6663054f218SWarner Losh }
6673054f218SWarner Losh 
668251bedd1SWarner Losh int
669251bedd1SWarner Losh create_socket(const char *name)
670251bedd1SWarner Losh {
671251bedd1SWarner Losh 	int fd, slen;
672251bedd1SWarner Losh 	struct sockaddr_un sun;
673251bedd1SWarner Losh 
674251bedd1SWarner Losh 	if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
675251bedd1SWarner Losh 		err(1, "socket");
676251bedd1SWarner Losh 	bzero(&sun, sizeof(sun));
677251bedd1SWarner Losh 	sun.sun_family = AF_UNIX;
678251bedd1SWarner Losh 	strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
679251bedd1SWarner Losh 	slen = SUN_LEN(&sun);
680251bedd1SWarner Losh 	unlink(name);
681e60fa014SJoe Marcus Clarke 	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
682e60fa014SJoe Marcus Clarke 	    	err(1, "fcntl");
683251bedd1SWarner Losh 	if (bind(fd, (struct sockaddr *) & sun, slen) < 0)
684251bedd1SWarner Losh 		err(1, "bind");
685251bedd1SWarner Losh 	listen(fd, 4);
68683818929SJoe Marcus Clarke 	chown(name, 0, 0);	/* XXX - root.wheel */
687e60fa014SJoe Marcus Clarke 	chmod(name, 0666);
688251bedd1SWarner Losh 	return (fd);
689251bedd1SWarner Losh }
690251bedd1SWarner Losh 
691251bedd1SWarner Losh list<int> clients;
692251bedd1SWarner Losh 
693251bedd1SWarner Losh void
694251bedd1SWarner Losh notify_clients(const char *data, int len)
695251bedd1SWarner Losh {
696251bedd1SWarner Losh 	list<int> bad;
697251bedd1SWarner Losh 	list<int>::const_iterator i;
698251bedd1SWarner Losh 
699251bedd1SWarner Losh 	for (i = clients.begin(); i != clients.end(); i++) {
700251bedd1SWarner Losh 		if (write(*i, data, len) <= 0) {
701251bedd1SWarner Losh 			bad.push_back(*i);
702251bedd1SWarner Losh 			close(*i);
703251bedd1SWarner Losh 		}
704251bedd1SWarner Losh 	}
705251bedd1SWarner Losh 
706251bedd1SWarner Losh 	for (i = bad.begin(); i != bad.end(); i++)
707251bedd1SWarner Losh 		clients.erase(find(clients.begin(), clients.end(), *i));
708251bedd1SWarner Losh }
709251bedd1SWarner Losh 
710251bedd1SWarner Losh void
711251bedd1SWarner Losh new_client(int fd)
712251bedd1SWarner Losh {
713251bedd1SWarner Losh 	int s;
714251bedd1SWarner Losh 
715251bedd1SWarner Losh 	s = accept(fd, NULL, NULL);
716251bedd1SWarner Losh 	if (s != -1)
717251bedd1SWarner Losh 		clients.push_back(s);
718251bedd1SWarner Losh }
719251bedd1SWarner Losh 
7203054f218SWarner Losh static void
7213054f218SWarner Losh event_loop(void)
7223054f218SWarner Losh {
7233054f218SWarner Losh 	int rv;
7243054f218SWarner Losh 	int fd;
7253054f218SWarner Losh 	char buffer[DEVCTL_MAXBUF];
7265a882d77SWarner Losh 	int once = 0;
727251bedd1SWarner Losh 	int server_fd, max_fd;
7285a882d77SWarner Losh 	timeval tv;
7295a882d77SWarner Losh 	fd_set fds;
7303054f218SWarner Losh 
7313054f218SWarner Losh 	fd = open(PATH_DEVCTL, O_RDONLY);
7323054f218SWarner Losh 	if (fd == -1)
733251bedd1SWarner Losh 		err(1, "Can't open devctl device %s", PATH_DEVCTL);
7343054f218SWarner Losh 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
735251bedd1SWarner Losh 		err(1, "Can't set close-on-exec flag on devctl");
736251bedd1SWarner Losh 	server_fd = create_socket(PIPE);
737251bedd1SWarner Losh 	max_fd = max(fd, server_fd) + 1;
7383054f218SWarner Losh 	while (1) {
7393054f218SWarner Losh 		if (romeo_must_die)
7403054f218SWarner Losh 			break;
7415a882d77SWarner Losh 		if (!once && !dflag && !nflag) {
7425a882d77SWarner Losh 			// Check to see if we have any events pending.
7435a882d77SWarner Losh 			tv.tv_sec = 0;
7445a882d77SWarner Losh 			tv.tv_usec = 0;
7455a882d77SWarner Losh 			FD_ZERO(&fds);
7465a882d77SWarner Losh 			FD_SET(fd, &fds);
7475a882d77SWarner Losh 			rv = select(fd + 1, &fds, &fds, &fds, &tv);
7485a882d77SWarner Losh 			// No events -> we've processed all pending events
749f3059505SWarner Losh 			if (rv == 0) {
7505a882d77SWarner Losh 				if (Dflag)
7515a882d77SWarner Losh 					fprintf(stderr, "Calling daemon\n");
7525a882d77SWarner Losh 				daemon(0, 0);
753fd6a8f23SWarner Losh 				cfg.drop_pidfile();
7545a882d77SWarner Losh 				once++;
7555a882d77SWarner Losh 			}
7565a882d77SWarner Losh 		}
757251bedd1SWarner Losh 		FD_ZERO(&fds);
758251bedd1SWarner Losh 		FD_SET(fd, &fds);
759251bedd1SWarner Losh 		FD_SET(server_fd, &fds);
760251bedd1SWarner Losh 		rv = select(max_fd, &fds, NULL, NULL, NULL);
761251bedd1SWarner Losh 		if (rv == -1) {
762251bedd1SWarner Losh 			if (errno == EINTR)
763251bedd1SWarner Losh 				continue;
764251bedd1SWarner Losh 			err(1, "select");
765251bedd1SWarner Losh 		}
766251bedd1SWarner Losh 		if (FD_ISSET(fd, &fds)) {
7673054f218SWarner Losh 			rv = read(fd, buffer, sizeof(buffer) - 1);
7683054f218SWarner Losh 			if (rv > 0) {
769251bedd1SWarner Losh 				notify_clients(buffer, rv);
7703054f218SWarner Losh 				buffer[rv] = '\0';
7713054f218SWarner Losh 				while (buffer[--rv] == '\n')
7723054f218SWarner Losh 					buffer[rv] = '\0';
7733054f218SWarner Losh 				process_event(buffer);
7743054f218SWarner Losh 			} else if (rv < 0) {
7753054f218SWarner Losh 				if (errno != EINTR)
7763054f218SWarner Losh 					break;
7773054f218SWarner Losh 			} else {
7783054f218SWarner Losh 				/* EOF */
7793054f218SWarner Losh 				break;
7803054f218SWarner Losh 			}
7813054f218SWarner Losh 		}
782251bedd1SWarner Losh 		if (FD_ISSET(server_fd, &fds))
783251bedd1SWarner Losh 			new_client(server_fd);
784251bedd1SWarner Losh 	}
7853054f218SWarner Losh 	close(fd);
7863054f218SWarner Losh }
7873054f218SWarner Losh 
7883054f218SWarner Losh /*
7893054f218SWarner Losh  * functions that the parser uses.
7903054f218SWarner Losh  */
7913054f218SWarner Losh void
7923054f218SWarner Losh add_attach(int prio, event_proc *p)
7933054f218SWarner Losh {
7943054f218SWarner Losh 	cfg.add_attach(prio, p);
7953054f218SWarner Losh }
7963054f218SWarner Losh 
7973054f218SWarner Losh void
7983054f218SWarner Losh add_detach(int prio, event_proc *p)
7993054f218SWarner Losh {
8003054f218SWarner Losh 	cfg.add_detach(prio, p);
8013054f218SWarner Losh }
8023054f218SWarner Losh 
8033054f218SWarner Losh void
8043054f218SWarner Losh add_directory(const char *dir)
8053054f218SWarner Losh {
8063054f218SWarner Losh 	cfg.add_directory(dir);
8073054f218SWarner Losh 	free(const_cast<char *>(dir));
8083054f218SWarner Losh }
8093054f218SWarner Losh 
8103054f218SWarner Losh void
8113054f218SWarner Losh add_nomatch(int prio, event_proc *p)
8123054f218SWarner Losh {
8133054f218SWarner Losh 	cfg.add_nomatch(prio, p);
8143054f218SWarner Losh }
8153054f218SWarner Losh 
816842ccec5SWarner Losh void
817842ccec5SWarner Losh add_notify(int prio, event_proc *p)
818842ccec5SWarner Losh {
819842ccec5SWarner Losh 	cfg.add_notify(prio, p);
820842ccec5SWarner Losh }
821842ccec5SWarner Losh 
8223054f218SWarner Losh event_proc *
8233054f218SWarner Losh add_to_event_proc(event_proc *ep, eps *eps)
8243054f218SWarner Losh {
8253054f218SWarner Losh 	if (ep == NULL)
8263054f218SWarner Losh 		ep = new event_proc();
8273054f218SWarner Losh 	ep->add(eps);
8283054f218SWarner Losh 	return (ep);
8293054f218SWarner Losh }
8303054f218SWarner Losh 
8313054f218SWarner Losh eps *
8323054f218SWarner Losh new_action(const char *cmd)
8333054f218SWarner Losh {
8343054f218SWarner Losh 	eps *e = new action(cmd);
8353054f218SWarner Losh 	free(const_cast<char *>(cmd));
8363054f218SWarner Losh 	return (e);
8373054f218SWarner Losh }
8383054f218SWarner Losh 
8393054f218SWarner Losh eps *
8403054f218SWarner Losh new_match(const char *var, const char *re)
8413054f218SWarner Losh {
8425102ef84SWarner Losh 	eps *e = new match(cfg, var, re);
8433054f218SWarner Losh 	free(const_cast<char *>(var));
8443054f218SWarner Losh 	free(const_cast<char *>(re));
8453054f218SWarner Losh 	return (e);
8463054f218SWarner Losh }
8473054f218SWarner Losh 
848cd70782bSWarner Losh eps *
849cd70782bSWarner Losh new_media(const char *var, const char *re)
850cd70782bSWarner Losh {
851cd70782bSWarner Losh 	eps *e = new media(cfg, var, re);
852cd70782bSWarner Losh 	free(const_cast<char *>(var));
853cd70782bSWarner Losh 	free(const_cast<char *>(re));
854cd70782bSWarner Losh 	return (e);
855cd70782bSWarner Losh }
856cd70782bSWarner Losh 
8573054f218SWarner Losh void
8583054f218SWarner Losh set_pidfile(const char *name)
8593054f218SWarner Losh {
8603054f218SWarner Losh 	cfg.set_pidfile(name);
8613054f218SWarner Losh 	free(const_cast<char *>(name));
8623054f218SWarner Losh }
8633054f218SWarner Losh 
8643054f218SWarner Losh void
8653054f218SWarner Losh set_variable(const char *var, const char *val)
8663054f218SWarner Losh {
8673054f218SWarner Losh 	cfg.set_variable(var, val);
8683054f218SWarner Losh 	free(const_cast<char *>(var));
8693054f218SWarner Losh 	free(const_cast<char *>(val));
8703054f218SWarner Losh }
8713054f218SWarner Losh 
8723054f218SWarner Losh 
8733054f218SWarner Losh 
8743054f218SWarner Losh static void
8753054f218SWarner Losh gensighand(int)
8763054f218SWarner Losh {
8773054f218SWarner Losh 	romeo_must_die++;
8783054f218SWarner Losh 	_exit(0);
8793054f218SWarner Losh }
8803054f218SWarner Losh 
8813054f218SWarner Losh static void
8823054f218SWarner Losh usage()
8833054f218SWarner Losh {
8848d646af5SRuslan Ermilov 	fprintf(stderr, "usage: %s [-Ddn]\n", getprogname());
8853054f218SWarner Losh 	exit(1);
8863054f218SWarner Losh }
8873054f218SWarner Losh 
8889c180898SWarner Losh static void
8899c180898SWarner Losh check_devd_enabled()
8909c180898SWarner Losh {
8919c180898SWarner Losh 	int val = 0;
8929c180898SWarner Losh 	size_t len;
8939c180898SWarner Losh 
8949c180898SWarner Losh 	len = sizeof(val);
89579f92e25SWarner Losh 	if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
8969c180898SWarner Losh 		errx(1, "devctl sysctl missing from kernel!");
8979c180898SWarner Losh 	if (val) {
8989c180898SWarner Losh 		warnx("Setting " SYSCTL " to 0");
8999c180898SWarner Losh 		val = 0;
9009c180898SWarner Losh 		sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
9019c180898SWarner Losh 	}
9029c180898SWarner Losh }
9039c180898SWarner Losh 
9043054f218SWarner Losh /*
9053054f218SWarner Losh  * main
9063054f218SWarner Losh  */
9073054f218SWarner Losh int
9083054f218SWarner Losh main(int argc, char **argv)
9093054f218SWarner Losh {
9103054f218SWarner Losh 	int ch;
9113054f218SWarner Losh 
9129c180898SWarner Losh 	check_devd_enabled();
913*8334958aSJoseph Koshy 	while ((ch = getopt(argc, argv, "Ddf:n")) != -1) {
9143054f218SWarner Losh 		switch (ch) {
9155a882d77SWarner Losh 		case 'D':
9165a882d77SWarner Losh 			Dflag++;
9175a882d77SWarner Losh 			break;
9183054f218SWarner Losh 		case 'd':
9193054f218SWarner Losh 			dflag++;
9203054f218SWarner Losh 			break;
921*8334958aSJoseph Koshy 		case 'f':
922*8334958aSJoseph Koshy 			configfile = optarg;
923*8334958aSJoseph Koshy 			break;
9245a882d77SWarner Losh 		case 'n':
9255a882d77SWarner Losh 			nflag++;
9265a882d77SWarner Losh 			break;
9273054f218SWarner Losh 		default:
9283054f218SWarner Losh 			usage();
9293054f218SWarner Losh 		}
9303054f218SWarner Losh 	}
9313054f218SWarner Losh 
9323054f218SWarner Losh 	cfg.parse();
933fd6a8f23SWarner Losh 	if (!dflag && nflag) {
9343054f218SWarner Losh 		daemon(0, 0);
9353054f218SWarner Losh 		cfg.drop_pidfile();
936fd6a8f23SWarner Losh 	}
937f32a7686SWarner Losh 	signal(SIGPIPE, SIG_IGN);
9383054f218SWarner Losh 	signal(SIGHUP, gensighand);
9393054f218SWarner Losh 	signal(SIGINT, gensighand);
9403054f218SWarner Losh 	signal(SIGTERM, gensighand);
9413054f218SWarner Losh 	event_loop();
9423054f218SWarner Losh 	return (0);
9433054f218SWarner Losh }
944