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