13054f218SWarner Losh /*- 2b8f92ce4SWarner Losh * Copyright (c) 2002-2010 M. Warner Losh. 33054f218SWarner Losh * All rights reserved. 43054f218SWarner Losh * 53054f218SWarner Losh * Redistribution and use in source and binary forms, with or without 63054f218SWarner Losh * modification, are permitted provided that the following conditions 73054f218SWarner Losh * are met: 83054f218SWarner Losh * 1. Redistributions of source code must retain the above copyright 93054f218SWarner Losh * notice, this list of conditions and the following disclaimer. 103054f218SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 113054f218SWarner Losh * notice, this list of conditions and the following disclaimer in the 123054f218SWarner Losh * documentation and/or other materials provided with the distribution. 133054f218SWarner Losh * 143054f218SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 153054f218SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 163054f218SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 173054f218SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 183054f218SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 193054f218SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 203054f218SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 213054f218SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 223054f218SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 233054f218SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 243054f218SWarner Losh * SUCH DAMAGE. 25b8f92ce4SWarner Losh * 26b8f92ce4SWarner Losh * my_system is a variation on lib/libc/stdlib/system.c: 27b8f92ce4SWarner Losh * 28b8f92ce4SWarner Losh * Copyright (c) 1988, 1993 29b8f92ce4SWarner Losh * The Regents of the University of California. All rights reserved. 30b8f92ce4SWarner Losh * 31b8f92ce4SWarner Losh * Redistribution and use in source and binary forms, with or without 32b8f92ce4SWarner Losh * modification, are permitted provided that the following conditions 33b8f92ce4SWarner Losh * are met: 34b8f92ce4SWarner Losh * 1. Redistributions of source code must retain the above copyright 35b8f92ce4SWarner Losh * notice, this list of conditions and the following disclaimer. 36b8f92ce4SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 37b8f92ce4SWarner Losh * notice, this list of conditions and the following disclaimer in the 38b8f92ce4SWarner Losh * documentation and/or other materials provided with the distribution. 39b8f92ce4SWarner Losh * 4. Neither the name of the University nor the names of its contributors 40b8f92ce4SWarner Losh * may be used to endorse or promote products derived from this software 41b8f92ce4SWarner Losh * without specific prior written permission. 42b8f92ce4SWarner Losh * 43b8f92ce4SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 44b8f92ce4SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45b8f92ce4SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46b8f92ce4SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 47b8f92ce4SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48b8f92ce4SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49b8f92ce4SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50b8f92ce4SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51b8f92ce4SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52b8f92ce4SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53b8f92ce4SWarner Losh * SUCH DAMAGE. 543054f218SWarner Losh */ 553054f218SWarner Losh 563054f218SWarner Losh /* 573054f218SWarner Losh * DEVD control daemon. 583054f218SWarner Losh */ 593054f218SWarner Losh 603054f218SWarner Losh // TODO list: 613054f218SWarner Losh // o devd.conf and devd man pages need a lot of help: 62251bedd1SWarner Losh // - devd needs to document the unix domain socket 633054f218SWarner Losh // - devd.conf needs more details on the supported statements. 643054f218SWarner Losh 653054f218SWarner Losh #include <sys/cdefs.h> 663054f218SWarner Losh __FBSDID("$FreeBSD$"); 673054f218SWarner Losh 683054f218SWarner Losh #include <sys/param.h> 69251bedd1SWarner Losh #include <sys/socket.h> 70251bedd1SWarner Losh #include <sys/stat.h> 719c180898SWarner Losh #include <sys/sysctl.h> 72251bedd1SWarner Losh #include <sys/types.h> 73b8f92ce4SWarner Losh #include <sys/wait.h> 74251bedd1SWarner Losh #include <sys/un.h> 753054f218SWarner Losh 76d005340fSEitan Adler #include <cctype> 77d005340fSEitan Adler #include <cerrno> 78d005340fSEitan Adler #include <cstdlib> 79d005340fSEitan Adler #include <cstdio> 80d005340fSEitan Adler #include <csignal> 81d005340fSEitan Adler #include <cstring> 82967ecf4aSAlan Somers #include <cstdarg> 83d005340fSEitan Adler 843054f218SWarner Losh #include <dirent.h> 853054f218SWarner Losh #include <err.h> 863054f218SWarner Losh #include <fcntl.h> 871a0cc6b1SPawel Jakub Dawidek #include <libutil.h> 88b8f92ce4SWarner Losh #include <paths.h> 89e1334f93SIan Lepore #include <poll.h> 905102ef84SWarner Losh #include <regex.h> 916d1014a3SAlan Somers #include <syslog.h> 923054f218SWarner Losh #include <unistd.h> 933054f218SWarner Losh 948b823408SWarner Losh #include <algorithm> 953054f218SWarner Losh #include <map> 963054f218SWarner Losh #include <string> 97251bedd1SWarner Losh #include <list> 98*3e9662dcSAlan Somers #include <stdexcept> 993054f218SWarner Losh #include <vector> 1003054f218SWarner Losh 1016aeeca8eSWarner Losh #include "devd.h" /* C compatible definitions */ 1026aeeca8eSWarner Losh #include "devd.hh" /* C++ class definitions */ 1033054f218SWarner Losh 1049102691eSAlan Somers #define STREAMPIPE "/var/run/devd.pipe" 1059102691eSAlan Somers #define SEQPACKETPIPE "/var/run/devd.seqpacket.pipe" 1063054f218SWarner Losh #define CF "/etc/devd.conf" 107ee38f2e0SMateusz Guzik #define SYSCTL "hw.bus.devctl_queue" 1083054f218SWarner Losh 109b026eddfSAlan Somers /* 110b026eddfSAlan Somers * Since the client socket is nonblocking, we must increase its send buffer to 111b026eddfSAlan Somers * handle brief event storms. On FreeBSD, AF_UNIX sockets don't have a receive 112d9ca81daSAlan Somers * buffer, so the client can't increase the buffersize by itself. 113b026eddfSAlan Somers * 114b026eddfSAlan Somers * For example, when creating a ZFS pool, devd emits one 165 character 115d9ca81daSAlan Somers * resource.fs.zfs.statechange message for each vdev in the pool. The kernel 116d9ca81daSAlan Somers * allocates a 4608B mbuf for each message. Modern technology places a limit of 117d9ca81daSAlan Somers * roughly 450 drives/rack, and it's unlikely that a zpool will ever be larger 118d9ca81daSAlan Somers * than that. 119d9ca81daSAlan Somers * 120d9ca81daSAlan Somers * 450 drives * 165 bytes / drive = 74250B of data in the sockbuf 121d9ca81daSAlan Somers * 450 drives * 4608B / drive = 2073600B of mbufs in the sockbuf 122d9ca81daSAlan Somers * 123d9ca81daSAlan Somers * We can't directly set the sockbuf's mbuf limit, but we can do it indirectly. 124d9ca81daSAlan Somers * The kernel sets it to the minimum of a hard-coded maximum value and sbcc * 125d9ca81daSAlan Somers * kern.ipc.sockbuf_waste_factor, where sbcc is the socket buffer size set by 126d9ca81daSAlan Somers * the user. The default value of kern.ipc.sockbuf_waste_factor is 8. If we 127d9ca81daSAlan Somers * set the bufsize to 256k and use the kern.ipc.sockbuf_waste_factor, then the 128d9ca81daSAlan Somers * kernel will set the mbuf limit to 2MB, which is just large enough for 450 129d9ca81daSAlan Somers * drives. It also happens to be the same as the hardcoded maximum value. 130b026eddfSAlan Somers */ 131d9ca81daSAlan Somers #define CLIENT_BUFSIZE 262144 132b026eddfSAlan Somers 1333054f218SWarner Losh using namespace std; 1343054f218SWarner Losh 1359102691eSAlan Somers typedef struct client { 1369102691eSAlan Somers int fd; 1379102691eSAlan Somers int socktype; 1389102691eSAlan Somers } client_t; 1399102691eSAlan Somers 1403054f218SWarner Losh extern FILE *yyin; 1413054f218SWarner Losh extern int lineno; 1423054f218SWarner Losh 143842ccec5SWarner Losh static const char notify = '!'; 1448b823408SWarner Losh static const char nomatch = '?'; 1458b823408SWarner Losh static const char attach = '+'; 1468b823408SWarner Losh static const char detach = '-'; 1478b823408SWarner Losh 1481a0cc6b1SPawel Jakub Dawidek static struct pidfh *pfh; 1491a0cc6b1SPawel Jakub Dawidek 1506a2ae0ebSAlan Somers static int no_daemon = 0; 1516a2ae0ebSAlan Somers static int daemonize_quick = 0; 1526a2ae0ebSAlan Somers static int quiet_mode = 0; 153be685e01SAlan Somers static unsigned total_events = 0; 154be685e01SAlan Somers static volatile sig_atomic_t got_siginfo = 0; 155fcdcaa88SEitan Adler static volatile sig_atomic_t romeo_must_die = 0; 1563054f218SWarner Losh 1578334958aSJoseph Koshy static const char *configfile = CF; 1588334958aSJoseph Koshy 1593449b15aSAlan Somers static void devdlog(int priority, const char* message, ...) 1603449b15aSAlan Somers __printflike(2, 3); 1613054f218SWarner Losh static void event_loop(void); 1623054f218SWarner Losh static void usage(void); 1633054f218SWarner Losh 1648b823408SWarner Losh template <class T> void 1658b823408SWarner Losh delete_and_clear(vector<T *> &v) 1668b823408SWarner Losh { 1678b823408SWarner Losh typename vector<T *>::const_iterator i; 1683054f218SWarner Losh 169edee691dSEitan Adler for (i = v.begin(); i != v.end(); ++i) 1708b823408SWarner Losh delete *i; 1718b823408SWarner Losh v.clear(); 1728b823408SWarner Losh } 1738b823408SWarner Losh 1743054f218SWarner Losh config cfg; 1753054f218SWarner Losh 1763054f218SWarner Losh event_proc::event_proc() : _prio(-1) 1773054f218SWarner Losh { 1785dfc0f6cSIan Lepore _epsvec.reserve(4); 1793054f218SWarner Losh } 1803054f218SWarner Losh 1813054f218SWarner Losh event_proc::~event_proc() 1823054f218SWarner Losh { 18313f8bb71SAlexander Nedotsukov delete_and_clear(_epsvec); 1843054f218SWarner Losh } 1853054f218SWarner Losh 1863054f218SWarner Losh void 1873054f218SWarner Losh event_proc::add(eps *eps) 1883054f218SWarner Losh { 1893054f218SWarner Losh _epsvec.push_back(eps); 1903054f218SWarner Losh } 1913054f218SWarner Losh 1923054f218SWarner Losh bool 193ef370346SEitan Adler event_proc::matches(config &c) const 1943054f218SWarner Losh { 1953054f218SWarner Losh vector<eps *>::const_iterator i; 1963054f218SWarner Losh 197edee691dSEitan Adler for (i = _epsvec.begin(); i != _epsvec.end(); ++i) 1983054f218SWarner Losh if (!(*i)->do_match(c)) 1993054f218SWarner Losh return (false); 2003054f218SWarner Losh return (true); 2013054f218SWarner Losh } 2023054f218SWarner Losh 2033054f218SWarner Losh bool 204ef370346SEitan Adler event_proc::run(config &c) const 2053054f218SWarner Losh { 2063054f218SWarner Losh vector<eps *>::const_iterator i; 2073054f218SWarner Losh 208edee691dSEitan Adler for (i = _epsvec.begin(); i != _epsvec.end(); ++i) 2093054f218SWarner Losh if (!(*i)->do_action(c)) 2103054f218SWarner Losh return (false); 2113054f218SWarner Losh return (true); 2123054f218SWarner Losh } 2133054f218SWarner Losh 2143054f218SWarner Losh action::action(const char *cmd) 2153054f218SWarner Losh : _cmd(cmd) 2163054f218SWarner Losh { 2173054f218SWarner Losh // nothing 2183054f218SWarner Losh } 2193054f218SWarner Losh 2203054f218SWarner Losh action::~action() 2213054f218SWarner Losh { 2223054f218SWarner Losh // nothing 2233054f218SWarner Losh } 2243054f218SWarner Losh 225b8f92ce4SWarner Losh static int 226b8f92ce4SWarner Losh my_system(const char *command) 227b8f92ce4SWarner Losh { 228b8f92ce4SWarner Losh pid_t pid, savedpid; 229b8f92ce4SWarner Losh int pstat; 230b8f92ce4SWarner Losh struct sigaction ign, intact, quitact; 231b8f92ce4SWarner Losh sigset_t newsigblock, oldsigblock; 232b8f92ce4SWarner Losh 233b8f92ce4SWarner Losh if (!command) /* just checking... */ 234b8f92ce4SWarner Losh return (1); 235b8f92ce4SWarner Losh 236b8f92ce4SWarner Losh /* 237b8f92ce4SWarner Losh * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save 238b8f92ce4SWarner Losh * existing signal dispositions. 239b8f92ce4SWarner Losh */ 240b8f92ce4SWarner Losh ign.sa_handler = SIG_IGN; 241b8f92ce4SWarner Losh ::sigemptyset(&ign.sa_mask); 242b8f92ce4SWarner Losh ign.sa_flags = 0; 243b8f92ce4SWarner Losh ::sigaction(SIGINT, &ign, &intact); 244b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &ign, &quitact); 245b8f92ce4SWarner Losh ::sigemptyset(&newsigblock); 246b8f92ce4SWarner Losh ::sigaddset(&newsigblock, SIGCHLD); 247b8f92ce4SWarner Losh ::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 248b8f92ce4SWarner Losh switch (pid = ::fork()) { 249b8f92ce4SWarner Losh case -1: /* error */ 250b8f92ce4SWarner Losh break; 251b8f92ce4SWarner Losh case 0: /* child */ 252b8f92ce4SWarner Losh /* 253b8f92ce4SWarner Losh * Restore original signal dispositions and exec the command. 254b8f92ce4SWarner Losh */ 255b8f92ce4SWarner Losh ::sigaction(SIGINT, &intact, NULL); 256b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &quitact, NULL); 257b8f92ce4SWarner Losh ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 258b8f92ce4SWarner Losh /* 259b8f92ce4SWarner Losh * Close the PID file, and all other open descriptors. 260b8f92ce4SWarner Losh * Inherit std{in,out,err} only. 261b8f92ce4SWarner Losh */ 262b8f92ce4SWarner Losh cfg.close_pidfile(); 263b8f92ce4SWarner Losh ::closefrom(3); 264b8f92ce4SWarner Losh ::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); 265b8f92ce4SWarner Losh ::_exit(127); 266b8f92ce4SWarner Losh default: /* parent */ 267b8f92ce4SWarner Losh savedpid = pid; 268b8f92ce4SWarner Losh do { 269b8f92ce4SWarner Losh pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0); 270b8f92ce4SWarner Losh } while (pid == -1 && errno == EINTR); 271b8f92ce4SWarner Losh break; 272b8f92ce4SWarner Losh } 273b8f92ce4SWarner Losh ::sigaction(SIGINT, &intact, NULL); 274b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &quitact, NULL); 275b8f92ce4SWarner Losh ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 276b8f92ce4SWarner Losh return (pid == -1 ? -1 : pstat); 277b8f92ce4SWarner Losh } 278b8f92ce4SWarner Losh 2793054f218SWarner Losh bool 2805102ef84SWarner Losh action::do_action(config &c) 2813054f218SWarner Losh { 2825dfc0f6cSIan Lepore string s = c.expand_string(_cmd.c_str()); 2831bb0777eSAndriy Gapon devdlog(LOG_INFO, "Executing '%s'\n", s.c_str()); 284b8f92ce4SWarner Losh my_system(s.c_str()); 2853054f218SWarner Losh return (true); 2863054f218SWarner Losh } 2873054f218SWarner Losh 2885dfc0f6cSIan Lepore match::match(config &c, const char *var, const char *re) : 2895dfc0f6cSIan Lepore _inv(re[0] == '!'), 2905dfc0f6cSIan Lepore _var(var), 2915dfc0f6cSIan Lepore _re(c.expand_string(_inv ? re + 1 : re, "^", "$")) 2923054f218SWarner Losh { 2939cb4a7bdSWarner Losh regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE); 2943054f218SWarner Losh } 2953054f218SWarner Losh 2963054f218SWarner Losh match::~match() 2973054f218SWarner Losh { 2985102ef84SWarner Losh regfree(&_regex); 2993054f218SWarner Losh } 3003054f218SWarner Losh 3013054f218SWarner Losh bool 3025102ef84SWarner Losh match::do_match(config &c) 3033054f218SWarner Losh { 3045bf08422SUlf Lilleengen const string &value = c.get_variable(_var); 3055102ef84SWarner Losh bool retval; 3065102ef84SWarner Losh 3076d1014a3SAlan Somers /* 3086d1014a3SAlan Somers * This function gets called WAY too often to justify calling syslog() 3096d1014a3SAlan Somers * each time, even at LOG_DEBUG. Because if syslogd isn't running, it 3106d1014a3SAlan Somers * can consume excessive amounts of systime inside of connect(). Only 3116d1014a3SAlan Somers * log when we're in -d mode. 3126d1014a3SAlan Somers */ 3136a2ae0ebSAlan Somers if (no_daemon) { 3146d1014a3SAlan Somers devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n", 3150321b694SHiroki Sato _var.c_str(), value.c_str(), _re.c_str(), _inv); 3166d1014a3SAlan Somers } 3178b823408SWarner Losh 3185102ef84SWarner Losh retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0); 3190321b694SHiroki Sato if (_inv == 1) 3200321b694SHiroki Sato retval = (retval == 0) ? 1 : 0; 3210321b694SHiroki Sato 3220285e9b1SAlan Somers return (retval); 3233054f218SWarner Losh } 3243054f218SWarner Losh 325cd70782bSWarner Losh #include <sys/sockio.h> 326cd70782bSWarner Losh #include <net/if.h> 327cd70782bSWarner Losh #include <net/if_media.h> 328cd70782bSWarner Losh 32994d2d4ebSBrooks Davis media::media(config &, const char *var, const char *type) 330cd70782bSWarner Losh : _var(var), _type(-1) 331cd70782bSWarner Losh { 332cd70782bSWarner Losh static struct ifmedia_description media_types[] = { 333cd70782bSWarner Losh { IFM_ETHER, "Ethernet" }, 334cd70782bSWarner Losh { IFM_TOKEN, "Tokenring" }, 335cd70782bSWarner Losh { IFM_FDDI, "FDDI" }, 336cd70782bSWarner Losh { IFM_IEEE80211, "802.11" }, 337cd70782bSWarner Losh { IFM_ATM, "ATM" }, 338cd70782bSWarner Losh { -1, "unknown" }, 339cd70782bSWarner Losh { 0, NULL }, 340cd70782bSWarner Losh }; 341edee691dSEitan Adler for (int i = 0; media_types[i].ifmt_string != NULL; ++i) 342cd70782bSWarner Losh if (strcasecmp(type, media_types[i].ifmt_string) == 0) { 343cd70782bSWarner Losh _type = media_types[i].ifmt_word; 344cd70782bSWarner Losh break; 345cd70782bSWarner Losh } 346cd70782bSWarner Losh } 347cd70782bSWarner Losh 348cd70782bSWarner Losh media::~media() 349cd70782bSWarner Losh { 350cd70782bSWarner Losh } 351cd70782bSWarner Losh 352cd70782bSWarner Losh bool 353cd70782bSWarner Losh media::do_match(config &c) 354cd70782bSWarner Losh { 35509c47e76SWarner Losh string value; 356cd70782bSWarner Losh struct ifmediareq ifmr; 357cd70782bSWarner Losh bool retval; 358cd70782bSWarner Losh int s; 359cd70782bSWarner Losh 36009c47e76SWarner Losh // Since we can be called from both a device attach/detach 36109c47e76SWarner Losh // context where device-name is defined and what we want, 36209c47e76SWarner Losh // as well as from a link status context, where subsystem is 36309c47e76SWarner Losh // the name of interest, first try device-name and fall back 36409c47e76SWarner Losh // to subsystem if none exists. 36509c47e76SWarner Losh value = c.get_variable("device-name"); 366a6393aa1SEitan Adler if (value.empty()) 367de02eec8SWarner Losh value = c.get_variable("subsystem"); 3686d1014a3SAlan Somers devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n", 369cd70782bSWarner Losh value.c_str(), _type); 370cd70782bSWarner Losh 371cd70782bSWarner Losh retval = false; 372cd70782bSWarner Losh 373cd70782bSWarner Losh s = socket(PF_INET, SOCK_DGRAM, 0); 374cd70782bSWarner Losh if (s >= 0) { 375cd70782bSWarner Losh memset(&ifmr, 0, sizeof(ifmr)); 376daa0d9ddSAlan Somers strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name)); 377cd70782bSWarner Losh 378cd70782bSWarner Losh if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 && 379cd70782bSWarner Losh ifmr.ifm_status & IFM_AVALID) { 3806d1014a3SAlan Somers devdlog(LOG_DEBUG, "%s has media type 0x%x\n", 381cd70782bSWarner Losh value.c_str(), IFM_TYPE(ifmr.ifm_active)); 382cd70782bSWarner Losh retval = (IFM_TYPE(ifmr.ifm_active) == _type); 383cd70782bSWarner Losh } else if (_type == -1) { 3846d1014a3SAlan Somers devdlog(LOG_DEBUG, "%s has unknown media type\n", 385cd70782bSWarner Losh value.c_str()); 386cd70782bSWarner Losh retval = true; 387cd70782bSWarner Losh } 388cd70782bSWarner Losh close(s); 389cd70782bSWarner Losh } 390cd70782bSWarner Losh 3910285e9b1SAlan Somers return (retval); 392cd70782bSWarner Losh } 393cd70782bSWarner Losh 3943054f218SWarner Losh const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_"; 3953054f218SWarner Losh const string var_list::nothing = ""; 3963054f218SWarner Losh 3973054f218SWarner Losh const string & 3983054f218SWarner Losh var_list::get_variable(const string &var) const 3993054f218SWarner Losh { 4003054f218SWarner Losh map<string, string>::const_iterator i; 4013054f218SWarner Losh 4023054f218SWarner Losh i = _vars.find(var); 4033054f218SWarner Losh if (i == _vars.end()) 4048b823408SWarner Losh return (var_list::bogus); 4053054f218SWarner Losh return (i->second); 4063054f218SWarner Losh } 4073054f218SWarner Losh 4083054f218SWarner Losh bool 4093054f218SWarner Losh var_list::is_set(const string &var) const 4103054f218SWarner Losh { 4113054f218SWarner Losh return (_vars.find(var) != _vars.end()); 4123054f218SWarner Losh } 4133054f218SWarner Losh 4143054f218SWarner Losh void 4153054f218SWarner Losh var_list::set_variable(const string &var, const string &val) 4163054f218SWarner Losh { 4176d1014a3SAlan Somers /* 4186d1014a3SAlan Somers * This function gets called WAY too often to justify calling syslog() 4196d1014a3SAlan Somers * each time, even at LOG_DEBUG. Because if syslogd isn't running, it 4206d1014a3SAlan Somers * can consume excessive amounts of systime inside of connect(). Only 4216d1014a3SAlan Somers * log when we're in -d mode. 4226d1014a3SAlan Somers */ 4236a2ae0ebSAlan Somers if (no_daemon) 4246d1014a3SAlan Somers devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str()); 4253054f218SWarner Losh _vars[var] = val; 4263054f218SWarner Losh } 4273054f218SWarner Losh 4283054f218SWarner Losh void 4293054f218SWarner Losh config::reset(void) 4303054f218SWarner Losh { 4313054f218SWarner Losh _dir_list.clear(); 4328b823408SWarner Losh delete_and_clear(_var_list_table); 4338b823408SWarner Losh delete_and_clear(_attach_list); 4348b823408SWarner Losh delete_and_clear(_detach_list); 4358b823408SWarner Losh delete_and_clear(_nomatch_list); 436842ccec5SWarner Losh delete_and_clear(_notify_list); 4373054f218SWarner Losh } 4383054f218SWarner Losh 4393054f218SWarner Losh void 4403054f218SWarner Losh config::parse_one_file(const char *fn) 4413054f218SWarner Losh { 4426d1014a3SAlan Somers devdlog(LOG_DEBUG, "Parsing %s\n", fn); 4433054f218SWarner Losh yyin = fopen(fn, "r"); 4443054f218SWarner Losh if (yyin == NULL) 4453054f218SWarner Losh err(1, "Cannot open config file %s", fn); 4464a3050fcSMaxim Konovalov lineno = 1; 4473054f218SWarner Losh if (yyparse() != 0) 4483054f218SWarner Losh errx(1, "Cannot parse %s at line %d", fn, lineno); 4493054f218SWarner Losh fclose(yyin); 4503054f218SWarner Losh } 4513054f218SWarner Losh 4523054f218SWarner Losh void 4533054f218SWarner Losh config::parse_files_in_dir(const char *dirname) 4543054f218SWarner Losh { 4553054f218SWarner Losh DIR *dirp; 4563054f218SWarner Losh struct dirent *dp; 4573054f218SWarner Losh char path[PATH_MAX]; 4583054f218SWarner Losh 4596d1014a3SAlan Somers devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname); 4603054f218SWarner Losh dirp = opendir(dirname); 4613054f218SWarner Losh if (dirp == NULL) 4623054f218SWarner Losh return; 4633054f218SWarner Losh readdir(dirp); /* Skip . */ 4643054f218SWarner Losh readdir(dirp); /* Skip .. */ 4653054f218SWarner Losh while ((dp = readdir(dirp)) != NULL) { 4663054f218SWarner Losh if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) { 4673054f218SWarner Losh snprintf(path, sizeof(path), "%s/%s", 4683054f218SWarner Losh dirname, dp->d_name); 4693054f218SWarner Losh parse_one_file(path); 4703054f218SWarner Losh } 4713054f218SWarner Losh } 4725983e891SKevin Lo closedir(dirp); 4733054f218SWarner Losh } 4743054f218SWarner Losh 4758b823408SWarner Losh class epv_greater { 4768b823408SWarner Losh public: 477ef370346SEitan Adler int operator()(event_proc *const&l1, event_proc *const&l2) const 4788b823408SWarner Losh { 4798b823408SWarner Losh return (l1->get_priority() > l2->get_priority()); 4808b823408SWarner Losh } 4818b823408SWarner Losh }; 4828b823408SWarner Losh 4838b823408SWarner Losh void 4848b823408SWarner Losh config::sort_vector(vector<event_proc *> &v) 4858b823408SWarner Losh { 4860c04d185SDimitry Andric stable_sort(v.begin(), v.end(), epv_greater()); 4878b823408SWarner Losh } 4888b823408SWarner Losh 4893054f218SWarner Losh void 4903054f218SWarner Losh config::parse(void) 4913054f218SWarner Losh { 4923054f218SWarner Losh vector<string>::const_iterator i; 4933054f218SWarner Losh 4948334958aSJoseph Koshy parse_one_file(configfile); 495edee691dSEitan Adler for (i = _dir_list.begin(); i != _dir_list.end(); ++i) 4963054f218SWarner Losh parse_files_in_dir((*i).c_str()); 4978b823408SWarner Losh sort_vector(_attach_list); 4988b823408SWarner Losh sort_vector(_detach_list); 4998b823408SWarner Losh sort_vector(_nomatch_list); 500842ccec5SWarner Losh sort_vector(_notify_list); 5013054f218SWarner Losh } 5023054f218SWarner Losh 5033054f218SWarner Losh void 5041a0cc6b1SPawel Jakub Dawidek config::open_pidfile() 5053054f218SWarner Losh { 5061a0cc6b1SPawel Jakub Dawidek pid_t otherpid; 5073054f218SWarner Losh 5085a3b1a3dSEitan Adler if (_pidfile.empty()) 5093054f218SWarner Losh return; 5101a0cc6b1SPawel Jakub Dawidek pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid); 5111a0cc6b1SPawel Jakub Dawidek if (pfh == NULL) { 5121a0cc6b1SPawel Jakub Dawidek if (errno == EEXIST) 5131a0cc6b1SPawel Jakub Dawidek errx(1, "devd already running, pid: %d", (int)otherpid); 5141a0cc6b1SPawel Jakub Dawidek warn("cannot open pid file"); 5151a0cc6b1SPawel Jakub Dawidek } 5161a0cc6b1SPawel Jakub Dawidek } 5171a0cc6b1SPawel Jakub Dawidek 5181a0cc6b1SPawel Jakub Dawidek void 5191a0cc6b1SPawel Jakub Dawidek config::write_pidfile() 5201a0cc6b1SPawel Jakub Dawidek { 5211a0cc6b1SPawel Jakub Dawidek 5221a0cc6b1SPawel Jakub Dawidek pidfile_write(pfh); 5231a0cc6b1SPawel Jakub Dawidek } 5241a0cc6b1SPawel Jakub Dawidek 5251a0cc6b1SPawel Jakub Dawidek void 526b8f92ce4SWarner Losh config::close_pidfile() 527b8f92ce4SWarner Losh { 528b8f92ce4SWarner Losh 529b8f92ce4SWarner Losh pidfile_close(pfh); 530b8f92ce4SWarner Losh } 531b8f92ce4SWarner Losh 532b8f92ce4SWarner Losh void 5331a0cc6b1SPawel Jakub Dawidek config::remove_pidfile() 5341a0cc6b1SPawel Jakub Dawidek { 5351a0cc6b1SPawel Jakub Dawidek 5361a0cc6b1SPawel Jakub Dawidek pidfile_remove(pfh); 5373054f218SWarner Losh } 5383054f218SWarner Losh 5393054f218SWarner Losh void 5403054f218SWarner Losh config::add_attach(int prio, event_proc *p) 5413054f218SWarner Losh { 5423054f218SWarner Losh p->set_priority(prio); 5433054f218SWarner Losh _attach_list.push_back(p); 5443054f218SWarner Losh } 5453054f218SWarner Losh 5463054f218SWarner Losh void 5473054f218SWarner Losh config::add_detach(int prio, event_proc *p) 5483054f218SWarner Losh { 5493054f218SWarner Losh p->set_priority(prio); 5503054f218SWarner Losh _detach_list.push_back(p); 5513054f218SWarner Losh } 5523054f218SWarner Losh 5533054f218SWarner Losh void 5543054f218SWarner Losh config::add_directory(const char *dir) 5553054f218SWarner Losh { 5563054f218SWarner Losh _dir_list.push_back(string(dir)); 5573054f218SWarner Losh } 5583054f218SWarner Losh 5593054f218SWarner Losh void 5603054f218SWarner Losh config::add_nomatch(int prio, event_proc *p) 5613054f218SWarner Losh { 5623054f218SWarner Losh p->set_priority(prio); 5633054f218SWarner Losh _nomatch_list.push_back(p); 5643054f218SWarner Losh } 5653054f218SWarner Losh 5663054f218SWarner Losh void 567842ccec5SWarner Losh config::add_notify(int prio, event_proc *p) 568842ccec5SWarner Losh { 569842ccec5SWarner Losh p->set_priority(prio); 570842ccec5SWarner Losh _notify_list.push_back(p); 571842ccec5SWarner Losh } 572842ccec5SWarner Losh 573842ccec5SWarner Losh void 5743054f218SWarner Losh config::set_pidfile(const char *fn) 5753054f218SWarner Losh { 5765a3b1a3dSEitan Adler _pidfile = fn; 5773054f218SWarner Losh } 5783054f218SWarner Losh 5793054f218SWarner Losh void 5803054f218SWarner Losh config::push_var_table() 5813054f218SWarner Losh { 5823054f218SWarner Losh var_list *vl; 5833054f218SWarner Losh 5843054f218SWarner Losh vl = new var_list(); 5853054f218SWarner Losh _var_list_table.push_back(vl); 5866d1014a3SAlan Somers devdlog(LOG_DEBUG, "Pushing table\n"); 5873054f218SWarner Losh } 5883054f218SWarner Losh 5893054f218SWarner Losh void 5903054f218SWarner Losh config::pop_var_table() 5913054f218SWarner Losh { 5923054f218SWarner Losh delete _var_list_table.back(); 5933054f218SWarner Losh _var_list_table.pop_back(); 5946d1014a3SAlan Somers devdlog(LOG_DEBUG, "Popping table\n"); 5953054f218SWarner Losh } 5963054f218SWarner Losh 5973054f218SWarner Losh void 5983054f218SWarner Losh config::set_variable(const char *var, const char *val) 5993054f218SWarner Losh { 6003054f218SWarner Losh _var_list_table.back()->set_variable(var, val); 6013054f218SWarner Losh } 6023054f218SWarner Losh 6033054f218SWarner Losh const string & 6043054f218SWarner Losh config::get_variable(const string &var) 6053054f218SWarner Losh { 6063054f218SWarner Losh vector<var_list *>::reverse_iterator i; 6073054f218SWarner Losh 608edee691dSEitan Adler for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) { 6093054f218SWarner Losh if ((*i)->is_set(var)) 6108b823408SWarner Losh return ((*i)->get_variable(var)); 6113054f218SWarner Losh } 6123054f218SWarner Losh return (var_list::nothing); 6133054f218SWarner Losh } 6143054f218SWarner Losh 6158b823408SWarner Losh bool 616ef370346SEitan Adler config::is_id_char(char ch) const 6178b823408SWarner Losh { 6188b823408SWarner Losh return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' || 6198b823408SWarner Losh ch == '-')); 6208b823408SWarner Losh } 6218b823408SWarner Losh 6225102ef84SWarner Losh void 6236cabd675SWarner Losh config::expand_one(const char *&src, string &dst) 6243054f218SWarner Losh { 6255102ef84SWarner Losh int count; 6265bf08422SUlf Lilleengen string buffer; 6275102ef84SWarner Losh 6288b823408SWarner Losh src++; 6295102ef84SWarner Losh // $$ -> $ 6305102ef84SWarner Losh if (*src == '$') { 6317d9e9c60SEitan Adler dst += *src++; 6325102ef84SWarner Losh return; 6335102ef84SWarner Losh } 6345102ef84SWarner Losh 6355102ef84SWarner Losh // $(foo) -> $(foo) 6368b823408SWarner Losh // Not sure if I want to support this or not, so for now we just pass 6378b823408SWarner Losh // it through. 6385102ef84SWarner Losh if (*src == '(') { 6397d9e9c60SEitan Adler dst += '$'; 6405102ef84SWarner Losh count = 1; 6416cabd675SWarner Losh /* If the string ends before ) is matched , return. */ 6426cabd675SWarner Losh while (count > 0 && *src) { 6435102ef84SWarner Losh if (*src == ')') 6445102ef84SWarner Losh count--; 6455102ef84SWarner Losh else if (*src == '(') 6465102ef84SWarner Losh count++; 6477d9e9c60SEitan Adler dst += *src++; 6485102ef84SWarner Losh } 6495102ef84SWarner Losh return; 6505102ef84SWarner Losh } 6515102ef84SWarner Losh 652b3d32292SWarner Losh // $[^-A-Za-z_*] -> $\1 653b3d32292SWarner Losh if (!isalpha(*src) && *src != '_' && *src != '-' && *src != '*') { 6547d9e9c60SEitan Adler dst += '$'; 6557d9e9c60SEitan Adler dst += *src++; 6565102ef84SWarner Losh return; 6575102ef84SWarner Losh } 6585102ef84SWarner Losh 6595102ef84SWarner Losh // $var -> replace with value 6606cabd675SWarner Losh do { 6617d9e9c60SEitan Adler buffer += *src++; 66266ff6821SWarner Losh } while (is_id_char(*src)); 6635a3b1a3dSEitan Adler dst.append(get_variable(buffer)); 6645102ef84SWarner Losh } 6655102ef84SWarner Losh 6665102ef84SWarner Losh const string 6675dfc0f6cSIan Lepore config::expand_string(const char *src, const char *prepend, const char *append) 6685102ef84SWarner Losh { 6695dfc0f6cSIan Lepore const char *var_at; 6706cabd675SWarner Losh string dst; 6715102ef84SWarner Losh 6725dfc0f6cSIan Lepore /* 6735dfc0f6cSIan Lepore * 128 bytes is enough for 2427 of 2438 expansions that happen 6745dfc0f6cSIan Lepore * while parsing config files, as tested on 2013-01-30. 6755dfc0f6cSIan Lepore */ 6765dfc0f6cSIan Lepore dst.reserve(128); 6775dfc0f6cSIan Lepore 6785dfc0f6cSIan Lepore if (prepend != NULL) 6795dfc0f6cSIan Lepore dst = prepend; 6805dfc0f6cSIan Lepore 6815dfc0f6cSIan Lepore for (;;) { 6825dfc0f6cSIan Lepore var_at = strchr(src, '$'); 6835dfc0f6cSIan Lepore if (var_at == NULL) { 6845dfc0f6cSIan Lepore dst.append(src); 6855dfc0f6cSIan Lepore break; 6865102ef84SWarner Losh } 6875dfc0f6cSIan Lepore dst.append(src, var_at - src); 6885dfc0f6cSIan Lepore src = var_at; 6895dfc0f6cSIan Lepore expand_one(src, dst); 6905dfc0f6cSIan Lepore } 6915dfc0f6cSIan Lepore 6925dfc0f6cSIan Lepore if (append != NULL) 6935dfc0f6cSIan Lepore dst.append(append); 6945102ef84SWarner Losh 6956cabd675SWarner Losh return (dst); 6963054f218SWarner Losh } 6973054f218SWarner Losh 6988b823408SWarner Losh bool 69911fd1366SEitan Adler config::chop_var(char *&buffer, char *&lhs, char *&rhs) const 7008b823408SWarner Losh { 7018b823408SWarner Losh char *walker; 7028b823408SWarner Losh 7038b823408SWarner Losh if (*buffer == '\0') 7048b823408SWarner Losh return (false); 7058b823408SWarner Losh walker = lhs = buffer; 7068b823408SWarner Losh while (is_id_char(*walker)) 7078b823408SWarner Losh walker++; 7088b823408SWarner Losh if (*walker != '=') 7098b823408SWarner Losh return (false); 7108b823408SWarner Losh walker++; // skip = 7118b823408SWarner Losh if (*walker == '"') { 7128b823408SWarner Losh walker++; // skip " 7138b823408SWarner Losh rhs = walker; 7148b823408SWarner Losh while (*walker && *walker != '"') 7158b823408SWarner Losh walker++; 7168b823408SWarner Losh if (*walker != '"') 7178b823408SWarner Losh return (false); 7188b823408SWarner Losh rhs[-2] = '\0'; 7198b823408SWarner Losh *walker++ = '\0'; 7208b823408SWarner Losh } else { 7218b823408SWarner Losh rhs = walker; 7228b823408SWarner Losh while (*walker && !isspace(*walker)) 7238b823408SWarner Losh walker++; 7248b823408SWarner Losh if (*walker != '\0') 7258b823408SWarner Losh *walker++ = '\0'; 7268b823408SWarner Losh rhs[-1] = '\0'; 7278b823408SWarner Losh } 728b1ee04b1SWarner Losh while (isspace(*walker)) 729b1ee04b1SWarner Losh walker++; 7308b823408SWarner Losh buffer = walker; 7318b823408SWarner Losh return (true); 7328b823408SWarner Losh } 7338b823408SWarner Losh 7348b823408SWarner Losh 7358b823408SWarner Losh char * 7368b823408SWarner Losh config::set_vars(char *buffer) 7378b823408SWarner Losh { 7388b823408SWarner Losh char *lhs; 7398b823408SWarner Losh char *rhs; 7408b823408SWarner Losh 7418b823408SWarner Losh while (1) { 7428b823408SWarner Losh if (!chop_var(buffer, lhs, rhs)) 7438b823408SWarner Losh break; 7448b823408SWarner Losh set_variable(lhs, rhs); 7458b823408SWarner Losh } 7468b823408SWarner Losh return (buffer); 7478b823408SWarner Losh } 7488b823408SWarner Losh 7498b823408SWarner Losh void 7508b823408SWarner Losh config::find_and_execute(char type) 7518b823408SWarner Losh { 7528b823408SWarner Losh vector<event_proc *> *l; 7538b823408SWarner Losh vector<event_proc *>::const_iterator i; 75494d2d4ebSBrooks Davis const char *s; 7558b823408SWarner Losh 7568b823408SWarner Losh switch (type) { 7578b823408SWarner Losh default: 7588b823408SWarner Losh return; 759842ccec5SWarner Losh case notify: 760842ccec5SWarner Losh l = &_notify_list; 761842ccec5SWarner Losh s = "notify"; 762842ccec5SWarner Losh break; 7638b823408SWarner Losh case nomatch: 7648b823408SWarner Losh l = &_nomatch_list; 7658b823408SWarner Losh s = "nomatch"; 7668b823408SWarner Losh break; 7678b823408SWarner Losh case attach: 7688b823408SWarner Losh l = &_attach_list; 7698b823408SWarner Losh s = "attach"; 7708b823408SWarner Losh break; 7718b823408SWarner Losh case detach: 7728b823408SWarner Losh l = &_detach_list; 7738b823408SWarner Losh s = "detach"; 7748b823408SWarner Losh break; 7758b823408SWarner Losh } 7766d1014a3SAlan Somers devdlog(LOG_DEBUG, "Processing %s event\n", s); 777edee691dSEitan Adler for (i = l->begin(); i != l->end(); ++i) { 7788b823408SWarner Losh if ((*i)->matches(*this)) { 7798b823408SWarner Losh (*i)->run(*this); 7808b823408SWarner Losh break; 7818b823408SWarner Losh } 7828b823408SWarner Losh } 7838b823408SWarner Losh 7848b823408SWarner Losh } 7858b823408SWarner Losh 7860285e9b1SAlan Somers 7873054f218SWarner Losh static void 7888b823408SWarner Losh process_event(char *buffer) 7893054f218SWarner Losh { 7903054f218SWarner Losh char type; 7913054f218SWarner Losh char *sp; 7923b336ac3SWarner Losh struct timeval tv; 7933b336ac3SWarner Losh char *timestr; 7943054f218SWarner Losh 7958b823408SWarner Losh sp = buffer + 1; 796f0038a8eSAlan Somers devdlog(LOG_INFO, "Processing event '%s'\n", buffer); 7973054f218SWarner Losh type = *buffer++; 7988b823408SWarner Losh cfg.push_var_table(); 799b3d32292SWarner Losh // $* is the entire line 800b3d32292SWarner Losh cfg.set_variable("*", buffer - 1); 801b3d32292SWarner Losh // $_ is the entire line without the initial character 802535595dbSWarner Losh cfg.set_variable("_", buffer); 8033b336ac3SWarner Losh 8043b336ac3SWarner Losh // Save the time this happened (as approximated by when we got 8053b336ac3SWarner Losh // around to processing it). 8063b336ac3SWarner Losh gettimeofday(&tv, NULL); 8073b336ac3SWarner Losh asprintf(×tr, "%jd.%06ld", (uintmax_t)tv.tv_sec, tv.tv_usec); 8083b336ac3SWarner Losh cfg.set_variable("timestamp", timestr); 8093b336ac3SWarner Losh free(timestr); 8103b336ac3SWarner Losh 8113b336ac3SWarner Losh // Match doesn't have a device, and the format is a little 8128b823408SWarner Losh // different, so handle it separately. 813842ccec5SWarner Losh switch (type) { 814842ccec5SWarner Losh case notify: 815b3d32292SWarner Losh //! (k=v)* 816842ccec5SWarner Losh sp = cfg.set_vars(sp); 817842ccec5SWarner Losh break; 818842ccec5SWarner Losh case nomatch: 819528d3f79SWarner Losh //? at location pnp-info on bus 820528d3f79SWarner Losh sp = strchr(sp, ' '); 821528d3f79SWarner Losh if (sp == NULL) 822528d3f79SWarner Losh return; /* Can't happen? */ 823528d3f79SWarner Losh *sp++ = '\0'; 8249b6cb520SWarner Losh while (isspace(*sp)) 8259b6cb520SWarner Losh sp++; 826842ccec5SWarner Losh if (strncmp(sp, "at ", 3) == 0) 827842ccec5SWarner Losh sp += 3; 828842ccec5SWarner Losh sp = cfg.set_vars(sp); 8299b6cb520SWarner Losh while (isspace(*sp)) 8309b6cb520SWarner Losh sp++; 831842ccec5SWarner Losh if (strncmp(sp, "on ", 3) == 0) 832842ccec5SWarner Losh cfg.set_variable("bus", sp + 3); 833842ccec5SWarner Losh break; 834842ccec5SWarner Losh case attach: /*FALLTHROUGH*/ 835842ccec5SWarner Losh case detach: 8368b823408SWarner Losh sp = strchr(sp, ' '); 8373054f218SWarner Losh if (sp == NULL) 8383054f218SWarner Losh return; /* Can't happen? */ 8398b823408SWarner Losh *sp++ = '\0'; 8408b823408SWarner Losh cfg.set_variable("device-name", buffer); 8419b6cb520SWarner Losh while (isspace(*sp)) 8429b6cb520SWarner Losh sp++; 8438b823408SWarner Losh if (strncmp(sp, "at ", 3) == 0) 8448b823408SWarner Losh sp += 3; 8458b823408SWarner Losh sp = cfg.set_vars(sp); 8469b6cb520SWarner Losh while (isspace(*sp)) 8479b6cb520SWarner Losh sp++; 8488b823408SWarner Losh if (strncmp(sp, "on ", 3) == 0) 8498b823408SWarner Losh cfg.set_variable("bus", sp + 3); 850842ccec5SWarner Losh break; 851b1ee04b1SWarner Losh } 852b1ee04b1SWarner Losh 8538b823408SWarner Losh cfg.find_and_execute(type); 8548b823408SWarner Losh cfg.pop_var_table(); 8553054f218SWarner Losh } 8563054f218SWarner Losh 857251bedd1SWarner Losh int 8589102691eSAlan Somers create_socket(const char *name, int socktype) 859251bedd1SWarner Losh { 860251bedd1SWarner Losh int fd, slen; 861251bedd1SWarner Losh struct sockaddr_un sun; 862251bedd1SWarner Losh 8639102691eSAlan Somers if ((fd = socket(PF_LOCAL, socktype, 0)) < 0) 864251bedd1SWarner Losh err(1, "socket"); 865251bedd1SWarner Losh bzero(&sun, sizeof(sun)); 866251bedd1SWarner Losh sun.sun_family = AF_UNIX; 867251bedd1SWarner Losh strlcpy(sun.sun_path, name, sizeof(sun.sun_path)); 868251bedd1SWarner Losh slen = SUN_LEN(&sun); 869251bedd1SWarner Losh unlink(name); 870e60fa014SJoe Marcus Clarke if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) 871e60fa014SJoe Marcus Clarke err(1, "fcntl"); 8725d586d29SDimitry Andric if (::bind(fd, (struct sockaddr *) & sun, slen) < 0) 873251bedd1SWarner Losh err(1, "bind"); 874251bedd1SWarner Losh listen(fd, 4); 875daa0d9ddSAlan Somers if (chown(name, 0, 0)) /* XXX - root.wheel */ 876daa0d9ddSAlan Somers err(1, "chown"); 877daa0d9ddSAlan Somers if (chmod(name, 0666)) 878daa0d9ddSAlan Somers err(1, "chmod"); 879251bedd1SWarner Losh return (fd); 880251bedd1SWarner Losh } 881251bedd1SWarner Losh 882463a577bSEitan Adler unsigned int max_clients = 10; /* Default, can be overridden on cmdline. */ 883e1334f93SIan Lepore unsigned int num_clients; 8849102691eSAlan Somers 8859102691eSAlan Somers list<client_t> clients; 886251bedd1SWarner Losh 887251bedd1SWarner Losh void 888251bedd1SWarner Losh notify_clients(const char *data, int len) 889251bedd1SWarner Losh { 8909102691eSAlan Somers list<client_t>::iterator i; 891251bedd1SWarner Losh 892e1334f93SIan Lepore /* 893e1334f93SIan Lepore * Deliver the data to all clients. Throw clients overboard at the 894e1334f93SIan Lepore * first sign of trouble. This reaps clients who've died or closed 895e1334f93SIan Lepore * their sockets, and also clients who are alive but failing to keep up 896e1334f93SIan Lepore * (or who are maliciously not reading, to consume buffer space in 897e1334f93SIan Lepore * kernel memory or tie up the limited number of available connections). 898e1334f93SIan Lepore */ 899e1334f93SIan Lepore for (i = clients.begin(); i != clients.end(); ) { 9009102691eSAlan Somers int flags; 9019102691eSAlan Somers if (i->socktype == SOCK_SEQPACKET) 9029102691eSAlan Somers flags = MSG_EOR; 9039102691eSAlan Somers else 9049102691eSAlan Somers flags = 0; 9059102691eSAlan Somers 9069102691eSAlan Somers if (send(i->fd, data, len, flags) != len) { 907e1334f93SIan Lepore --num_clients; 9089102691eSAlan Somers close(i->fd); 909e1334f93SIan Lepore i = clients.erase(i); 9109102691eSAlan Somers devdlog(LOG_WARNING, "notify_clients: send() failed; " 9116d1014a3SAlan Somers "dropping unresponsive client\n"); 912e1334f93SIan Lepore } else 913e1334f93SIan Lepore ++i; 914251bedd1SWarner Losh } 915251bedd1SWarner Losh } 916251bedd1SWarner Losh 917e1334f93SIan Lepore void 918e1334f93SIan Lepore check_clients(void) 919e1334f93SIan Lepore { 920e1334f93SIan Lepore int s; 921e1334f93SIan Lepore struct pollfd pfd; 9229102691eSAlan Somers list<client_t>::iterator i; 923e1334f93SIan Lepore 924e1334f93SIan Lepore /* 925e1334f93SIan Lepore * Check all existing clients to see if any of them have disappeared. 926e1334f93SIan Lepore * Normally we reap clients when we get an error trying to send them an 927e1334f93SIan Lepore * event. This check eliminates the problem of an ever-growing list of 928e1334f93SIan Lepore * zombie clients because we're never writing to them on a system 929e1334f93SIan Lepore * without frequent device-change activity. 930e1334f93SIan Lepore */ 931e1334f93SIan Lepore pfd.events = 0; 932e1334f93SIan Lepore for (i = clients.begin(); i != clients.end(); ) { 9339102691eSAlan Somers pfd.fd = i->fd; 934e1334f93SIan Lepore s = poll(&pfd, 1, 0); 935e1334f93SIan Lepore if ((s < 0 && s != EINTR ) || 936e1334f93SIan Lepore (s > 0 && (pfd.revents & POLLHUP))) { 937e1334f93SIan Lepore --num_clients; 9389102691eSAlan Somers close(i->fd); 939e1334f93SIan Lepore i = clients.erase(i); 9406d1014a3SAlan Somers devdlog(LOG_NOTICE, "check_clients: " 9416d1014a3SAlan Somers "dropping disconnected client\n"); 942e1334f93SIan Lepore } else 943e1334f93SIan Lepore ++i; 944e1334f93SIan Lepore } 945251bedd1SWarner Losh } 946251bedd1SWarner Losh 947251bedd1SWarner Losh void 9489102691eSAlan Somers new_client(int fd, int socktype) 949251bedd1SWarner Losh { 9509102691eSAlan Somers client_t s; 951b026eddfSAlan Somers int sndbuf_size; 952251bedd1SWarner Losh 953e1334f93SIan Lepore /* 954e1334f93SIan Lepore * First go reap any zombie clients, then accept the connection, and 955e1334f93SIan Lepore * shut down the read side to stop clients from consuming kernel memory 956e1334f93SIan Lepore * by sending large buffers full of data we'll never read. 957e1334f93SIan Lepore */ 958e1334f93SIan Lepore check_clients(); 9599102691eSAlan Somers s.socktype = socktype; 9609102691eSAlan Somers s.fd = accept(fd, NULL, NULL); 9619102691eSAlan Somers if (s.fd != -1) { 962b026eddfSAlan Somers sndbuf_size = CLIENT_BUFSIZE; 9639102691eSAlan Somers if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, 964b026eddfSAlan Somers sizeof(sndbuf_size))) 965b026eddfSAlan Somers err(1, "setsockopt"); 9669102691eSAlan Somers shutdown(s.fd, SHUT_RD); 967251bedd1SWarner Losh clients.push_back(s); 968e1334f93SIan Lepore ++num_clients; 969b026eddfSAlan Somers } else 970b026eddfSAlan Somers err(1, "accept"); 971251bedd1SWarner Losh } 972251bedd1SWarner Losh 9733054f218SWarner Losh static void 9743054f218SWarner Losh event_loop(void) 9753054f218SWarner Losh { 9763054f218SWarner Losh int rv; 9773054f218SWarner Losh int fd; 9783054f218SWarner Losh char buffer[DEVCTL_MAXBUF]; 9795a882d77SWarner Losh int once = 0; 9809102691eSAlan Somers int stream_fd, seqpacket_fd, max_fd; 981e1334f93SIan Lepore int accepting; 9825a882d77SWarner Losh timeval tv; 9835a882d77SWarner Losh fd_set fds; 9843054f218SWarner Losh 985901610f4SPawel Jakub Dawidek fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC); 9863054f218SWarner Losh if (fd == -1) 987251bedd1SWarner Losh err(1, "Can't open devctl device %s", PATH_DEVCTL); 9889102691eSAlan Somers stream_fd = create_socket(STREAMPIPE, SOCK_STREAM); 9899102691eSAlan Somers seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET); 990e1334f93SIan Lepore accepting = 1; 9919102691eSAlan Somers max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1; 99267ae3bf1SEitan Adler while (!romeo_must_die) { 9936a2ae0ebSAlan Somers if (!once && !no_daemon && !daemonize_quick) { 9945a882d77SWarner Losh // Check to see if we have any events pending. 9955a882d77SWarner Losh tv.tv_sec = 0; 9965a882d77SWarner Losh tv.tv_usec = 0; 9975a882d77SWarner Losh FD_ZERO(&fds); 9985a882d77SWarner Losh FD_SET(fd, &fds); 9995a882d77SWarner Losh rv = select(fd + 1, &fds, &fds, &fds, &tv); 10005a882d77SWarner Losh // No events -> we've processed all pending events 1001f3059505SWarner Losh if (rv == 0) { 10026d1014a3SAlan Somers devdlog(LOG_DEBUG, "Calling daemon\n"); 10031a0cc6b1SPawel Jakub Dawidek cfg.remove_pidfile(); 10041a0cc6b1SPawel Jakub Dawidek cfg.open_pidfile(); 10055a882d77SWarner Losh daemon(0, 0); 10061a0cc6b1SPawel Jakub Dawidek cfg.write_pidfile(); 10075a882d77SWarner Losh once++; 10085a882d77SWarner Losh } 10095a882d77SWarner Losh } 1010e1334f93SIan Lepore /* 1011e1334f93SIan Lepore * When we've already got the max number of clients, stop 10129102691eSAlan Somers * accepting new connections (don't put the listening sockets in 10139102691eSAlan Somers * the set), shrink the accept() queue to reject connections 10149102691eSAlan Somers * quickly, and poll the existing clients more often, so that we 10159102691eSAlan Somers * notice more quickly when any of them disappear to free up 10169102691eSAlan Somers * client slots. 1017e1334f93SIan Lepore */ 1018251bedd1SWarner Losh FD_ZERO(&fds); 1019251bedd1SWarner Losh FD_SET(fd, &fds); 1020e1334f93SIan Lepore if (num_clients < max_clients) { 1021e1334f93SIan Lepore if (!accepting) { 10229102691eSAlan Somers listen(stream_fd, max_clients); 10239102691eSAlan Somers listen(seqpacket_fd, max_clients); 1024e1334f93SIan Lepore accepting = 1; 1025e1334f93SIan Lepore } 10269102691eSAlan Somers FD_SET(stream_fd, &fds); 10279102691eSAlan Somers FD_SET(seqpacket_fd, &fds); 1028e1334f93SIan Lepore tv.tv_sec = 60; 1029e1334f93SIan Lepore tv.tv_usec = 0; 1030e1334f93SIan Lepore } else { 1031e1334f93SIan Lepore if (accepting) { 10329102691eSAlan Somers listen(stream_fd, 0); 10339102691eSAlan Somers listen(seqpacket_fd, 0); 1034e1334f93SIan Lepore accepting = 0; 1035e1334f93SIan Lepore } 1036e1334f93SIan Lepore tv.tv_sec = 2; 1037e1334f93SIan Lepore tv.tv_usec = 0; 1038e1334f93SIan Lepore } 1039e1334f93SIan Lepore rv = select(max_fd, &fds, NULL, NULL, &tv); 1040be685e01SAlan Somers if (got_siginfo) { 1041f0038a8eSAlan Somers devdlog(LOG_NOTICE, "Events received so far=%u\n", 1042be685e01SAlan Somers total_events); 1043be685e01SAlan Somers got_siginfo = 0; 1044be685e01SAlan Somers } 1045251bedd1SWarner Losh if (rv == -1) { 1046251bedd1SWarner Losh if (errno == EINTR) 1047251bedd1SWarner Losh continue; 1048251bedd1SWarner Losh err(1, "select"); 1049e1334f93SIan Lepore } else if (rv == 0) 1050e1334f93SIan Lepore check_clients(); 1051251bedd1SWarner Losh if (FD_ISSET(fd, &fds)) { 10523054f218SWarner Losh rv = read(fd, buffer, sizeof(buffer) - 1); 10533054f218SWarner Losh if (rv > 0) { 1054be685e01SAlan Somers total_events++; 10556d1014a3SAlan Somers if (rv == sizeof(buffer) - 1) { 10566d1014a3SAlan Somers devdlog(LOG_WARNING, "Warning: " 10576d1014a3SAlan Somers "available event data exceeded " 10586d1014a3SAlan Somers "buffer space\n"); 10596d1014a3SAlan Somers } 1060251bedd1SWarner Losh notify_clients(buffer, rv); 10613054f218SWarner Losh buffer[rv] = '\0'; 10623054f218SWarner Losh while (buffer[--rv] == '\n') 10633054f218SWarner Losh buffer[rv] = '\0'; 1064daa0d9ddSAlan Somers try { 10653054f218SWarner Losh process_event(buffer); 1066daa0d9ddSAlan Somers } 1067daa0d9ddSAlan Somers catch (std::length_error e) { 1068daa0d9ddSAlan Somers devdlog(LOG_ERR, "Dropping event %s " 1069daa0d9ddSAlan Somers "due to low memory", buffer); 1070daa0d9ddSAlan Somers } 10713054f218SWarner Losh } else if (rv < 0) { 10723054f218SWarner Losh if (errno != EINTR) 10733054f218SWarner Losh break; 10743054f218SWarner Losh } else { 10753054f218SWarner Losh /* EOF */ 10763054f218SWarner Losh break; 10773054f218SWarner Losh } 10783054f218SWarner Losh } 10799102691eSAlan Somers if (FD_ISSET(stream_fd, &fds)) 10809102691eSAlan Somers new_client(stream_fd, SOCK_STREAM); 10819102691eSAlan Somers /* 10829102691eSAlan Somers * Aside from the socket type, both sockets use the same 10839102691eSAlan Somers * protocol, so we can process clients the same way. 10849102691eSAlan Somers */ 10859102691eSAlan Somers if (FD_ISSET(seqpacket_fd, &fds)) 10869102691eSAlan Somers new_client(seqpacket_fd, SOCK_SEQPACKET); 1087251bedd1SWarner Losh } 1088daa0d9ddSAlan Somers close(seqpacket_fd); 1089daa0d9ddSAlan Somers close(stream_fd); 10903054f218SWarner Losh close(fd); 10913054f218SWarner Losh } 10920285e9b1SAlan Somers 10933054f218SWarner Losh /* 10943054f218SWarner Losh * functions that the parser uses. 10953054f218SWarner Losh */ 10963054f218SWarner Losh void 10973054f218SWarner Losh add_attach(int prio, event_proc *p) 10983054f218SWarner Losh { 10993054f218SWarner Losh cfg.add_attach(prio, p); 11003054f218SWarner Losh } 11013054f218SWarner Losh 11023054f218SWarner Losh void 11033054f218SWarner Losh add_detach(int prio, event_proc *p) 11043054f218SWarner Losh { 11053054f218SWarner Losh cfg.add_detach(prio, p); 11063054f218SWarner Losh } 11073054f218SWarner Losh 11083054f218SWarner Losh void 11093054f218SWarner Losh add_directory(const char *dir) 11103054f218SWarner Losh { 11113054f218SWarner Losh cfg.add_directory(dir); 11123054f218SWarner Losh free(const_cast<char *>(dir)); 11133054f218SWarner Losh } 11143054f218SWarner Losh 11153054f218SWarner Losh void 11163054f218SWarner Losh add_nomatch(int prio, event_proc *p) 11173054f218SWarner Losh { 11183054f218SWarner Losh cfg.add_nomatch(prio, p); 11193054f218SWarner Losh } 11203054f218SWarner Losh 1121842ccec5SWarner Losh void 1122842ccec5SWarner Losh add_notify(int prio, event_proc *p) 1123842ccec5SWarner Losh { 1124842ccec5SWarner Losh cfg.add_notify(prio, p); 1125842ccec5SWarner Losh } 1126842ccec5SWarner Losh 11273054f218SWarner Losh event_proc * 11283054f218SWarner Losh add_to_event_proc(event_proc *ep, eps *eps) 11293054f218SWarner Losh { 11303054f218SWarner Losh if (ep == NULL) 11313054f218SWarner Losh ep = new event_proc(); 11323054f218SWarner Losh ep->add(eps); 11333054f218SWarner Losh return (ep); 11343054f218SWarner Losh } 11353054f218SWarner Losh 11363054f218SWarner Losh eps * 11373054f218SWarner Losh new_action(const char *cmd) 11383054f218SWarner Losh { 11393054f218SWarner Losh eps *e = new action(cmd); 11403054f218SWarner Losh free(const_cast<char *>(cmd)); 11413054f218SWarner Losh return (e); 11423054f218SWarner Losh } 11433054f218SWarner Losh 11443054f218SWarner Losh eps * 11453054f218SWarner Losh new_match(const char *var, const char *re) 11463054f218SWarner Losh { 11475102ef84SWarner Losh eps *e = new match(cfg, var, re); 11483054f218SWarner Losh free(const_cast<char *>(var)); 11493054f218SWarner Losh free(const_cast<char *>(re)); 11503054f218SWarner Losh return (e); 11513054f218SWarner Losh } 11523054f218SWarner Losh 1153cd70782bSWarner Losh eps * 1154cd70782bSWarner Losh new_media(const char *var, const char *re) 1155cd70782bSWarner Losh { 1156cd70782bSWarner Losh eps *e = new media(cfg, var, re); 1157cd70782bSWarner Losh free(const_cast<char *>(var)); 1158cd70782bSWarner Losh free(const_cast<char *>(re)); 1159cd70782bSWarner Losh return (e); 1160cd70782bSWarner Losh } 1161cd70782bSWarner Losh 11623054f218SWarner Losh void 11633054f218SWarner Losh set_pidfile(const char *name) 11643054f218SWarner Losh { 11653054f218SWarner Losh cfg.set_pidfile(name); 11663054f218SWarner Losh free(const_cast<char *>(name)); 11673054f218SWarner Losh } 11683054f218SWarner Losh 11693054f218SWarner Losh void 11703054f218SWarner Losh set_variable(const char *var, const char *val) 11713054f218SWarner Losh { 11723054f218SWarner Losh cfg.set_variable(var, val); 11733054f218SWarner Losh free(const_cast<char *>(var)); 11743054f218SWarner Losh free(const_cast<char *>(val)); 11753054f218SWarner Losh } 11763054f218SWarner Losh 11770285e9b1SAlan Somers 11783054f218SWarner Losh 11793054f218SWarner Losh static void 11803054f218SWarner Losh gensighand(int) 11813054f218SWarner Losh { 1182fcdcaa88SEitan Adler romeo_must_die = 1; 11833054f218SWarner Losh } 11843054f218SWarner Losh 11856d1014a3SAlan Somers /* 1186be685e01SAlan Somers * SIGINFO handler. Will print useful statistics to the syslog or stderr 1187be685e01SAlan Somers * as appropriate 1188be685e01SAlan Somers */ 1189be685e01SAlan Somers static void 1190be685e01SAlan Somers siginfohand(int) 1191be685e01SAlan Somers { 1192be685e01SAlan Somers got_siginfo = 1; 1193be685e01SAlan Somers } 1194be685e01SAlan Somers 1195be685e01SAlan Somers /* 11962e024bc2SAndriy Gapon * Local logging function. Prints to syslog if we're daemonized; stderr 11976d1014a3SAlan Somers * otherwise. 11986d1014a3SAlan Somers */ 11996d1014a3SAlan Somers static void 12006d1014a3SAlan Somers devdlog(int priority, const char* fmt, ...) 12016d1014a3SAlan Somers { 12026d1014a3SAlan Somers va_list argp; 12036d1014a3SAlan Somers 12046d1014a3SAlan Somers va_start(argp, fmt); 12056a2ae0ebSAlan Somers if (no_daemon) 12066d1014a3SAlan Somers vfprintf(stderr, fmt, argp); 12074d00d13cSWarner Losh else if (quiet_mode == 0 || priority <= LOG_WARNING) 12086d1014a3SAlan Somers vsyslog(priority, fmt, argp); 12096d1014a3SAlan Somers va_end(argp); 12106d1014a3SAlan Somers } 12116d1014a3SAlan Somers 12123054f218SWarner Losh static void 12133054f218SWarner Losh usage() 12143054f218SWarner Losh { 12156a2ae0ebSAlan Somers fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n", 1216e1334f93SIan Lepore getprogname()); 12173054f218SWarner Losh exit(1); 12183054f218SWarner Losh } 12193054f218SWarner Losh 12209c180898SWarner Losh static void 12219c180898SWarner Losh check_devd_enabled() 12229c180898SWarner Losh { 12239c180898SWarner Losh int val = 0; 12249c180898SWarner Losh size_t len; 12259c180898SWarner Losh 12269c180898SWarner Losh len = sizeof(val); 122779f92e25SWarner Losh if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0) 12289c180898SWarner Losh errx(1, "devctl sysctl missing from kernel!"); 1229ee38f2e0SMateusz Guzik if (val == 0) { 1230ee38f2e0SMateusz Guzik warnx("Setting " SYSCTL " to 1000"); 1231ee38f2e0SMateusz Guzik val = 1000; 1232daa0d9ddSAlan Somers if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val))) 1233daa0d9ddSAlan Somers err(1, "sysctlbyname"); 12349c180898SWarner Losh } 12359c180898SWarner Losh } 12369c180898SWarner Losh 12373054f218SWarner Losh /* 12383054f218SWarner Losh * main 12393054f218SWarner Losh */ 12403054f218SWarner Losh int 12413054f218SWarner Losh main(int argc, char **argv) 12423054f218SWarner Losh { 12433054f218SWarner Losh int ch; 12443054f218SWarner Losh 12459c180898SWarner Losh check_devd_enabled(); 12466a2ae0ebSAlan Somers while ((ch = getopt(argc, argv, "df:l:nq")) != -1) { 12473054f218SWarner Losh switch (ch) { 12483054f218SWarner Losh case 'd': 12496a2ae0ebSAlan Somers no_daemon = 1; 12503054f218SWarner Losh break; 12518334958aSJoseph Koshy case 'f': 12528334958aSJoseph Koshy configfile = optarg; 12538334958aSJoseph Koshy break; 1254e1334f93SIan Lepore case 'l': 1255e1334f93SIan Lepore max_clients = MAX(1, strtoul(optarg, NULL, 0)); 1256e1334f93SIan Lepore break; 12575a882d77SWarner Losh case 'n': 12586a2ae0ebSAlan Somers daemonize_quick = 1; 12596a2ae0ebSAlan Somers break; 12606a2ae0ebSAlan Somers case 'q': 12616a2ae0ebSAlan Somers quiet_mode = 1; 12625a882d77SWarner Losh break; 12633054f218SWarner Losh default: 12643054f218SWarner Losh usage(); 12653054f218SWarner Losh } 12663054f218SWarner Losh } 12673054f218SWarner Losh 12683054f218SWarner Losh cfg.parse(); 12696a2ae0ebSAlan Somers if (!no_daemon && daemonize_quick) { 12701a0cc6b1SPawel Jakub Dawidek cfg.open_pidfile(); 12713054f218SWarner Losh daemon(0, 0); 12721a0cc6b1SPawel Jakub Dawidek cfg.write_pidfile(); 1273fd6a8f23SWarner Losh } 1274f32a7686SWarner Losh signal(SIGPIPE, SIG_IGN); 12753054f218SWarner Losh signal(SIGHUP, gensighand); 12763054f218SWarner Losh signal(SIGINT, gensighand); 12773054f218SWarner Losh signal(SIGTERM, gensighand); 1278be685e01SAlan Somers signal(SIGINFO, siginfohand); 12793054f218SWarner Losh event_loop(); 12803054f218SWarner Losh return (0); 12813054f218SWarner Losh } 1282