xref: /freebsd/sbin/devd/devd.cc (revision 32e86a82)
13054f218SWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause
38a16b7a1SPedro F. Giffuni  *
4f86e6000SWarner Losh  * Copyright (c) 2002-2010 M. Warner Losh <imp@FreeBSD.org>
53054f218SWarner Losh  *
63054f218SWarner Losh  * Redistribution and use in source and binary forms, with or without
73054f218SWarner Losh  * modification, are permitted provided that the following conditions
83054f218SWarner Losh  * are met:
93054f218SWarner Losh  * 1. Redistributions of source code must retain the above copyright
103054f218SWarner Losh  *    notice, this list of conditions and the following disclaimer.
113054f218SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
123054f218SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
133054f218SWarner Losh  *    documentation and/or other materials provided with the distribution.
143054f218SWarner Losh  *
153054f218SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163054f218SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173054f218SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183054f218SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193054f218SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203054f218SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213054f218SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223054f218SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233054f218SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243054f218SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253054f218SWarner Losh  * SUCH DAMAGE.
26b8f92ce4SWarner Losh  *
27b8f92ce4SWarner Losh  * my_system is a variation on lib/libc/stdlib/system.c:
28b8f92ce4SWarner Losh  *
29b8f92ce4SWarner Losh  * Copyright (c) 1988, 1993
30b8f92ce4SWarner Losh  *	The Regents of the University of California.  All rights reserved.
31b8f92ce4SWarner Losh  *
32b8f92ce4SWarner Losh  * Redistribution and use in source and binary forms, with or without
33b8f92ce4SWarner Losh  * modification, are permitted provided that the following conditions
34b8f92ce4SWarner Losh  * are met:
35b8f92ce4SWarner Losh  * 1. Redistributions of source code must retain the above copyright
36b8f92ce4SWarner Losh  *    notice, this list of conditions and the following disclaimer.
37b8f92ce4SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
38b8f92ce4SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
39b8f92ce4SWarner Losh  *    documentation and/or other materials provided with the distribution.
40fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
41b8f92ce4SWarner Losh  *    may be used to endorse or promote products derived from this software
42b8f92ce4SWarner Losh  *    without specific prior written permission.
43b8f92ce4SWarner Losh  *
44b8f92ce4SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45b8f92ce4SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46b8f92ce4SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47b8f92ce4SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48b8f92ce4SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49b8f92ce4SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50b8f92ce4SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51b8f92ce4SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52b8f92ce4SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53b8f92ce4SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54b8f92ce4SWarner Losh  * SUCH DAMAGE.
553054f218SWarner Losh  */
563054f218SWarner Losh 
573054f218SWarner Losh /*
583054f218SWarner Losh  * DEVD control daemon.
593054f218SWarner Losh  */
603054f218SWarner Losh 
613054f218SWarner Losh // TODO list:
623054f218SWarner Losh //	o devd.conf and devd man pages need a lot of help:
63251bedd1SWarner Losh //	  - devd needs to document the unix domain socket
643054f218SWarner Losh //	  - devd.conf needs more details on the supported statements.
653054f218SWarner Losh 
663054f218SWarner Losh #include <sys/param.h>
67251bedd1SWarner Losh #include <sys/socket.h>
68251bedd1SWarner Losh #include <sys/stat.h>
699c180898SWarner Losh #include <sys/sysctl.h>
70251bedd1SWarner Losh #include <sys/types.h>
71b8f92ce4SWarner Losh #include <sys/wait.h>
72251bedd1SWarner Losh #include <sys/un.h>
733054f218SWarner Losh 
74d005340fSEitan Adler #include <cctype>
75d005340fSEitan Adler #include <cerrno>
76d005340fSEitan Adler #include <cstdlib>
77d005340fSEitan Adler #include <cstdio>
78d005340fSEitan Adler #include <csignal>
79d005340fSEitan Adler #include <cstring>
80967ecf4aSAlan Somers #include <cstdarg>
81d005340fSEitan Adler 
823054f218SWarner Losh #include <dirent.h>
833054f218SWarner Losh #include <err.h>
843054f218SWarner Losh #include <fcntl.h>
851a0cc6b1SPawel Jakub Dawidek #include <libutil.h>
86b8f92ce4SWarner Losh #include <paths.h>
87e1334f93SIan Lepore #include <poll.h>
885102ef84SWarner Losh #include <regex.h>
896d1014a3SAlan Somers #include <syslog.h>
903054f218SWarner Losh #include <unistd.h>
913054f218SWarner Losh 
928b823408SWarner Losh #include <algorithm>
933054f218SWarner Losh #include <map>
943054f218SWarner Losh #include <string>
95251bedd1SWarner Losh #include <list>
963e9662dcSAlan Somers #include <stdexcept>
973054f218SWarner Losh #include <vector>
983054f218SWarner Losh 
996aeeca8eSWarner Losh #include "devd.h"		/* C compatible definitions */
1006aeeca8eSWarner Losh #include "devd.hh"		/* C++ class definitions */
1013054f218SWarner Losh 
1029102691eSAlan Somers #define STREAMPIPE "/var/run/devd.pipe"
1039102691eSAlan Somers #define SEQPACKETPIPE "/var/run/devd.seqpacket.pipe"
1043054f218SWarner Losh #define CF "/etc/devd.conf"
105ee38f2e0SMateusz Guzik #define SYSCTL "hw.bus.devctl_queue"
1063054f218SWarner Losh 
107b026eddfSAlan Somers /*
108b026eddfSAlan Somers  * Since the client socket is nonblocking, we must increase its send buffer to
109b026eddfSAlan Somers  * handle brief event storms.  On FreeBSD, AF_UNIX sockets don't have a receive
110d9ca81daSAlan Somers  * buffer, so the client can't increase the buffersize by itself.
111b026eddfSAlan Somers  *
112b026eddfSAlan Somers  * For example, when creating a ZFS pool, devd emits one 165 character
113d9ca81daSAlan Somers  * resource.fs.zfs.statechange message for each vdev in the pool.  The kernel
114d9ca81daSAlan Somers  * allocates a 4608B mbuf for each message.  Modern technology places a limit of
115d9ca81daSAlan Somers  * roughly 450 drives/rack, and it's unlikely that a zpool will ever be larger
116d9ca81daSAlan Somers  * than that.
117d9ca81daSAlan Somers  *
118d9ca81daSAlan Somers  * 450 drives * 165 bytes / drive = 74250B of data in the sockbuf
119d9ca81daSAlan Somers  * 450 drives * 4608B / drive = 2073600B of mbufs in the sockbuf
120d9ca81daSAlan Somers  *
121d9ca81daSAlan Somers  * We can't directly set the sockbuf's mbuf limit, but we can do it indirectly.
122d9ca81daSAlan Somers  * The kernel sets it to the minimum of a hard-coded maximum value and sbcc *
123d9ca81daSAlan Somers  * kern.ipc.sockbuf_waste_factor, where sbcc is the socket buffer size set by
124d9ca81daSAlan Somers  * the user.  The default value of kern.ipc.sockbuf_waste_factor is 8.  If we
125d9ca81daSAlan Somers  * set the bufsize to 256k and use the kern.ipc.sockbuf_waste_factor, then the
126d9ca81daSAlan Somers  * kernel will set the mbuf limit to 2MB, which is just large enough for 450
127d9ca81daSAlan Somers  * drives.  It also happens to be the same as the hardcoded maximum value.
128b026eddfSAlan Somers  */
129d9ca81daSAlan Somers #define CLIENT_BUFSIZE 262144
130b026eddfSAlan Somers 
1313054f218SWarner Losh using namespace std;
1323054f218SWarner Losh 
1339102691eSAlan Somers typedef struct client {
1349102691eSAlan Somers 	int fd;
1359102691eSAlan Somers 	int socktype;
1369102691eSAlan Somers } client_t;
1379102691eSAlan Somers 
1383054f218SWarner Losh extern FILE *yyin;
1393054f218SWarner Losh 
140842ccec5SWarner Losh static const char notify = '!';
1418b823408SWarner Losh static const char nomatch = '?';
1428b823408SWarner Losh static const char attach = '+';
1438b823408SWarner Losh static const char detach = '-';
1448b823408SWarner Losh 
1451a0cc6b1SPawel Jakub Dawidek static struct pidfh *pfh;
1461a0cc6b1SPawel Jakub Dawidek 
1476a2ae0ebSAlan Somers static int no_daemon = 0;
1486a2ae0ebSAlan Somers static int daemonize_quick = 0;
1496a2ae0ebSAlan Somers static int quiet_mode = 0;
150be685e01SAlan Somers static unsigned total_events = 0;
151be685e01SAlan Somers static volatile sig_atomic_t got_siginfo = 0;
152fcdcaa88SEitan Adler static volatile sig_atomic_t romeo_must_die = 0;
1533054f218SWarner Losh 
1548334958aSJoseph Koshy static const char *configfile = CF;
1558334958aSJoseph Koshy 
1563449b15aSAlan Somers static void devdlog(int priority, const char* message, ...)
1573449b15aSAlan Somers 	__printflike(2, 3);
1583054f218SWarner Losh static void event_loop(void);
159510a8c88SEitan Adler static void usage(void) __dead2;
1603054f218SWarner Losh 
1618b823408SWarner Losh template <class T> void
delete_and_clear(vector<T * > & v)1628b823408SWarner Losh delete_and_clear(vector<T *> &v)
1638b823408SWarner Losh {
1648b823408SWarner Losh 	typename vector<T *>::const_iterator i;
1653054f218SWarner Losh 
166edee691dSEitan Adler 	for (i = v.begin(); i != v.end(); ++i)
1678b823408SWarner Losh 		delete *i;
1688b823408SWarner Losh 	v.clear();
1698b823408SWarner Losh }
1708b823408SWarner Losh 
1710bc60783SEitan Adler static config cfg;
1723054f218SWarner Losh 
17376527435SWarner Losh static const char *curr_cf = NULL;
17476527435SWarner Losh 
event_proc()1753054f218SWarner Losh event_proc::event_proc() : _prio(-1)
1763054f218SWarner Losh {
1775dfc0f6cSIan Lepore 	_epsvec.reserve(4);
1783054f218SWarner Losh }
1793054f218SWarner Losh 
~event_proc()1803054f218SWarner Losh event_proc::~event_proc()
1813054f218SWarner Losh {
18213f8bb71SAlexander Nedotsukov 	delete_and_clear(_epsvec);
1833054f218SWarner Losh }
1843054f218SWarner Losh 
1853054f218SWarner Losh void
add(eps * eps)1863054f218SWarner Losh event_proc::add(eps *eps)
1873054f218SWarner Losh {
1883054f218SWarner Losh 	_epsvec.push_back(eps);
1893054f218SWarner Losh }
1903054f218SWarner Losh 
1913054f218SWarner Losh bool
matches(config & c) const192ef370346SEitan Adler event_proc::matches(config &c) const
1933054f218SWarner Losh {
1943054f218SWarner Losh 	vector<eps *>::const_iterator i;
1953054f218SWarner Losh 
196edee691dSEitan Adler 	for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
1973054f218SWarner Losh 		if (!(*i)->do_match(c))
1983054f218SWarner Losh 			return (false);
1993054f218SWarner Losh 	return (true);
2003054f218SWarner Losh }
2013054f218SWarner Losh 
2023054f218SWarner Losh bool
run(config & c) const203ef370346SEitan Adler event_proc::run(config &c) const
2043054f218SWarner Losh {
2053054f218SWarner Losh 	vector<eps *>::const_iterator i;
2063054f218SWarner Losh 
207edee691dSEitan Adler 	for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
2083054f218SWarner Losh 		if (!(*i)->do_action(c))
2093054f218SWarner Losh 			return (false);
2103054f218SWarner Losh 	return (true);
2113054f218SWarner Losh }
2123054f218SWarner Losh 
action(const char * cmd)2133054f218SWarner Losh action::action(const char *cmd)
2143054f218SWarner Losh 	: _cmd(cmd)
2153054f218SWarner Losh {
2163054f218SWarner Losh 	// nothing
2173054f218SWarner Losh }
2183054f218SWarner Losh 
~action()2193054f218SWarner Losh action::~action()
2203054f218SWarner Losh {
2213054f218SWarner Losh 	// nothing
2223054f218SWarner Losh }
2233054f218SWarner Losh 
224b8f92ce4SWarner Losh static int
my_system(const char * command)225b8f92ce4SWarner Losh my_system(const char *command)
226b8f92ce4SWarner Losh {
227b8f92ce4SWarner Losh 	pid_t pid, savedpid;
228b8f92ce4SWarner Losh 	int pstat;
229b8f92ce4SWarner Losh 	struct sigaction ign, intact, quitact;
230b8f92ce4SWarner Losh 	sigset_t newsigblock, oldsigblock;
231b8f92ce4SWarner Losh 
232b8f92ce4SWarner Losh 	if (!command)		/* just checking... */
233b8f92ce4SWarner Losh 		return (1);
234b8f92ce4SWarner Losh 
235b8f92ce4SWarner Losh 	/*
236b8f92ce4SWarner Losh 	 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
237b8f92ce4SWarner Losh 	 * existing signal dispositions.
238b8f92ce4SWarner Losh 	 */
239b8f92ce4SWarner Losh 	ign.sa_handler = SIG_IGN;
240b8f92ce4SWarner Losh 	::sigemptyset(&ign.sa_mask);
241b8f92ce4SWarner Losh 	ign.sa_flags = 0;
242b8f92ce4SWarner Losh 	::sigaction(SIGINT, &ign, &intact);
243b8f92ce4SWarner Losh 	::sigaction(SIGQUIT, &ign, &quitact);
244b8f92ce4SWarner Losh 	::sigemptyset(&newsigblock);
245b8f92ce4SWarner Losh 	::sigaddset(&newsigblock, SIGCHLD);
246b8f92ce4SWarner Losh 	::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
247b8f92ce4SWarner Losh 	switch (pid = ::fork()) {
248b8f92ce4SWarner Losh 	case -1:			/* error */
249b8f92ce4SWarner Losh 		break;
250b8f92ce4SWarner Losh 	case 0:				/* child */
251b8f92ce4SWarner Losh 		/*
252b8f92ce4SWarner Losh 		 * Restore original signal dispositions and exec the command.
253b8f92ce4SWarner Losh 		 */
254b8f92ce4SWarner Losh 		::sigaction(SIGINT, &intact, NULL);
255b8f92ce4SWarner Losh 		::sigaction(SIGQUIT,  &quitact, NULL);
256b8f92ce4SWarner Losh 		::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
257b8f92ce4SWarner Losh 		/*
258b8f92ce4SWarner Losh 		 * Close the PID file, and all other open descriptors.
259b8f92ce4SWarner Losh 		 * Inherit std{in,out,err} only.
260b8f92ce4SWarner Losh 		 */
261b8f92ce4SWarner Losh 		cfg.close_pidfile();
262b8f92ce4SWarner Losh 		::closefrom(3);
26327744ca7SEitan Adler 		::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
264b8f92ce4SWarner Losh 		::_exit(127);
265b8f92ce4SWarner Losh 	default:			/* parent */
266b8f92ce4SWarner Losh 		savedpid = pid;
267b8f92ce4SWarner Losh 		do {
26827744ca7SEitan Adler 			pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0);
269b8f92ce4SWarner Losh 		} while (pid == -1 && errno == EINTR);
270b8f92ce4SWarner Losh 		break;
271b8f92ce4SWarner Losh 	}
272b8f92ce4SWarner Losh 	::sigaction(SIGINT, &intact, NULL);
273b8f92ce4SWarner Losh 	::sigaction(SIGQUIT,  &quitact, NULL);
274b8f92ce4SWarner Losh 	::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
275b8f92ce4SWarner Losh 	return (pid == -1 ? -1 : pstat);
276b8f92ce4SWarner Losh }
277b8f92ce4SWarner Losh 
2783054f218SWarner Losh bool
do_action(config & c)2795102ef84SWarner Losh action::do_action(config &c)
2803054f218SWarner Losh {
2815dfc0f6cSIan Lepore 	string s = c.expand_string(_cmd.c_str());
2821bb0777eSAndriy Gapon 	devdlog(LOG_INFO, "Executing '%s'\n", s.c_str());
283b8f92ce4SWarner Losh 	my_system(s.c_str());
2843054f218SWarner Losh 	return (true);
2853054f218SWarner Losh }
2863054f218SWarner Losh 
match(config & c,const char * var,const char * re)2875dfc0f6cSIan Lepore match::match(config &c, const char *var, const char *re) :
2885dfc0f6cSIan Lepore 	_inv(re[0] == '!'),
2895dfc0f6cSIan Lepore 	_var(var),
2905dfc0f6cSIan Lepore 	_re(c.expand_string(_inv ? re + 1 : re, "^", "$"))
2913054f218SWarner Losh {
2929cb4a7bdSWarner Losh 	regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE);
2933054f218SWarner Losh }
2943054f218SWarner Losh 
~match()2953054f218SWarner Losh match::~match()
2963054f218SWarner Losh {
2975102ef84SWarner Losh 	regfree(&_regex);
2983054f218SWarner Losh }
2993054f218SWarner Losh 
3003054f218SWarner Losh bool
do_match(config & c)3015102ef84SWarner Losh match::do_match(config &c)
3023054f218SWarner Losh {
3035bf08422SUlf Lilleengen 	const string &value = c.get_variable(_var);
3045102ef84SWarner Losh 	bool retval;
3055102ef84SWarner Losh 
3066d1014a3SAlan Somers 	/*
3076d1014a3SAlan Somers 	 * This function gets called WAY too often to justify calling syslog()
3086d1014a3SAlan Somers 	 * each time, even at LOG_DEBUG.  Because if syslogd isn't running, it
3096d1014a3SAlan Somers 	 * can consume excessive amounts of systime inside of connect().  Only
3106d1014a3SAlan Somers 	 * log when we're in -d mode.
3116d1014a3SAlan Somers 	 */
3126a2ae0ebSAlan Somers 	if (no_daemon) {
3136d1014a3SAlan Somers 		devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n",
3140321b694SHiroki Sato 		    _var.c_str(), value.c_str(), _re.c_str(), _inv);
3156d1014a3SAlan Somers 	}
3168b823408SWarner Losh 
3175102ef84SWarner Losh 	retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
3180321b694SHiroki Sato 	if (_inv == 1)
3190321b694SHiroki Sato 		retval = (retval == 0) ? 1 : 0;
3200321b694SHiroki Sato 
3210285e9b1SAlan Somers 	return (retval);
3223054f218SWarner Losh }
3233054f218SWarner Losh 
324cd70782bSWarner Losh #include <sys/sockio.h>
325cd70782bSWarner Losh #include <net/if.h>
326cd70782bSWarner Losh #include <net/if_media.h>
327cd70782bSWarner Losh 
media(config &,const char * var,const char * type)32894d2d4ebSBrooks Davis media::media(config &, const char *var, const char *type)
329cd70782bSWarner Losh 	: _var(var), _type(-1)
330cd70782bSWarner Losh {
331cd70782bSWarner Losh 	static struct ifmedia_description media_types[] = {
332cd70782bSWarner Losh 		{ IFM_ETHER,		"Ethernet" },
333cd70782bSWarner Losh 		{ IFM_IEEE80211,	"802.11" },
334cd70782bSWarner Losh 		{ IFM_ATM,		"ATM" },
335cd70782bSWarner Losh 		{ -1,			"unknown" },
336cd70782bSWarner Losh 		{ 0, NULL },
337cd70782bSWarner Losh 	};
338edee691dSEitan Adler 	for (int i = 0; media_types[i].ifmt_string != NULL; ++i)
339cd70782bSWarner Losh 		if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
340cd70782bSWarner Losh 			_type = media_types[i].ifmt_word;
341cd70782bSWarner Losh 			break;
342cd70782bSWarner Losh 		}
343cd70782bSWarner Losh }
344cd70782bSWarner Losh 
~media()345cd70782bSWarner Losh media::~media()
346cd70782bSWarner Losh {
347cd70782bSWarner Losh }
348cd70782bSWarner Losh 
349cd70782bSWarner Losh bool
do_match(config & c)350cd70782bSWarner Losh media::do_match(config &c)
351cd70782bSWarner Losh {
35209c47e76SWarner Losh 	string value;
353cd70782bSWarner Losh 	struct ifmediareq ifmr;
354cd70782bSWarner Losh 	bool retval;
355cd70782bSWarner Losh 	int s;
356cd70782bSWarner Losh 
35709c47e76SWarner Losh 	// Since we can be called from both a device attach/detach
35809c47e76SWarner Losh 	// context where device-name is defined and what we want,
35909c47e76SWarner Losh 	// as well as from a link status context, where subsystem is
36009c47e76SWarner Losh 	// the name of interest, first try device-name and fall back
36109c47e76SWarner Losh 	// to subsystem if none exists.
36209c47e76SWarner Losh 	value = c.get_variable("device-name");
363a6393aa1SEitan Adler 	if (value.empty())
364de02eec8SWarner Losh 		value = c.get_variable("subsystem");
3656d1014a3SAlan Somers 	devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n",
366cd70782bSWarner Losh 		    value.c_str(), _type);
367cd70782bSWarner Losh 
368cd70782bSWarner Losh 	retval = false;
369cd70782bSWarner Losh 
370cd70782bSWarner Losh 	s = socket(PF_INET, SOCK_DGRAM, 0);
371cd70782bSWarner Losh 	if (s >= 0) {
372cd70782bSWarner Losh 		memset(&ifmr, 0, sizeof(ifmr));
373daa0d9ddSAlan Somers 		strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
374cd70782bSWarner Losh 
37527744ca7SEitan Adler 		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
376cd70782bSWarner Losh 		    ifmr.ifm_status & IFM_AVALID) {
3776d1014a3SAlan Somers 			devdlog(LOG_DEBUG, "%s has media type 0x%x\n",
378cd70782bSWarner Losh 				    value.c_str(), IFM_TYPE(ifmr.ifm_active));
379cd70782bSWarner Losh 			retval = (IFM_TYPE(ifmr.ifm_active) == _type);
380cd70782bSWarner Losh 		} else if (_type == -1) {
3816d1014a3SAlan Somers 			devdlog(LOG_DEBUG, "%s has unknown media type\n",
382cd70782bSWarner Losh 				    value.c_str());
383cd70782bSWarner Losh 			retval = true;
384cd70782bSWarner Losh 		}
385cd70782bSWarner Losh 		close(s);
386cd70782bSWarner Losh 	}
387cd70782bSWarner Losh 
3880285e9b1SAlan Somers 	return (retval);
389cd70782bSWarner Losh }
390cd70782bSWarner Losh 
3913054f218SWarner Losh const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
3923054f218SWarner Losh const string var_list::nothing = "";
3933054f218SWarner Losh 
3943054f218SWarner Losh const string &
get_variable(const string & var) const3953054f218SWarner Losh var_list::get_variable(const string &var) const
3963054f218SWarner Losh {
3973054f218SWarner Losh 	map<string, string>::const_iterator i;
3983054f218SWarner Losh 
3993054f218SWarner Losh 	i = _vars.find(var);
4003054f218SWarner Losh 	if (i == _vars.end())
4018b823408SWarner Losh 		return (var_list::bogus);
4023054f218SWarner Losh 	return (i->second);
4033054f218SWarner Losh }
4043054f218SWarner Losh 
4053054f218SWarner Losh bool
is_set(const string & var) const4063054f218SWarner Losh var_list::is_set(const string &var) const
4073054f218SWarner Losh {
4083054f218SWarner Losh 	return (_vars.find(var) != _vars.end());
4093054f218SWarner Losh }
4103054f218SWarner Losh 
411192af3b7SWarner Losh /** fix_value
412192af3b7SWarner Losh  *
413192af3b7SWarner Losh  * Removes quoted characters that have made it this far. \" are
414192af3b7SWarner Losh  * converted to ". For all other characters, both \ and following
415192af3b7SWarner Losh  * character. So the string 'fre\:\"' is translated to 'fred\:"'.
416192af3b7SWarner Losh  */
417416823b1SWarner Losh std::string
fix_value(const std::string & val) const418192af3b7SWarner Losh var_list::fix_value(const std::string &val) const
419192af3b7SWarner Losh {
420416823b1SWarner Losh         std::string rv(val);
421416823b1SWarner Losh         std::string::size_type pos(0);
422192af3b7SWarner Losh 
423416823b1SWarner Losh         while ((pos = rv.find("\\\"", pos)) != rv.npos) {
424416823b1SWarner Losh                 rv.erase(pos, 1);
425192af3b7SWarner Losh         }
426416823b1SWarner Losh         return (rv);
427192af3b7SWarner Losh }
428192af3b7SWarner Losh 
4293054f218SWarner Losh void
set_variable(const string & var,const string & val)4303054f218SWarner Losh var_list::set_variable(const string &var, const string &val)
4313054f218SWarner Losh {
4326d1014a3SAlan Somers 	/*
4336d1014a3SAlan Somers 	 * This function gets called WAY too often to justify calling syslog()
4346d1014a3SAlan Somers 	 * each time, even at LOG_DEBUG.  Because if syslogd isn't running, it
4356d1014a3SAlan Somers 	 * can consume excessive amounts of systime inside of connect().  Only
4366d1014a3SAlan Somers 	 * log when we're in -d mode.
4376d1014a3SAlan Somers 	 */
438192af3b7SWarner Losh 	_vars[var] = fix_value(val);
4396a2ae0ebSAlan Somers 	if (no_daemon)
4406d1014a3SAlan Somers 		devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str());
4413054f218SWarner Losh }
4423054f218SWarner Losh 
4433054f218SWarner Losh void
reset(void)4443054f218SWarner Losh config::reset(void)
4453054f218SWarner Losh {
4463054f218SWarner Losh 	_dir_list.clear();
4478b823408SWarner Losh 	delete_and_clear(_var_list_table);
4488b823408SWarner Losh 	delete_and_clear(_attach_list);
4498b823408SWarner Losh 	delete_and_clear(_detach_list);
4508b823408SWarner Losh 	delete_and_clear(_nomatch_list);
451842ccec5SWarner Losh 	delete_and_clear(_notify_list);
4523054f218SWarner Losh }
4533054f218SWarner Losh 
45476527435SWarner Losh /*
45576527435SWarner Losh  * Called recursively as new files are included, so current stack of old names
45676527435SWarner Losh  * saved in each instance of 'old' on the call stack. Called single threaded
45776527435SWarner Losh  * so global varaibles curr_cf and lineno (and all of yacc's parser state)
45876527435SWarner Losh  * are safe to access w/o a lock.
45976527435SWarner Losh  */
4603054f218SWarner Losh void
parse_one_file(const char * fn)4613054f218SWarner Losh config::parse_one_file(const char *fn)
4623054f218SWarner Losh {
46376527435SWarner Losh 	const char *old;
46476527435SWarner Losh 
4656d1014a3SAlan Somers 	devdlog(LOG_DEBUG, "Parsing %s\n", fn);
4663054f218SWarner Losh 	yyin = fopen(fn, "r");
46776527435SWarner Losh 	old = curr_cf;
46876527435SWarner Losh 	curr_cf = fn;
4693054f218SWarner Losh 	if (yyin == NULL)
4703054f218SWarner Losh 		err(1, "Cannot open config file %s", fn);
4714a3050fcSMaxim Konovalov 	lineno = 1;
4723054f218SWarner Losh 	if (yyparse() != 0)
4733054f218SWarner Losh 		errx(1, "Cannot parse %s at line %d", fn, lineno);
4743054f218SWarner Losh 	fclose(yyin);
47576527435SWarner Losh 	curr_cf = old;
4763054f218SWarner Losh }
4773054f218SWarner Losh 
4783054f218SWarner Losh void
parse_files_in_dir(const char * dirname)4793054f218SWarner Losh config::parse_files_in_dir(const char *dirname)
4803054f218SWarner Losh {
4813054f218SWarner Losh 	DIR *dirp;
4823054f218SWarner Losh 	struct dirent *dp;
4833054f218SWarner Losh 	char path[PATH_MAX];
4843054f218SWarner Losh 
4856d1014a3SAlan Somers 	devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname);
4863054f218SWarner Losh 	dirp = opendir(dirname);
4873054f218SWarner Losh 	if (dirp == NULL)
4883054f218SWarner Losh 		return;
4893054f218SWarner Losh 	readdir(dirp);		/* Skip . */
4903054f218SWarner Losh 	readdir(dirp);		/* Skip .. */
4913054f218SWarner Losh 	while ((dp = readdir(dirp)) != NULL) {
4923054f218SWarner Losh 		if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
4933054f218SWarner Losh 			snprintf(path, sizeof(path), "%s/%s",
4943054f218SWarner Losh 			    dirname, dp->d_name);
4953054f218SWarner Losh 			parse_one_file(path);
4963054f218SWarner Losh 		}
4973054f218SWarner Losh 	}
4985983e891SKevin Lo 	closedir(dirp);
4993054f218SWarner Losh }
5003054f218SWarner Losh 
5018b823408SWarner Losh class epv_greater {
5028b823408SWarner Losh public:
operator ()(event_proc * const & l1,event_proc * const & l2) const503ef370346SEitan Adler 	int operator()(event_proc *const&l1, event_proc *const&l2) const
5048b823408SWarner Losh 	{
5058b823408SWarner Losh 		return (l1->get_priority() > l2->get_priority());
5068b823408SWarner Losh 	}
5078b823408SWarner Losh };
5088b823408SWarner Losh 
5098b823408SWarner Losh void
sort_vector(vector<event_proc * > & v)5108b823408SWarner Losh config::sort_vector(vector<event_proc *> &v)
5118b823408SWarner Losh {
5120c04d185SDimitry Andric 	stable_sort(v.begin(), v.end(), epv_greater());
5138b823408SWarner Losh }
5148b823408SWarner Losh 
5153054f218SWarner Losh void
parse(void)5163054f218SWarner Losh config::parse(void)
5173054f218SWarner Losh {
5183054f218SWarner Losh 	vector<string>::const_iterator i;
5193054f218SWarner Losh 
5208334958aSJoseph Koshy 	parse_one_file(configfile);
521edee691dSEitan Adler 	for (i = _dir_list.begin(); i != _dir_list.end(); ++i)
5223054f218SWarner Losh 		parse_files_in_dir((*i).c_str());
5238b823408SWarner Losh 	sort_vector(_attach_list);
5248b823408SWarner Losh 	sort_vector(_detach_list);
5258b823408SWarner Losh 	sort_vector(_nomatch_list);
526842ccec5SWarner Losh 	sort_vector(_notify_list);
5273054f218SWarner Losh }
5283054f218SWarner Losh 
5293054f218SWarner Losh void
open_pidfile()5301a0cc6b1SPawel Jakub Dawidek config::open_pidfile()
5313054f218SWarner Losh {
5321a0cc6b1SPawel Jakub Dawidek 	pid_t otherpid;
5333054f218SWarner Losh 
5345a3b1a3dSEitan Adler 	if (_pidfile.empty())
5353054f218SWarner Losh 		return;
5361a0cc6b1SPawel Jakub Dawidek 	pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
5371a0cc6b1SPawel Jakub Dawidek 	if (pfh == NULL) {
5381a0cc6b1SPawel Jakub Dawidek 		if (errno == EEXIST)
53927744ca7SEitan Adler 			errx(1, "devd already running, pid: %d", (int)otherpid);
5401a0cc6b1SPawel Jakub Dawidek 		warn("cannot open pid file");
5411a0cc6b1SPawel Jakub Dawidek 	}
5421a0cc6b1SPawel Jakub Dawidek }
5431a0cc6b1SPawel Jakub Dawidek 
5441a0cc6b1SPawel Jakub Dawidek void
write_pidfile()5451a0cc6b1SPawel Jakub Dawidek config::write_pidfile()
5461a0cc6b1SPawel Jakub Dawidek {
5471a0cc6b1SPawel Jakub Dawidek 
5481a0cc6b1SPawel Jakub Dawidek 	pidfile_write(pfh);
5491a0cc6b1SPawel Jakub Dawidek }
5501a0cc6b1SPawel Jakub Dawidek 
5511a0cc6b1SPawel Jakub Dawidek void
close_pidfile()552b8f92ce4SWarner Losh config::close_pidfile()
553b8f92ce4SWarner Losh {
554b8f92ce4SWarner Losh 
555b8f92ce4SWarner Losh 	pidfile_close(pfh);
556b8f92ce4SWarner Losh }
557b8f92ce4SWarner Losh 
558b8f92ce4SWarner Losh void
remove_pidfile()5591a0cc6b1SPawel Jakub Dawidek config::remove_pidfile()
5601a0cc6b1SPawel Jakub Dawidek {
5611a0cc6b1SPawel Jakub Dawidek 
5621a0cc6b1SPawel Jakub Dawidek 	pidfile_remove(pfh);
5633054f218SWarner Losh }
5643054f218SWarner Losh 
5653054f218SWarner Losh void
add_attach(int prio,event_proc * p)5663054f218SWarner Losh config::add_attach(int prio, event_proc *p)
5673054f218SWarner Losh {
5683054f218SWarner Losh 	p->set_priority(prio);
5693054f218SWarner Losh 	_attach_list.push_back(p);
5703054f218SWarner Losh }
5713054f218SWarner Losh 
5723054f218SWarner Losh void
add_detach(int prio,event_proc * p)5733054f218SWarner Losh config::add_detach(int prio, event_proc *p)
5743054f218SWarner Losh {
5753054f218SWarner Losh 	p->set_priority(prio);
5763054f218SWarner Losh 	_detach_list.push_back(p);
5773054f218SWarner Losh }
5783054f218SWarner Losh 
5793054f218SWarner Losh void
add_directory(const char * dir)5803054f218SWarner Losh config::add_directory(const char *dir)
5813054f218SWarner Losh {
5823054f218SWarner Losh 	_dir_list.push_back(string(dir));
5833054f218SWarner Losh }
5843054f218SWarner Losh 
5853054f218SWarner Losh void
add_nomatch(int prio,event_proc * p)5863054f218SWarner Losh config::add_nomatch(int prio, event_proc *p)
5873054f218SWarner Losh {
5883054f218SWarner Losh 	p->set_priority(prio);
5893054f218SWarner Losh 	_nomatch_list.push_back(p);
5903054f218SWarner Losh }
5913054f218SWarner Losh 
5923054f218SWarner Losh void
add_notify(int prio,event_proc * p)593842ccec5SWarner Losh config::add_notify(int prio, event_proc *p)
594842ccec5SWarner Losh {
595842ccec5SWarner Losh 	p->set_priority(prio);
596842ccec5SWarner Losh 	_notify_list.push_back(p);
597842ccec5SWarner Losh }
598842ccec5SWarner Losh 
599842ccec5SWarner Losh void
set_pidfile(const char * fn)6003054f218SWarner Losh config::set_pidfile(const char *fn)
6013054f218SWarner Losh {
6025a3b1a3dSEitan Adler 	_pidfile = fn;
6033054f218SWarner Losh }
6043054f218SWarner Losh 
6053054f218SWarner Losh void
push_var_table()6063054f218SWarner Losh config::push_var_table()
6073054f218SWarner Losh {
6083054f218SWarner Losh 	var_list *vl;
6093054f218SWarner Losh 
6103054f218SWarner Losh 	vl = new var_list();
6113054f218SWarner Losh 	_var_list_table.push_back(vl);
6126d1014a3SAlan Somers 	devdlog(LOG_DEBUG, "Pushing table\n");
6133054f218SWarner Losh }
6143054f218SWarner Losh 
6153054f218SWarner Losh void
pop_var_table()6163054f218SWarner Losh config::pop_var_table()
6173054f218SWarner Losh {
6183054f218SWarner Losh 	delete _var_list_table.back();
6193054f218SWarner Losh 	_var_list_table.pop_back();
6206d1014a3SAlan Somers 	devdlog(LOG_DEBUG, "Popping table\n");
6213054f218SWarner Losh }
6223054f218SWarner Losh 
6233054f218SWarner Losh void
set_variable(const char * var,const char * val)6243054f218SWarner Losh config::set_variable(const char *var, const char *val)
6253054f218SWarner Losh {
6263054f218SWarner Losh 	_var_list_table.back()->set_variable(var, val);
6273054f218SWarner Losh }
6283054f218SWarner Losh 
6293054f218SWarner Losh const string &
get_variable(const string & var)6303054f218SWarner Losh config::get_variable(const string &var)
6313054f218SWarner Losh {
6323054f218SWarner Losh 	vector<var_list *>::reverse_iterator i;
6333054f218SWarner Losh 
634edee691dSEitan Adler 	for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) {
6353054f218SWarner Losh 		if ((*i)->is_set(var))
6368b823408SWarner Losh 			return ((*i)->get_variable(var));
6373054f218SWarner Losh 	}
6383054f218SWarner Losh 	return (var_list::nothing);
6393054f218SWarner Losh }
6403054f218SWarner Losh 
6418b823408SWarner Losh bool
is_id_char(char ch) const642ef370346SEitan Adler config::is_id_char(char ch) const
6438b823408SWarner Losh {
6448b823408SWarner Losh 	return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
6458b823408SWarner Losh 	    ch == '-'));
6468b823408SWarner Losh }
6478b823408SWarner Losh 
64854aa4076SWarner Losh string
shell_quote(const string & s)64954aa4076SWarner Losh config::shell_quote(const string &s)
65054aa4076SWarner Losh {
65154aa4076SWarner Losh 	string buffer;
65295cbefb3SWarner Losh 	const char *cs, *ce;
65395cbefb3SWarner Losh 	char c;
65454aa4076SWarner Losh 
65554aa4076SWarner Losh 	/*
65654aa4076SWarner Losh 	 * Enclose the string in $' ' with escapes for ' and / characters making
65754aa4076SWarner Losh 	 * it one argument and ensuring the shell won't be affected by its
65854aa4076SWarner Losh 	 * usual list of candidates.
65954aa4076SWarner Losh 	 */
66054aa4076SWarner Losh 	buffer.reserve(s.length() * 3 / 2);
66154aa4076SWarner Losh 	buffer += '$';
66254aa4076SWarner Losh 	buffer += '\'';
66395cbefb3SWarner Losh 	cs = s.c_str();
66495cbefb3SWarner Losh 	ce = cs + strlen(cs);
66595cbefb3SWarner Losh 	for (; cs < ce; cs++) {
66695cbefb3SWarner Losh 		c = *cs;
66754aa4076SWarner Losh 		if (c == '\'' || c == '\\') {
66854aa4076SWarner Losh 			buffer += '\\';
66954aa4076SWarner Losh 		}
67054aa4076SWarner Losh 		buffer += c;
67154aa4076SWarner Losh 	}
67254aa4076SWarner Losh 	buffer += '\'';
67354aa4076SWarner Losh 
67454aa4076SWarner Losh 	return buffer;
67554aa4076SWarner Losh }
67654aa4076SWarner Losh 
6775102ef84SWarner Losh void
expand_one(const char * & src,string & dst,bool is_shell)6786577e8c4SWarner Losh config::expand_one(const char *&src, string &dst, bool is_shell)
6793054f218SWarner Losh {
6805102ef84SWarner Losh 	int count;
6815bf08422SUlf Lilleengen 	string buffer;
6825102ef84SWarner Losh 
6838b823408SWarner Losh 	src++;
6845102ef84SWarner Losh 	// $$ -> $
6855102ef84SWarner Losh 	if (*src == '$') {
6867d9e9c60SEitan Adler 		dst += *src++;
6875102ef84SWarner Losh 		return;
6885102ef84SWarner Losh 	}
6895102ef84SWarner Losh 
6905102ef84SWarner Losh 	// $(foo) -> $(foo)
69154aa4076SWarner Losh 	// This is the escape hatch for passing down shell subcommands
6925102ef84SWarner Losh 	if (*src == '(') {
6937d9e9c60SEitan Adler 		dst += '$';
694de579766SAlexander Motin 		count = 0;
6956cabd675SWarner Losh 		/* If the string ends before ) is matched , return. */
696de579766SAlexander Motin 		do {
6975102ef84SWarner Losh 			if (*src == ')')
6985102ef84SWarner Losh 				count--;
6995102ef84SWarner Losh 			else if (*src == '(')
7005102ef84SWarner Losh 				count++;
7017d9e9c60SEitan Adler 			dst += *src++;
702de579766SAlexander Motin 		} while (count > 0 && *src);
7035102ef84SWarner Losh 		return;
7045102ef84SWarner Losh 	}
7055102ef84SWarner Losh 
706b3d32292SWarner Losh 	// $[^-A-Za-z_*] -> $\1
707b3d32292SWarner Losh 	if (!isalpha(*src) && *src != '_' && *src != '-' && *src != '*') {
7087d9e9c60SEitan Adler 		dst += '$';
7097d9e9c60SEitan Adler 		dst += *src++;
7105102ef84SWarner Losh 		return;
7115102ef84SWarner Losh 	}
7125102ef84SWarner Losh 
7135102ef84SWarner Losh 	// $var -> replace with value
7146cabd675SWarner Losh 	do {
7157d9e9c60SEitan Adler 		buffer += *src++;
71666ff6821SWarner Losh 	} while (is_id_char(*src));
7176577e8c4SWarner Losh 	dst.append(is_shell ? shell_quote(get_variable(buffer)) : get_variable(buffer));
7185102ef84SWarner Losh }
7195102ef84SWarner Losh 
7205102ef84SWarner Losh const string
expand_string(const char * src,const char * prepend,const char * append)7215dfc0f6cSIan Lepore config::expand_string(const char *src, const char *prepend, const char *append)
7225102ef84SWarner Losh {
7235dfc0f6cSIan Lepore 	const char *var_at;
7246cabd675SWarner Losh 	string dst;
7255102ef84SWarner Losh 
7265dfc0f6cSIan Lepore 	/*
7275dfc0f6cSIan Lepore 	 * 128 bytes is enough for 2427 of 2438 expansions that happen
7285dfc0f6cSIan Lepore 	 * while parsing config files, as tested on 2013-01-30.
7295dfc0f6cSIan Lepore 	 */
7305dfc0f6cSIan Lepore 	dst.reserve(128);
7315dfc0f6cSIan Lepore 
7325dfc0f6cSIan Lepore 	if (prepend != NULL)
7335dfc0f6cSIan Lepore 		dst = prepend;
7345dfc0f6cSIan Lepore 
7355dfc0f6cSIan Lepore 	for (;;) {
7365dfc0f6cSIan Lepore 		var_at = strchr(src, '$');
7375dfc0f6cSIan Lepore 		if (var_at == NULL) {
7385dfc0f6cSIan Lepore 			dst.append(src);
7395dfc0f6cSIan Lepore 			break;
7405102ef84SWarner Losh 		}
7415dfc0f6cSIan Lepore 		dst.append(src, var_at - src);
7425dfc0f6cSIan Lepore 		src = var_at;
7436577e8c4SWarner Losh 		expand_one(src, dst, prepend == NULL);
7445dfc0f6cSIan Lepore 	}
7455dfc0f6cSIan Lepore 
7465dfc0f6cSIan Lepore 	if (append != NULL)
7475dfc0f6cSIan Lepore 		dst.append(append);
7485102ef84SWarner Losh 
7496cabd675SWarner Losh 	return (dst);
7503054f218SWarner Losh }
7513054f218SWarner Losh 
7528b823408SWarner Losh bool
chop_var(char * & buffer,char * & lhs,char * & rhs) const75311fd1366SEitan Adler config::chop_var(char *&buffer, char *&lhs, char *&rhs) const
7548b823408SWarner Losh {
7558b823408SWarner Losh 	char *walker;
7568b823408SWarner Losh 
7578b823408SWarner Losh 	if (*buffer == '\0')
7588b823408SWarner Losh 		return (false);
7598b823408SWarner Losh 	walker = lhs = buffer;
7608b823408SWarner Losh 	while (is_id_char(*walker))
7618b823408SWarner Losh 		walker++;
7628b823408SWarner Losh 	if (*walker != '=')
7638b823408SWarner Losh 		return (false);
7648b823408SWarner Losh 	walker++;		// skip =
7658b823408SWarner Losh 	if (*walker == '"') {
7668b823408SWarner Losh 		walker++;	// skip "
7678b823408SWarner Losh 		rhs = walker;
768192af3b7SWarner Losh 		while (*walker && *walker != '"') {
769192af3b7SWarner Losh 			// Skip \" ... We leave it in the string and strip the \ later.
770192af3b7SWarner Losh 			// due to the super simplistic parser that we have here.
771192af3b7SWarner Losh 			if (*walker == '\\' && walker[1] == '"')
7728b823408SWarner Losh 				walker++;
773192af3b7SWarner Losh 			walker++;
774192af3b7SWarner Losh 		}
7758b823408SWarner Losh 		if (*walker != '"')
7768b823408SWarner Losh 			return (false);
7778b823408SWarner Losh 		rhs[-2] = '\0';
7788b823408SWarner Losh 		*walker++ = '\0';
7798b823408SWarner Losh 	} else {
7808b823408SWarner Losh 		rhs = walker;
7818b823408SWarner Losh 		while (*walker && !isspace(*walker))
7828b823408SWarner Losh 			walker++;
7838b823408SWarner Losh 		if (*walker != '\0')
7848b823408SWarner Losh 			*walker++ = '\0';
7858b823408SWarner Losh 		rhs[-1] = '\0';
7868b823408SWarner Losh 	}
787b1ee04b1SWarner Losh 	while (isspace(*walker))
788b1ee04b1SWarner Losh 		walker++;
7898b823408SWarner Losh 	buffer = walker;
7908b823408SWarner Losh 	return (true);
7918b823408SWarner Losh }
7928b823408SWarner Losh 
7938b823408SWarner Losh 
7948b823408SWarner Losh char *
set_vars(char * buffer)7958b823408SWarner Losh config::set_vars(char *buffer)
7968b823408SWarner Losh {
7978b823408SWarner Losh 	char *lhs;
7988b823408SWarner Losh 	char *rhs;
7998b823408SWarner Losh 
8008b823408SWarner Losh 	while (1) {
8018b823408SWarner Losh 		if (!chop_var(buffer, lhs, rhs))
8028b823408SWarner Losh 			break;
8038b823408SWarner Losh 		set_variable(lhs, rhs);
8048b823408SWarner Losh 	}
8058b823408SWarner Losh 	return (buffer);
8068b823408SWarner Losh }
8078b823408SWarner Losh 
8088b823408SWarner Losh void
find_and_execute(char type)8098b823408SWarner Losh config::find_and_execute(char type)
8108b823408SWarner Losh {
8118b823408SWarner Losh 	vector<event_proc *> *l;
8128b823408SWarner Losh 	vector<event_proc *>::const_iterator i;
81394d2d4ebSBrooks Davis 	const char *s;
8148b823408SWarner Losh 
8158b823408SWarner Losh 	switch (type) {
8168b823408SWarner Losh 	default:
8178b823408SWarner Losh 		return;
818842ccec5SWarner Losh 	case notify:
819842ccec5SWarner Losh 		l = &_notify_list;
820842ccec5SWarner Losh 		s = "notify";
821842ccec5SWarner Losh 		break;
8228b823408SWarner Losh 	case nomatch:
8238b823408SWarner Losh 		l = &_nomatch_list;
8248b823408SWarner Losh 		s = "nomatch";
8258b823408SWarner Losh 		break;
8268b823408SWarner Losh 	case attach:
8278b823408SWarner Losh 		l = &_attach_list;
8288b823408SWarner Losh 		s = "attach";
8298b823408SWarner Losh 		break;
8308b823408SWarner Losh 	case detach:
8318b823408SWarner Losh 		l = &_detach_list;
8328b823408SWarner Losh 		s = "detach";
8338b823408SWarner Losh 		break;
8348b823408SWarner Losh 	}
8356d1014a3SAlan Somers 	devdlog(LOG_DEBUG, "Processing %s event\n", s);
836edee691dSEitan Adler 	for (i = l->begin(); i != l->end(); ++i) {
8378b823408SWarner Losh 		if ((*i)->matches(*this)) {
8388b823408SWarner Losh 			(*i)->run(*this);
8398b823408SWarner Losh 			break;
8408b823408SWarner Losh 		}
8418b823408SWarner Losh 	}
8428b823408SWarner Losh 
8438b823408SWarner Losh }
8448b823408SWarner Losh 
8450285e9b1SAlan Somers 
8463054f218SWarner Losh static void
process_event(char * buffer)8478b823408SWarner Losh process_event(char *buffer)
8483054f218SWarner Losh {
8493054f218SWarner Losh 	char type;
8503054f218SWarner Losh 	char *sp;
8513b336ac3SWarner Losh 	struct timeval tv;
8523b336ac3SWarner Losh 	char *timestr;
8533054f218SWarner Losh 
8548b823408SWarner Losh 	sp = buffer + 1;
855f0038a8eSAlan Somers 	devdlog(LOG_INFO, "Processing event '%s'\n", buffer);
8563054f218SWarner Losh 	type = *buffer++;
8578b823408SWarner Losh 	cfg.push_var_table();
858b3d32292SWarner Losh 	// $* is the entire line
859b3d32292SWarner Losh 	cfg.set_variable("*", buffer - 1);
860b3d32292SWarner Losh 	// $_ is the entire line without the initial character
861535595dbSWarner Losh 	cfg.set_variable("_", buffer);
8623b336ac3SWarner Losh 
8633b336ac3SWarner Losh 	// Save the time this happened (as approximated by when we got
8643b336ac3SWarner Losh 	// around to processing it).
8653b336ac3SWarner Losh 	gettimeofday(&tv, NULL);
86627744ca7SEitan Adler 	asprintf(&timestr, "%jd.%06ld", (uintmax_t)tv.tv_sec, tv.tv_usec);
8673b336ac3SWarner Losh 	cfg.set_variable("timestamp", timestr);
8683b336ac3SWarner Losh 	free(timestr);
8693b336ac3SWarner Losh 
8703b336ac3SWarner Losh 	// Match doesn't have a device, and the format is a little
8718b823408SWarner Losh 	// different, so handle it separately.
872842ccec5SWarner Losh 	switch (type) {
873842ccec5SWarner Losh 	case notify:
874b3d32292SWarner Losh 		//! (k=v)*
875842ccec5SWarner Losh 		sp = cfg.set_vars(sp);
876842ccec5SWarner Losh 		break;
877842ccec5SWarner Losh 	case nomatch:
878528d3f79SWarner Losh 		//? at location pnp-info on bus
879528d3f79SWarner Losh 		sp = strchr(sp, ' ');
880528d3f79SWarner Losh 		if (sp == NULL)
881528d3f79SWarner Losh 			return;	/* Can't happen? */
882528d3f79SWarner Losh 		*sp++ = '\0';
8839b6cb520SWarner Losh 		while (isspace(*sp))
8849b6cb520SWarner Losh 			sp++;
885842ccec5SWarner Losh 		if (strncmp(sp, "at ", 3) == 0)
886842ccec5SWarner Losh 			sp += 3;
887842ccec5SWarner Losh 		sp = cfg.set_vars(sp);
8889b6cb520SWarner Losh 		while (isspace(*sp))
8899b6cb520SWarner Losh 			sp++;
890842ccec5SWarner Losh 		if (strncmp(sp, "on ", 3) == 0)
891842ccec5SWarner Losh 			cfg.set_variable("bus", sp + 3);
892842ccec5SWarner Losh 		break;
893842ccec5SWarner Losh 	case attach:	/*FALLTHROUGH*/
894842ccec5SWarner Losh 	case detach:
8958b823408SWarner Losh 		sp = strchr(sp, ' ');
8963054f218SWarner Losh 		if (sp == NULL)
8973054f218SWarner Losh 			return;	/* Can't happen? */
8988b823408SWarner Losh 		*sp++ = '\0';
8998b823408SWarner Losh 		cfg.set_variable("device-name", buffer);
9009b6cb520SWarner Losh 		while (isspace(*sp))
9019b6cb520SWarner Losh 			sp++;
9028b823408SWarner Losh 		if (strncmp(sp, "at ", 3) == 0)
9038b823408SWarner Losh 			sp += 3;
9048b823408SWarner Losh 		sp = cfg.set_vars(sp);
9059b6cb520SWarner Losh 		while (isspace(*sp))
9069b6cb520SWarner Losh 			sp++;
9078b823408SWarner Losh 		if (strncmp(sp, "on ", 3) == 0)
9088b823408SWarner Losh 			cfg.set_variable("bus", sp + 3);
909842ccec5SWarner Losh 		break;
910b1ee04b1SWarner Losh 	}
911b1ee04b1SWarner Losh 
9128b823408SWarner Losh 	cfg.find_and_execute(type);
9138b823408SWarner Losh 	cfg.pop_var_table();
9143054f218SWarner Losh }
9153054f218SWarner Losh 
916514e2dbfSEitan Adler static int
create_socket(const char * name,int socktype)9179102691eSAlan Somers create_socket(const char *name, int socktype)
918251bedd1SWarner Losh {
919251bedd1SWarner Losh 	int fd, slen;
920251bedd1SWarner Losh 	struct sockaddr_un sun;
921251bedd1SWarner Losh 
9229102691eSAlan Somers 	if ((fd = socket(PF_LOCAL, socktype, 0)) < 0)
923251bedd1SWarner Losh 		err(1, "socket");
924251bedd1SWarner Losh 	bzero(&sun, sizeof(sun));
925251bedd1SWarner Losh 	sun.sun_family = AF_UNIX;
926251bedd1SWarner Losh 	strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
927251bedd1SWarner Losh 	slen = SUN_LEN(&sun);
928251bedd1SWarner Losh 	unlink(name);
929e60fa014SJoe Marcus Clarke 	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
930e60fa014SJoe Marcus Clarke 	    	err(1, "fcntl");
93127744ca7SEitan Adler 	if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
932251bedd1SWarner Losh 		err(1, "bind");
933251bedd1SWarner Losh 	listen(fd, 4);
934daa0d9ddSAlan Somers 	if (chown(name, 0, 0))	/* XXX - root.wheel */
935daa0d9ddSAlan Somers 		err(1, "chown");
936daa0d9ddSAlan Somers 	if (chmod(name, 0666))
937daa0d9ddSAlan Somers 		err(1, "chmod");
938251bedd1SWarner Losh 	return (fd);
939251bedd1SWarner Losh }
940251bedd1SWarner Losh 
9410bc60783SEitan Adler static unsigned int max_clients = 10;	/* Default, can be overridden on cmdline. */
9420bc60783SEitan Adler static unsigned int num_clients;
9439102691eSAlan Somers 
9440bc60783SEitan Adler static list<client_t> clients;
945251bedd1SWarner Losh 
946514e2dbfSEitan Adler static void
notify_clients(const char * data,int len)947251bedd1SWarner Losh notify_clients(const char *data, int len)
948251bedd1SWarner Losh {
9499102691eSAlan Somers 	list<client_t>::iterator i;
950251bedd1SWarner Losh 
951e1334f93SIan Lepore 	/*
952e1334f93SIan Lepore 	 * Deliver the data to all clients.  Throw clients overboard at the
953e1334f93SIan Lepore 	 * first sign of trouble.  This reaps clients who've died or closed
954e1334f93SIan Lepore 	 * their sockets, and also clients who are alive but failing to keep up
955e1334f93SIan Lepore 	 * (or who are maliciously not reading, to consume buffer space in
956e1334f93SIan Lepore 	 * kernel memory or tie up the limited number of available connections).
957e1334f93SIan Lepore 	 */
958e1334f93SIan Lepore 	for (i = clients.begin(); i != clients.end(); ) {
9599102691eSAlan Somers 		int flags;
9609102691eSAlan Somers 		if (i->socktype == SOCK_SEQPACKET)
9619102691eSAlan Somers 			flags = MSG_EOR;
9629102691eSAlan Somers 		else
9639102691eSAlan Somers 			flags = 0;
9649102691eSAlan Somers 
9659102691eSAlan Somers 		if (send(i->fd, data, len, flags) != len) {
966e1334f93SIan Lepore 			--num_clients;
9679102691eSAlan Somers 			close(i->fd);
968e1334f93SIan Lepore 			i = clients.erase(i);
9699102691eSAlan Somers 			devdlog(LOG_WARNING, "notify_clients: send() failed; "
9706d1014a3SAlan Somers 			    "dropping unresponsive client\n");
971e1334f93SIan Lepore 		} else
972e1334f93SIan Lepore 			++i;
973251bedd1SWarner Losh 	}
974251bedd1SWarner Losh }
975251bedd1SWarner Losh 
976514e2dbfSEitan Adler static void
check_clients(void)977e1334f93SIan Lepore check_clients(void)
978e1334f93SIan Lepore {
979e1334f93SIan Lepore 	int s;
980e1334f93SIan Lepore 	struct pollfd pfd;
9819102691eSAlan Somers 	list<client_t>::iterator i;
982e1334f93SIan Lepore 
983e1334f93SIan Lepore 	/*
984e1334f93SIan Lepore 	 * Check all existing clients to see if any of them have disappeared.
985e1334f93SIan Lepore 	 * Normally we reap clients when we get an error trying to send them an
986e1334f93SIan Lepore 	 * event.  This check eliminates the problem of an ever-growing list of
987e1334f93SIan Lepore 	 * zombie clients because we're never writing to them on a system
988e1334f93SIan Lepore 	 * without frequent device-change activity.
989e1334f93SIan Lepore 	 */
990e1334f93SIan Lepore 	pfd.events = 0;
991e1334f93SIan Lepore 	for (i = clients.begin(); i != clients.end(); ) {
9929102691eSAlan Somers 		pfd.fd = i->fd;
993e1334f93SIan Lepore 		s = poll(&pfd, 1, 0);
994e1334f93SIan Lepore 		if ((s < 0 && s != EINTR ) ||
995e1334f93SIan Lepore 		    (s > 0 && (pfd.revents & POLLHUP))) {
996e1334f93SIan Lepore 			--num_clients;
9979102691eSAlan Somers 			close(i->fd);
998e1334f93SIan Lepore 			i = clients.erase(i);
9996d1014a3SAlan Somers 			devdlog(LOG_NOTICE, "check_clients:  "
10006d1014a3SAlan Somers 			    "dropping disconnected client\n");
1001e1334f93SIan Lepore 		} else
1002e1334f93SIan Lepore 			++i;
1003e1334f93SIan Lepore 	}
1004251bedd1SWarner Losh }
1005251bedd1SWarner Losh 
1006514e2dbfSEitan Adler static void
new_client(int fd,int socktype)10079102691eSAlan Somers new_client(int fd, int socktype)
1008251bedd1SWarner Losh {
10099102691eSAlan Somers 	client_t s;
1010b026eddfSAlan Somers 	int sndbuf_size;
1011251bedd1SWarner Losh 
1012e1334f93SIan Lepore 	/*
1013e1334f93SIan Lepore 	 * First go reap any zombie clients, then accept the connection, and
1014e1334f93SIan Lepore 	 * shut down the read side to stop clients from consuming kernel memory
1015e1334f93SIan Lepore 	 * by sending large buffers full of data we'll never read.
1016e1334f93SIan Lepore 	 */
1017e1334f93SIan Lepore 	check_clients();
10189102691eSAlan Somers 	s.socktype = socktype;
10199102691eSAlan Somers 	s.fd = accept(fd, NULL, NULL);
10209102691eSAlan Somers 	if (s.fd != -1) {
1021b026eddfSAlan Somers 		sndbuf_size = CLIENT_BUFSIZE;
10229102691eSAlan Somers 		if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
1023b026eddfSAlan Somers 		    sizeof(sndbuf_size)))
1024b026eddfSAlan Somers 			err(1, "setsockopt");
10259102691eSAlan Somers 		shutdown(s.fd, SHUT_RD);
1026251bedd1SWarner Losh 		clients.push_back(s);
1027e1334f93SIan Lepore 		++num_clients;
1028b026eddfSAlan Somers 	} else
1029b026eddfSAlan Somers 		err(1, "accept");
1030251bedd1SWarner Losh }
1031251bedd1SWarner Losh 
10323054f218SWarner Losh static void
event_loop(void)10333054f218SWarner Losh event_loop(void)
10343054f218SWarner Losh {
10353054f218SWarner Losh 	int rv;
10363054f218SWarner Losh 	int fd;
10373054f218SWarner Losh 	char buffer[DEVCTL_MAXBUF];
10385a882d77SWarner Losh 	int once = 0;
10399102691eSAlan Somers 	int stream_fd, seqpacket_fd, max_fd;
1040e1334f93SIan Lepore 	int accepting;
10415a882d77SWarner Losh 	timeval tv;
10425a882d77SWarner Losh 	fd_set fds;
10433054f218SWarner Losh 
1044901610f4SPawel Jakub Dawidek 	fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC);
10453054f218SWarner Losh 	if (fd == -1)
1046251bedd1SWarner Losh 		err(1, "Can't open devctl device %s", PATH_DEVCTL);
10479102691eSAlan Somers 	stream_fd = create_socket(STREAMPIPE, SOCK_STREAM);
10489102691eSAlan Somers 	seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET);
1049e1334f93SIan Lepore 	accepting = 1;
10509102691eSAlan Somers 	max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1;
105167ae3bf1SEitan Adler 	while (!romeo_must_die) {
10526a2ae0ebSAlan Somers 		if (!once && !no_daemon && !daemonize_quick) {
10535a882d77SWarner Losh 			// Check to see if we have any events pending.
10545a882d77SWarner Losh 			tv.tv_sec = 0;
10555a882d77SWarner Losh 			tv.tv_usec = 0;
10565a882d77SWarner Losh 			FD_ZERO(&fds);
10575a882d77SWarner Losh 			FD_SET(fd, &fds);
1058dacbef68SEitan Adler 			rv = select(fd + 1, &fds, NULL, NULL, &tv);
10595a882d77SWarner Losh 			// No events -> we've processed all pending events
1060f3059505SWarner Losh 			if (rv == 0) {
10616d1014a3SAlan Somers 				devdlog(LOG_DEBUG, "Calling daemon\n");
10621a0cc6b1SPawel Jakub Dawidek 				cfg.remove_pidfile();
10631a0cc6b1SPawel Jakub Dawidek 				cfg.open_pidfile();
10645a882d77SWarner Losh 				daemon(0, 0);
10651a0cc6b1SPawel Jakub Dawidek 				cfg.write_pidfile();
10665a882d77SWarner Losh 				once++;
10675a882d77SWarner Losh 			}
10685a882d77SWarner Losh 		}
1069e1334f93SIan Lepore 		/*
1070e1334f93SIan Lepore 		 * When we've already got the max number of clients, stop
10719102691eSAlan Somers 		 * accepting new connections (don't put the listening sockets in
10729102691eSAlan Somers 		 * the set), shrink the accept() queue to reject connections
10739102691eSAlan Somers 		 * quickly, and poll the existing clients more often, so that we
10749102691eSAlan Somers 		 * notice more quickly when any of them disappear to free up
10759102691eSAlan Somers 		 * client slots.
1076e1334f93SIan Lepore 		 */
1077251bedd1SWarner Losh 		FD_ZERO(&fds);
1078251bedd1SWarner Losh 		FD_SET(fd, &fds);
1079e1334f93SIan Lepore 		if (num_clients < max_clients) {
1080e1334f93SIan Lepore 			if (!accepting) {
10819102691eSAlan Somers 				listen(stream_fd, max_clients);
10829102691eSAlan Somers 				listen(seqpacket_fd, max_clients);
1083e1334f93SIan Lepore 				accepting = 1;
1084e1334f93SIan Lepore 			}
10859102691eSAlan Somers 			FD_SET(stream_fd, &fds);
10869102691eSAlan Somers 			FD_SET(seqpacket_fd, &fds);
1087e1334f93SIan Lepore 			tv.tv_sec = 60;
1088e1334f93SIan Lepore 			tv.tv_usec = 0;
1089e1334f93SIan Lepore 		} else {
1090e1334f93SIan Lepore 			if (accepting) {
10919102691eSAlan Somers 				listen(stream_fd, 0);
10929102691eSAlan Somers 				listen(seqpacket_fd, 0);
1093e1334f93SIan Lepore 				accepting = 0;
1094e1334f93SIan Lepore 			}
1095e1334f93SIan Lepore 			tv.tv_sec = 2;
1096e1334f93SIan Lepore 			tv.tv_usec = 0;
1097e1334f93SIan Lepore 		}
1098e1334f93SIan Lepore 		rv = select(max_fd, &fds, NULL, NULL, &tv);
1099be685e01SAlan Somers 		if (got_siginfo) {
1100f0038a8eSAlan Somers 			devdlog(LOG_NOTICE, "Events received so far=%u\n",
1101be685e01SAlan Somers 			    total_events);
1102be685e01SAlan Somers 			got_siginfo = 0;
1103be685e01SAlan Somers 		}
1104251bedd1SWarner Losh 		if (rv == -1) {
1105251bedd1SWarner Losh 			if (errno == EINTR)
1106251bedd1SWarner Losh 				continue;
1107251bedd1SWarner Losh 			err(1, "select");
1108e1334f93SIan Lepore 		} else if (rv == 0)
1109e1334f93SIan Lepore 			check_clients();
1110251bedd1SWarner Losh 		if (FD_ISSET(fd, &fds)) {
11113054f218SWarner Losh 			rv = read(fd, buffer, sizeof(buffer) - 1);
11123054f218SWarner Losh 			if (rv > 0) {
1113be685e01SAlan Somers 				total_events++;
11146d1014a3SAlan Somers 				if (rv == sizeof(buffer) - 1) {
11156d1014a3SAlan Somers 					devdlog(LOG_WARNING, "Warning: "
11166d1014a3SAlan Somers 					    "available event data exceeded "
11176d1014a3SAlan Somers 					    "buffer space\n");
11186d1014a3SAlan Somers 				}
1119251bedd1SWarner Losh 				notify_clients(buffer, rv);
11203054f218SWarner Losh 				buffer[rv] = '\0';
11213054f218SWarner Losh 				while (buffer[--rv] == '\n')
11223054f218SWarner Losh 					buffer[rv] = '\0';
1123daa0d9ddSAlan Somers 				try {
11243054f218SWarner Losh 					process_event(buffer);
1125daa0d9ddSAlan Somers 				}
1126958160f3SEitan Adler 				catch (const std::length_error& e) {
1127daa0d9ddSAlan Somers 					devdlog(LOG_ERR, "Dropping event %s "
1128daa0d9ddSAlan Somers 					    "due to low memory", buffer);
1129daa0d9ddSAlan Somers 				}
11303054f218SWarner Losh 			} else if (rv < 0) {
11313054f218SWarner Losh 				if (errno != EINTR)
11323054f218SWarner Losh 					break;
11333054f218SWarner Losh 			} else {
11343054f218SWarner Losh 				/* EOF */
11353054f218SWarner Losh 				break;
11363054f218SWarner Losh 			}
11373054f218SWarner Losh 		}
11389102691eSAlan Somers 		if (FD_ISSET(stream_fd, &fds))
11399102691eSAlan Somers 			new_client(stream_fd, SOCK_STREAM);
11409102691eSAlan Somers 		/*
11419102691eSAlan Somers 		 * Aside from the socket type, both sockets use the same
11429102691eSAlan Somers 		 * protocol, so we can process clients the same way.
11439102691eSAlan Somers 		 */
11449102691eSAlan Somers 		if (FD_ISSET(seqpacket_fd, &fds))
11459102691eSAlan Somers 			new_client(seqpacket_fd, SOCK_SEQPACKET);
1146251bedd1SWarner Losh 	}
114791cfa6b8SAlan Somers 	cfg.remove_pidfile();
1148daa0d9ddSAlan Somers 	close(seqpacket_fd);
1149daa0d9ddSAlan Somers 	close(stream_fd);
11503054f218SWarner Losh 	close(fd);
11513054f218SWarner Losh }
11520285e9b1SAlan Somers 
11533054f218SWarner Losh /*
11543054f218SWarner Losh  * functions that the parser uses.
11553054f218SWarner Losh  */
11563054f218SWarner Losh void
add_attach(int prio,event_proc * p)11573054f218SWarner Losh add_attach(int prio, event_proc *p)
11583054f218SWarner Losh {
11593054f218SWarner Losh 	cfg.add_attach(prio, p);
11603054f218SWarner Losh }
11613054f218SWarner Losh 
11623054f218SWarner Losh void
add_detach(int prio,event_proc * p)11633054f218SWarner Losh add_detach(int prio, event_proc *p)
11643054f218SWarner Losh {
11653054f218SWarner Losh 	cfg.add_detach(prio, p);
11663054f218SWarner Losh }
11673054f218SWarner Losh 
11683054f218SWarner Losh void
add_directory(const char * dir)11693054f218SWarner Losh add_directory(const char *dir)
11703054f218SWarner Losh {
11713054f218SWarner Losh 	cfg.add_directory(dir);
11723054f218SWarner Losh 	free(const_cast<char *>(dir));
11733054f218SWarner Losh }
11743054f218SWarner Losh 
11753054f218SWarner Losh void
add_nomatch(int prio,event_proc * p)11763054f218SWarner Losh add_nomatch(int prio, event_proc *p)
11773054f218SWarner Losh {
11783054f218SWarner Losh 	cfg.add_nomatch(prio, p);
11793054f218SWarner Losh }
11803054f218SWarner Losh 
1181842ccec5SWarner Losh void
add_notify(int prio,event_proc * p)1182842ccec5SWarner Losh add_notify(int prio, event_proc *p)
1183842ccec5SWarner Losh {
1184842ccec5SWarner Losh 	cfg.add_notify(prio, p);
1185842ccec5SWarner Losh }
1186842ccec5SWarner Losh 
11873054f218SWarner Losh event_proc *
add_to_event_proc(event_proc * ep,eps * eps)11883054f218SWarner Losh add_to_event_proc(event_proc *ep, eps *eps)
11893054f218SWarner Losh {
11903054f218SWarner Losh 	if (ep == NULL)
11913054f218SWarner Losh 		ep = new event_proc();
11923054f218SWarner Losh 	ep->add(eps);
11933054f218SWarner Losh 	return (ep);
11943054f218SWarner Losh }
11953054f218SWarner Losh 
11963054f218SWarner Losh eps *
new_action(const char * cmd)11973054f218SWarner Losh new_action(const char *cmd)
11983054f218SWarner Losh {
11993054f218SWarner Losh 	eps *e = new action(cmd);
12003054f218SWarner Losh 	free(const_cast<char *>(cmd));
12013054f218SWarner Losh 	return (e);
12023054f218SWarner Losh }
12033054f218SWarner Losh 
12043054f218SWarner Losh eps *
new_match(const char * var,const char * re)12053054f218SWarner Losh new_match(const char *var, const char *re)
12063054f218SWarner Losh {
120776527435SWarner Losh 	/*
120876527435SWarner Losh 	 * In FreeBSD 14, we changed the system=kern to system=kernel for the
120976527435SWarner Losh 	 * resume message to match all the other 'kernel' messages. Generate a
121076527435SWarner Losh 	 * warning for the life of 14.x that we've 'fixed' the file on the fly,
121176527435SWarner Losh 	 * but make it a fatal error in 15.x and newer.
121276527435SWarner Losh 	 */
121376527435SWarner Losh 	if (strcmp(var, "kern") == 0) {
121476527435SWarner Losh #if __FreeBSD_version < 1500000
121576527435SWarner Losh 		devdlog(LOG_WARNING,
121676527435SWarner Losh 		    "Changing deprecated system='kern' to new name 'kernel' in %s line %d.",
121776527435SWarner Losh 		    curr_cf, lineno);
121876527435SWarner Losh 		free(const_cast<char *>(var));
121976527435SWarner Losh 		var = strdup("kernel");
122076527435SWarner Losh #elif  __FreeBSD_version < 1600000
122176527435SWarner Losh 		errx(1, "Encountered deprecated system=\"kern\" rule in %s line %d",
122276527435SWarner Losh 		    curr_cf, lineno);
122376527435SWarner Losh #else
122476527435SWarner Losh #error "Remove this gross hack"
122576527435SWarner Losh #endif
122676527435SWarner Losh 	}
122776527435SWarner Losh 
12285102ef84SWarner Losh 	eps *e = new match(cfg, var, re);
12293054f218SWarner Losh 	free(const_cast<char *>(var));
12303054f218SWarner Losh 	free(const_cast<char *>(re));
12313054f218SWarner Losh 	return (e);
12323054f218SWarner Losh }
12333054f218SWarner Losh 
1234cd70782bSWarner Losh eps *
new_media(const char * var,const char * re)1235cd70782bSWarner Losh new_media(const char *var, const char *re)
1236cd70782bSWarner Losh {
1237cd70782bSWarner Losh 	eps *e = new media(cfg, var, re);
1238cd70782bSWarner Losh 	free(const_cast<char *>(var));
1239cd70782bSWarner Losh 	free(const_cast<char *>(re));
1240cd70782bSWarner Losh 	return (e);
1241cd70782bSWarner Losh }
1242cd70782bSWarner Losh 
12433054f218SWarner Losh void
set_pidfile(const char * name)12443054f218SWarner Losh set_pidfile(const char *name)
12453054f218SWarner Losh {
12463054f218SWarner Losh 	cfg.set_pidfile(name);
12473054f218SWarner Losh 	free(const_cast<char *>(name));
12483054f218SWarner Losh }
12493054f218SWarner Losh 
12503054f218SWarner Losh void
set_variable(const char * var,const char * val)12513054f218SWarner Losh set_variable(const char *var, const char *val)
12523054f218SWarner Losh {
12533054f218SWarner Losh 	cfg.set_variable(var, val);
12543054f218SWarner Losh 	free(const_cast<char *>(var));
12553054f218SWarner Losh 	free(const_cast<char *>(val));
12563054f218SWarner Losh }
12573054f218SWarner Losh 
12580285e9b1SAlan Somers 
12593054f218SWarner Losh 
12603054f218SWarner Losh static void
gensighand(int)12613054f218SWarner Losh gensighand(int)
12623054f218SWarner Losh {
1263fcdcaa88SEitan Adler 	romeo_must_die = 1;
12643054f218SWarner Losh }
12653054f218SWarner Losh 
12666d1014a3SAlan Somers /*
1267be685e01SAlan Somers  * SIGINFO handler.  Will print useful statistics to the syslog or stderr
1268be685e01SAlan Somers  * as appropriate
1269be685e01SAlan Somers  */
1270be685e01SAlan Somers static void
siginfohand(int)1271be685e01SAlan Somers siginfohand(int)
1272be685e01SAlan Somers {
1273be685e01SAlan Somers 	got_siginfo = 1;
1274be685e01SAlan Somers }
1275be685e01SAlan Somers 
1276be685e01SAlan Somers /*
12772e024bc2SAndriy Gapon  * Local logging function.  Prints to syslog if we're daemonized; stderr
12786d1014a3SAlan Somers  * otherwise.
12796d1014a3SAlan Somers  */
12806d1014a3SAlan Somers static void
devdlog(int priority,const char * fmt,...)12816d1014a3SAlan Somers devdlog(int priority, const char* fmt, ...)
12826d1014a3SAlan Somers {
12836d1014a3SAlan Somers 	va_list argp;
12846d1014a3SAlan Somers 
12856d1014a3SAlan Somers 	va_start(argp, fmt);
12866a2ae0ebSAlan Somers 	if (no_daemon)
12876d1014a3SAlan Somers 		vfprintf(stderr, fmt, argp);
12884d00d13cSWarner Losh 	else if (quiet_mode == 0 || priority <= LOG_WARNING)
12896d1014a3SAlan Somers 		vsyslog(priority, fmt, argp);
12906d1014a3SAlan Somers 	va_end(argp);
12916d1014a3SAlan Somers }
12926d1014a3SAlan Somers 
12933054f218SWarner Losh static void
usage()12943054f218SWarner Losh usage()
12953054f218SWarner Losh {
12966a2ae0ebSAlan Somers 	fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n",
1297e1334f93SIan Lepore 	    getprogname());
12983054f218SWarner Losh 	exit(1);
12993054f218SWarner Losh }
13003054f218SWarner Losh 
13019c180898SWarner Losh static void
check_devd_enabled()13029c180898SWarner Losh check_devd_enabled()
13039c180898SWarner Losh {
13049c180898SWarner Losh 	int val = 0;
13059c180898SWarner Losh 	size_t len;
13069c180898SWarner Losh 
13079c180898SWarner Losh 	len = sizeof(val);
130879f92e25SWarner Losh 	if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
13099c180898SWarner Losh 		errx(1, "devctl sysctl missing from kernel!");
1310ee38f2e0SMateusz Guzik 	if (val == 0) {
1311ee38f2e0SMateusz Guzik 		warnx("Setting " SYSCTL " to 1000");
1312ee38f2e0SMateusz Guzik 		val = 1000;
1313daa0d9ddSAlan Somers 		if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)))
1314daa0d9ddSAlan Somers 			err(1, "sysctlbyname");
13159c180898SWarner Losh 	}
13169c180898SWarner Losh }
13179c180898SWarner Losh 
13183054f218SWarner Losh /*
13193054f218SWarner Losh  * main
13203054f218SWarner Losh  */
13213054f218SWarner Losh int
main(int argc,char ** argv)13223054f218SWarner Losh main(int argc, char **argv)
13233054f218SWarner Losh {
13243054f218SWarner Losh 	int ch;
13253054f218SWarner Losh 
13269c180898SWarner Losh 	check_devd_enabled();
13276a2ae0ebSAlan Somers 	while ((ch = getopt(argc, argv, "df:l:nq")) != -1) {
13283054f218SWarner Losh 		switch (ch) {
13293054f218SWarner Losh 		case 'd':
13306a2ae0ebSAlan Somers 			no_daemon = 1;
13313054f218SWarner Losh 			break;
13328334958aSJoseph Koshy 		case 'f':
13338334958aSJoseph Koshy 			configfile = optarg;
13348334958aSJoseph Koshy 			break;
1335e1334f93SIan Lepore 		case 'l':
1336e1334f93SIan Lepore 			max_clients = MAX(1, strtoul(optarg, NULL, 0));
1337e1334f93SIan Lepore 			break;
13385a882d77SWarner Losh 		case 'n':
13396a2ae0ebSAlan Somers 			daemonize_quick = 1;
13406a2ae0ebSAlan Somers 			break;
13416a2ae0ebSAlan Somers 		case 'q':
13426a2ae0ebSAlan Somers 			quiet_mode = 1;
13435a882d77SWarner Losh 			break;
13443054f218SWarner Losh 		default:
13453054f218SWarner Losh 			usage();
13463054f218SWarner Losh 		}
13473054f218SWarner Losh 	}
13483054f218SWarner Losh 
13493054f218SWarner Losh 	cfg.parse();
13506a2ae0ebSAlan Somers 	if (!no_daemon && daemonize_quick) {
13511a0cc6b1SPawel Jakub Dawidek 		cfg.open_pidfile();
13523054f218SWarner Losh 		daemon(0, 0);
13531a0cc6b1SPawel Jakub Dawidek 		cfg.write_pidfile();
1354fd6a8f23SWarner Losh 	}
1355f32a7686SWarner Losh 	signal(SIGPIPE, SIG_IGN);
13563054f218SWarner Losh 	signal(SIGHUP, gensighand);
13573054f218SWarner Losh 	signal(SIGINT, gensighand);
13583054f218SWarner Losh 	signal(SIGTERM, gensighand);
1359be685e01SAlan Somers 	signal(SIGINFO, siginfohand);
13603054f218SWarner Losh 	event_loop();
13613054f218SWarner Losh 	return (0);
13623054f218SWarner Losh }
1363