xref: /freebsd/sbin/devd/devd.cc (revision 9a14aa017b21c292740c00ee098195cd46642730)
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$");
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 <ctype.h>
77 #include <dirent.h>
78 #include <errno.h>
79 #include <err.h>
80 #include <fcntl.h>
81 #include <libutil.h>
82 #include <paths.h>
83 #include <regex.h>
84 #include <signal.h>
85 #include <stdlib.h>
86 #include <stdio.h>
87 #include <string.h>
88 #include <unistd.h>
89 
90 #include <algorithm>
91 #include <map>
92 #include <string>
93 #include <list>
94 #include <vector>
95 
96 #include "devd.h"		/* C compatible definitions */
97 #include "devd.hh"		/* C++ class definitions */
98 
99 #define PIPE "/var/run/devd.pipe"
100 #define CF "/etc/devd.conf"
101 #define SYSCTL "hw.bus.devctl_disable"
102 
103 using namespace std;
104 
105 extern FILE *yyin;
106 extern int lineno;
107 
108 static const char notify = '!';
109 static const char nomatch = '?';
110 static const char attach = '+';
111 static const char detach = '-';
112 
113 static struct pidfh *pfh;
114 
115 int Dflag;
116 int dflag;
117 int nflag;
118 int romeo_must_die = 0;
119 
120 static const char *configfile = CF;
121 
122 static void event_loop(void);
123 static void usage(void);
124 
125 template <class T> void
126 delete_and_clear(vector<T *> &v)
127 {
128 	typename vector<T *>::const_iterator i;
129 
130 	for (i = v.begin(); i != v.end(); i++)
131 		delete *i;
132 	v.clear();
133 }
134 
135 config cfg;
136 
137 event_proc::event_proc() : _prio(-1)
138 {
139 	// nothing
140 }
141 
142 event_proc::~event_proc()
143 {
144 	delete_and_clear(_epsvec);
145 }
146 
147 void
148 event_proc::add(eps *eps)
149 {
150 	_epsvec.push_back(eps);
151 }
152 
153 bool
154 event_proc::matches(config &c)
155 {
156 	vector<eps *>::const_iterator i;
157 
158 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
159 		if (!(*i)->do_match(c))
160 			return (false);
161 	return (true);
162 }
163 
164 bool
165 event_proc::run(config &c)
166 {
167 	vector<eps *>::const_iterator i;
168 
169 	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
170 		if (!(*i)->do_action(c))
171 			return (false);
172 	return (true);
173 }
174 
175 action::action(const char *cmd)
176 	: _cmd(cmd)
177 {
178 	// nothing
179 }
180 
181 action::~action()
182 {
183 	// nothing
184 }
185 
186 static int
187 my_system(const char *command)
188 {
189 	pid_t pid, savedpid;
190 	int pstat;
191 	struct sigaction ign, intact, quitact;
192 	sigset_t newsigblock, oldsigblock;
193 
194 	if (!command)		/* just checking... */
195 		return(1);
196 
197 	/*
198 	 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
199 	 * existing signal dispositions.
200 	 */
201 	ign.sa_handler = SIG_IGN;
202 	::sigemptyset(&ign.sa_mask);
203 	ign.sa_flags = 0;
204 	::sigaction(SIGINT, &ign, &intact);
205 	::sigaction(SIGQUIT, &ign, &quitact);
206 	::sigemptyset(&newsigblock);
207 	::sigaddset(&newsigblock, SIGCHLD);
208 	::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
209 	switch (pid = ::fork()) {
210 	case -1:			/* error */
211 		break;
212 	case 0:				/* child */
213 		/*
214 		 * Restore original signal dispositions and exec the command.
215 		 */
216 		::sigaction(SIGINT, &intact, NULL);
217 		::sigaction(SIGQUIT,  &quitact, NULL);
218 		::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
219 		/*
220 		 * Close the PID file, and all other open descriptors.
221 		 * Inherit std{in,out,err} only.
222 		 */
223 		cfg.close_pidfile();
224 		::closefrom(3);
225 		::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
226 		::_exit(127);
227 	default:			/* parent */
228 		savedpid = pid;
229 		do {
230 			pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0);
231 		} while (pid == -1 && errno == EINTR);
232 		break;
233 	}
234 	::sigaction(SIGINT, &intact, NULL);
235 	::sigaction(SIGQUIT,  &quitact, NULL);
236 	::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
237 	return (pid == -1 ? -1 : pstat);
238 }
239 
240 bool
241 action::do_action(config &c)
242 {
243 	string s = c.expand_string(_cmd);
244 	if (Dflag)
245 		fprintf(stderr, "Executing '%s'\n", s.c_str());
246 	my_system(s.c_str());
247 	return (true);
248 }
249 
250 match::match(config &c, const char *var, const char *re)
251 	: _var(var)
252 {
253 	_re = "^";
254 	if (!c.expand_string(string(re)).empty() &&
255 	    c.expand_string(string(re)).at(0) == '!') {
256 		_re.append(c.expand_string(string(re)).substr(1));
257 		_inv = 1;
258 	} else {
259 		_re.append(c.expand_string(string(re)));
260 		_inv = 0;
261 	}
262 	_re.append("$");
263 	regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE);
264 }
265 
266 match::~match()
267 {
268 	regfree(&_regex);
269 }
270 
271 bool
272 match::do_match(config &c)
273 {
274 	const string &value = c.get_variable(_var);
275 	bool retval;
276 
277 	if (Dflag)
278 		fprintf(stderr, "Testing %s=%s against %s, invert=%d\n",
279 		    _var.c_str(), value.c_str(), _re.c_str(), _inv);
280 
281 	retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
282 	if (_inv == 1)
283 		retval = (retval == 0) ? 1 : 0;
284 
285 	return retval;
286 }
287 
288 #include <sys/sockio.h>
289 #include <net/if.h>
290 #include <net/if_media.h>
291 
292 media::media(config &, const char *var, const char *type)
293 	: _var(var), _type(-1)
294 {
295 	static struct ifmedia_description media_types[] = {
296 		{ IFM_ETHER,		"Ethernet" },
297 		{ IFM_TOKEN,		"Tokenring" },
298 		{ IFM_FDDI,		"FDDI" },
299 		{ IFM_IEEE80211,	"802.11" },
300 		{ IFM_ATM,		"ATM" },
301 		{ -1,			"unknown" },
302 		{ 0, NULL },
303 	};
304 	for (int i = 0; media_types[i].ifmt_string != NULL; i++)
305 		if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
306 			_type = media_types[i].ifmt_word;
307 			break;
308 		}
309 }
310 
311 media::~media()
312 {
313 }
314 
315 bool
316 media::do_match(config &c)
317 {
318 	string value;
319 	struct ifmediareq ifmr;
320 	bool retval;
321 	int s;
322 
323 	// Since we can be called from both a device attach/detach
324 	// context where device-name is defined and what we want,
325 	// as well as from a link status context, where subsystem is
326 	// the name of interest, first try device-name and fall back
327 	// to subsystem if none exists.
328 	value = c.get_variable("device-name");
329 	if (value.length() == 0)
330 		value = c.get_variable("subsystem");
331 	if (Dflag)
332 		fprintf(stderr, "Testing media type of %s against 0x%x\n",
333 		    value.c_str(), _type);
334 
335 	retval = false;
336 
337 	s = socket(PF_INET, SOCK_DGRAM, 0);
338 	if (s >= 0) {
339 		memset(&ifmr, 0, sizeof(ifmr));
340 		strncpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
341 
342 		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
343 		    ifmr.ifm_status & IFM_AVALID) {
344 			if (Dflag)
345 				fprintf(stderr, "%s has media type 0x%x\n",
346 				    value.c_str(), IFM_TYPE(ifmr.ifm_active));
347 			retval = (IFM_TYPE(ifmr.ifm_active) == _type);
348 		} else if (_type == -1) {
349 			if (Dflag)
350 				fprintf(stderr, "%s has unknown media type\n",
351 				    value.c_str());
352 			retval = true;
353 		}
354 		close(s);
355 	}
356 
357 	return retval;
358 }
359 
360 const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
361 const string var_list::nothing = "";
362 
363 const string &
364 var_list::get_variable(const string &var) const
365 {
366 	map<string, string>::const_iterator i;
367 
368 	i = _vars.find(var);
369 	if (i == _vars.end())
370 		return (var_list::bogus);
371 	return (i->second);
372 }
373 
374 bool
375 var_list::is_set(const string &var) const
376 {
377 	return (_vars.find(var) != _vars.end());
378 }
379 
380 void
381 var_list::set_variable(const string &var, const string &val)
382 {
383 	if (Dflag)
384 		fprintf(stderr, "setting %s=%s\n", var.c_str(), val.c_str());
385 	_vars[var] = val;
386 }
387 
388 void
389 config::reset(void)
390 {
391 	_dir_list.clear();
392 	delete_and_clear(_var_list_table);
393 	delete_and_clear(_attach_list);
394 	delete_and_clear(_detach_list);
395 	delete_and_clear(_nomatch_list);
396 	delete_and_clear(_notify_list);
397 }
398 
399 void
400 config::parse_one_file(const char *fn)
401 {
402 	if (Dflag)
403 		fprintf(stderr, "Parsing %s\n", fn);
404 	yyin = fopen(fn, "r");
405 	if (yyin == NULL)
406 		err(1, "Cannot open config file %s", fn);
407 	lineno = 1;
408 	if (yyparse() != 0)
409 		errx(1, "Cannot parse %s at line %d", fn, lineno);
410 	fclose(yyin);
411 }
412 
413 void
414 config::parse_files_in_dir(const char *dirname)
415 {
416 	DIR *dirp;
417 	struct dirent *dp;
418 	char path[PATH_MAX];
419 
420 	if (Dflag)
421 		fprintf(stderr, "Parsing files in %s\n", dirname);
422 	dirp = opendir(dirname);
423 	if (dirp == NULL)
424 		return;
425 	readdir(dirp);		/* Skip . */
426 	readdir(dirp);		/* Skip .. */
427 	while ((dp = readdir(dirp)) != NULL) {
428 		if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
429 			snprintf(path, sizeof(path), "%s/%s",
430 			    dirname, dp->d_name);
431 			parse_one_file(path);
432 		}
433 	}
434 	closedir(dirp);
435 }
436 
437 class epv_greater {
438 public:
439 	int operator()(event_proc *const&l1, event_proc *const&l2)
440 	{
441 		return (l1->get_priority() > l2->get_priority());
442 	}
443 };
444 
445 void
446 config::sort_vector(vector<event_proc *> &v)
447 {
448 	sort(v.begin(), v.end(), epv_greater());
449 }
450 
451 void
452 config::parse(void)
453 {
454 	vector<string>::const_iterator i;
455 
456 	parse_one_file(configfile);
457 	for (i = _dir_list.begin(); i != _dir_list.end(); i++)
458 		parse_files_in_dir((*i).c_str());
459 	sort_vector(_attach_list);
460 	sort_vector(_detach_list);
461 	sort_vector(_nomatch_list);
462 	sort_vector(_notify_list);
463 }
464 
465 void
466 config::open_pidfile()
467 {
468 	pid_t otherpid;
469 
470 	if (_pidfile == "")
471 		return;
472 	pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
473 	if (pfh == NULL) {
474 		if (errno == EEXIST)
475 			errx(1, "devd already running, pid: %d", (int)otherpid);
476 		warn("cannot open pid file");
477 	}
478 }
479 
480 void
481 config::write_pidfile()
482 {
483 
484 	pidfile_write(pfh);
485 }
486 
487 void
488 config::close_pidfile()
489 {
490 
491 	pidfile_close(pfh);
492 }
493 
494 void
495 config::remove_pidfile()
496 {
497 
498 	pidfile_remove(pfh);
499 }
500 
501 void
502 config::add_attach(int prio, event_proc *p)
503 {
504 	p->set_priority(prio);
505 	_attach_list.push_back(p);
506 }
507 
508 void
509 config::add_detach(int prio, event_proc *p)
510 {
511 	p->set_priority(prio);
512 	_detach_list.push_back(p);
513 }
514 
515 void
516 config::add_directory(const char *dir)
517 {
518 	_dir_list.push_back(string(dir));
519 }
520 
521 void
522 config::add_nomatch(int prio, event_proc *p)
523 {
524 	p->set_priority(prio);
525 	_nomatch_list.push_back(p);
526 }
527 
528 void
529 config::add_notify(int prio, event_proc *p)
530 {
531 	p->set_priority(prio);
532 	_notify_list.push_back(p);
533 }
534 
535 void
536 config::set_pidfile(const char *fn)
537 {
538 	_pidfile = string(fn);
539 }
540 
541 void
542 config::push_var_table()
543 {
544 	var_list *vl;
545 
546 	vl = new var_list();
547 	_var_list_table.push_back(vl);
548 	if (Dflag)
549 		fprintf(stderr, "Pushing table\n");
550 }
551 
552 void
553 config::pop_var_table()
554 {
555 	delete _var_list_table.back();
556 	_var_list_table.pop_back();
557 	if (Dflag)
558 		fprintf(stderr, "Popping table\n");
559 }
560 
561 void
562 config::set_variable(const char *var, const char *val)
563 {
564 	_var_list_table.back()->set_variable(var, val);
565 }
566 
567 const string &
568 config::get_variable(const string &var)
569 {
570 	vector<var_list *>::reverse_iterator i;
571 
572 	for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); i++) {
573 		if ((*i)->is_set(var))
574 			return ((*i)->get_variable(var));
575 	}
576 	return (var_list::nothing);
577 }
578 
579 bool
580 config::is_id_char(char ch)
581 {
582 	return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
583 	    ch == '-'));
584 }
585 
586 void
587 config::expand_one(const char *&src, string &dst)
588 {
589 	int count;
590 	string buffer;
591 
592 	src++;
593 	// $$ -> $
594 	if (*src == '$') {
595 		dst.append(src++, 1);
596 		return;
597 	}
598 
599 	// $(foo) -> $(foo)
600 	// Not sure if I want to support this or not, so for now we just pass
601 	// it through.
602 	if (*src == '(') {
603 		dst.append("$");
604 		count = 1;
605 		/* If the string ends before ) is matched , return. */
606 		while (count > 0 && *src) {
607 			if (*src == ')')
608 				count--;
609 			else if (*src == '(')
610 				count++;
611 			dst.append(src++, 1);
612 		}
613 		return;
614 	}
615 
616 	// ${^A-Za-z] -> $\1
617 	if (!isalpha(*src)) {
618 		dst.append("$");
619 		dst.append(src++, 1);
620 		return;
621 	}
622 
623 	// $var -> replace with value
624 	do {
625 		buffer.append(src++, 1);
626 	} while (is_id_char(*src));
627 	buffer.append("", 1);
628 	dst.append(get_variable(buffer.c_str()));
629 }
630 
631 const string
632 config::expand_string(const string &s)
633 {
634 	const char *src;
635 	string dst;
636 
637 	src = s.c_str();
638 	while (*src) {
639 		if (*src == '$')
640 			expand_one(src, dst);
641 		else
642 			dst.append(src++, 1);
643 	}
644 	dst.append("", 1);
645 
646 	return (dst);
647 }
648 
649 bool
650 config::chop_var(char *&buffer, char *&lhs, char *&rhs)
651 {
652 	char *walker;
653 
654 	if (*buffer == '\0')
655 		return (false);
656 	walker = lhs = buffer;
657 	while (is_id_char(*walker))
658 		walker++;
659 	if (*walker != '=')
660 		return (false);
661 	walker++;		// skip =
662 	if (*walker == '"') {
663 		walker++;	// skip "
664 		rhs = walker;
665 		while (*walker && *walker != '"')
666 			walker++;
667 		if (*walker != '"')
668 			return (false);
669 		rhs[-2] = '\0';
670 		*walker++ = '\0';
671 	} else {
672 		rhs = walker;
673 		while (*walker && !isspace(*walker))
674 			walker++;
675 		if (*walker != '\0')
676 			*walker++ = '\0';
677 		rhs[-1] = '\0';
678 	}
679 	while (isspace(*walker))
680 		walker++;
681 	buffer = walker;
682 	return (true);
683 }
684 
685 
686 char *
687 config::set_vars(char *buffer)
688 {
689 	char *lhs;
690 	char *rhs;
691 
692 	while (1) {
693 		if (!chop_var(buffer, lhs, rhs))
694 			break;
695 		set_variable(lhs, rhs);
696 	}
697 	return (buffer);
698 }
699 
700 void
701 config::find_and_execute(char type)
702 {
703 	vector<event_proc *> *l;
704 	vector<event_proc *>::const_iterator i;
705 	const char *s;
706 
707 	switch (type) {
708 	default:
709 		return;
710 	case notify:
711 		l = &_notify_list;
712 		s = "notify";
713 		break;
714 	case nomatch:
715 		l = &_nomatch_list;
716 		s = "nomatch";
717 		break;
718 	case attach:
719 		l = &_attach_list;
720 		s = "attach";
721 		break;
722 	case detach:
723 		l = &_detach_list;
724 		s = "detach";
725 		break;
726 	}
727 	if (Dflag)
728 		fprintf(stderr, "Processing %s event\n", s);
729 	for (i = l->begin(); i != l->end(); i++) {
730 		if ((*i)->matches(*this)) {
731 			(*i)->run(*this);
732 			break;
733 		}
734 	}
735 
736 }
737 
738 
739 static void
740 process_event(char *buffer)
741 {
742 	char type;
743 	char *sp;
744 
745 	sp = buffer + 1;
746 	if (Dflag)
747 		fprintf(stderr, "Processing event '%s'\n", buffer);
748 	type = *buffer++;
749 	cfg.push_var_table();
750 	// No match doesn't have a device, and the format is a little
751 	// different, so handle it separately.
752 	switch (type) {
753 	case notify:
754 		sp = cfg.set_vars(sp);
755 		break;
756 	case nomatch:
757 		//? at location pnp-info on bus
758 		sp = strchr(sp, ' ');
759 		if (sp == NULL)
760 			return;	/* Can't happen? */
761 		*sp++ = '\0';
762 		while (isspace(*sp))
763 			sp++;
764 		if (strncmp(sp, "at ", 3) == 0)
765 			sp += 3;
766 		sp = cfg.set_vars(sp);
767 		while (isspace(*sp))
768 			sp++;
769 		if (strncmp(sp, "on ", 3) == 0)
770 			cfg.set_variable("bus", sp + 3);
771 		break;
772 	case attach:	/*FALLTHROUGH*/
773 	case detach:
774 		sp = strchr(sp, ' ');
775 		if (sp == NULL)
776 			return;	/* Can't happen? */
777 		*sp++ = '\0';
778 		cfg.set_variable("device-name", buffer);
779 		while (isspace(*sp))
780 			sp++;
781 		if (strncmp(sp, "at ", 3) == 0)
782 			sp += 3;
783 		sp = cfg.set_vars(sp);
784 		while (isspace(*sp))
785 			sp++;
786 		if (strncmp(sp, "on ", 3) == 0)
787 			cfg.set_variable("bus", sp + 3);
788 		break;
789 	}
790 
791 	cfg.find_and_execute(type);
792 	cfg.pop_var_table();
793 }
794 
795 int
796 create_socket(const char *name)
797 {
798 	int fd, slen;
799 	struct sockaddr_un sun;
800 
801 	if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
802 		err(1, "socket");
803 	bzero(&sun, sizeof(sun));
804 	sun.sun_family = AF_UNIX;
805 	strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
806 	slen = SUN_LEN(&sun);
807 	unlink(name);
808 	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
809 	    	err(1, "fcntl");
810 	if (bind(fd, (struct sockaddr *) & sun, slen) < 0)
811 		err(1, "bind");
812 	listen(fd, 4);
813 	chown(name, 0, 0);	/* XXX - root.wheel */
814 	chmod(name, 0666);
815 	return (fd);
816 }
817 
818 list<int> clients;
819 
820 void
821 notify_clients(const char *data, int len)
822 {
823 	list<int> bad;
824 	list<int>::const_iterator i;
825 
826 	for (i = clients.begin(); i != clients.end(); i++) {
827 		if (write(*i, data, len) <= 0) {
828 			bad.push_back(*i);
829 			close(*i);
830 		}
831 	}
832 
833 	for (i = bad.begin(); i != bad.end(); i++)
834 		clients.erase(find(clients.begin(), clients.end(), *i));
835 }
836 
837 void
838 new_client(int fd)
839 {
840 	int s;
841 
842 	s = accept(fd, NULL, NULL);
843 	if (s != -1)
844 		clients.push_back(s);
845 }
846 
847 static void
848 event_loop(void)
849 {
850 	int rv;
851 	int fd;
852 	char buffer[DEVCTL_MAXBUF];
853 	int once = 0;
854 	int server_fd, max_fd;
855 	timeval tv;
856 	fd_set fds;
857 
858 	fd = open(PATH_DEVCTL, O_RDONLY);
859 	if (fd == -1)
860 		err(1, "Can't open devctl device %s", PATH_DEVCTL);
861 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
862 		err(1, "Can't set close-on-exec flag on devctl");
863 	server_fd = create_socket(PIPE);
864 	max_fd = max(fd, server_fd) + 1;
865 	while (1) {
866 		if (romeo_must_die)
867 			break;
868 		if (!once && !dflag && !nflag) {
869 			// Check to see if we have any events pending.
870 			tv.tv_sec = 0;
871 			tv.tv_usec = 0;
872 			FD_ZERO(&fds);
873 			FD_SET(fd, &fds);
874 			rv = select(fd + 1, &fds, &fds, &fds, &tv);
875 			// No events -> we've processed all pending events
876 			if (rv == 0) {
877 				if (Dflag)
878 					fprintf(stderr, "Calling daemon\n");
879 				cfg.remove_pidfile();
880 				cfg.open_pidfile();
881 				daemon(0, 0);
882 				cfg.write_pidfile();
883 				once++;
884 			}
885 		}
886 		FD_ZERO(&fds);
887 		FD_SET(fd, &fds);
888 		FD_SET(server_fd, &fds);
889 		rv = select(max_fd, &fds, NULL, NULL, NULL);
890 		if (rv == -1) {
891 			if (errno == EINTR)
892 				continue;
893 			err(1, "select");
894 		}
895 		if (FD_ISSET(fd, &fds)) {
896 			rv = read(fd, buffer, sizeof(buffer) - 1);
897 			if (rv > 0) {
898 				notify_clients(buffer, rv);
899 				buffer[rv] = '\0';
900 				while (buffer[--rv] == '\n')
901 					buffer[rv] = '\0';
902 				process_event(buffer);
903 			} else if (rv < 0) {
904 				if (errno != EINTR)
905 					break;
906 			} else {
907 				/* EOF */
908 				break;
909 			}
910 		}
911 		if (FD_ISSET(server_fd, &fds))
912 			new_client(server_fd);
913 	}
914 	close(fd);
915 }
916 
917 /*
918  * functions that the parser uses.
919  */
920 void
921 add_attach(int prio, event_proc *p)
922 {
923 	cfg.add_attach(prio, p);
924 }
925 
926 void
927 add_detach(int prio, event_proc *p)
928 {
929 	cfg.add_detach(prio, p);
930 }
931 
932 void
933 add_directory(const char *dir)
934 {
935 	cfg.add_directory(dir);
936 	free(const_cast<char *>(dir));
937 }
938 
939 void
940 add_nomatch(int prio, event_proc *p)
941 {
942 	cfg.add_nomatch(prio, p);
943 }
944 
945 void
946 add_notify(int prio, event_proc *p)
947 {
948 	cfg.add_notify(prio, p);
949 }
950 
951 event_proc *
952 add_to_event_proc(event_proc *ep, eps *eps)
953 {
954 	if (ep == NULL)
955 		ep = new event_proc();
956 	ep->add(eps);
957 	return (ep);
958 }
959 
960 eps *
961 new_action(const char *cmd)
962 {
963 	eps *e = new action(cmd);
964 	free(const_cast<char *>(cmd));
965 	return (e);
966 }
967 
968 eps *
969 new_match(const char *var, const char *re)
970 {
971 	eps *e = new match(cfg, var, re);
972 	free(const_cast<char *>(var));
973 	free(const_cast<char *>(re));
974 	return (e);
975 }
976 
977 eps *
978 new_media(const char *var, const char *re)
979 {
980 	eps *e = new media(cfg, var, re);
981 	free(const_cast<char *>(var));
982 	free(const_cast<char *>(re));
983 	return (e);
984 }
985 
986 void
987 set_pidfile(const char *name)
988 {
989 	cfg.set_pidfile(name);
990 	free(const_cast<char *>(name));
991 }
992 
993 void
994 set_variable(const char *var, const char *val)
995 {
996 	cfg.set_variable(var, val);
997 	free(const_cast<char *>(var));
998 	free(const_cast<char *>(val));
999 }
1000 
1001 
1002 
1003 static void
1004 gensighand(int)
1005 {
1006 	romeo_must_die++;
1007 	_exit(0);
1008 }
1009 
1010 static void
1011 usage()
1012 {
1013 	fprintf(stderr, "usage: %s [-Ddn] [-f file]\n", getprogname());
1014 	exit(1);
1015 }
1016 
1017 static void
1018 check_devd_enabled()
1019 {
1020 	int val = 0;
1021 	size_t len;
1022 
1023 	len = sizeof(val);
1024 	if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
1025 		errx(1, "devctl sysctl missing from kernel!");
1026 	if (val) {
1027 		warnx("Setting " SYSCTL " to 0");
1028 		val = 0;
1029 		sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
1030 	}
1031 }
1032 
1033 /*
1034  * main
1035  */
1036 int
1037 main(int argc, char **argv)
1038 {
1039 	int ch;
1040 
1041 	check_devd_enabled();
1042 	while ((ch = getopt(argc, argv, "Ddf:n")) != -1) {
1043 		switch (ch) {
1044 		case 'D':
1045 			Dflag++;
1046 			break;
1047 		case 'd':
1048 			dflag++;
1049 			break;
1050 		case 'f':
1051 			configfile = optarg;
1052 			break;
1053 		case 'n':
1054 			nflag++;
1055 			break;
1056 		default:
1057 			usage();
1058 		}
1059 	}
1060 
1061 	cfg.parse();
1062 	if (!dflag && nflag) {
1063 		cfg.open_pidfile();
1064 		daemon(0, 0);
1065 		cfg.write_pidfile();
1066 	}
1067 	signal(SIGPIPE, SIG_IGN);
1068 	signal(SIGHUP, gensighand);
1069 	signal(SIGINT, gensighand);
1070 	signal(SIGTERM, gensighand);
1071 	event_loop();
1072 	return (0);
1073 }
1074