xref: /freebsd/sbin/devd/devd.cc (revision 716dfa4cb85cd32e18ed3a8b01404f7c540bbf6d)
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 (bind(fd, (struct sockaddr *) & sun, slen) < 0)
676 		err(1, "bind");
677 	listen(fd, 4);
678 	fchown(fd, 0, 0);	/* XXX - root.wheel */
679 	fchmod(fd, 0660);
680 	return (fd);
681 }
682 
683 list<int> clients;
684 
685 void
686 notify_clients(const char *data, int len)
687 {
688 	list<int> bad;
689 	list<int>::const_iterator i;
690 
691 	for (i = clients.begin(); i != clients.end(); i++) {
692 		if (write(*i, data, len) <= 0) {
693 			bad.push_back(*i);
694 			close(*i);
695 		}
696 	}
697 
698 	for (i = bad.begin(); i != bad.end(); i++)
699 		clients.erase(find(clients.begin(), clients.end(), *i));
700 }
701 
702 void
703 new_client(int fd)
704 {
705 	int s;
706 
707 	s = accept(fd, NULL, NULL);
708 	if (s != -1)
709 		clients.push_back(s);
710 }
711 
712 static void
713 event_loop(void)
714 {
715 	int rv;
716 	int fd;
717 	char buffer[DEVCTL_MAXBUF];
718 	int once = 0;
719 	int server_fd, max_fd;
720 	timeval tv;
721 	fd_set fds;
722 
723 	fd = open(PATH_DEVCTL, O_RDONLY);
724 	if (fd == -1)
725 		err(1, "Can't open devctl device %s", PATH_DEVCTL);
726 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
727 		err(1, "Can't set close-on-exec flag on devctl");
728 	server_fd = create_socket(PIPE);
729 	max_fd = max(fd, server_fd) + 1;
730 	while (1) {
731 		if (romeo_must_die)
732 			break;
733 		if (!once && !dflag && !nflag) {
734 			// Check to see if we have any events pending.
735 			tv.tv_sec = 0;
736 			tv.tv_usec = 0;
737 			FD_ZERO(&fds);
738 			FD_SET(fd, &fds);
739 			rv = select(fd + 1, &fds, &fds, &fds, &tv);
740 			// No events -> we've processed all pending events
741 			if (rv == 0) {
742 				if (Dflag)
743 					fprintf(stderr, "Calling daemon\n");
744 				daemon(0, 0);
745 				cfg.drop_pidfile();
746 				once++;
747 			}
748 		}
749 		FD_ZERO(&fds);
750 		FD_SET(fd, &fds);
751 		FD_SET(server_fd, &fds);
752 		rv = select(max_fd, &fds, NULL, NULL, NULL);
753 		if (rv == -1) {
754 			if (errno == EINTR)
755 				continue;
756 			err(1, "select");
757 		}
758 		if (FD_ISSET(fd, &fds)) {
759 			rv = read(fd, buffer, sizeof(buffer) - 1);
760 			if (rv > 0) {
761 				notify_clients(buffer, rv);
762 				buffer[rv] = '\0';
763 				while (buffer[--rv] == '\n')
764 					buffer[rv] = '\0';
765 				process_event(buffer);
766 			} else if (rv < 0) {
767 				if (errno != EINTR)
768 					break;
769 			} else {
770 				/* EOF */
771 				break;
772 			}
773 		}
774 		if (FD_ISSET(server_fd, &fds))
775 			new_client(server_fd);
776 	}
777 	close(fd);
778 }
779 
780 /*
781  * functions that the parser uses.
782  */
783 void
784 add_attach(int prio, event_proc *p)
785 {
786 	cfg.add_attach(prio, p);
787 }
788 
789 void
790 add_detach(int prio, event_proc *p)
791 {
792 	cfg.add_detach(prio, p);
793 }
794 
795 void
796 add_directory(const char *dir)
797 {
798 	cfg.add_directory(dir);
799 	free(const_cast<char *>(dir));
800 }
801 
802 void
803 add_nomatch(int prio, event_proc *p)
804 {
805 	cfg.add_nomatch(prio, p);
806 }
807 
808 void
809 add_notify(int prio, event_proc *p)
810 {
811 	cfg.add_notify(prio, p);
812 }
813 
814 event_proc *
815 add_to_event_proc(event_proc *ep, eps *eps)
816 {
817 	if (ep == NULL)
818 		ep = new event_proc();
819 	ep->add(eps);
820 	return (ep);
821 }
822 
823 eps *
824 new_action(const char *cmd)
825 {
826 	eps *e = new action(cmd);
827 	free(const_cast<char *>(cmd));
828 	return (e);
829 }
830 
831 eps *
832 new_match(const char *var, const char *re)
833 {
834 	eps *e = new match(cfg, var, re);
835 	free(const_cast<char *>(var));
836 	free(const_cast<char *>(re));
837 	return (e);
838 }
839 
840 eps *
841 new_media(const char *var, const char *re)
842 {
843 	eps *e = new media(cfg, var, re);
844 	free(const_cast<char *>(var));
845 	free(const_cast<char *>(re));
846 	return (e);
847 }
848 
849 void
850 set_pidfile(const char *name)
851 {
852 	cfg.set_pidfile(name);
853 	free(const_cast<char *>(name));
854 }
855 
856 void
857 set_variable(const char *var, const char *val)
858 {
859 	cfg.set_variable(var, val);
860 	free(const_cast<char *>(var));
861 	free(const_cast<char *>(val));
862 }
863 
864 
865 
866 static void
867 gensighand(int)
868 {
869 	romeo_must_die++;
870 	_exit(0);
871 }
872 
873 static void
874 usage()
875 {
876 	fprintf(stderr, "usage: %s [-Ddn]\n", getprogname());
877 	exit(1);
878 }
879 
880 static void
881 check_devd_enabled()
882 {
883 	int val = 0;
884 	size_t len;
885 
886 	len = sizeof(val);
887 	if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
888 		errx(1, "devctl sysctl missing from kernel!");
889 	if (val) {
890 		warnx("Setting " SYSCTL " to 0");
891 		val = 0;
892 		sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
893 	}
894 }
895 
896 /*
897  * main
898  */
899 int
900 main(int argc, char **argv)
901 {
902 	int ch;
903 
904 	check_devd_enabled();
905 	while ((ch = getopt(argc, argv, "Ddn")) != -1) {
906 		switch (ch) {
907 		case 'D':
908 			Dflag++;
909 			break;
910 		case 'd':
911 			dflag++;
912 			break;
913 		case 'n':
914 			nflag++;
915 			break;
916 		default:
917 			usage();
918 		}
919 	}
920 
921 	cfg.parse();
922 	if (!dflag && nflag) {
923 		daemon(0, 0);
924 		cfg.drop_pidfile();
925 	}
926 	signal(SIGPIPE, SIG_IGN);
927 	signal(SIGHUP, gensighand);
928 	signal(SIGINT, gensighand);
929 	signal(SIGTERM, gensighand);
930 	event_loop();
931 	return (0);
932 }
933