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