xref: /freebsd-14.2/sbin/devd/devd.cc (revision 251bedd1)
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:
33*251bedd1SWarner 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>
40*251bedd1SWarner Losh #include <sys/socket.h>
41*251bedd1SWarner Losh #include <sys/stat.h>
429c180898SWarner Losh #include <sys/sysctl.h>
43*251bedd1SWarner Losh #include <sys/types.h>
44*251bedd1SWarner 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>
523054f218SWarner Losh #include <stdlib.h>
533054f218SWarner Losh #include <stdio.h>
543054f218SWarner Losh #include <string.h>
553054f218SWarner Losh #include <unistd.h>
563054f218SWarner Losh 
578b823408SWarner Losh #include <algorithm>
583054f218SWarner Losh #include <map>
593054f218SWarner Losh #include <string>
60*251bedd1SWarner Losh #include <list>
613054f218SWarner Losh #include <vector>
623054f218SWarner Losh 
636aeeca8eSWarner Losh #include "devd.h"		/* C compatible definitions */
646aeeca8eSWarner Losh #include "devd.hh"		/* C++ class definitions */
653054f218SWarner Losh 
66*251bedd1SWarner Losh #define PIPE "/var/run/devd.pipe"
673054f218SWarner Losh #define CF "/etc/devd.conf"
689c180898SWarner Losh #define SYSCTL "hw.bus.devctl_disable"
693054f218SWarner Losh 
703054f218SWarner Losh using namespace std;
713054f218SWarner Losh 
723054f218SWarner Losh extern FILE *yyin;
733054f218SWarner Losh extern int lineno;
743054f218SWarner Losh 
75842ccec5SWarner Losh static const char notify = '!';
768b823408SWarner Losh static const char nomatch = '?';
778b823408SWarner Losh static const char attach = '+';
788b823408SWarner Losh static const char detach = '-';
798b823408SWarner Losh 
805a882d77SWarner Losh int Dflag;
813054f218SWarner Losh int dflag;
82812f53a7SWarner Losh int nflag;
833054f218SWarner Losh int romeo_must_die = 0;
843054f218SWarner Losh 
853054f218SWarner Losh static void event_loop(void);
863054f218SWarner Losh static void usage(void);
873054f218SWarner Losh 
888b823408SWarner Losh template <class T> void
898b823408SWarner Losh delete_and_clear(vector<T *> &v)
908b823408SWarner Losh {
918b823408SWarner Losh 	typename vector<T *>::const_iterator i;
923054f218SWarner Losh 
938b823408SWarner Losh 	for (i = v.begin(); i != v.end(); i++)
948b823408SWarner Losh 		delete *i;
958b823408SWarner Losh 	v.clear();
968b823408SWarner Losh }
978b823408SWarner Losh 
983054f218SWarner Losh config cfg;
993054f218SWarner Losh 
1003054f218SWarner Losh event_proc::event_proc() : _prio(-1)
1013054f218SWarner Losh {
1023054f218SWarner Losh 	// nothing
1033054f218SWarner Losh }
1043054f218SWarner Losh 
1053054f218SWarner Losh event_proc::~event_proc()
1063054f218SWarner Losh {
1073054f218SWarner Losh 	vector<eps *>::const_iterator i;
1083054f218SWarner Losh 
1093054f218SWarner Losh 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
1103054f218SWarner Losh 		delete *i;
1113054f218SWarner Losh 	_epsvec.clear();
1123054f218SWarner Losh }
1133054f218SWarner Losh 
1143054f218SWarner Losh void
1153054f218SWarner Losh event_proc::add(eps *eps)
1163054f218SWarner Losh {
1173054f218SWarner Losh 	_epsvec.push_back(eps);
1183054f218SWarner Losh }
1193054f218SWarner Losh 
1203054f218SWarner Losh bool
1213054f218SWarner Losh event_proc::matches(config &c)
1223054f218SWarner Losh {
1233054f218SWarner Losh 	vector<eps *>::const_iterator i;
1243054f218SWarner Losh 
1253054f218SWarner Losh 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
1263054f218SWarner Losh 		if (!(*i)->do_match(c))
1273054f218SWarner Losh 			return (false);
1283054f218SWarner Losh 	return (true);
1293054f218SWarner Losh }
1303054f218SWarner Losh 
1313054f218SWarner Losh bool
1323054f218SWarner Losh event_proc::run(config &c)
1333054f218SWarner Losh {
1343054f218SWarner Losh 	vector<eps *>::const_iterator i;
1353054f218SWarner Losh 
1363054f218SWarner Losh 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
1373054f218SWarner Losh 		if (!(*i)->do_action(c))
1383054f218SWarner Losh 			return (false);
1393054f218SWarner Losh 	return (true);
1403054f218SWarner Losh }
1413054f218SWarner Losh 
1423054f218SWarner Losh action::action(const char *cmd)
1433054f218SWarner Losh 	: _cmd(cmd)
1443054f218SWarner Losh {
1453054f218SWarner Losh 	// nothing
1463054f218SWarner Losh }
1473054f218SWarner Losh 
1483054f218SWarner Losh action::~action()
1493054f218SWarner Losh {
1503054f218SWarner Losh 	// nothing
1513054f218SWarner Losh }
1523054f218SWarner Losh 
1533054f218SWarner Losh bool
1545102ef84SWarner Losh action::do_action(config &c)
1553054f218SWarner Losh {
1568b823408SWarner Losh 	string s = c.expand_string(_cmd);
1575a882d77SWarner Losh 	if (Dflag)
1588b823408SWarner Losh 		fprintf(stderr, "Executing '%s'\n", s.c_str());
1598b823408SWarner Losh 	::system(s.c_str());
1603054f218SWarner Losh 	return (true);
1613054f218SWarner Losh }
1623054f218SWarner Losh 
1635102ef84SWarner Losh match::match(config &c, const char *var, const char *re)
1648b823408SWarner Losh 	: _var(var)
1653054f218SWarner Losh {
1668b823408SWarner Losh 	string pattern = re;
1678b823408SWarner Losh 	_re = "^";
1688b823408SWarner Losh 	_re.append(c.expand_string(string(re)));
1698b823408SWarner Losh 	_re.append("$");
1708b823408SWarner Losh 	regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB);
1713054f218SWarner Losh }
1723054f218SWarner Losh 
1733054f218SWarner Losh match::~match()
1743054f218SWarner Losh {
1755102ef84SWarner Losh 	regfree(&_regex);
1763054f218SWarner Losh }
1773054f218SWarner Losh 
1783054f218SWarner Losh bool
1795102ef84SWarner Losh match::do_match(config &c)
1803054f218SWarner Losh {
1815102ef84SWarner Losh 	string value = c.get_variable(_var);
1825102ef84SWarner Losh 	bool retval;
1835102ef84SWarner Losh 
1845a882d77SWarner Losh 	if (Dflag)
1858b823408SWarner Losh 		fprintf(stderr, "Testing %s=%s against %s\n", _var.c_str(),
1868b823408SWarner Losh 		    value.c_str(), _re.c_str());
1878b823408SWarner Losh 
1885102ef84SWarner Losh 	retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
1895102ef84SWarner Losh 	return retval;
1903054f218SWarner Losh }
1913054f218SWarner Losh 
1923054f218SWarner Losh const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
1933054f218SWarner Losh const string var_list::nothing = "";
1943054f218SWarner Losh 
1953054f218SWarner Losh const string &
1963054f218SWarner Losh var_list::get_variable(const string &var) const
1973054f218SWarner Losh {
1983054f218SWarner Losh 	map<string, string>::const_iterator i;
1993054f218SWarner Losh 
2003054f218SWarner Losh 	i = _vars.find(var);
2013054f218SWarner Losh 	if (i == _vars.end())
2028b823408SWarner Losh 		return (var_list::bogus);
2033054f218SWarner Losh 	return (i->second);
2043054f218SWarner Losh }
2053054f218SWarner Losh 
2063054f218SWarner Losh bool
2073054f218SWarner Losh var_list::is_set(const string &var) const
2083054f218SWarner Losh {
2093054f218SWarner Losh 	return (_vars.find(var) != _vars.end());
2103054f218SWarner Losh }
2113054f218SWarner Losh 
2123054f218SWarner Losh void
2133054f218SWarner Losh var_list::set_variable(const string &var, const string &val)
2143054f218SWarner Losh {
2155a882d77SWarner Losh 	if (Dflag)
2168b823408SWarner Losh 		fprintf(stderr, "%s=%s\n", var.c_str(), val.c_str());
2173054f218SWarner Losh 	_vars[var] = val;
2183054f218SWarner Losh }
2193054f218SWarner Losh 
2203054f218SWarner Losh void
2213054f218SWarner Losh config::reset(void)
2223054f218SWarner Losh {
2233054f218SWarner Losh 	_dir_list.clear();
2248b823408SWarner Losh 	delete_and_clear(_var_list_table);
2258b823408SWarner Losh 	delete_and_clear(_attach_list);
2268b823408SWarner Losh 	delete_and_clear(_detach_list);
2278b823408SWarner Losh 	delete_and_clear(_nomatch_list);
228842ccec5SWarner Losh 	delete_and_clear(_notify_list);
2293054f218SWarner Losh }
2303054f218SWarner Losh 
2313054f218SWarner Losh void
2323054f218SWarner Losh config::parse_one_file(const char *fn)
2333054f218SWarner Losh {
2345a882d77SWarner Losh 	if (Dflag)
2353054f218SWarner Losh 		printf("Parsing %s\n", fn);
2363054f218SWarner Losh 	yyin = fopen(fn, "r");
2373054f218SWarner Losh 	if (yyin == NULL)
2383054f218SWarner Losh 		err(1, "Cannot open config file %s", fn);
2393054f218SWarner Losh 	if (yyparse() != 0)
2403054f218SWarner Losh 		errx(1, "Cannot parse %s at line %d", fn, lineno);
2413054f218SWarner Losh 	fclose(yyin);
2423054f218SWarner Losh }
2433054f218SWarner Losh 
2443054f218SWarner Losh void
2453054f218SWarner Losh config::parse_files_in_dir(const char *dirname)
2463054f218SWarner Losh {
2473054f218SWarner Losh 	DIR *dirp;
2483054f218SWarner Losh 	struct dirent *dp;
2493054f218SWarner Losh 	char path[PATH_MAX];
2503054f218SWarner Losh 
2515a882d77SWarner Losh 	if (Dflag)
2523054f218SWarner Losh 		printf("Parsing files in %s\n", dirname);
2533054f218SWarner Losh 	dirp = opendir(dirname);
2543054f218SWarner Losh 	if (dirp == NULL)
2553054f218SWarner Losh 		return;
2563054f218SWarner Losh 	readdir(dirp);		/* Skip . */
2573054f218SWarner Losh 	readdir(dirp);		/* Skip .. */
2583054f218SWarner Losh 	while ((dp = readdir(dirp)) != NULL) {
2593054f218SWarner Losh 		if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
2603054f218SWarner Losh 			snprintf(path, sizeof(path), "%s/%s",
2613054f218SWarner Losh 			    dirname, dp->d_name);
2623054f218SWarner Losh 			parse_one_file(path);
2633054f218SWarner Losh 		}
2643054f218SWarner Losh 	}
2653054f218SWarner Losh }
2663054f218SWarner Losh 
2678b823408SWarner Losh class epv_greater {
2688b823408SWarner Losh public:
2698b823408SWarner Losh 	int operator()(event_proc *const&l1, event_proc *const&l2)
2708b823408SWarner Losh 	{
2718b823408SWarner Losh 		return (l1->get_priority() > l2->get_priority());
2728b823408SWarner Losh 	}
2738b823408SWarner Losh };
2748b823408SWarner Losh 
2758b823408SWarner Losh void
2768b823408SWarner Losh config::sort_vector(vector<event_proc *> &v)
2778b823408SWarner Losh {
2788b823408SWarner Losh 	sort(v.begin(), v.end(), epv_greater());
2798b823408SWarner Losh }
2808b823408SWarner Losh 
2813054f218SWarner Losh void
2823054f218SWarner Losh config::parse(void)
2833054f218SWarner Losh {
2843054f218SWarner Losh 	vector<string>::const_iterator i;
2853054f218SWarner Losh 
2863054f218SWarner Losh 	parse_one_file(CF);
2873054f218SWarner Losh 	for (i = _dir_list.begin(); i != _dir_list.end(); i++)
2883054f218SWarner Losh 		parse_files_in_dir((*i).c_str());
2898b823408SWarner Losh 	sort_vector(_attach_list);
2908b823408SWarner Losh 	sort_vector(_detach_list);
2918b823408SWarner Losh 	sort_vector(_nomatch_list);
292842ccec5SWarner Losh 	sort_vector(_notify_list);
2933054f218SWarner Losh }
2943054f218SWarner Losh 
2953054f218SWarner Losh void
2963054f218SWarner Losh config::drop_pidfile()
2973054f218SWarner Losh {
2983054f218SWarner Losh 	FILE *fp;
2993054f218SWarner Losh 
3003054f218SWarner Losh 	if (_pidfile == "")
3013054f218SWarner Losh 		return;
3023054f218SWarner Losh 	fp = fopen(_pidfile.c_str(), "w");
3033054f218SWarner Losh 	if (fp == NULL)
3043054f218SWarner Losh 		return;
3053054f218SWarner Losh 	fprintf(fp, "%d\n", getpid());
3063054f218SWarner Losh 	fclose(fp);
3073054f218SWarner Losh }
3083054f218SWarner Losh 
3093054f218SWarner Losh void
3103054f218SWarner Losh config::add_attach(int prio, event_proc *p)
3113054f218SWarner Losh {
3123054f218SWarner Losh 	p->set_priority(prio);
3133054f218SWarner Losh 	_attach_list.push_back(p);
3143054f218SWarner Losh }
3153054f218SWarner Losh 
3163054f218SWarner Losh void
3173054f218SWarner Losh config::add_detach(int prio, event_proc *p)
3183054f218SWarner Losh {
3193054f218SWarner Losh 	p->set_priority(prio);
3203054f218SWarner Losh 	_detach_list.push_back(p);
3213054f218SWarner Losh }
3223054f218SWarner Losh 
3233054f218SWarner Losh void
3243054f218SWarner Losh config::add_directory(const char *dir)
3253054f218SWarner Losh {
3263054f218SWarner Losh 	_dir_list.push_back(string(dir));
3273054f218SWarner Losh }
3283054f218SWarner Losh 
3293054f218SWarner Losh void
3303054f218SWarner Losh config::add_nomatch(int prio, event_proc *p)
3313054f218SWarner Losh {
3323054f218SWarner Losh 	p->set_priority(prio);
3333054f218SWarner Losh 	_nomatch_list.push_back(p);
3343054f218SWarner Losh }
3353054f218SWarner Losh 
3363054f218SWarner Losh void
337842ccec5SWarner Losh config::add_notify(int prio, event_proc *p)
338842ccec5SWarner Losh {
339842ccec5SWarner Losh 	p->set_priority(prio);
340842ccec5SWarner Losh 	_notify_list.push_back(p);
341842ccec5SWarner Losh }
342842ccec5SWarner Losh 
343842ccec5SWarner Losh void
3443054f218SWarner Losh config::set_pidfile(const char *fn)
3453054f218SWarner Losh {
3463054f218SWarner Losh 	_pidfile = string(fn);
3473054f218SWarner Losh }
3483054f218SWarner Losh 
3493054f218SWarner Losh void
3503054f218SWarner Losh config::push_var_table()
3513054f218SWarner Losh {
3523054f218SWarner Losh 	var_list *vl;
3533054f218SWarner Losh 
3543054f218SWarner Losh 	vl = new var_list();
3553054f218SWarner Losh 	_var_list_table.push_back(vl);
3565a882d77SWarner Losh 	if (Dflag)
3578b823408SWarner Losh 		fprintf(stderr, "Pushing table\n");
3583054f218SWarner Losh }
3593054f218SWarner Losh 
3603054f218SWarner Losh void
3613054f218SWarner Losh config::pop_var_table()
3623054f218SWarner Losh {
3633054f218SWarner Losh 	delete _var_list_table.back();
3643054f218SWarner Losh 	_var_list_table.pop_back();
3655a882d77SWarner Losh 	if (Dflag)
3668b823408SWarner Losh 		fprintf(stderr, "Popping table\n");
3673054f218SWarner Losh }
3683054f218SWarner Losh 
3693054f218SWarner Losh void
3703054f218SWarner Losh config::set_variable(const char *var, const char *val)
3713054f218SWarner Losh {
3723054f218SWarner Losh 	_var_list_table.back()->set_variable(var, val);
3733054f218SWarner Losh }
3743054f218SWarner Losh 
3753054f218SWarner Losh const string &
3763054f218SWarner Losh config::get_variable(const string &var)
3773054f218SWarner Losh {
3783054f218SWarner Losh 	vector<var_list *>::reverse_iterator i;
3793054f218SWarner Losh 
3803054f218SWarner Losh 	for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); i++) {
3813054f218SWarner Losh 		if ((*i)->is_set(var))
3828b823408SWarner Losh 			return ((*i)->get_variable(var));
3833054f218SWarner Losh 	}
3843054f218SWarner Losh 	return (var_list::nothing);
3853054f218SWarner Losh }
3863054f218SWarner Losh 
3878b823408SWarner Losh bool
3888b823408SWarner Losh config::is_id_char(char ch)
3898b823408SWarner Losh {
3908b823408SWarner Losh 	return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
3918b823408SWarner Losh 	    ch == '-'));
3928b823408SWarner Losh }
3938b823408SWarner Losh 
3945102ef84SWarner Losh void
3956cabd675SWarner Losh config::expand_one(const char *&src, string &dst)
3963054f218SWarner Losh {
3975102ef84SWarner Losh 	int count;
3986cabd675SWarner Losh 	string buffer, varstr;
3995102ef84SWarner Losh 
4008b823408SWarner Losh 	src++;
4015102ef84SWarner Losh 	// $$ -> $
4025102ef84SWarner Losh 	if (*src == '$') {
4036cabd675SWarner Losh 		dst.append(src++, 1);
4045102ef84SWarner Losh 		return;
4055102ef84SWarner Losh 	}
4065102ef84SWarner Losh 
4075102ef84SWarner Losh 	// $(foo) -> $(foo)
4088b823408SWarner Losh 	// Not sure if I want to support this or not, so for now we just pass
4098b823408SWarner Losh 	// it through.
4105102ef84SWarner Losh 	if (*src == '(') {
4116cabd675SWarner Losh 		dst.append("$");
4125102ef84SWarner Losh 		count = 1;
4136cabd675SWarner Losh 		/* If the string ends before ) is matched , return. */
4146cabd675SWarner Losh 		while (count > 0 && *src) {
4155102ef84SWarner Losh 			if (*src == ')')
4165102ef84SWarner Losh 				count--;
4175102ef84SWarner Losh 			else if (*src == '(')
4185102ef84SWarner Losh 				count++;
4196cabd675SWarner Losh 			dst.append(src++, 1);
4205102ef84SWarner Losh 		}
4215102ef84SWarner Losh 		return;
4225102ef84SWarner Losh 	}
4235102ef84SWarner Losh 
4245102ef84SWarner Losh 	// ${^A-Za-z] -> $\1
4255102ef84SWarner Losh 	if (!isalpha(*src)) {
4266cabd675SWarner Losh 		dst.append("$");
4276cabd675SWarner Losh 		dst.append(src++, 1);
4285102ef84SWarner Losh 		return;
4295102ef84SWarner Losh 	}
4305102ef84SWarner Losh 
4315102ef84SWarner Losh 	// $var -> replace with value
4326cabd675SWarner Losh 	do {
4336cabd675SWarner Losh 		buffer.append(src++, 1);
43466ff6821SWarner Losh 	} while (is_id_char(*src));
4356cabd675SWarner Losh 	buffer.append("", 1);
4366cabd675SWarner Losh 	varstr = get_variable(buffer.c_str());
4376cabd675SWarner Losh 	dst.append(varstr);
4385102ef84SWarner Losh }
4395102ef84SWarner Losh 
4405102ef84SWarner Losh const string
4415102ef84SWarner Losh config::expand_string(const string &s)
4425102ef84SWarner Losh {
4435102ef84SWarner Losh 	const char *src;
4446cabd675SWarner Losh 	string dst;
4455102ef84SWarner Losh 
4465102ef84SWarner Losh 	src = s.c_str();
4475102ef84SWarner Losh 	while (*src) {
4485102ef84SWarner Losh 		if (*src == '$')
4496cabd675SWarner Losh 			expand_one(src, dst);
4505102ef84SWarner Losh 		else
4516cabd675SWarner Losh 			dst.append(src++, 1);
4525102ef84SWarner Losh 	}
4536cabd675SWarner Losh 	dst.append("", 1);
4545102ef84SWarner Losh 
4556cabd675SWarner Losh 	return (dst);
4563054f218SWarner Losh }
4573054f218SWarner Losh 
4588b823408SWarner Losh bool
4598b823408SWarner Losh config::chop_var(char *&buffer, char *&lhs, char *&rhs)
4608b823408SWarner Losh {
4618b823408SWarner Losh 	char *walker;
4628b823408SWarner Losh 
4638b823408SWarner Losh 	if (*buffer == '\0')
4648b823408SWarner Losh 		return (false);
4658b823408SWarner Losh 	walker = lhs = buffer;
4668b823408SWarner Losh 	while (is_id_char(*walker))
4678b823408SWarner Losh 		walker++;
4688b823408SWarner Losh 	if (*walker != '=')
4698b823408SWarner Losh 		return (false);
4708b823408SWarner Losh 	walker++;		// skip =
4718b823408SWarner Losh 	if (*walker == '"') {
4728b823408SWarner Losh 		walker++;	// skip "
4738b823408SWarner Losh 		rhs = walker;
4748b823408SWarner Losh 		while (*walker && *walker != '"')
4758b823408SWarner Losh 			walker++;
4768b823408SWarner Losh 		if (*walker != '"')
4778b823408SWarner Losh 			return (false);
4788b823408SWarner Losh 		rhs[-2] = '\0';
4798b823408SWarner Losh 		*walker++ = '\0';
4808b823408SWarner Losh 	} else {
4818b823408SWarner Losh 		rhs = walker;
4828b823408SWarner Losh 		while (*walker && !isspace(*walker))
4838b823408SWarner Losh 			walker++;
4848b823408SWarner Losh 		if (*walker != '\0')
4858b823408SWarner Losh 			*walker++ = '\0';
4868b823408SWarner Losh 		rhs[-1] = '\0';
4878b823408SWarner Losh 	}
488b1ee04b1SWarner Losh 	while (isspace(*walker))
489b1ee04b1SWarner Losh 		walker++;
4908b823408SWarner Losh 	buffer = walker;
4918b823408SWarner Losh 	return (true);
4928b823408SWarner Losh }
4938b823408SWarner Losh 
4948b823408SWarner Losh 
4958b823408SWarner Losh char *
4968b823408SWarner Losh config::set_vars(char *buffer)
4978b823408SWarner Losh {
4988b823408SWarner Losh 	char *lhs;
4998b823408SWarner Losh 	char *rhs;
5008b823408SWarner Losh 
5018b823408SWarner Losh 	while (1) {
5028b823408SWarner Losh 		if (!chop_var(buffer, lhs, rhs))
5038b823408SWarner Losh 			break;
5048b823408SWarner Losh 		set_variable(lhs, rhs);
5058b823408SWarner Losh 	}
5068b823408SWarner Losh 	return (buffer);
5078b823408SWarner Losh }
5088b823408SWarner Losh 
5098b823408SWarner Losh void
5108b823408SWarner Losh config::find_and_execute(char type)
5118b823408SWarner Losh {
5128b823408SWarner Losh 	vector<event_proc *> *l;
5138b823408SWarner Losh 	vector<event_proc *>::const_iterator i;
5148b823408SWarner Losh 	char *s;
5158b823408SWarner Losh 
5168b823408SWarner Losh 	switch (type) {
5178b823408SWarner Losh 	default:
5188b823408SWarner Losh 		return;
519842ccec5SWarner Losh 	case notify:
520842ccec5SWarner Losh 		l = &_notify_list;
521842ccec5SWarner Losh 		s = "notify";
522842ccec5SWarner Losh 		break;
5238b823408SWarner Losh 	case nomatch:
5248b823408SWarner Losh 		l = &_nomatch_list;
5258b823408SWarner Losh 		s = "nomatch";
5268b823408SWarner Losh 		break;
5278b823408SWarner Losh 	case attach:
5288b823408SWarner Losh 		l = &_attach_list;
5298b823408SWarner Losh 		s = "attach";
5308b823408SWarner Losh 		break;
5318b823408SWarner Losh 	case detach:
5328b823408SWarner Losh 		l = &_detach_list;
5338b823408SWarner Losh 		s = "detach";
5348b823408SWarner Losh 		break;
5358b823408SWarner Losh 	}
5365a882d77SWarner Losh 	if (Dflag)
5378b823408SWarner Losh 		fprintf(stderr, "Processing %s event\n", s);
5388b823408SWarner Losh 	for (i = l->begin(); i != l->end(); i++) {
5398b823408SWarner Losh 		if ((*i)->matches(*this)) {
5408b823408SWarner Losh 			(*i)->run(*this);
5418b823408SWarner Losh 			break;
5428b823408SWarner Losh 		}
5438b823408SWarner Losh 	}
5448b823408SWarner Losh 
5458b823408SWarner Losh }
5468b823408SWarner Losh 
5473054f218SWarner Losh 
5483054f218SWarner Losh static void
5498b823408SWarner Losh process_event(char *buffer)
5503054f218SWarner Losh {
5513054f218SWarner Losh 	char type;
5523054f218SWarner Losh 	char *sp;
5533054f218SWarner Losh 
5548b823408SWarner Losh 	sp = buffer + 1;
5555a882d77SWarner Losh 	if (Dflag)
5568b823408SWarner Losh 		fprintf(stderr, "Processing event '%s'\n", buffer);
5573054f218SWarner Losh 	type = *buffer++;
5588b823408SWarner Losh 	cfg.push_var_table();
5598b823408SWarner Losh 	// No match doesn't have a device, and the format is a little
5608b823408SWarner Losh 	// different, so handle it separately.
561842ccec5SWarner Losh 	switch (type) {
562842ccec5SWarner Losh 	case notify:
563842ccec5SWarner Losh 		sp = cfg.set_vars(sp);
564842ccec5SWarner Losh 		break;
565842ccec5SWarner Losh 	case nomatch:
566842ccec5SWarner Losh 		//?vars at location on bus
567842ccec5SWarner Losh 		sp = cfg.set_vars(sp);
568842ccec5SWarner Losh 		if (strncmp(sp, "at ", 3) == 0)
569842ccec5SWarner Losh 			sp += 3;
570842ccec5SWarner Losh 		sp = cfg.set_vars(sp);
571842ccec5SWarner Losh 		if (strncmp(sp, "on ", 3) == 0)
572842ccec5SWarner Losh 			cfg.set_variable("bus", sp + 3);
573842ccec5SWarner Losh 		break;
574842ccec5SWarner Losh 	case attach:	/*FALLTHROUGH*/
575842ccec5SWarner Losh 	case detach:
5768b823408SWarner Losh 		sp = strchr(sp, ' ');
5773054f218SWarner Losh 		if (sp == NULL)
5783054f218SWarner Losh 			return;	/* Can't happen? */
5798b823408SWarner Losh 		*sp++ = '\0';
5808b823408SWarner Losh 		cfg.set_variable("device-name", buffer);
5818b823408SWarner Losh 		if (strncmp(sp, "at ", 3) == 0)
5828b823408SWarner Losh 			sp += 3;
5838b823408SWarner Losh 		sp = cfg.set_vars(sp);
5848b823408SWarner Losh 		if (strncmp(sp, "on ", 3) == 0)
5858b823408SWarner Losh 			cfg.set_variable("bus", sp + 3);
586842ccec5SWarner Losh 		break;
587b1ee04b1SWarner Losh 	}
588b1ee04b1SWarner Losh 
5898b823408SWarner Losh 	cfg.find_and_execute(type);
5908b823408SWarner Losh 	cfg.pop_var_table();
5913054f218SWarner Losh }
5923054f218SWarner Losh 
593*251bedd1SWarner Losh int
594*251bedd1SWarner Losh create_socket(const char *name)
595*251bedd1SWarner Losh {
596*251bedd1SWarner Losh 	int fd, slen;
597*251bedd1SWarner Losh 	struct sockaddr_un sun;
598*251bedd1SWarner Losh 
599*251bedd1SWarner Losh 	if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
600*251bedd1SWarner Losh 		err(1, "socket");
601*251bedd1SWarner Losh 	bzero(&sun, sizeof(sun));
602*251bedd1SWarner Losh 	sun.sun_family = AF_UNIX;
603*251bedd1SWarner Losh 	strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
604*251bedd1SWarner Losh 	slen = SUN_LEN(&sun);
605*251bedd1SWarner Losh 	unlink(name);
606*251bedd1SWarner Losh 	if (bind(fd, (struct sockaddr *) & sun, slen) < 0)
607*251bedd1SWarner Losh 		err(1, "bind");
608*251bedd1SWarner Losh 	listen(fd, 4);
609*251bedd1SWarner Losh 	fchown(fd, 0, 0);	/* XXX - root.wheel */
610*251bedd1SWarner Losh 	fchmod(fd, 0660);
611*251bedd1SWarner Losh 	return (fd);
612*251bedd1SWarner Losh }
613*251bedd1SWarner Losh 
614*251bedd1SWarner Losh list<int> clients;
615*251bedd1SWarner Losh 
616*251bedd1SWarner Losh void
617*251bedd1SWarner Losh notify_clients(const char *data, int len)
618*251bedd1SWarner Losh {
619*251bedd1SWarner Losh 	list<int> bad;
620*251bedd1SWarner Losh 	list<int>::const_iterator i;
621*251bedd1SWarner Losh 
622*251bedd1SWarner Losh 	for (i = clients.begin(); i != clients.end(); i++) {
623*251bedd1SWarner Losh 		if (write(*i, data, len) <= 0) {
624*251bedd1SWarner Losh 			bad.push_back(*i);
625*251bedd1SWarner Losh 			close(*i);
626*251bedd1SWarner Losh 		}
627*251bedd1SWarner Losh 	}
628*251bedd1SWarner Losh 
629*251bedd1SWarner Losh 	for (i = bad.begin(); i != bad.end(); i++)
630*251bedd1SWarner Losh 		clients.erase(find(clients.begin(), clients.end(), *i));
631*251bedd1SWarner Losh }
632*251bedd1SWarner Losh 
633*251bedd1SWarner Losh void
634*251bedd1SWarner Losh new_client(int fd)
635*251bedd1SWarner Losh {
636*251bedd1SWarner Losh 	int s;
637*251bedd1SWarner Losh 
638*251bedd1SWarner Losh 	s = accept(fd, NULL, NULL);
639*251bedd1SWarner Losh 	if (s != -1)
640*251bedd1SWarner Losh 		clients.push_back(s);
641*251bedd1SWarner Losh }
642*251bedd1SWarner Losh 
6433054f218SWarner Losh static void
6443054f218SWarner Losh event_loop(void)
6453054f218SWarner Losh {
6463054f218SWarner Losh 	int rv;
6473054f218SWarner Losh 	int fd;
6483054f218SWarner Losh 	char buffer[DEVCTL_MAXBUF];
6495a882d77SWarner Losh 	int once = 0;
650*251bedd1SWarner Losh 	int server_fd, max_fd;
6515a882d77SWarner Losh 	timeval tv;
6525a882d77SWarner Losh 	fd_set fds;
6533054f218SWarner Losh 
6543054f218SWarner Losh 	fd = open(PATH_DEVCTL, O_RDONLY);
6553054f218SWarner Losh 	if (fd == -1)
656*251bedd1SWarner Losh 		err(1, "Can't open devctl device %s", PATH_DEVCTL);
6573054f218SWarner Losh 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
658*251bedd1SWarner Losh 		err(1, "Can't set close-on-exec flag on devctl");
659*251bedd1SWarner Losh 	server_fd = create_socket(PIPE);
660*251bedd1SWarner Losh 	max_fd = max(fd, server_fd) + 1;
6613054f218SWarner Losh 	while (1) {
6623054f218SWarner Losh 		if (romeo_must_die)
6633054f218SWarner Losh 			break;
6645a882d77SWarner Losh 		if (!once && !dflag && !nflag) {
6655a882d77SWarner Losh 			// Check to see if we have any events pending.
6665a882d77SWarner Losh 			tv.tv_sec = 0;
6675a882d77SWarner Losh 			tv.tv_usec = 0;
6685a882d77SWarner Losh 			FD_ZERO(&fds);
6695a882d77SWarner Losh 			FD_SET(fd, &fds);
6705a882d77SWarner Losh 			rv = select(fd + 1, &fds, &fds, &fds, &tv);
6715a882d77SWarner Losh 			// No events -> we've processed all pending events
672f3059505SWarner Losh 			if (rv == 0) {
6735a882d77SWarner Losh 				if (Dflag)
6745a882d77SWarner Losh 					fprintf(stderr, "Calling daemon\n");
6755a882d77SWarner Losh 				daemon(0, 0);
676fd6a8f23SWarner Losh 				cfg.drop_pidfile();
6775a882d77SWarner Losh 				once++;
6785a882d77SWarner Losh 			}
6795a882d77SWarner Losh 		}
680*251bedd1SWarner Losh 		FD_ZERO(&fds);
681*251bedd1SWarner Losh 		FD_SET(fd, &fds);
682*251bedd1SWarner Losh 		FD_SET(server_fd, &fds);
683*251bedd1SWarner Losh 		rv = select(max_fd, &fds, NULL, NULL, NULL);
684*251bedd1SWarner Losh 		if (rv == -1) {
685*251bedd1SWarner Losh 			if (errno == EINTR)
686*251bedd1SWarner Losh 				continue;
687*251bedd1SWarner Losh 			err(1, "select");
688*251bedd1SWarner Losh 		}
689*251bedd1SWarner Losh 		if (FD_ISSET(fd, &fds)) {
6903054f218SWarner Losh 			rv = read(fd, buffer, sizeof(buffer) - 1);
6913054f218SWarner Losh 			if (rv > 0) {
692*251bedd1SWarner Losh 				notify_clients(buffer, rv);
6933054f218SWarner Losh 				buffer[rv] = '\0';
6943054f218SWarner Losh 				while (buffer[--rv] == '\n')
6953054f218SWarner Losh 					buffer[rv] = '\0';
6963054f218SWarner Losh 				process_event(buffer);
6973054f218SWarner Losh 			} else if (rv < 0) {
6983054f218SWarner Losh 				if (errno != EINTR)
6993054f218SWarner Losh 					break;
7003054f218SWarner Losh 			} else {
7013054f218SWarner Losh 				/* EOF */
7023054f218SWarner Losh 				break;
7033054f218SWarner Losh 			}
7043054f218SWarner Losh 		}
705*251bedd1SWarner Losh 		if (FD_ISSET(server_fd, &fds))
706*251bedd1SWarner Losh 			new_client(server_fd);
707*251bedd1SWarner Losh 	}
7083054f218SWarner Losh 	close(fd);
7093054f218SWarner Losh }
7103054f218SWarner Losh 
7113054f218SWarner Losh /*
7123054f218SWarner Losh  * functions that the parser uses.
7133054f218SWarner Losh  */
7143054f218SWarner Losh void
7153054f218SWarner Losh add_attach(int prio, event_proc *p)
7163054f218SWarner Losh {
7173054f218SWarner Losh 	cfg.add_attach(prio, p);
7183054f218SWarner Losh }
7193054f218SWarner Losh 
7203054f218SWarner Losh void
7213054f218SWarner Losh add_detach(int prio, event_proc *p)
7223054f218SWarner Losh {
7233054f218SWarner Losh 	cfg.add_detach(prio, p);
7243054f218SWarner Losh }
7253054f218SWarner Losh 
7263054f218SWarner Losh void
7273054f218SWarner Losh add_directory(const char *dir)
7283054f218SWarner Losh {
7293054f218SWarner Losh 	cfg.add_directory(dir);
7303054f218SWarner Losh 	free(const_cast<char *>(dir));
7313054f218SWarner Losh }
7323054f218SWarner Losh 
7333054f218SWarner Losh void
7343054f218SWarner Losh add_nomatch(int prio, event_proc *p)
7353054f218SWarner Losh {
7363054f218SWarner Losh 	cfg.add_nomatch(prio, p);
7373054f218SWarner Losh }
7383054f218SWarner Losh 
739842ccec5SWarner Losh void
740842ccec5SWarner Losh add_notify(int prio, event_proc *p)
741842ccec5SWarner Losh {
742842ccec5SWarner Losh 	cfg.add_notify(prio, p);
743842ccec5SWarner Losh }
744842ccec5SWarner Losh 
7453054f218SWarner Losh event_proc *
7463054f218SWarner Losh add_to_event_proc(event_proc *ep, eps *eps)
7473054f218SWarner Losh {
7483054f218SWarner Losh 	if (ep == NULL)
7493054f218SWarner Losh 		ep = new event_proc();
7503054f218SWarner Losh 	ep->add(eps);
7513054f218SWarner Losh 	return (ep);
7523054f218SWarner Losh }
7533054f218SWarner Losh 
7543054f218SWarner Losh eps *
7553054f218SWarner Losh new_action(const char *cmd)
7563054f218SWarner Losh {
7573054f218SWarner Losh 	eps *e = new action(cmd);
7583054f218SWarner Losh 	free(const_cast<char *>(cmd));
7593054f218SWarner Losh 	return (e);
7603054f218SWarner Losh }
7613054f218SWarner Losh 
7623054f218SWarner Losh eps *
7633054f218SWarner Losh new_match(const char *var, const char *re)
7643054f218SWarner Losh {
7655102ef84SWarner Losh 	eps *e = new match(cfg, var, re);
7663054f218SWarner Losh 	free(const_cast<char *>(var));
7673054f218SWarner Losh 	free(const_cast<char *>(re));
7683054f218SWarner Losh 	return (e);
7693054f218SWarner Losh }
7703054f218SWarner Losh 
7713054f218SWarner Losh void
7723054f218SWarner Losh set_pidfile(const char *name)
7733054f218SWarner Losh {
7743054f218SWarner Losh 	cfg.set_pidfile(name);
7753054f218SWarner Losh 	free(const_cast<char *>(name));
7763054f218SWarner Losh }
7773054f218SWarner Losh 
7783054f218SWarner Losh void
7793054f218SWarner Losh set_variable(const char *var, const char *val)
7803054f218SWarner Losh {
7813054f218SWarner Losh 	cfg.set_variable(var, val);
7823054f218SWarner Losh 	free(const_cast<char *>(var));
7833054f218SWarner Losh 	free(const_cast<char *>(val));
7843054f218SWarner Losh }
7853054f218SWarner Losh 
7863054f218SWarner Losh 
7873054f218SWarner Losh 
7883054f218SWarner Losh static void
7893054f218SWarner Losh gensighand(int)
7903054f218SWarner Losh {
7913054f218SWarner Losh 	romeo_must_die++;
7923054f218SWarner Losh 	_exit(0);
7933054f218SWarner Losh }
7943054f218SWarner Losh 
7953054f218SWarner Losh static void
7963054f218SWarner Losh usage()
7973054f218SWarner Losh {
7989f9bcbdeSWarner Losh 	fprintf(stderr, "usage: %s [-d]\n", getprogname());
7993054f218SWarner Losh 	exit(1);
8003054f218SWarner Losh }
8013054f218SWarner Losh 
8029c180898SWarner Losh static void
8039c180898SWarner Losh check_devd_enabled()
8049c180898SWarner Losh {
8059c180898SWarner Losh 	int val = 0;
8069c180898SWarner Losh 	size_t len;
8079c180898SWarner Losh 
8089c180898SWarner Losh 	len = sizeof(val);
80979f92e25SWarner Losh 	if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
8109c180898SWarner Losh 		errx(1, "devctl sysctl missing from kernel!");
8119c180898SWarner Losh 	if (val) {
8129c180898SWarner Losh 		warnx("Setting " SYSCTL " to 0");
8139c180898SWarner Losh 		val = 0;
8149c180898SWarner Losh 		sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
8159c180898SWarner Losh 	}
8169c180898SWarner Losh }
8179c180898SWarner Losh 
8183054f218SWarner Losh /*
8193054f218SWarner Losh  * main
8203054f218SWarner Losh  */
8213054f218SWarner Losh int
8223054f218SWarner Losh main(int argc, char **argv)
8233054f218SWarner Losh {
8243054f218SWarner Losh 	int ch;
8253054f218SWarner Losh 
8269c180898SWarner Losh 	check_devd_enabled();
8275a882d77SWarner Losh 	while ((ch = getopt(argc, argv, "Ddn")) != -1) {
8283054f218SWarner Losh 		switch (ch) {
8295a882d77SWarner Losh 		case 'D':
8305a882d77SWarner Losh 			Dflag++;
8315a882d77SWarner Losh 			break;
8323054f218SWarner Losh 		case 'd':
8333054f218SWarner Losh 			dflag++;
8343054f218SWarner Losh 			break;
8355a882d77SWarner Losh 		case 'n':
8365a882d77SWarner Losh 			nflag++;
8375a882d77SWarner Losh 			break;
8383054f218SWarner Losh 		default:
8393054f218SWarner Losh 			usage();
8403054f218SWarner Losh 		}
8413054f218SWarner Losh 	}
8423054f218SWarner Losh 
8433054f218SWarner Losh 	cfg.parse();
844fd6a8f23SWarner Losh 	if (!dflag && nflag) {
8453054f218SWarner Losh 		daemon(0, 0);
8463054f218SWarner Losh 		cfg.drop_pidfile();
847fd6a8f23SWarner Losh 	}
8483054f218SWarner Losh 	signal(SIGHUP, gensighand);
8493054f218SWarner Losh 	signal(SIGINT, gensighand);
8503054f218SWarner Losh 	signal(SIGTERM, gensighand);
8513054f218SWarner Losh 	event_loop();
8523054f218SWarner Losh 	return (0);
8533054f218SWarner Losh }
854