1/*-
2 * Copyright (c) 2002-2010 M. Warner Losh.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * my_system is a variation on lib/libc/stdlib/system.c:
27 *
28 * Copyright (c) 1988, 1993
29 *	The Regents of the University of California.  All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 *    notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 *    notice, this list of conditions and the following disclaimer in the
38 *    documentation and/or other materials provided with the distribution.
39 * 4. Neither the name of the University nor the names of its contributors
40 *    may be used to endorse or promote products derived from this software
41 *    without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56/*
57 * DEVD control daemon.
58 */
59
60// TODO list:
61//	o devd.conf and devd man pages need a lot of help:
62//	  - devd needs to document the unix domain socket
63//	  - devd.conf needs more details on the supported statements.
64
65#include <sys/cdefs.h>
66__FBSDID("$FreeBSD: stable/11/sbin/devd/devd.cc 356133 2019-12-27 18:53:07Z mav $");
67
68#include <sys/param.h>
69#include <sys/socket.h>
70#include <sys/stat.h>
71#include <sys/sysctl.h>
72#include <sys/types.h>
73#include <sys/wait.h>
74#include <sys/un.h>
75
76#include <cctype>
77#include <cerrno>
78#include <cstdlib>
79#include <cstdio>
80#include <csignal>
81#include <cstring>
82#include <cstdarg>
83
84#include <dirent.h>
85#include <err.h>
86#include <fcntl.h>
87#include <libutil.h>
88#include <paths.h>
89#include <poll.h>
90#include <regex.h>
91#include <syslog.h>
92#include <unistd.h>
93
94#include <algorithm>
95#include <map>
96#include <string>
97#include <list>
98#include <stdexcept>
99#include <vector>
100
101#include "devd.h"		/* C compatible definitions */
102#include "devd.hh"		/* C++ class definitions */
103
104#define STREAMPIPE "/var/run/devd.pipe"
105#define SEQPACKETPIPE "/var/run/devd.seqpacket.pipe"
106#define CF "/etc/devd.conf"
107#define SYSCTL "hw.bus.devctl_queue"
108
109/*
110 * Since the client socket is nonblocking, we must increase its send buffer to
111 * handle brief event storms.  On FreeBSD, AF_UNIX sockets don't have a receive
112 * buffer, so the client can't increase the buffersize by itself.
113 *
114 * For example, when creating a ZFS pool, devd emits one 165 character
115 * resource.fs.zfs.statechange message for each vdev in the pool.  The kernel
116 * allocates a 4608B mbuf for each message.  Modern technology places a limit of
117 * roughly 450 drives/rack, and it's unlikely that a zpool will ever be larger
118 * than that.
119 *
120 * 450 drives * 165 bytes / drive = 74250B of data in the sockbuf
121 * 450 drives * 4608B / drive = 2073600B of mbufs in the sockbuf
122 *
123 * We can't directly set the sockbuf's mbuf limit, but we can do it indirectly.
124 * The kernel sets it to the minimum of a hard-coded maximum value and sbcc *
125 * kern.ipc.sockbuf_waste_factor, where sbcc is the socket buffer size set by
126 * the user.  The default value of kern.ipc.sockbuf_waste_factor is 8.  If we
127 * set the bufsize to 256k and use the kern.ipc.sockbuf_waste_factor, then the
128 * kernel will set the mbuf limit to 2MB, which is just large enough for 450
129 * drives.  It also happens to be the same as the hardcoded maximum value.
130 */
131#define CLIENT_BUFSIZE 262144
132
133using namespace std;
134
135typedef struct client {
136	int fd;
137	int socktype;
138} client_t;
139
140extern FILE *yyin;
141extern int lineno;
142
143static const char notify = '!';
144static const char nomatch = '?';
145static const char attach = '+';
146static const char detach = '-';
147
148static struct pidfh *pfh;
149
150static int no_daemon = 0;
151static int daemonize_quick = 0;
152static int quiet_mode = 0;
153static unsigned total_events = 0;
154static volatile sig_atomic_t got_siginfo = 0;
155static volatile sig_atomic_t romeo_must_die = 0;
156
157static const char *configfile = CF;
158
159static void devdlog(int priority, const char* message, ...)
160	__printflike(2, 3);
161static void event_loop(void);
162static void usage(void) __dead2;
163
164template <class T> void
165delete_and_clear(vector<T *> &v)
166{
167	typename vector<T *>::const_iterator i;
168
169	for (i = v.begin(); i != v.end(); ++i)
170		delete *i;
171	v.clear();
172}
173
174config cfg;
175
176event_proc::event_proc() : _prio(-1)
177{
178	_epsvec.reserve(4);
179}
180
181event_proc::~event_proc()
182{
183	delete_and_clear(_epsvec);
184}
185
186void
187event_proc::add(eps *eps)
188{
189	_epsvec.push_back(eps);
190}
191
192bool
193event_proc::matches(config &c) const
194{
195	vector<eps *>::const_iterator i;
196
197	for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
198		if (!(*i)->do_match(c))
199			return (false);
200	return (true);
201}
202
203bool
204event_proc::run(config &c) const
205{
206	vector<eps *>::const_iterator i;
207
208	for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
209		if (!(*i)->do_action(c))
210			return (false);
211	return (true);
212}
213
214action::action(const char *cmd)
215	: _cmd(cmd)
216{
217	// nothing
218}
219
220action::~action()
221{
222	// nothing
223}
224
225static int
226my_system(const char *command)
227{
228	pid_t pid, savedpid;
229	int pstat;
230	struct sigaction ign, intact, quitact;
231	sigset_t newsigblock, oldsigblock;
232
233	if (!command)		/* just checking... */
234		return (1);
235
236	/*
237	 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
238	 * existing signal dispositions.
239	 */
240	ign.sa_handler = SIG_IGN;
241	::sigemptyset(&ign.sa_mask);
242	ign.sa_flags = 0;
243	::sigaction(SIGINT, &ign, &intact);
244	::sigaction(SIGQUIT, &ign, &quitact);
245	::sigemptyset(&newsigblock);
246	::sigaddset(&newsigblock, SIGCHLD);
247	::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
248	switch (pid = ::fork()) {
249	case -1:			/* error */
250		break;
251	case 0:				/* child */
252		/*
253		 * Restore original signal dispositions and exec the command.
254		 */
255		::sigaction(SIGINT, &intact, NULL);
256		::sigaction(SIGQUIT,  &quitact, NULL);
257		::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
258		/*
259		 * Close the PID file, and all other open descriptors.
260		 * Inherit std{in,out,err} only.
261		 */
262		cfg.close_pidfile();
263		::closefrom(3);
264		::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
265		::_exit(127);
266	default:			/* parent */
267		savedpid = pid;
268		do {
269			pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0);
270		} while (pid == -1 && errno == EINTR);
271		break;
272	}
273	::sigaction(SIGINT, &intact, NULL);
274	::sigaction(SIGQUIT,  &quitact, NULL);
275	::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
276	return (pid == -1 ? -1 : pstat);
277}
278
279bool
280action::do_action(config &c)
281{
282	string s = c.expand_string(_cmd.c_str());
283	devdlog(LOG_INFO, "Executing '%s'\n", s.c_str());
284	my_system(s.c_str());
285	return (true);
286}
287
288match::match(config &c, const char *var, const char *re) :
289	_inv(re[0] == '!'),
290	_var(var),
291	_re(c.expand_string(_inv ? re + 1 : re, "^", "$"))
292{
293	regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE);
294}
295
296match::~match()
297{
298	regfree(&_regex);
299}
300
301bool
302match::do_match(config &c)
303{
304	const string &value = c.get_variable(_var);
305	bool retval;
306
307	/*
308	 * This function gets called WAY too often to justify calling syslog()
309	 * each time, even at LOG_DEBUG.  Because if syslogd isn't running, it
310	 * can consume excessive amounts of systime inside of connect().  Only
311	 * log when we're in -d mode.
312	 */
313	if (no_daemon) {
314		devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n",
315		    _var.c_str(), value.c_str(), _re.c_str(), _inv);
316	}
317
318	retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
319	if (_inv == 1)
320		retval = (retval == 0) ? 1 : 0;
321
322	return (retval);
323}
324
325#include <sys/sockio.h>
326#include <net/if.h>
327#include <net/if_media.h>
328
329media::media(config &, const char *var, const char *type)
330	: _var(var), _type(-1)
331{
332	static struct ifmedia_description media_types[] = {
333		{ IFM_ETHER,		"Ethernet" },
334		{ IFM_TOKEN,		"Tokenring" },
335		{ IFM_FDDI,		"FDDI" },
336		{ IFM_IEEE80211,	"802.11" },
337		{ IFM_ATM,		"ATM" },
338		{ -1,			"unknown" },
339		{ 0, NULL },
340	};
341	for (int i = 0; media_types[i].ifmt_string != NULL; ++i)
342		if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
343			_type = media_types[i].ifmt_word;
344			break;
345		}
346}
347
348media::~media()
349{
350}
351
352bool
353media::do_match(config &c)
354{
355	string value;
356	struct ifmediareq ifmr;
357	bool retval;
358	int s;
359
360	// Since we can be called from both a device attach/detach
361	// context where device-name is defined and what we want,
362	// as well as from a link status context, where subsystem is
363	// the name of interest, first try device-name and fall back
364	// to subsystem if none exists.
365	value = c.get_variable("device-name");
366	if (value.empty())
367		value = c.get_variable("subsystem");
368	devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n",
369		    value.c_str(), _type);
370
371	retval = false;
372
373	s = socket(PF_INET, SOCK_DGRAM, 0);
374	if (s >= 0) {
375		memset(&ifmr, 0, sizeof(ifmr));
376		strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
377
378		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
379		    ifmr.ifm_status & IFM_AVALID) {
380			devdlog(LOG_DEBUG, "%s has media type 0x%x\n",
381				    value.c_str(), IFM_TYPE(ifmr.ifm_active));
382			retval = (IFM_TYPE(ifmr.ifm_active) == _type);
383		} else if (_type == -1) {
384			devdlog(LOG_DEBUG, "%s has unknown media type\n",
385				    value.c_str());
386			retval = true;
387		}
388		close(s);
389	}
390
391	return (retval);
392}
393
394const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
395const string var_list::nothing = "";
396
397const string &
398var_list::get_variable(const string &var) const
399{
400	map<string, string>::const_iterator i;
401
402	i = _vars.find(var);
403	if (i == _vars.end())
404		return (var_list::bogus);
405	return (i->second);
406}
407
408bool
409var_list::is_set(const string &var) const
410{
411	return (_vars.find(var) != _vars.end());
412}
413
414void
415var_list::set_variable(const string &var, const string &val)
416{
417	/*
418	 * This function gets called WAY too often to justify calling syslog()
419	 * each time, even at LOG_DEBUG.  Because if syslogd isn't running, it
420	 * can consume excessive amounts of systime inside of connect().  Only
421	 * log when we're in -d mode.
422	 */
423	if (no_daemon)
424		devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str());
425	_vars[var] = val;
426}
427
428void
429config::reset(void)
430{
431	_dir_list.clear();
432	delete_and_clear(_var_list_table);
433	delete_and_clear(_attach_list);
434	delete_and_clear(_detach_list);
435	delete_and_clear(_nomatch_list);
436	delete_and_clear(_notify_list);
437}
438
439void
440config::parse_one_file(const char *fn)
441{
442	devdlog(LOG_DEBUG, "Parsing %s\n", fn);
443	yyin = fopen(fn, "r");
444	if (yyin == NULL)
445		err(1, "Cannot open config file %s", fn);
446	lineno = 1;
447	if (yyparse() != 0)
448		errx(1, "Cannot parse %s at line %d", fn, lineno);
449	fclose(yyin);
450}
451
452void
453config::parse_files_in_dir(const char *dirname)
454{
455	DIR *dirp;
456	struct dirent *dp;
457	char path[PATH_MAX];
458
459	devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname);
460	dirp = opendir(dirname);
461	if (dirp == NULL)
462		return;
463	readdir(dirp);		/* Skip . */
464	readdir(dirp);		/* Skip .. */
465	while ((dp = readdir(dirp)) != NULL) {
466		if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
467			snprintf(path, sizeof(path), "%s/%s",
468			    dirname, dp->d_name);
469			parse_one_file(path);
470		}
471	}
472	closedir(dirp);
473}
474
475class epv_greater {
476public:
477	int operator()(event_proc *const&l1, event_proc *const&l2) const
478	{
479		return (l1->get_priority() > l2->get_priority());
480	}
481};
482
483void
484config::sort_vector(vector<event_proc *> &v)
485{
486	stable_sort(v.begin(), v.end(), epv_greater());
487}
488
489void
490config::parse(void)
491{
492	vector<string>::const_iterator i;
493
494	parse_one_file(configfile);
495	for (i = _dir_list.begin(); i != _dir_list.end(); ++i)
496		parse_files_in_dir((*i).c_str());
497	sort_vector(_attach_list);
498	sort_vector(_detach_list);
499	sort_vector(_nomatch_list);
500	sort_vector(_notify_list);
501}
502
503void
504config::open_pidfile()
505{
506	pid_t otherpid;
507
508	if (_pidfile.empty())
509		return;
510	pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
511	if (pfh == NULL) {
512		if (errno == EEXIST)
513			errx(1, "devd already running, pid: %d", (int)otherpid);
514		warn("cannot open pid file");
515	}
516}
517
518void
519config::write_pidfile()
520{
521
522	pidfile_write(pfh);
523}
524
525void
526config::close_pidfile()
527{
528
529	pidfile_close(pfh);
530}
531
532void
533config::remove_pidfile()
534{
535
536	pidfile_remove(pfh);
537}
538
539void
540config::add_attach(int prio, event_proc *p)
541{
542	p->set_priority(prio);
543	_attach_list.push_back(p);
544}
545
546void
547config::add_detach(int prio, event_proc *p)
548{
549	p->set_priority(prio);
550	_detach_list.push_back(p);
551}
552
553void
554config::add_directory(const char *dir)
555{
556	_dir_list.push_back(string(dir));
557}
558
559void
560config::add_nomatch(int prio, event_proc *p)
561{
562	p->set_priority(prio);
563	_nomatch_list.push_back(p);
564}
565
566void
567config::add_notify(int prio, event_proc *p)
568{
569	p->set_priority(prio);
570	_notify_list.push_back(p);
571}
572
573void
574config::set_pidfile(const char *fn)
575{
576	_pidfile = fn;
577}
578
579void
580config::push_var_table()
581{
582	var_list *vl;
583
584	vl = new var_list();
585	_var_list_table.push_back(vl);
586	devdlog(LOG_DEBUG, "Pushing table\n");
587}
588
589void
590config::pop_var_table()
591{
592	delete _var_list_table.back();
593	_var_list_table.pop_back();
594	devdlog(LOG_DEBUG, "Popping table\n");
595}
596
597void
598config::set_variable(const char *var, const char *val)
599{
600	_var_list_table.back()->set_variable(var, val);
601}
602
603const string &
604config::get_variable(const string &var)
605{
606	vector<var_list *>::reverse_iterator i;
607
608	for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) {
609		if ((*i)->is_set(var))
610			return ((*i)->get_variable(var));
611	}
612	return (var_list::nothing);
613}
614
615bool
616config::is_id_char(char ch) const
617{
618	return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
619	    ch == '-'));
620}
621
622void
623config::expand_one(const char *&src, string &dst)
624{
625	int count;
626	string buffer;
627
628	src++;
629	// $$ -> $
630	if (*src == '$') {
631		dst += *src++;
632		return;
633	}
634
635	// $(foo) -> $(foo)
636	// Not sure if I want to support this or not, so for now we just pass
637	// it through.
638	if (*src == '(') {
639		dst += '$';
640		count = 0;
641		/* If the string ends before ) is matched , return. */
642		do {
643			if (*src == ')')
644				count--;
645			else if (*src == '(')
646				count++;
647			dst += *src++;
648		} while (count > 0 && *src);
649		return;
650	}
651
652	// $[^-A-Za-z_*] -> $\1
653	if (!isalpha(*src) && *src != '_' && *src != '-' && *src != '*') {
654		dst += '$';
655		dst += *src++;
656		return;
657	}
658
659	// $var -> replace with value
660	do {
661		buffer += *src++;
662	} while (is_id_char(*src));
663	dst.append(get_variable(buffer));
664}
665
666const string
667config::expand_string(const char *src, const char *prepend, const char *append)
668{
669	const char *var_at;
670	string dst;
671
672	/*
673	 * 128 bytes is enough for 2427 of 2438 expansions that happen
674	 * while parsing config files, as tested on 2013-01-30.
675	 */
676	dst.reserve(128);
677
678	if (prepend != NULL)
679		dst = prepend;
680
681	for (;;) {
682		var_at = strchr(src, '$');
683		if (var_at == NULL) {
684			dst.append(src);
685			break;
686		}
687		dst.append(src, var_at - src);
688		src = var_at;
689		expand_one(src, dst);
690	}
691
692	if (append != NULL)
693		dst.append(append);
694
695	return (dst);
696}
697
698bool
699config::chop_var(char *&buffer, char *&lhs, char *&rhs) const
700{
701	char *walker;
702
703	if (*buffer == '\0')
704		return (false);
705	walker = lhs = buffer;
706	while (is_id_char(*walker))
707		walker++;
708	if (*walker != '=')
709		return (false);
710	walker++;		// skip =
711	if (*walker == '"') {
712		walker++;	// skip "
713		rhs = walker;
714		while (*walker && *walker != '"')
715			walker++;
716		if (*walker != '"')
717			return (false);
718		rhs[-2] = '\0';
719		*walker++ = '\0';
720	} else {
721		rhs = walker;
722		while (*walker && !isspace(*walker))
723			walker++;
724		if (*walker != '\0')
725			*walker++ = '\0';
726		rhs[-1] = '\0';
727	}
728	while (isspace(*walker))
729		walker++;
730	buffer = walker;
731	return (true);
732}
733
734
735char *
736config::set_vars(char *buffer)
737{
738	char *lhs;
739	char *rhs;
740
741	while (1) {
742		if (!chop_var(buffer, lhs, rhs))
743			break;
744		set_variable(lhs, rhs);
745	}
746	return (buffer);
747}
748
749void
750config::find_and_execute(char type)
751{
752	vector<event_proc *> *l;
753	vector<event_proc *>::const_iterator i;
754	const char *s;
755
756	switch (type) {
757	default:
758		return;
759	case notify:
760		l = &_notify_list;
761		s = "notify";
762		break;
763	case nomatch:
764		l = &_nomatch_list;
765		s = "nomatch";
766		break;
767	case attach:
768		l = &_attach_list;
769		s = "attach";
770		break;
771	case detach:
772		l = &_detach_list;
773		s = "detach";
774		break;
775	}
776	devdlog(LOG_DEBUG, "Processing %s event\n", s);
777	for (i = l->begin(); i != l->end(); ++i) {
778		if ((*i)->matches(*this)) {
779			(*i)->run(*this);
780			break;
781		}
782	}
783
784}
785
786
787static void
788process_event(char *buffer)
789{
790	char type;
791	char *sp;
792	struct timeval tv;
793	char *timestr;
794
795	sp = buffer + 1;
796	devdlog(LOG_INFO, "Processing event '%s'\n", buffer);
797	type = *buffer++;
798	cfg.push_var_table();
799	// $* is the entire line
800	cfg.set_variable("*", buffer - 1);
801	// $_ is the entire line without the initial character
802	cfg.set_variable("_", buffer);
803
804	// Save the time this happened (as approximated by when we got
805	// around to processing it).
806	gettimeofday(&tv, NULL);
807	asprintf(&timestr, "%jd.%06ld", (uintmax_t)tv.tv_sec, tv.tv_usec);
808	cfg.set_variable("timestamp", timestr);
809	free(timestr);
810
811	// Match doesn't have a device, and the format is a little
812	// different, so handle it separately.
813	switch (type) {
814	case notify:
815		//! (k=v)*
816		sp = cfg.set_vars(sp);
817		break;
818	case nomatch:
819		//? at location pnp-info on bus
820		sp = strchr(sp, ' ');
821		if (sp == NULL)
822			return;	/* Can't happen? */
823		*sp++ = '\0';
824		while (isspace(*sp))
825			sp++;
826		if (strncmp(sp, "at ", 3) == 0)
827			sp += 3;
828		sp = cfg.set_vars(sp);
829		while (isspace(*sp))
830			sp++;
831		if (strncmp(sp, "on ", 3) == 0)
832			cfg.set_variable("bus", sp + 3);
833		break;
834	case attach:	/*FALLTHROUGH*/
835	case detach:
836		sp = strchr(sp, ' ');
837		if (sp == NULL)
838			return;	/* Can't happen? */
839		*sp++ = '\0';
840		cfg.set_variable("device-name", buffer);
841		while (isspace(*sp))
842			sp++;
843		if (strncmp(sp, "at ", 3) == 0)
844			sp += 3;
845		sp = cfg.set_vars(sp);
846		while (isspace(*sp))
847			sp++;
848		if (strncmp(sp, "on ", 3) == 0)
849			cfg.set_variable("bus", sp + 3);
850		break;
851	}
852
853	cfg.find_and_execute(type);
854	cfg.pop_var_table();
855}
856
857int
858create_socket(const char *name, int socktype)
859{
860	int fd, slen;
861	struct sockaddr_un sun;
862
863	if ((fd = socket(PF_LOCAL, socktype, 0)) < 0)
864		err(1, "socket");
865	bzero(&sun, sizeof(sun));
866	sun.sun_family = AF_UNIX;
867	strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
868	slen = SUN_LEN(&sun);
869	unlink(name);
870	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
871	    	err(1, "fcntl");
872	if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
873		err(1, "bind");
874	listen(fd, 4);
875	if (chown(name, 0, 0))	/* XXX - root.wheel */
876		err(1, "chown");
877	if (chmod(name, 0666))
878		err(1, "chmod");
879	return (fd);
880}
881
882unsigned int max_clients = 10;	/* Default, can be overridden on cmdline. */
883unsigned int num_clients;
884
885list<client_t> clients;
886
887void
888notify_clients(const char *data, int len)
889{
890	list<client_t>::iterator i;
891
892	/*
893	 * Deliver the data to all clients.  Throw clients overboard at the
894	 * first sign of trouble.  This reaps clients who've died or closed
895	 * their sockets, and also clients who are alive but failing to keep up
896	 * (or who are maliciously not reading, to consume buffer space in
897	 * kernel memory or tie up the limited number of available connections).
898	 */
899	for (i = clients.begin(); i != clients.end(); ) {
900		int flags;
901		if (i->socktype == SOCK_SEQPACKET)
902			flags = MSG_EOR;
903		else
904			flags = 0;
905
906		if (send(i->fd, data, len, flags) != len) {
907			--num_clients;
908			close(i->fd);
909			i = clients.erase(i);
910			devdlog(LOG_WARNING, "notify_clients: send() failed; "
911			    "dropping unresponsive client\n");
912		} else
913			++i;
914	}
915}
916
917void
918check_clients(void)
919{
920	int s;
921	struct pollfd pfd;
922	list<client_t>::iterator i;
923
924	/*
925	 * Check all existing clients to see if any of them have disappeared.
926	 * Normally we reap clients when we get an error trying to send them an
927	 * event.  This check eliminates the problem of an ever-growing list of
928	 * zombie clients because we're never writing to them on a system
929	 * without frequent device-change activity.
930	 */
931	pfd.events = 0;
932	for (i = clients.begin(); i != clients.end(); ) {
933		pfd.fd = i->fd;
934		s = poll(&pfd, 1, 0);
935		if ((s < 0 && s != EINTR ) ||
936		    (s > 0 && (pfd.revents & POLLHUP))) {
937			--num_clients;
938			close(i->fd);
939			i = clients.erase(i);
940			devdlog(LOG_NOTICE, "check_clients:  "
941			    "dropping disconnected client\n");
942		} else
943			++i;
944	}
945}
946
947void
948new_client(int fd, int socktype)
949{
950	client_t s;
951	int sndbuf_size;
952
953	/*
954	 * First go reap any zombie clients, then accept the connection, and
955	 * shut down the read side to stop clients from consuming kernel memory
956	 * by sending large buffers full of data we'll never read.
957	 */
958	check_clients();
959	s.socktype = socktype;
960	s.fd = accept(fd, NULL, NULL);
961	if (s.fd != -1) {
962		sndbuf_size = CLIENT_BUFSIZE;
963		if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
964		    sizeof(sndbuf_size)))
965			err(1, "setsockopt");
966		shutdown(s.fd, SHUT_RD);
967		clients.push_back(s);
968		++num_clients;
969	} else
970		err(1, "accept");
971}
972
973static void
974event_loop(void)
975{
976	int rv;
977	int fd;
978	char buffer[DEVCTL_MAXBUF];
979	int once = 0;
980	int stream_fd, seqpacket_fd, max_fd;
981	int accepting;
982	timeval tv;
983	fd_set fds;
984
985	fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC);
986	if (fd == -1)
987		err(1, "Can't open devctl device %s", PATH_DEVCTL);
988	stream_fd = create_socket(STREAMPIPE, SOCK_STREAM);
989	seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET);
990	accepting = 1;
991	max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1;
992	while (!romeo_must_die) {
993		if (!once && !no_daemon && !daemonize_quick) {
994			// Check to see if we have any events pending.
995			tv.tv_sec = 0;
996			tv.tv_usec = 0;
997			FD_ZERO(&fds);
998			FD_SET(fd, &fds);
999			rv = select(fd + 1, &fds, &fds, &fds, &tv);
1000			// No events -> we've processed all pending events
1001			if (rv == 0) {
1002				devdlog(LOG_DEBUG, "Calling daemon\n");
1003				cfg.remove_pidfile();
1004				cfg.open_pidfile();
1005				daemon(0, 0);
1006				cfg.write_pidfile();
1007				once++;
1008			}
1009		}
1010		/*
1011		 * When we've already got the max number of clients, stop
1012		 * accepting new connections (don't put the listening sockets in
1013		 * the set), shrink the accept() queue to reject connections
1014		 * quickly, and poll the existing clients more often, so that we
1015		 * notice more quickly when any of them disappear to free up
1016		 * client slots.
1017		 */
1018		FD_ZERO(&fds);
1019		FD_SET(fd, &fds);
1020		if (num_clients < max_clients) {
1021			if (!accepting) {
1022				listen(stream_fd, max_clients);
1023				listen(seqpacket_fd, max_clients);
1024				accepting = 1;
1025			}
1026			FD_SET(stream_fd, &fds);
1027			FD_SET(seqpacket_fd, &fds);
1028			tv.tv_sec = 60;
1029			tv.tv_usec = 0;
1030		} else {
1031			if (accepting) {
1032				listen(stream_fd, 0);
1033				listen(seqpacket_fd, 0);
1034				accepting = 0;
1035			}
1036			tv.tv_sec = 2;
1037			tv.tv_usec = 0;
1038		}
1039		rv = select(max_fd, &fds, NULL, NULL, &tv);
1040		if (got_siginfo) {
1041			devdlog(LOG_NOTICE, "Events received so far=%u\n",
1042			    total_events);
1043			got_siginfo = 0;
1044		}
1045		if (rv == -1) {
1046			if (errno == EINTR)
1047				continue;
1048			err(1, "select");
1049		} else if (rv == 0)
1050			check_clients();
1051		if (FD_ISSET(fd, &fds)) {
1052			rv = read(fd, buffer, sizeof(buffer) - 1);
1053			if (rv > 0) {
1054				total_events++;
1055				if (rv == sizeof(buffer) - 1) {
1056					devdlog(LOG_WARNING, "Warning: "
1057					    "available event data exceeded "
1058					    "buffer space\n");
1059				}
1060				notify_clients(buffer, rv);
1061				buffer[rv] = '\0';
1062				while (buffer[--rv] == '\n')
1063					buffer[rv] = '\0';
1064				try {
1065					process_event(buffer);
1066				}
1067				catch (std::length_error e) {
1068					devdlog(LOG_ERR, "Dropping event %s "
1069					    "due to low memory", buffer);
1070				}
1071			} else if (rv < 0) {
1072				if (errno != EINTR)
1073					break;
1074			} else {
1075				/* EOF */
1076				break;
1077			}
1078		}
1079		if (FD_ISSET(stream_fd, &fds))
1080			new_client(stream_fd, SOCK_STREAM);
1081		/*
1082		 * Aside from the socket type, both sockets use the same
1083		 * protocol, so we can process clients the same way.
1084		 */
1085		if (FD_ISSET(seqpacket_fd, &fds))
1086			new_client(seqpacket_fd, SOCK_SEQPACKET);
1087	}
1088	cfg.remove_pidfile();
1089	close(seqpacket_fd);
1090	close(stream_fd);
1091	close(fd);
1092}
1093
1094/*
1095 * functions that the parser uses.
1096 */
1097void
1098add_attach(int prio, event_proc *p)
1099{
1100	cfg.add_attach(prio, p);
1101}
1102
1103void
1104add_detach(int prio, event_proc *p)
1105{
1106	cfg.add_detach(prio, p);
1107}
1108
1109void
1110add_directory(const char *dir)
1111{
1112	cfg.add_directory(dir);
1113	free(const_cast<char *>(dir));
1114}
1115
1116void
1117add_nomatch(int prio, event_proc *p)
1118{
1119	cfg.add_nomatch(prio, p);
1120}
1121
1122void
1123add_notify(int prio, event_proc *p)
1124{
1125	cfg.add_notify(prio, p);
1126}
1127
1128event_proc *
1129add_to_event_proc(event_proc *ep, eps *eps)
1130{
1131	if (ep == NULL)
1132		ep = new event_proc();
1133	ep->add(eps);
1134	return (ep);
1135}
1136
1137eps *
1138new_action(const char *cmd)
1139{
1140	eps *e = new action(cmd);
1141	free(const_cast<char *>(cmd));
1142	return (e);
1143}
1144
1145eps *
1146new_match(const char *var, const char *re)
1147{
1148	eps *e = new match(cfg, var, re);
1149	free(const_cast<char *>(var));
1150	free(const_cast<char *>(re));
1151	return (e);
1152}
1153
1154eps *
1155new_media(const char *var, const char *re)
1156{
1157	eps *e = new media(cfg, var, re);
1158	free(const_cast<char *>(var));
1159	free(const_cast<char *>(re));
1160	return (e);
1161}
1162
1163void
1164set_pidfile(const char *name)
1165{
1166	cfg.set_pidfile(name);
1167	free(const_cast<char *>(name));
1168}
1169
1170void
1171set_variable(const char *var, const char *val)
1172{
1173	cfg.set_variable(var, val);
1174	free(const_cast<char *>(var));
1175	free(const_cast<char *>(val));
1176}
1177
1178
1179
1180static void
1181gensighand(int)
1182{
1183	romeo_must_die = 1;
1184}
1185
1186/*
1187 * SIGINFO handler.  Will print useful statistics to the syslog or stderr
1188 * as appropriate
1189 */
1190static void
1191siginfohand(int)
1192{
1193	got_siginfo = 1;
1194}
1195
1196/*
1197 * Local logging function.  Prints to syslog if we're daemonized; stderr
1198 * otherwise.
1199 */
1200static void
1201devdlog(int priority, const char* fmt, ...)
1202{
1203	va_list argp;
1204
1205	va_start(argp, fmt);
1206	if (no_daemon)
1207		vfprintf(stderr, fmt, argp);
1208	else if ((! quiet_mode) || (priority <= LOG_WARNING))
1209		vsyslog(priority, fmt, argp);
1210	va_end(argp);
1211}
1212
1213static void
1214usage()
1215{
1216	fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n",
1217	    getprogname());
1218	exit(1);
1219}
1220
1221static void
1222check_devd_enabled()
1223{
1224	int val = 0;
1225	size_t len;
1226
1227	len = sizeof(val);
1228	if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
1229		errx(1, "devctl sysctl missing from kernel!");
1230	if (val == 0) {
1231		warnx("Setting " SYSCTL " to 1000");
1232		val = 1000;
1233		if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)))
1234			err(1, "sysctlbyname");
1235	}
1236}
1237
1238/*
1239 * main
1240 */
1241int
1242main(int argc, char **argv)
1243{
1244	int ch;
1245
1246	check_devd_enabled();
1247	while ((ch = getopt(argc, argv, "df:l:nq")) != -1) {
1248		switch (ch) {
1249		case 'd':
1250			no_daemon = 1;
1251			break;
1252		case 'f':
1253			configfile = optarg;
1254			break;
1255		case 'l':
1256			max_clients = MAX(1, strtoul(optarg, NULL, 0));
1257			break;
1258		case 'n':
1259			daemonize_quick = 1;
1260			break;
1261		case 'q':
1262			quiet_mode = 1;
1263			break;
1264		default:
1265			usage();
1266		}
1267	}
1268
1269	cfg.parse();
1270	if (!no_daemon && daemonize_quick) {
1271		cfg.open_pidfile();
1272		daemon(0, 0);
1273		cfg.write_pidfile();
1274	}
1275	signal(SIGPIPE, SIG_IGN);
1276	signal(SIGHUP, gensighand);
1277	signal(SIGINT, gensighand);
1278	signal(SIGTERM, gensighand);
1279	signal(SIGINFO, siginfohand);
1280	event_loop();
1281	return (0);
1282}
1283