xref: /freebsd-14.2/sbin/devd/devd.cc (revision 79f92e25)
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:
333054f218SWarner Losh //	  - devd.conf needs to lose the warning about zone files.
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>
403054f218SWarner Losh #include <sys/types.h>
419c180898SWarner Losh #include <sys/sysctl.h>
423054f218SWarner Losh 
435102ef84SWarner Losh #include <ctype.h>
443054f218SWarner Losh #include <dirent.h>
453054f218SWarner Losh #include <errno.h>
463054f218SWarner Losh #include <err.h>
473054f218SWarner Losh #include <fcntl.h>
485102ef84SWarner Losh #include <regex.h>
493054f218SWarner Losh #include <stdlib.h>
503054f218SWarner Losh #include <stdio.h>
513054f218SWarner Losh #include <string.h>
523054f218SWarner Losh #include <unistd.h>
533054f218SWarner Losh 
548b823408SWarner Losh #include <algorithm>
553054f218SWarner Losh #include <map>
563054f218SWarner Losh #include <string>
573054f218SWarner Losh #include <vector>
583054f218SWarner Losh 
596aeeca8eSWarner Losh #include "devd.h"		/* C compatible definitions */
606aeeca8eSWarner Losh #include "devd.hh"		/* C++ class definitions */
613054f218SWarner Losh 
623054f218SWarner Losh #define CF "/etc/devd.conf"
639c180898SWarner Losh #define SYSCTL "hw.bus.devctl_disable"
643054f218SWarner Losh 
653054f218SWarner Losh using namespace std;
663054f218SWarner Losh 
673054f218SWarner Losh extern FILE *yyin;
683054f218SWarner Losh extern int lineno;
693054f218SWarner Losh 
708b823408SWarner Losh static const char nomatch = '?';
718b823408SWarner Losh static const char attach = '+';
728b823408SWarner Losh static const char detach = '-';
738b823408SWarner Losh 
745a882d77SWarner Losh int Dflag;
753054f218SWarner Losh int dflag;
76812f53a7SWarner Losh int nflag;
773054f218SWarner Losh int romeo_must_die = 0;
783054f218SWarner Losh 
793054f218SWarner Losh static void event_loop(void);
803054f218SWarner Losh static void usage(void);
813054f218SWarner Losh 
828b823408SWarner Losh template <class T> void
838b823408SWarner Losh delete_and_clear(vector<T *> &v)
848b823408SWarner Losh {
858b823408SWarner Losh 	typename vector<T *>::const_iterator i;
863054f218SWarner Losh 
878b823408SWarner Losh 	for (i = v.begin(); i != v.end(); i++)
888b823408SWarner Losh 		delete *i;
898b823408SWarner Losh 	v.clear();
908b823408SWarner Losh }
918b823408SWarner Losh 
923054f218SWarner Losh config cfg;
933054f218SWarner Losh 
943054f218SWarner Losh event_proc::event_proc() : _prio(-1)
953054f218SWarner Losh {
963054f218SWarner Losh 	// nothing
973054f218SWarner Losh }
983054f218SWarner Losh 
993054f218SWarner Losh event_proc::~event_proc()
1003054f218SWarner Losh {
1013054f218SWarner Losh 	vector<eps *>::const_iterator i;
1023054f218SWarner Losh 
1033054f218SWarner Losh 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
1043054f218SWarner Losh 		delete *i;
1053054f218SWarner Losh 	_epsvec.clear();
1063054f218SWarner Losh }
1073054f218SWarner Losh 
1083054f218SWarner Losh void
1093054f218SWarner Losh event_proc::add(eps *eps)
1103054f218SWarner Losh {
1113054f218SWarner Losh 	_epsvec.push_back(eps);
1123054f218SWarner Losh }
1133054f218SWarner Losh 
1143054f218SWarner Losh bool
1153054f218SWarner Losh event_proc::matches(config &c)
1163054f218SWarner Losh {
1173054f218SWarner Losh 	vector<eps *>::const_iterator i;
1183054f218SWarner Losh 
1193054f218SWarner Losh 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
1203054f218SWarner Losh 		if (!(*i)->do_match(c))
1213054f218SWarner Losh 			return (false);
1223054f218SWarner Losh 	return (true);
1233054f218SWarner Losh }
1243054f218SWarner Losh 
1253054f218SWarner Losh bool
1263054f218SWarner Losh event_proc::run(config &c)
1273054f218SWarner Losh {
1283054f218SWarner Losh 	vector<eps *>::const_iterator i;
1293054f218SWarner Losh 
1303054f218SWarner Losh 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
1313054f218SWarner Losh 		if (!(*i)->do_action(c))
1323054f218SWarner Losh 			return (false);
1333054f218SWarner Losh 	return (true);
1343054f218SWarner Losh }
1353054f218SWarner Losh 
1363054f218SWarner Losh action::action(const char *cmd)
1373054f218SWarner Losh 	: _cmd(cmd)
1383054f218SWarner Losh {
1393054f218SWarner Losh 	// nothing
1403054f218SWarner Losh }
1413054f218SWarner Losh 
1423054f218SWarner Losh action::~action()
1433054f218SWarner Losh {
1443054f218SWarner Losh 	// nothing
1453054f218SWarner Losh }
1463054f218SWarner Losh 
1473054f218SWarner Losh bool
1485102ef84SWarner Losh action::do_action(config &c)
1493054f218SWarner Losh {
1508b823408SWarner Losh 	string s = c.expand_string(_cmd);
1515a882d77SWarner Losh 	if (Dflag)
1528b823408SWarner Losh 		fprintf(stderr, "Executing '%s'\n", s.c_str());
1538b823408SWarner Losh 	::system(s.c_str());
1543054f218SWarner Losh 	return (true);
1553054f218SWarner Losh }
1563054f218SWarner Losh 
1575102ef84SWarner Losh match::match(config &c, const char *var, const char *re)
1588b823408SWarner Losh 	: _var(var)
1593054f218SWarner Losh {
1608b823408SWarner Losh 	string pattern = re;
1618b823408SWarner Losh 	_re = "^";
1628b823408SWarner Losh 	_re.append(c.expand_string(string(re)));
1638b823408SWarner Losh 	_re.append("$");
1648b823408SWarner Losh 	regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB);
1653054f218SWarner Losh }
1663054f218SWarner Losh 
1673054f218SWarner Losh match::~match()
1683054f218SWarner Losh {
1695102ef84SWarner Losh 	regfree(&_regex);
1703054f218SWarner Losh }
1713054f218SWarner Losh 
1723054f218SWarner Losh bool
1735102ef84SWarner Losh match::do_match(config &c)
1743054f218SWarner Losh {
1755102ef84SWarner Losh 	string value = c.get_variable(_var);
1765102ef84SWarner Losh 	bool retval;
1775102ef84SWarner Losh 
1785a882d77SWarner Losh 	if (Dflag)
1798b823408SWarner Losh 		fprintf(stderr, "Testing %s=%s against %s\n", _var.c_str(),
1808b823408SWarner Losh 		    value.c_str(), _re.c_str());
1818b823408SWarner Losh 
1825102ef84SWarner Losh 	retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
1835102ef84SWarner Losh 	return retval;
1843054f218SWarner Losh }
1853054f218SWarner Losh 
1863054f218SWarner Losh const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
1873054f218SWarner Losh const string var_list::nothing = "";
1883054f218SWarner Losh 
1893054f218SWarner Losh const string &
1903054f218SWarner Losh var_list::get_variable(const string &var) const
1913054f218SWarner Losh {
1923054f218SWarner Losh 	map<string, string>::const_iterator i;
1933054f218SWarner Losh 
1943054f218SWarner Losh 	i = _vars.find(var);
1953054f218SWarner Losh 	if (i == _vars.end())
1968b823408SWarner Losh 		return (var_list::bogus);
1973054f218SWarner Losh 	return (i->second);
1983054f218SWarner Losh }
1993054f218SWarner Losh 
2003054f218SWarner Losh bool
2013054f218SWarner Losh var_list::is_set(const string &var) const
2023054f218SWarner Losh {
2033054f218SWarner Losh 	return (_vars.find(var) != _vars.end());
2043054f218SWarner Losh }
2053054f218SWarner Losh 
2063054f218SWarner Losh void
2073054f218SWarner Losh var_list::set_variable(const string &var, const string &val)
2083054f218SWarner Losh {
2095a882d77SWarner Losh 	if (Dflag)
2108b823408SWarner Losh 		fprintf(stderr, "%s=%s\n", var.c_str(), val.c_str());
2113054f218SWarner Losh 	_vars[var] = val;
2123054f218SWarner Losh }
2133054f218SWarner Losh 
2143054f218SWarner Losh void
2153054f218SWarner Losh config::reset(void)
2163054f218SWarner Losh {
2173054f218SWarner Losh 	_dir_list.clear();
2188b823408SWarner Losh 	delete_and_clear(_var_list_table);
2198b823408SWarner Losh 	delete_and_clear(_attach_list);
2208b823408SWarner Losh 	delete_and_clear(_detach_list);
2218b823408SWarner Losh 	delete_and_clear(_nomatch_list);
2223054f218SWarner Losh }
2233054f218SWarner Losh 
2243054f218SWarner Losh void
2253054f218SWarner Losh config::parse_one_file(const char *fn)
2263054f218SWarner Losh {
2275a882d77SWarner Losh 	if (Dflag)
2283054f218SWarner Losh 		printf("Parsing %s\n", fn);
2293054f218SWarner Losh 	yyin = fopen(fn, "r");
2303054f218SWarner Losh 	if (yyin == NULL)
2313054f218SWarner Losh 		err(1, "Cannot open config file %s", fn);
2323054f218SWarner Losh 	if (yyparse() != 0)
2333054f218SWarner Losh 		errx(1, "Cannot parse %s at line %d", fn, lineno);
2343054f218SWarner Losh 	fclose(yyin);
2353054f218SWarner Losh }
2363054f218SWarner Losh 
2373054f218SWarner Losh void
2383054f218SWarner Losh config::parse_files_in_dir(const char *dirname)
2393054f218SWarner Losh {
2403054f218SWarner Losh 	DIR *dirp;
2413054f218SWarner Losh 	struct dirent *dp;
2423054f218SWarner Losh 	char path[PATH_MAX];
2433054f218SWarner Losh 
2445a882d77SWarner Losh 	if (Dflag)
2453054f218SWarner Losh 		printf("Parsing files in %s\n", dirname);
2463054f218SWarner Losh 	dirp = opendir(dirname);
2473054f218SWarner Losh 	if (dirp == NULL)
2483054f218SWarner Losh 		return;
2493054f218SWarner Losh 	readdir(dirp);		/* Skip . */
2503054f218SWarner Losh 	readdir(dirp);		/* Skip .. */
2513054f218SWarner Losh 	while ((dp = readdir(dirp)) != NULL) {
2523054f218SWarner Losh 		if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
2533054f218SWarner Losh 			snprintf(path, sizeof(path), "%s/%s",
2543054f218SWarner Losh 			    dirname, dp->d_name);
2553054f218SWarner Losh 			parse_one_file(path);
2563054f218SWarner Losh 		}
2573054f218SWarner Losh 	}
2583054f218SWarner Losh }
2593054f218SWarner Losh 
2608b823408SWarner Losh class epv_greater {
2618b823408SWarner Losh public:
2628b823408SWarner Losh 	int operator()(event_proc *const&l1, event_proc *const&l2)
2638b823408SWarner Losh 	{
2648b823408SWarner Losh 		return (l1->get_priority() > l2->get_priority());
2658b823408SWarner Losh 	}
2668b823408SWarner Losh };
2678b823408SWarner Losh 
2688b823408SWarner Losh void
2698b823408SWarner Losh config::sort_vector(vector<event_proc *> &v)
2708b823408SWarner Losh {
2718b823408SWarner Losh 	sort(v.begin(), v.end(), epv_greater());
2728b823408SWarner Losh }
2738b823408SWarner Losh 
2743054f218SWarner Losh void
2753054f218SWarner Losh config::parse(void)
2763054f218SWarner Losh {
2773054f218SWarner Losh 	vector<string>::const_iterator i;
2783054f218SWarner Losh 
2793054f218SWarner Losh 	parse_one_file(CF);
2803054f218SWarner Losh 	for (i = _dir_list.begin(); i != _dir_list.end(); i++)
2813054f218SWarner Losh 		parse_files_in_dir((*i).c_str());
2828b823408SWarner Losh 	sort_vector(_attach_list);
2838b823408SWarner Losh 	sort_vector(_detach_list);
2848b823408SWarner Losh 	sort_vector(_nomatch_list);
2853054f218SWarner Losh }
2863054f218SWarner Losh 
2873054f218SWarner Losh void
2883054f218SWarner Losh config::drop_pidfile()
2893054f218SWarner Losh {
2903054f218SWarner Losh 	FILE *fp;
2913054f218SWarner Losh 
2923054f218SWarner Losh 	if (_pidfile == "")
2933054f218SWarner Losh 		return;
2943054f218SWarner Losh 	fp = fopen(_pidfile.c_str(), "w");
2953054f218SWarner Losh 	if (fp == NULL)
2963054f218SWarner Losh 		return;
2973054f218SWarner Losh 	fprintf(fp, "%d\n", getpid());
2983054f218SWarner Losh 	fclose(fp);
2993054f218SWarner Losh }
3003054f218SWarner Losh 
3013054f218SWarner Losh void
3023054f218SWarner Losh config::add_attach(int prio, event_proc *p)
3033054f218SWarner Losh {
3043054f218SWarner Losh 	p->set_priority(prio);
3053054f218SWarner Losh 	_attach_list.push_back(p);
3063054f218SWarner Losh }
3073054f218SWarner Losh 
3083054f218SWarner Losh void
3093054f218SWarner Losh config::add_detach(int prio, event_proc *p)
3103054f218SWarner Losh {
3113054f218SWarner Losh 	p->set_priority(prio);
3123054f218SWarner Losh 	_detach_list.push_back(p);
3133054f218SWarner Losh }
3143054f218SWarner Losh 
3153054f218SWarner Losh void
3163054f218SWarner Losh config::add_directory(const char *dir)
3173054f218SWarner Losh {
3183054f218SWarner Losh 	_dir_list.push_back(string(dir));
3193054f218SWarner Losh }
3203054f218SWarner Losh 
3213054f218SWarner Losh void
3223054f218SWarner Losh config::add_nomatch(int prio, event_proc *p)
3233054f218SWarner Losh {
3243054f218SWarner Losh 	p->set_priority(prio);
3253054f218SWarner Losh 	_nomatch_list.push_back(p);
3263054f218SWarner Losh }
3273054f218SWarner Losh 
3283054f218SWarner Losh void
3293054f218SWarner Losh config::set_pidfile(const char *fn)
3303054f218SWarner Losh {
3313054f218SWarner Losh 	_pidfile = string(fn);
3323054f218SWarner Losh }
3333054f218SWarner Losh 
3343054f218SWarner Losh void
3353054f218SWarner Losh config::push_var_table()
3363054f218SWarner Losh {
3373054f218SWarner Losh 	var_list *vl;
3383054f218SWarner Losh 
3393054f218SWarner Losh 	vl = new var_list();
3403054f218SWarner Losh 	_var_list_table.push_back(vl);
3415a882d77SWarner Losh 	if (Dflag)
3428b823408SWarner Losh 		fprintf(stderr, "Pushing table\n");
3433054f218SWarner Losh }
3443054f218SWarner Losh 
3453054f218SWarner Losh void
3463054f218SWarner Losh config::pop_var_table()
3473054f218SWarner Losh {
3483054f218SWarner Losh 	delete _var_list_table.back();
3493054f218SWarner Losh 	_var_list_table.pop_back();
3505a882d77SWarner Losh 	if (Dflag)
3518b823408SWarner Losh 		fprintf(stderr, "Popping table\n");
3523054f218SWarner Losh }
3533054f218SWarner Losh 
3543054f218SWarner Losh void
3553054f218SWarner Losh config::set_variable(const char *var, const char *val)
3563054f218SWarner Losh {
3573054f218SWarner Losh 	_var_list_table.back()->set_variable(var, val);
3583054f218SWarner Losh }
3593054f218SWarner Losh 
3603054f218SWarner Losh const string &
3613054f218SWarner Losh config::get_variable(const string &var)
3623054f218SWarner Losh {
3633054f218SWarner Losh 	vector<var_list *>::reverse_iterator i;
3643054f218SWarner Losh 
3653054f218SWarner Losh 	for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); i++) {
3663054f218SWarner Losh 		if ((*i)->is_set(var))
3678b823408SWarner Losh 			return ((*i)->get_variable(var));
3683054f218SWarner Losh 	}
3693054f218SWarner Losh 	return (var_list::nothing);
3703054f218SWarner Losh }
3713054f218SWarner Losh 
3728b823408SWarner Losh bool
3738b823408SWarner Losh config::is_id_char(char ch)
3748b823408SWarner Losh {
3758b823408SWarner Losh 	return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
3768b823408SWarner Losh 	    ch == '-'));
3778b823408SWarner Losh }
3788b823408SWarner Losh 
3795102ef84SWarner Losh void
3806cabd675SWarner Losh config::expand_one(const char *&src, string &dst)
3813054f218SWarner Losh {
3825102ef84SWarner Losh 	int count;
3836cabd675SWarner Losh 	string buffer, varstr;
3845102ef84SWarner Losh 
3858b823408SWarner Losh 	src++;
3865102ef84SWarner Losh 	// $$ -> $
3875102ef84SWarner Losh 	if (*src == '$') {
3886cabd675SWarner Losh 		dst.append(src++, 1);
3895102ef84SWarner Losh 		return;
3905102ef84SWarner Losh 	}
3915102ef84SWarner Losh 
3925102ef84SWarner Losh 	// $(foo) -> $(foo)
3938b823408SWarner Losh 	// Not sure if I want to support this or not, so for now we just pass
3948b823408SWarner Losh 	// it through.
3955102ef84SWarner Losh 	if (*src == '(') {
3966cabd675SWarner Losh 		dst.append("$");
3975102ef84SWarner Losh 		count = 1;
3986cabd675SWarner Losh 		/* If the string ends before ) is matched , return. */
3996cabd675SWarner Losh 		while (count > 0 && *src) {
4005102ef84SWarner Losh 			if (*src == ')')
4015102ef84SWarner Losh 				count--;
4025102ef84SWarner Losh 			else if (*src == '(')
4035102ef84SWarner Losh 				count++;
4046cabd675SWarner Losh 			dst.append(src++, 1);
4055102ef84SWarner Losh 		}
4065102ef84SWarner Losh 		return;
4075102ef84SWarner Losh 	}
4085102ef84SWarner Losh 
4095102ef84SWarner Losh 	// ${^A-Za-z] -> $\1
4105102ef84SWarner Losh 	if (!isalpha(*src)) {
4116cabd675SWarner Losh 		dst.append("$");
4126cabd675SWarner Losh 		dst.append(src++, 1);
4135102ef84SWarner Losh 		return;
4145102ef84SWarner Losh 	}
4155102ef84SWarner Losh 
4165102ef84SWarner Losh 	// $var -> replace with value
4176cabd675SWarner Losh 	do {
4186cabd675SWarner Losh 		buffer.append(src++, 1);
41966ff6821SWarner Losh 	} while (is_id_char(*src));
4206cabd675SWarner Losh 	buffer.append("", 1);
4216cabd675SWarner Losh 	varstr = get_variable(buffer.c_str());
4226cabd675SWarner Losh 	dst.append(varstr);
4235102ef84SWarner Losh }
4245102ef84SWarner Losh 
4255102ef84SWarner Losh const string
4265102ef84SWarner Losh config::expand_string(const string &s)
4275102ef84SWarner Losh {
4285102ef84SWarner Losh 	const char *src;
4296cabd675SWarner Losh 	string dst;
4305102ef84SWarner Losh 
4315102ef84SWarner Losh 	src = s.c_str();
4325102ef84SWarner Losh 	while (*src) {
4335102ef84SWarner Losh 		if (*src == '$')
4346cabd675SWarner Losh 			expand_one(src, dst);
4355102ef84SWarner Losh 		else
4366cabd675SWarner Losh 			dst.append(src++, 1);
4375102ef84SWarner Losh 	}
4386cabd675SWarner Losh 	dst.append("", 1);
4395102ef84SWarner Losh 
4406cabd675SWarner Losh 	return (dst);
4413054f218SWarner Losh }
4423054f218SWarner Losh 
4438b823408SWarner Losh bool
4448b823408SWarner Losh config::chop_var(char *&buffer, char *&lhs, char *&rhs)
4458b823408SWarner Losh {
4468b823408SWarner Losh 	char *walker;
4478b823408SWarner Losh 
4488b823408SWarner Losh 	if (*buffer == '\0')
4498b823408SWarner Losh 		return (false);
4508b823408SWarner Losh 	walker = lhs = buffer;
4518b823408SWarner Losh 	while (is_id_char(*walker))
4528b823408SWarner Losh 		walker++;
4538b823408SWarner Losh 	if (*walker != '=')
4548b823408SWarner Losh 		return (false);
4558b823408SWarner Losh 	walker++;		// skip =
4568b823408SWarner Losh 	if (*walker == '"') {
4578b823408SWarner Losh 		walker++;	// skip "
4588b823408SWarner Losh 		rhs = walker;
4598b823408SWarner Losh 		while (*walker && *walker != '"')
4608b823408SWarner Losh 			walker++;
4618b823408SWarner Losh 		if (*walker != '"')
4628b823408SWarner Losh 			return (false);
4638b823408SWarner Losh 		rhs[-2] = '\0';
4648b823408SWarner Losh 		*walker++ = '\0';
4658b823408SWarner Losh 	} else {
4668b823408SWarner Losh 		rhs = walker;
4678b823408SWarner Losh 		while (*walker && !isspace(*walker))
4688b823408SWarner Losh 			walker++;
4698b823408SWarner Losh 		if (*walker != '\0')
4708b823408SWarner Losh 			*walker++ = '\0';
4718b823408SWarner Losh 		rhs[-1] = '\0';
4728b823408SWarner Losh 	}
473b1ee04b1SWarner Losh 	while (isspace(*walker))
474b1ee04b1SWarner Losh 		walker++;
4758b823408SWarner Losh 	buffer = walker;
4768b823408SWarner Losh 	return (true);
4778b823408SWarner Losh }
4788b823408SWarner Losh 
4798b823408SWarner Losh 
4808b823408SWarner Losh char *
4818b823408SWarner Losh config::set_vars(char *buffer)
4828b823408SWarner Losh {
4838b823408SWarner Losh 	char *lhs;
4848b823408SWarner Losh 	char *rhs;
4858b823408SWarner Losh 
4868b823408SWarner Losh 	while (1) {
4878b823408SWarner Losh 		if (!chop_var(buffer, lhs, rhs))
4888b823408SWarner Losh 			break;
4898b823408SWarner Losh 		set_variable(lhs, rhs);
4908b823408SWarner Losh 	}
4918b823408SWarner Losh 	return (buffer);
4928b823408SWarner Losh }
4938b823408SWarner Losh 
4948b823408SWarner Losh void
4958b823408SWarner Losh config::find_and_execute(char type)
4968b823408SWarner Losh {
4978b823408SWarner Losh 	vector<event_proc *> *l;
4988b823408SWarner Losh 	vector<event_proc *>::const_iterator i;
4998b823408SWarner Losh 	char *s;
5008b823408SWarner Losh 
5018b823408SWarner Losh 	switch (type) {
5028b823408SWarner Losh 	default:
5038b823408SWarner Losh 		return;
5048b823408SWarner Losh 	case nomatch:
5058b823408SWarner Losh 		l = &_nomatch_list;
5068b823408SWarner Losh 		s = "nomatch";
5078b823408SWarner Losh 		break;
5088b823408SWarner Losh 	case attach:
5098b823408SWarner Losh 		l = &_attach_list;
5108b823408SWarner Losh 		s = "attach";
5118b823408SWarner Losh 		break;
5128b823408SWarner Losh 	case detach:
5138b823408SWarner Losh 		l = &_detach_list;
5148b823408SWarner Losh 		s = "detach";
5158b823408SWarner Losh 		break;
5168b823408SWarner Losh 	}
5175a882d77SWarner Losh 	if (Dflag)
5188b823408SWarner Losh 		fprintf(stderr, "Processing %s event\n", s);
5198b823408SWarner Losh 	for (i = l->begin(); i != l->end(); i++) {
5208b823408SWarner Losh 		if ((*i)->matches(*this)) {
5218b823408SWarner Losh 			(*i)->run(*this);
5228b823408SWarner Losh 			break;
5238b823408SWarner Losh 		}
5248b823408SWarner Losh 	}
5258b823408SWarner Losh 
5268b823408SWarner Losh }
5278b823408SWarner Losh 
5283054f218SWarner Losh 
5293054f218SWarner Losh static void
5308b823408SWarner Losh process_event(char *buffer)
5313054f218SWarner Losh {
5323054f218SWarner Losh 	char type;
5333054f218SWarner Losh 	char *sp;
5343054f218SWarner Losh 
5358b823408SWarner Losh 	sp = buffer + 1;
5365a882d77SWarner Losh 	if (Dflag)
5378b823408SWarner Losh 		fprintf(stderr, "Processing event '%s'\n", buffer);
5383054f218SWarner Losh 	type = *buffer++;
5398b823408SWarner Losh 	cfg.push_var_table();
5408b823408SWarner Losh 	// No match doesn't have a device, and the format is a little
5418b823408SWarner Losh 	// different, so handle it separately.
5428b823408SWarner Losh 	if (type != nomatch) {
5438b823408SWarner Losh 		sp = strchr(sp, ' ');
5443054f218SWarner Losh 		if (sp == NULL)
5453054f218SWarner Losh 			return;	/* Can't happen? */
5468b823408SWarner Losh 		*sp++ = '\0';
5478b823408SWarner Losh 		cfg.set_variable("device-name", buffer);
5488b823408SWarner Losh 		if (strncmp(sp, "at ", 3) == 0)
5498b823408SWarner Losh 			sp += 3;
5508b823408SWarner Losh 		sp = cfg.set_vars(sp);
5518b823408SWarner Losh 		if (strncmp(sp, "on ", 3) == 0)
5528b823408SWarner Losh 			cfg.set_variable("bus", sp + 3);
553b1ee04b1SWarner Losh 	} else {
554b1ee04b1SWarner Losh 		//?vars at location on bus
555b1ee04b1SWarner Losh 		sp = cfg.set_vars(sp);
556b1ee04b1SWarner Losh 		if (strncmp(sp, "at ", 3) == 0)
557b1ee04b1SWarner Losh 			sp += 3;
558b1ee04b1SWarner Losh 		sp = cfg.set_vars(sp);
559b1ee04b1SWarner Losh 		if (strncmp(sp, "on ", 3) == 0)
560b1ee04b1SWarner Losh 			cfg.set_variable("bus", sp + 3);
561b1ee04b1SWarner Losh 	}
562b1ee04b1SWarner Losh 
5638b823408SWarner Losh 	cfg.find_and_execute(type);
5648b823408SWarner Losh 	cfg.pop_var_table();
5653054f218SWarner Losh }
5663054f218SWarner Losh 
5673054f218SWarner Losh static void
5683054f218SWarner Losh event_loop(void)
5693054f218SWarner Losh {
5703054f218SWarner Losh 	int rv;
5713054f218SWarner Losh 	int fd;
5723054f218SWarner Losh 	char buffer[DEVCTL_MAXBUF];
5735a882d77SWarner Losh 	int once = 0;
5745a882d77SWarner Losh 	timeval tv;
5755a882d77SWarner Losh 	fd_set fds;
5763054f218SWarner Losh 
5773054f218SWarner Losh 	fd = open(PATH_DEVCTL, O_RDONLY);
5783054f218SWarner Losh 	if (fd == -1)
5793054f218SWarner Losh 		err(1, "Can't open devctl");
5803054f218SWarner Losh 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
5813054f218SWarner Losh 		err(1, "Can't set close-on-exec flag");
5823054f218SWarner Losh 	while (1) {
5833054f218SWarner Losh 		if (romeo_must_die)
5843054f218SWarner Losh 			break;
5855a882d77SWarner Losh 		if (!once && !dflag && !nflag) {
5865a882d77SWarner Losh 			// Check to see if we have any events pending.
5875a882d77SWarner Losh 			tv.tv_sec = 0;
5885a882d77SWarner Losh 			tv.tv_usec = 0;
5895a882d77SWarner Losh 			FD_ZERO(&fds);
5905a882d77SWarner Losh 			FD_SET(fd, &fds);
5915a882d77SWarner Losh 			rv = select(fd + 1, &fds, &fds, &fds, &tv);
5925a882d77SWarner Losh 			// No events -> we've processed all pending events
593812f53a7SWarner Losh 			// == 2 is a kernel bug, but we hang if we don't
594812f53a7SWarner Losh 			// make allowances for a while.
595812f53a7SWarner Losh 			if (rv == 0 || rv == 2) {
5965a882d77SWarner Losh 				if (Dflag)
5975a882d77SWarner Losh 					fprintf(stderr, "Calling daemon\n");
5985a882d77SWarner Losh 				daemon(0, 0);
5995a882d77SWarner Losh 				once++;
6005a882d77SWarner Losh 			}
6015a882d77SWarner Losh 		}
6023054f218SWarner Losh 		rv = read(fd, buffer, sizeof(buffer) - 1);
6033054f218SWarner Losh 		if (rv > 0) {
6043054f218SWarner Losh 			buffer[rv] = '\0';
6053054f218SWarner Losh 			while (buffer[--rv] == '\n')
6063054f218SWarner Losh 				buffer[rv] = '\0';
6073054f218SWarner Losh 			process_event(buffer);
6083054f218SWarner Losh 		} else if (rv < 0) {
6093054f218SWarner Losh 			if (errno != EINTR)
6103054f218SWarner Losh 				break;
6113054f218SWarner Losh 		} else {
6123054f218SWarner Losh 			/* EOF */
6133054f218SWarner Losh 			break;
6143054f218SWarner Losh 		}
6153054f218SWarner Losh 	}
6163054f218SWarner Losh 	close(fd);
6173054f218SWarner Losh }
6183054f218SWarner Losh 
6193054f218SWarner Losh /*
6203054f218SWarner Losh  * functions that the parser uses.
6213054f218SWarner Losh  */
6223054f218SWarner Losh void
6233054f218SWarner Losh add_attach(int prio, event_proc *p)
6243054f218SWarner Losh {
6253054f218SWarner Losh 	cfg.add_attach(prio, p);
6263054f218SWarner Losh }
6273054f218SWarner Losh 
6283054f218SWarner Losh void
6293054f218SWarner Losh add_detach(int prio, event_proc *p)
6303054f218SWarner Losh {
6313054f218SWarner Losh 	cfg.add_detach(prio, p);
6323054f218SWarner Losh }
6333054f218SWarner Losh 
6343054f218SWarner Losh void
6353054f218SWarner Losh add_directory(const char *dir)
6363054f218SWarner Losh {
6373054f218SWarner Losh 	cfg.add_directory(dir);
6383054f218SWarner Losh 	free(const_cast<char *>(dir));
6393054f218SWarner Losh }
6403054f218SWarner Losh 
6413054f218SWarner Losh void
6423054f218SWarner Losh add_nomatch(int prio, event_proc *p)
6433054f218SWarner Losh {
6443054f218SWarner Losh 	cfg.add_nomatch(prio, p);
6453054f218SWarner Losh }
6463054f218SWarner Losh 
6473054f218SWarner Losh event_proc *
6483054f218SWarner Losh add_to_event_proc(event_proc *ep, eps *eps)
6493054f218SWarner Losh {
6503054f218SWarner Losh 	if (ep == NULL)
6513054f218SWarner Losh 		ep = new event_proc();
6523054f218SWarner Losh 	ep->add(eps);
6533054f218SWarner Losh 	return (ep);
6543054f218SWarner Losh }
6553054f218SWarner Losh 
6563054f218SWarner Losh eps *
6573054f218SWarner Losh new_action(const char *cmd)
6583054f218SWarner Losh {
6593054f218SWarner Losh 	eps *e = new action(cmd);
6603054f218SWarner Losh 	free(const_cast<char *>(cmd));
6613054f218SWarner Losh 	return (e);
6623054f218SWarner Losh }
6633054f218SWarner Losh 
6643054f218SWarner Losh eps *
6653054f218SWarner Losh new_match(const char *var, const char *re)
6663054f218SWarner Losh {
6675102ef84SWarner Losh 	eps *e = new match(cfg, var, re);
6683054f218SWarner Losh 	free(const_cast<char *>(var));
6693054f218SWarner Losh 	free(const_cast<char *>(re));
6703054f218SWarner Losh 	return (e);
6713054f218SWarner Losh }
6723054f218SWarner Losh 
6733054f218SWarner Losh void
6743054f218SWarner Losh set_pidfile(const char *name)
6753054f218SWarner Losh {
6763054f218SWarner Losh 	cfg.set_pidfile(name);
6773054f218SWarner Losh 	free(const_cast<char *>(name));
6783054f218SWarner Losh }
6793054f218SWarner Losh 
6803054f218SWarner Losh void
6813054f218SWarner Losh set_variable(const char *var, const char *val)
6823054f218SWarner Losh {
6833054f218SWarner Losh 	cfg.set_variable(var, val);
6843054f218SWarner Losh 	free(const_cast<char *>(var));
6853054f218SWarner Losh 	free(const_cast<char *>(val));
6863054f218SWarner Losh }
6873054f218SWarner Losh 
6883054f218SWarner Losh 
6893054f218SWarner Losh 
6903054f218SWarner Losh static void
6913054f218SWarner Losh gensighand(int)
6923054f218SWarner Losh {
6933054f218SWarner Losh 	romeo_must_die++;
6943054f218SWarner Losh 	_exit(0);
6953054f218SWarner Losh }
6963054f218SWarner Losh 
6973054f218SWarner Losh static void
6983054f218SWarner Losh usage()
6993054f218SWarner Losh {
7009f9bcbdeSWarner Losh 	fprintf(stderr, "usage: %s [-d]\n", getprogname());
7013054f218SWarner Losh 	exit(1);
7023054f218SWarner Losh }
7033054f218SWarner Losh 
7049c180898SWarner Losh static void
7059c180898SWarner Losh check_devd_enabled()
7069c180898SWarner Losh {
7079c180898SWarner Losh 	int val = 0;
7089c180898SWarner Losh 	size_t len;
7099c180898SWarner Losh 
7109c180898SWarner Losh 	len = sizeof(val);
711*79f92e25SWarner Losh 	if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
7129c180898SWarner Losh 		errx(1, "devctl sysctl missing from kernel!");
7139c180898SWarner Losh 	if (val) {
7149c180898SWarner Losh 		warnx("Setting " SYSCTL " to 0");
7159c180898SWarner Losh 		val = 0;
7169c180898SWarner Losh 		sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
7179c180898SWarner Losh 	}
7189c180898SWarner Losh }
7199c180898SWarner Losh 
7203054f218SWarner Losh /*
7213054f218SWarner Losh  * main
7223054f218SWarner Losh  */
7233054f218SWarner Losh int
7243054f218SWarner Losh main(int argc, char **argv)
7253054f218SWarner Losh {
7263054f218SWarner Losh 	int ch;
7273054f218SWarner Losh 
7289c180898SWarner Losh 	check_devd_enabled();
7295a882d77SWarner Losh 	while ((ch = getopt(argc, argv, "Ddn")) != -1) {
7303054f218SWarner Losh 		switch (ch) {
7315a882d77SWarner Losh 		case 'D':
7325a882d77SWarner Losh 			Dflag++;
7335a882d77SWarner Losh 			break;
7343054f218SWarner Losh 		case 'd':
7353054f218SWarner Losh 			dflag++;
7363054f218SWarner Losh 			break;
7375a882d77SWarner Losh 		case 'n':
7385a882d77SWarner Losh 			nflag++;
7395a882d77SWarner Losh 			break;
7403054f218SWarner Losh 		default:
7413054f218SWarner Losh 			usage();
7423054f218SWarner Losh 		}
7433054f218SWarner Losh 	}
7443054f218SWarner Losh 
7453054f218SWarner Losh 	cfg.parse();
7465a882d77SWarner Losh 	if (!dflag && nflag)
7473054f218SWarner Losh 		daemon(0, 0);
7483054f218SWarner Losh 	cfg.drop_pidfile();
7493054f218SWarner Losh 	signal(SIGHUP, gensighand);
7503054f218SWarner Losh 	signal(SIGINT, gensighand);
7513054f218SWarner Losh 	signal(SIGTERM, gensighand);
7523054f218SWarner Losh 	event_loop();
7533054f218SWarner Losh 	return (0);
7543054f218SWarner Losh }
755