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> 983054f218SWarner Losh #include <vector> 993054f218SWarner Losh 1006aeeca8eSWarner Losh #include "devd.h" /* C compatible definitions */ 1016aeeca8eSWarner Losh #include "devd.hh" /* C++ class definitions */ 1023054f218SWarner Losh 103251bedd1SWarner Losh #define PIPE "/var/run/devd.pipe" 1043054f218SWarner Losh #define CF "/etc/devd.conf" 105*ee38f2e0SMateusz 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 110b026eddfSAlan Somers * buffer, so the client can't increate the buffersize by itself. 111b026eddfSAlan Somers * 112b026eddfSAlan Somers * For example, when creating a ZFS pool, devd emits one 165 character 113b026eddfSAlan Somers * resource.fs.zfs.statechange message for each vdev in the pool. A 64k 114b026eddfSAlan Somers * buffer has enough space for almost 400 drives, which would be very large but 115b026eddfSAlan Somers * not impossibly large pool. A 128k buffer has enough space for 794 drives, 116b026eddfSAlan Somers * which is more than can fit in a rack with modern technology. 117b026eddfSAlan Somers */ 118b026eddfSAlan Somers #define CLIENT_BUFSIZE 131072 119b026eddfSAlan Somers 1203054f218SWarner Losh using namespace std; 1213054f218SWarner Losh 1223054f218SWarner Losh extern FILE *yyin; 1233054f218SWarner Losh extern int lineno; 1243054f218SWarner Losh 125842ccec5SWarner Losh static const char notify = '!'; 1268b823408SWarner Losh static const char nomatch = '?'; 1278b823408SWarner Losh static const char attach = '+'; 1288b823408SWarner Losh static const char detach = '-'; 1298b823408SWarner Losh 1301a0cc6b1SPawel Jakub Dawidek static struct pidfh *pfh; 1311a0cc6b1SPawel Jakub Dawidek 1326a2ae0ebSAlan Somers static int no_daemon = 0; 1336a2ae0ebSAlan Somers static int daemonize_quick = 0; 1346a2ae0ebSAlan Somers static int quiet_mode = 0; 135be685e01SAlan Somers static unsigned total_events = 0; 136be685e01SAlan Somers static volatile sig_atomic_t got_siginfo = 0; 137fcdcaa88SEitan Adler static volatile sig_atomic_t romeo_must_die = 0; 1383054f218SWarner Losh 1398334958aSJoseph Koshy static const char *configfile = CF; 1408334958aSJoseph Koshy 1413449b15aSAlan Somers static void devdlog(int priority, const char* message, ...) 1423449b15aSAlan Somers __printflike(2, 3); 1433054f218SWarner Losh static void event_loop(void); 1443054f218SWarner Losh static void usage(void); 1453054f218SWarner Losh 1468b823408SWarner Losh template <class T> void 1478b823408SWarner Losh delete_and_clear(vector<T *> &v) 1488b823408SWarner Losh { 1498b823408SWarner Losh typename vector<T *>::const_iterator i; 1503054f218SWarner Losh 151edee691dSEitan Adler for (i = v.begin(); i != v.end(); ++i) 1528b823408SWarner Losh delete *i; 1538b823408SWarner Losh v.clear(); 1548b823408SWarner Losh } 1558b823408SWarner Losh 1563054f218SWarner Losh config cfg; 1573054f218SWarner Losh 1583054f218SWarner Losh event_proc::event_proc() : _prio(-1) 1593054f218SWarner Losh { 1605dfc0f6cSIan Lepore _epsvec.reserve(4); 1613054f218SWarner Losh } 1623054f218SWarner Losh 1633054f218SWarner Losh event_proc::~event_proc() 1643054f218SWarner Losh { 16513f8bb71SAlexander Nedotsukov delete_and_clear(_epsvec); 1663054f218SWarner Losh } 1673054f218SWarner Losh 1683054f218SWarner Losh void 1693054f218SWarner Losh event_proc::add(eps *eps) 1703054f218SWarner Losh { 1713054f218SWarner Losh _epsvec.push_back(eps); 1723054f218SWarner Losh } 1733054f218SWarner Losh 1743054f218SWarner Losh bool 175ef370346SEitan Adler event_proc::matches(config &c) const 1763054f218SWarner Losh { 1773054f218SWarner Losh vector<eps *>::const_iterator i; 1783054f218SWarner Losh 179edee691dSEitan Adler for (i = _epsvec.begin(); i != _epsvec.end(); ++i) 1803054f218SWarner Losh if (!(*i)->do_match(c)) 1813054f218SWarner Losh return (false); 1823054f218SWarner Losh return (true); 1833054f218SWarner Losh } 1843054f218SWarner Losh 1853054f218SWarner Losh bool 186ef370346SEitan Adler event_proc::run(config &c) const 1873054f218SWarner Losh { 1883054f218SWarner Losh vector<eps *>::const_iterator i; 1893054f218SWarner Losh 190edee691dSEitan Adler for (i = _epsvec.begin(); i != _epsvec.end(); ++i) 1913054f218SWarner Losh if (!(*i)->do_action(c)) 1923054f218SWarner Losh return (false); 1933054f218SWarner Losh return (true); 1943054f218SWarner Losh } 1953054f218SWarner Losh 1963054f218SWarner Losh action::action(const char *cmd) 1973054f218SWarner Losh : _cmd(cmd) 1983054f218SWarner Losh { 1993054f218SWarner Losh // nothing 2003054f218SWarner Losh } 2013054f218SWarner Losh 2023054f218SWarner Losh action::~action() 2033054f218SWarner Losh { 2043054f218SWarner Losh // nothing 2053054f218SWarner Losh } 2063054f218SWarner Losh 207b8f92ce4SWarner Losh static int 208b8f92ce4SWarner Losh my_system(const char *command) 209b8f92ce4SWarner Losh { 210b8f92ce4SWarner Losh pid_t pid, savedpid; 211b8f92ce4SWarner Losh int pstat; 212b8f92ce4SWarner Losh struct sigaction ign, intact, quitact; 213b8f92ce4SWarner Losh sigset_t newsigblock, oldsigblock; 214b8f92ce4SWarner Losh 215b8f92ce4SWarner Losh if (!command) /* just checking... */ 216b8f92ce4SWarner Losh return (1); 217b8f92ce4SWarner Losh 218b8f92ce4SWarner Losh /* 219b8f92ce4SWarner Losh * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save 220b8f92ce4SWarner Losh * existing signal dispositions. 221b8f92ce4SWarner Losh */ 222b8f92ce4SWarner Losh ign.sa_handler = SIG_IGN; 223b8f92ce4SWarner Losh ::sigemptyset(&ign.sa_mask); 224b8f92ce4SWarner Losh ign.sa_flags = 0; 225b8f92ce4SWarner Losh ::sigaction(SIGINT, &ign, &intact); 226b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &ign, &quitact); 227b8f92ce4SWarner Losh ::sigemptyset(&newsigblock); 228b8f92ce4SWarner Losh ::sigaddset(&newsigblock, SIGCHLD); 229b8f92ce4SWarner Losh ::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 230b8f92ce4SWarner Losh switch (pid = ::fork()) { 231b8f92ce4SWarner Losh case -1: /* error */ 232b8f92ce4SWarner Losh break; 233b8f92ce4SWarner Losh case 0: /* child */ 234b8f92ce4SWarner Losh /* 235b8f92ce4SWarner Losh * Restore original signal dispositions and exec the command. 236b8f92ce4SWarner Losh */ 237b8f92ce4SWarner Losh ::sigaction(SIGINT, &intact, NULL); 238b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &quitact, NULL); 239b8f92ce4SWarner Losh ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 240b8f92ce4SWarner Losh /* 241b8f92ce4SWarner Losh * Close the PID file, and all other open descriptors. 242b8f92ce4SWarner Losh * Inherit std{in,out,err} only. 243b8f92ce4SWarner Losh */ 244b8f92ce4SWarner Losh cfg.close_pidfile(); 245b8f92ce4SWarner Losh ::closefrom(3); 246b8f92ce4SWarner Losh ::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); 247b8f92ce4SWarner Losh ::_exit(127); 248b8f92ce4SWarner Losh default: /* parent */ 249b8f92ce4SWarner Losh savedpid = pid; 250b8f92ce4SWarner Losh do { 251b8f92ce4SWarner Losh pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0); 252b8f92ce4SWarner Losh } while (pid == -1 && errno == EINTR); 253b8f92ce4SWarner Losh break; 254b8f92ce4SWarner Losh } 255b8f92ce4SWarner Losh ::sigaction(SIGINT, &intact, NULL); 256b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &quitact, NULL); 257b8f92ce4SWarner Losh ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 258b8f92ce4SWarner Losh return (pid == -1 ? -1 : pstat); 259b8f92ce4SWarner Losh } 260b8f92ce4SWarner Losh 2613054f218SWarner Losh bool 2625102ef84SWarner Losh action::do_action(config &c) 2633054f218SWarner Losh { 2645dfc0f6cSIan Lepore string s = c.expand_string(_cmd.c_str()); 2651bb0777eSAndriy Gapon devdlog(LOG_INFO, "Executing '%s'\n", s.c_str()); 266b8f92ce4SWarner Losh my_system(s.c_str()); 2673054f218SWarner Losh return (true); 2683054f218SWarner Losh } 2693054f218SWarner Losh 2705dfc0f6cSIan Lepore match::match(config &c, const char *var, const char *re) : 2715dfc0f6cSIan Lepore _inv(re[0] == '!'), 2725dfc0f6cSIan Lepore _var(var), 2735dfc0f6cSIan Lepore _re(c.expand_string(_inv ? re + 1 : re, "^", "$")) 2743054f218SWarner Losh { 2759cb4a7bdSWarner Losh regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE); 2763054f218SWarner Losh } 2773054f218SWarner Losh 2783054f218SWarner Losh match::~match() 2793054f218SWarner Losh { 2805102ef84SWarner Losh regfree(&_regex); 2813054f218SWarner Losh } 2823054f218SWarner Losh 2833054f218SWarner Losh bool 2845102ef84SWarner Losh match::do_match(config &c) 2853054f218SWarner Losh { 2865bf08422SUlf Lilleengen const string &value = c.get_variable(_var); 2875102ef84SWarner Losh bool retval; 2885102ef84SWarner Losh 2896d1014a3SAlan Somers /* 2906d1014a3SAlan Somers * This function gets called WAY too often to justify calling syslog() 2916d1014a3SAlan Somers * each time, even at LOG_DEBUG. Because if syslogd isn't running, it 2926d1014a3SAlan Somers * can consume excessive amounts of systime inside of connect(). Only 2936d1014a3SAlan Somers * log when we're in -d mode. 2946d1014a3SAlan Somers */ 2956a2ae0ebSAlan Somers if (no_daemon) { 2966d1014a3SAlan Somers devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n", 2970321b694SHiroki Sato _var.c_str(), value.c_str(), _re.c_str(), _inv); 2986d1014a3SAlan Somers } 2998b823408SWarner Losh 3005102ef84SWarner Losh retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0); 3010321b694SHiroki Sato if (_inv == 1) 3020321b694SHiroki Sato retval = (retval == 0) ? 1 : 0; 3030321b694SHiroki Sato 3040285e9b1SAlan Somers return (retval); 3053054f218SWarner Losh } 3063054f218SWarner Losh 307cd70782bSWarner Losh #include <sys/sockio.h> 308cd70782bSWarner Losh #include <net/if.h> 309cd70782bSWarner Losh #include <net/if_media.h> 310cd70782bSWarner Losh 31194d2d4ebSBrooks Davis media::media(config &, const char *var, const char *type) 312cd70782bSWarner Losh : _var(var), _type(-1) 313cd70782bSWarner Losh { 314cd70782bSWarner Losh static struct ifmedia_description media_types[] = { 315cd70782bSWarner Losh { IFM_ETHER, "Ethernet" }, 316cd70782bSWarner Losh { IFM_TOKEN, "Tokenring" }, 317cd70782bSWarner Losh { IFM_FDDI, "FDDI" }, 318cd70782bSWarner Losh { IFM_IEEE80211, "802.11" }, 319cd70782bSWarner Losh { IFM_ATM, "ATM" }, 320cd70782bSWarner Losh { -1, "unknown" }, 321cd70782bSWarner Losh { 0, NULL }, 322cd70782bSWarner Losh }; 323edee691dSEitan Adler for (int i = 0; media_types[i].ifmt_string != NULL; ++i) 324cd70782bSWarner Losh if (strcasecmp(type, media_types[i].ifmt_string) == 0) { 325cd70782bSWarner Losh _type = media_types[i].ifmt_word; 326cd70782bSWarner Losh break; 327cd70782bSWarner Losh } 328cd70782bSWarner Losh } 329cd70782bSWarner Losh 330cd70782bSWarner Losh media::~media() 331cd70782bSWarner Losh { 332cd70782bSWarner Losh } 333cd70782bSWarner Losh 334cd70782bSWarner Losh bool 335cd70782bSWarner Losh media::do_match(config &c) 336cd70782bSWarner Losh { 33709c47e76SWarner Losh string value; 338cd70782bSWarner Losh struct ifmediareq ifmr; 339cd70782bSWarner Losh bool retval; 340cd70782bSWarner Losh int s; 341cd70782bSWarner Losh 34209c47e76SWarner Losh // Since we can be called from both a device attach/detach 34309c47e76SWarner Losh // context where device-name is defined and what we want, 34409c47e76SWarner Losh // as well as from a link status context, where subsystem is 34509c47e76SWarner Losh // the name of interest, first try device-name and fall back 34609c47e76SWarner Losh // to subsystem if none exists. 34709c47e76SWarner Losh value = c.get_variable("device-name"); 348a6393aa1SEitan Adler if (value.empty()) 349de02eec8SWarner Losh value = c.get_variable("subsystem"); 3506d1014a3SAlan Somers devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n", 351cd70782bSWarner Losh value.c_str(), _type); 352cd70782bSWarner Losh 353cd70782bSWarner Losh retval = false; 354cd70782bSWarner Losh 355cd70782bSWarner Losh s = socket(PF_INET, SOCK_DGRAM, 0); 356cd70782bSWarner Losh if (s >= 0) { 357cd70782bSWarner Losh memset(&ifmr, 0, sizeof(ifmr)); 358cd70782bSWarner Losh strncpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name)); 359cd70782bSWarner Losh 360cd70782bSWarner Losh if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 && 361cd70782bSWarner Losh ifmr.ifm_status & IFM_AVALID) { 3626d1014a3SAlan Somers devdlog(LOG_DEBUG, "%s has media type 0x%x\n", 363cd70782bSWarner Losh value.c_str(), IFM_TYPE(ifmr.ifm_active)); 364cd70782bSWarner Losh retval = (IFM_TYPE(ifmr.ifm_active) == _type); 365cd70782bSWarner Losh } else if (_type == -1) { 3666d1014a3SAlan Somers devdlog(LOG_DEBUG, "%s has unknown media type\n", 367cd70782bSWarner Losh value.c_str()); 368cd70782bSWarner Losh retval = true; 369cd70782bSWarner Losh } 370cd70782bSWarner Losh close(s); 371cd70782bSWarner Losh } 372cd70782bSWarner Losh 3730285e9b1SAlan Somers return (retval); 374cd70782bSWarner Losh } 375cd70782bSWarner Losh 3763054f218SWarner Losh const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_"; 3773054f218SWarner Losh const string var_list::nothing = ""; 3783054f218SWarner Losh 3793054f218SWarner Losh const string & 3803054f218SWarner Losh var_list::get_variable(const string &var) const 3813054f218SWarner Losh { 3823054f218SWarner Losh map<string, string>::const_iterator i; 3833054f218SWarner Losh 3843054f218SWarner Losh i = _vars.find(var); 3853054f218SWarner Losh if (i == _vars.end()) 3868b823408SWarner Losh return (var_list::bogus); 3873054f218SWarner Losh return (i->second); 3883054f218SWarner Losh } 3893054f218SWarner Losh 3903054f218SWarner Losh bool 3913054f218SWarner Losh var_list::is_set(const string &var) const 3923054f218SWarner Losh { 3933054f218SWarner Losh return (_vars.find(var) != _vars.end()); 3943054f218SWarner Losh } 3953054f218SWarner Losh 3963054f218SWarner Losh void 3973054f218SWarner Losh var_list::set_variable(const string &var, const string &val) 3983054f218SWarner Losh { 3996d1014a3SAlan Somers /* 4006d1014a3SAlan Somers * This function gets called WAY too often to justify calling syslog() 4016d1014a3SAlan Somers * each time, even at LOG_DEBUG. Because if syslogd isn't running, it 4026d1014a3SAlan Somers * can consume excessive amounts of systime inside of connect(). Only 4036d1014a3SAlan Somers * log when we're in -d mode. 4046d1014a3SAlan Somers */ 4056a2ae0ebSAlan Somers if (no_daemon) 4066d1014a3SAlan Somers devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str()); 4073054f218SWarner Losh _vars[var] = val; 4083054f218SWarner Losh } 4093054f218SWarner Losh 4103054f218SWarner Losh void 4113054f218SWarner Losh config::reset(void) 4123054f218SWarner Losh { 4133054f218SWarner Losh _dir_list.clear(); 4148b823408SWarner Losh delete_and_clear(_var_list_table); 4158b823408SWarner Losh delete_and_clear(_attach_list); 4168b823408SWarner Losh delete_and_clear(_detach_list); 4178b823408SWarner Losh delete_and_clear(_nomatch_list); 418842ccec5SWarner Losh delete_and_clear(_notify_list); 4193054f218SWarner Losh } 4203054f218SWarner Losh 4213054f218SWarner Losh void 4223054f218SWarner Losh config::parse_one_file(const char *fn) 4233054f218SWarner Losh { 4246d1014a3SAlan Somers devdlog(LOG_DEBUG, "Parsing %s\n", fn); 4253054f218SWarner Losh yyin = fopen(fn, "r"); 4263054f218SWarner Losh if (yyin == NULL) 4273054f218SWarner Losh err(1, "Cannot open config file %s", fn); 4284a3050fcSMaxim Konovalov lineno = 1; 4293054f218SWarner Losh if (yyparse() != 0) 4303054f218SWarner Losh errx(1, "Cannot parse %s at line %d", fn, lineno); 4313054f218SWarner Losh fclose(yyin); 4323054f218SWarner Losh } 4333054f218SWarner Losh 4343054f218SWarner Losh void 4353054f218SWarner Losh config::parse_files_in_dir(const char *dirname) 4363054f218SWarner Losh { 4373054f218SWarner Losh DIR *dirp; 4383054f218SWarner Losh struct dirent *dp; 4393054f218SWarner Losh char path[PATH_MAX]; 4403054f218SWarner Losh 4416d1014a3SAlan Somers devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname); 4423054f218SWarner Losh dirp = opendir(dirname); 4433054f218SWarner Losh if (dirp == NULL) 4443054f218SWarner Losh return; 4453054f218SWarner Losh readdir(dirp); /* Skip . */ 4463054f218SWarner Losh readdir(dirp); /* Skip .. */ 4473054f218SWarner Losh while ((dp = readdir(dirp)) != NULL) { 4483054f218SWarner Losh if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) { 4493054f218SWarner Losh snprintf(path, sizeof(path), "%s/%s", 4503054f218SWarner Losh dirname, dp->d_name); 4513054f218SWarner Losh parse_one_file(path); 4523054f218SWarner Losh } 4533054f218SWarner Losh } 4545983e891SKevin Lo closedir(dirp); 4553054f218SWarner Losh } 4563054f218SWarner Losh 4578b823408SWarner Losh class epv_greater { 4588b823408SWarner Losh public: 459ef370346SEitan Adler int operator()(event_proc *const&l1, event_proc *const&l2) const 4608b823408SWarner Losh { 4618b823408SWarner Losh return (l1->get_priority() > l2->get_priority()); 4628b823408SWarner Losh } 4638b823408SWarner Losh }; 4648b823408SWarner Losh 4658b823408SWarner Losh void 4668b823408SWarner Losh config::sort_vector(vector<event_proc *> &v) 4678b823408SWarner Losh { 4680c04d185SDimitry Andric stable_sort(v.begin(), v.end(), epv_greater()); 4698b823408SWarner Losh } 4708b823408SWarner Losh 4713054f218SWarner Losh void 4723054f218SWarner Losh config::parse(void) 4733054f218SWarner Losh { 4743054f218SWarner Losh vector<string>::const_iterator i; 4753054f218SWarner Losh 4768334958aSJoseph Koshy parse_one_file(configfile); 477edee691dSEitan Adler for (i = _dir_list.begin(); i != _dir_list.end(); ++i) 4783054f218SWarner Losh parse_files_in_dir((*i).c_str()); 4798b823408SWarner Losh sort_vector(_attach_list); 4808b823408SWarner Losh sort_vector(_detach_list); 4818b823408SWarner Losh sort_vector(_nomatch_list); 482842ccec5SWarner Losh sort_vector(_notify_list); 4833054f218SWarner Losh } 4843054f218SWarner Losh 4853054f218SWarner Losh void 4861a0cc6b1SPawel Jakub Dawidek config::open_pidfile() 4873054f218SWarner Losh { 4881a0cc6b1SPawel Jakub Dawidek pid_t otherpid; 4893054f218SWarner Losh 4905a3b1a3dSEitan Adler if (_pidfile.empty()) 4913054f218SWarner Losh return; 4921a0cc6b1SPawel Jakub Dawidek pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid); 4931a0cc6b1SPawel Jakub Dawidek if (pfh == NULL) { 4941a0cc6b1SPawel Jakub Dawidek if (errno == EEXIST) 4951a0cc6b1SPawel Jakub Dawidek errx(1, "devd already running, pid: %d", (int)otherpid); 4961a0cc6b1SPawel Jakub Dawidek warn("cannot open pid file"); 4971a0cc6b1SPawel Jakub Dawidek } 4981a0cc6b1SPawel Jakub Dawidek } 4991a0cc6b1SPawel Jakub Dawidek 5001a0cc6b1SPawel Jakub Dawidek void 5011a0cc6b1SPawel Jakub Dawidek config::write_pidfile() 5021a0cc6b1SPawel Jakub Dawidek { 5031a0cc6b1SPawel Jakub Dawidek 5041a0cc6b1SPawel Jakub Dawidek pidfile_write(pfh); 5051a0cc6b1SPawel Jakub Dawidek } 5061a0cc6b1SPawel Jakub Dawidek 5071a0cc6b1SPawel Jakub Dawidek void 508b8f92ce4SWarner Losh config::close_pidfile() 509b8f92ce4SWarner Losh { 510b8f92ce4SWarner Losh 511b8f92ce4SWarner Losh pidfile_close(pfh); 512b8f92ce4SWarner Losh } 513b8f92ce4SWarner Losh 514b8f92ce4SWarner Losh void 5151a0cc6b1SPawel Jakub Dawidek config::remove_pidfile() 5161a0cc6b1SPawel Jakub Dawidek { 5171a0cc6b1SPawel Jakub Dawidek 5181a0cc6b1SPawel Jakub Dawidek pidfile_remove(pfh); 5193054f218SWarner Losh } 5203054f218SWarner Losh 5213054f218SWarner Losh void 5223054f218SWarner Losh config::add_attach(int prio, event_proc *p) 5233054f218SWarner Losh { 5243054f218SWarner Losh p->set_priority(prio); 5253054f218SWarner Losh _attach_list.push_back(p); 5263054f218SWarner Losh } 5273054f218SWarner Losh 5283054f218SWarner Losh void 5293054f218SWarner Losh config::add_detach(int prio, event_proc *p) 5303054f218SWarner Losh { 5313054f218SWarner Losh p->set_priority(prio); 5323054f218SWarner Losh _detach_list.push_back(p); 5333054f218SWarner Losh } 5343054f218SWarner Losh 5353054f218SWarner Losh void 5363054f218SWarner Losh config::add_directory(const char *dir) 5373054f218SWarner Losh { 5383054f218SWarner Losh _dir_list.push_back(string(dir)); 5393054f218SWarner Losh } 5403054f218SWarner Losh 5413054f218SWarner Losh void 5423054f218SWarner Losh config::add_nomatch(int prio, event_proc *p) 5433054f218SWarner Losh { 5443054f218SWarner Losh p->set_priority(prio); 5453054f218SWarner Losh _nomatch_list.push_back(p); 5463054f218SWarner Losh } 5473054f218SWarner Losh 5483054f218SWarner Losh void 549842ccec5SWarner Losh config::add_notify(int prio, event_proc *p) 550842ccec5SWarner Losh { 551842ccec5SWarner Losh p->set_priority(prio); 552842ccec5SWarner Losh _notify_list.push_back(p); 553842ccec5SWarner Losh } 554842ccec5SWarner Losh 555842ccec5SWarner Losh void 5563054f218SWarner Losh config::set_pidfile(const char *fn) 5573054f218SWarner Losh { 5585a3b1a3dSEitan Adler _pidfile = fn; 5593054f218SWarner Losh } 5603054f218SWarner Losh 5613054f218SWarner Losh void 5623054f218SWarner Losh config::push_var_table() 5633054f218SWarner Losh { 5643054f218SWarner Losh var_list *vl; 5653054f218SWarner Losh 5663054f218SWarner Losh vl = new var_list(); 5673054f218SWarner Losh _var_list_table.push_back(vl); 5686d1014a3SAlan Somers devdlog(LOG_DEBUG, "Pushing table\n"); 5693054f218SWarner Losh } 5703054f218SWarner Losh 5713054f218SWarner Losh void 5723054f218SWarner Losh config::pop_var_table() 5733054f218SWarner Losh { 5743054f218SWarner Losh delete _var_list_table.back(); 5753054f218SWarner Losh _var_list_table.pop_back(); 5766d1014a3SAlan Somers devdlog(LOG_DEBUG, "Popping table\n"); 5773054f218SWarner Losh } 5783054f218SWarner Losh 5793054f218SWarner Losh void 5803054f218SWarner Losh config::set_variable(const char *var, const char *val) 5813054f218SWarner Losh { 5823054f218SWarner Losh _var_list_table.back()->set_variable(var, val); 5833054f218SWarner Losh } 5843054f218SWarner Losh 5853054f218SWarner Losh const string & 5863054f218SWarner Losh config::get_variable(const string &var) 5873054f218SWarner Losh { 5883054f218SWarner Losh vector<var_list *>::reverse_iterator i; 5893054f218SWarner Losh 590edee691dSEitan Adler for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) { 5913054f218SWarner Losh if ((*i)->is_set(var)) 5928b823408SWarner Losh return ((*i)->get_variable(var)); 5933054f218SWarner Losh } 5943054f218SWarner Losh return (var_list::nothing); 5953054f218SWarner Losh } 5963054f218SWarner Losh 5978b823408SWarner Losh bool 598ef370346SEitan Adler config::is_id_char(char ch) const 5998b823408SWarner Losh { 6008b823408SWarner Losh return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' || 6018b823408SWarner Losh ch == '-')); 6028b823408SWarner Losh } 6038b823408SWarner Losh 6045102ef84SWarner Losh void 6056cabd675SWarner Losh config::expand_one(const char *&src, string &dst) 6063054f218SWarner Losh { 6075102ef84SWarner Losh int count; 6085bf08422SUlf Lilleengen string buffer; 6095102ef84SWarner Losh 6108b823408SWarner Losh src++; 6115102ef84SWarner Losh // $$ -> $ 6125102ef84SWarner Losh if (*src == '$') { 6137d9e9c60SEitan Adler dst += *src++; 6145102ef84SWarner Losh return; 6155102ef84SWarner Losh } 6165102ef84SWarner Losh 6175102ef84SWarner Losh // $(foo) -> $(foo) 6188b823408SWarner Losh // Not sure if I want to support this or not, so for now we just pass 6198b823408SWarner Losh // it through. 6205102ef84SWarner Losh if (*src == '(') { 6217d9e9c60SEitan Adler dst += '$'; 6225102ef84SWarner Losh count = 1; 6236cabd675SWarner Losh /* If the string ends before ) is matched , return. */ 6246cabd675SWarner Losh while (count > 0 && *src) { 6255102ef84SWarner Losh if (*src == ')') 6265102ef84SWarner Losh count--; 6275102ef84SWarner Losh else if (*src == '(') 6285102ef84SWarner Losh count++; 6297d9e9c60SEitan Adler dst += *src++; 6305102ef84SWarner Losh } 6315102ef84SWarner Losh return; 6325102ef84SWarner Losh } 6335102ef84SWarner Losh 6344cb9d1beSEitan Adler // $[^A-Za-z] -> $\1 6355102ef84SWarner Losh if (!isalpha(*src)) { 6367d9e9c60SEitan Adler dst += '$'; 6377d9e9c60SEitan Adler dst += *src++; 6385102ef84SWarner Losh return; 6395102ef84SWarner Losh } 6405102ef84SWarner Losh 6415102ef84SWarner Losh // $var -> replace with value 6426cabd675SWarner Losh do { 6437d9e9c60SEitan Adler buffer += *src++; 64466ff6821SWarner Losh } while (is_id_char(*src)); 6455a3b1a3dSEitan Adler dst.append(get_variable(buffer)); 6465102ef84SWarner Losh } 6475102ef84SWarner Losh 6485102ef84SWarner Losh const string 6495dfc0f6cSIan Lepore config::expand_string(const char *src, const char *prepend, const char *append) 6505102ef84SWarner Losh { 6515dfc0f6cSIan Lepore const char *var_at; 6526cabd675SWarner Losh string dst; 6535102ef84SWarner Losh 6545dfc0f6cSIan Lepore /* 6555dfc0f6cSIan Lepore * 128 bytes is enough for 2427 of 2438 expansions that happen 6565dfc0f6cSIan Lepore * while parsing config files, as tested on 2013-01-30. 6575dfc0f6cSIan Lepore */ 6585dfc0f6cSIan Lepore dst.reserve(128); 6595dfc0f6cSIan Lepore 6605dfc0f6cSIan Lepore if (prepend != NULL) 6615dfc0f6cSIan Lepore dst = prepend; 6625dfc0f6cSIan Lepore 6635dfc0f6cSIan Lepore for (;;) { 6645dfc0f6cSIan Lepore var_at = strchr(src, '$'); 6655dfc0f6cSIan Lepore if (var_at == NULL) { 6665dfc0f6cSIan Lepore dst.append(src); 6675dfc0f6cSIan Lepore break; 6685102ef84SWarner Losh } 6695dfc0f6cSIan Lepore dst.append(src, var_at - src); 6705dfc0f6cSIan Lepore src = var_at; 6715dfc0f6cSIan Lepore expand_one(src, dst); 6725dfc0f6cSIan Lepore } 6735dfc0f6cSIan Lepore 6745dfc0f6cSIan Lepore if (append != NULL) 6755dfc0f6cSIan Lepore dst.append(append); 6765102ef84SWarner Losh 6776cabd675SWarner Losh return (dst); 6783054f218SWarner Losh } 6793054f218SWarner Losh 6808b823408SWarner Losh bool 68111fd1366SEitan Adler config::chop_var(char *&buffer, char *&lhs, char *&rhs) const 6828b823408SWarner Losh { 6838b823408SWarner Losh char *walker; 6848b823408SWarner Losh 6858b823408SWarner Losh if (*buffer == '\0') 6868b823408SWarner Losh return (false); 6878b823408SWarner Losh walker = lhs = buffer; 6888b823408SWarner Losh while (is_id_char(*walker)) 6898b823408SWarner Losh walker++; 6908b823408SWarner Losh if (*walker != '=') 6918b823408SWarner Losh return (false); 6928b823408SWarner Losh walker++; // skip = 6938b823408SWarner Losh if (*walker == '"') { 6948b823408SWarner Losh walker++; // skip " 6958b823408SWarner Losh rhs = walker; 6968b823408SWarner Losh while (*walker && *walker != '"') 6978b823408SWarner Losh walker++; 6988b823408SWarner Losh if (*walker != '"') 6998b823408SWarner Losh return (false); 7008b823408SWarner Losh rhs[-2] = '\0'; 7018b823408SWarner Losh *walker++ = '\0'; 7028b823408SWarner Losh } else { 7038b823408SWarner Losh rhs = walker; 7048b823408SWarner Losh while (*walker && !isspace(*walker)) 7058b823408SWarner Losh walker++; 7068b823408SWarner Losh if (*walker != '\0') 7078b823408SWarner Losh *walker++ = '\0'; 7088b823408SWarner Losh rhs[-1] = '\0'; 7098b823408SWarner Losh } 710b1ee04b1SWarner Losh while (isspace(*walker)) 711b1ee04b1SWarner Losh walker++; 7128b823408SWarner Losh buffer = walker; 7138b823408SWarner Losh return (true); 7148b823408SWarner Losh } 7158b823408SWarner Losh 7168b823408SWarner Losh 7178b823408SWarner Losh char * 7188b823408SWarner Losh config::set_vars(char *buffer) 7198b823408SWarner Losh { 7208b823408SWarner Losh char *lhs; 7218b823408SWarner Losh char *rhs; 7228b823408SWarner Losh 7238b823408SWarner Losh while (1) { 7248b823408SWarner Losh if (!chop_var(buffer, lhs, rhs)) 7258b823408SWarner Losh break; 7268b823408SWarner Losh set_variable(lhs, rhs); 7278b823408SWarner Losh } 7288b823408SWarner Losh return (buffer); 7298b823408SWarner Losh } 7308b823408SWarner Losh 7318b823408SWarner Losh void 7328b823408SWarner Losh config::find_and_execute(char type) 7338b823408SWarner Losh { 7348b823408SWarner Losh vector<event_proc *> *l; 7358b823408SWarner Losh vector<event_proc *>::const_iterator i; 73694d2d4ebSBrooks Davis const char *s; 7378b823408SWarner Losh 7388b823408SWarner Losh switch (type) { 7398b823408SWarner Losh default: 7408b823408SWarner Losh return; 741842ccec5SWarner Losh case notify: 742842ccec5SWarner Losh l = &_notify_list; 743842ccec5SWarner Losh s = "notify"; 744842ccec5SWarner Losh break; 7458b823408SWarner Losh case nomatch: 7468b823408SWarner Losh l = &_nomatch_list; 7478b823408SWarner Losh s = "nomatch"; 7488b823408SWarner Losh break; 7498b823408SWarner Losh case attach: 7508b823408SWarner Losh l = &_attach_list; 7518b823408SWarner Losh s = "attach"; 7528b823408SWarner Losh break; 7538b823408SWarner Losh case detach: 7548b823408SWarner Losh l = &_detach_list; 7558b823408SWarner Losh s = "detach"; 7568b823408SWarner Losh break; 7578b823408SWarner Losh } 7586d1014a3SAlan Somers devdlog(LOG_DEBUG, "Processing %s event\n", s); 759edee691dSEitan Adler for (i = l->begin(); i != l->end(); ++i) { 7608b823408SWarner Losh if ((*i)->matches(*this)) { 7618b823408SWarner Losh (*i)->run(*this); 7628b823408SWarner Losh break; 7638b823408SWarner Losh } 7648b823408SWarner Losh } 7658b823408SWarner Losh 7668b823408SWarner Losh } 7678b823408SWarner Losh 7680285e9b1SAlan Somers 7693054f218SWarner Losh static void 7708b823408SWarner Losh process_event(char *buffer) 7713054f218SWarner Losh { 7723054f218SWarner Losh char type; 7733054f218SWarner Losh char *sp; 7743054f218SWarner Losh 7758b823408SWarner Losh sp = buffer + 1; 776f0038a8eSAlan Somers devdlog(LOG_INFO, "Processing event '%s'\n", buffer); 7773054f218SWarner Losh type = *buffer++; 7788b823408SWarner Losh cfg.push_var_table(); 7798b823408SWarner Losh // No match doesn't have a device, and the format is a little 7808b823408SWarner Losh // different, so handle it separately. 781842ccec5SWarner Losh switch (type) { 782842ccec5SWarner Losh case notify: 783842ccec5SWarner Losh sp = cfg.set_vars(sp); 784842ccec5SWarner Losh break; 785842ccec5SWarner Losh case nomatch: 786528d3f79SWarner Losh //? at location pnp-info on bus 787528d3f79SWarner Losh sp = strchr(sp, ' '); 788528d3f79SWarner Losh if (sp == NULL) 789528d3f79SWarner Losh return; /* Can't happen? */ 790528d3f79SWarner Losh *sp++ = '\0'; 7919b6cb520SWarner Losh while (isspace(*sp)) 7929b6cb520SWarner Losh sp++; 793842ccec5SWarner Losh if (strncmp(sp, "at ", 3) == 0) 794842ccec5SWarner Losh sp += 3; 795842ccec5SWarner Losh sp = cfg.set_vars(sp); 7969b6cb520SWarner Losh while (isspace(*sp)) 7979b6cb520SWarner Losh sp++; 798842ccec5SWarner Losh if (strncmp(sp, "on ", 3) == 0) 799842ccec5SWarner Losh cfg.set_variable("bus", sp + 3); 800842ccec5SWarner Losh break; 801842ccec5SWarner Losh case attach: /*FALLTHROUGH*/ 802842ccec5SWarner Losh case detach: 8038b823408SWarner Losh sp = strchr(sp, ' '); 8043054f218SWarner Losh if (sp == NULL) 8053054f218SWarner Losh return; /* Can't happen? */ 8068b823408SWarner Losh *sp++ = '\0'; 8078b823408SWarner Losh cfg.set_variable("device-name", buffer); 8089b6cb520SWarner Losh while (isspace(*sp)) 8099b6cb520SWarner Losh sp++; 8108b823408SWarner Losh if (strncmp(sp, "at ", 3) == 0) 8118b823408SWarner Losh sp += 3; 8128b823408SWarner Losh sp = cfg.set_vars(sp); 8139b6cb520SWarner Losh while (isspace(*sp)) 8149b6cb520SWarner Losh sp++; 8158b823408SWarner Losh if (strncmp(sp, "on ", 3) == 0) 8168b823408SWarner Losh cfg.set_variable("bus", sp + 3); 817842ccec5SWarner Losh break; 818b1ee04b1SWarner Losh } 819b1ee04b1SWarner Losh 8208b823408SWarner Losh cfg.find_and_execute(type); 8218b823408SWarner Losh cfg.pop_var_table(); 8223054f218SWarner Losh } 8233054f218SWarner Losh 824251bedd1SWarner Losh int 825251bedd1SWarner Losh create_socket(const char *name) 826251bedd1SWarner Losh { 827251bedd1SWarner Losh int fd, slen; 828251bedd1SWarner Losh struct sockaddr_un sun; 829251bedd1SWarner Losh 830251bedd1SWarner Losh if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) 831251bedd1SWarner Losh err(1, "socket"); 832251bedd1SWarner Losh bzero(&sun, sizeof(sun)); 833251bedd1SWarner Losh sun.sun_family = AF_UNIX; 834251bedd1SWarner Losh strlcpy(sun.sun_path, name, sizeof(sun.sun_path)); 835251bedd1SWarner Losh slen = SUN_LEN(&sun); 836251bedd1SWarner Losh unlink(name); 837e60fa014SJoe Marcus Clarke if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) 838e60fa014SJoe Marcus Clarke err(1, "fcntl"); 8395d586d29SDimitry Andric if (::bind(fd, (struct sockaddr *) & sun, slen) < 0) 840251bedd1SWarner Losh err(1, "bind"); 841251bedd1SWarner Losh listen(fd, 4); 84283818929SJoe Marcus Clarke chown(name, 0, 0); /* XXX - root.wheel */ 843e60fa014SJoe Marcus Clarke chmod(name, 0666); 844251bedd1SWarner Losh return (fd); 845251bedd1SWarner Losh } 846251bedd1SWarner Losh 847e1334f93SIan Lepore unsigned int max_clients = 10; /* Default, can be overriden on cmdline. */ 848e1334f93SIan Lepore unsigned int num_clients; 849251bedd1SWarner Losh list<int> clients; 850251bedd1SWarner Losh 851251bedd1SWarner Losh void 852251bedd1SWarner Losh notify_clients(const char *data, int len) 853251bedd1SWarner Losh { 854e1334f93SIan Lepore list<int>::iterator i; 855251bedd1SWarner Losh 856e1334f93SIan Lepore /* 857e1334f93SIan Lepore * Deliver the data to all clients. Throw clients overboard at the 858e1334f93SIan Lepore * first sign of trouble. This reaps clients who've died or closed 859e1334f93SIan Lepore * their sockets, and also clients who are alive but failing to keep up 860e1334f93SIan Lepore * (or who are maliciously not reading, to consume buffer space in 861e1334f93SIan Lepore * kernel memory or tie up the limited number of available connections). 862e1334f93SIan Lepore */ 863e1334f93SIan Lepore for (i = clients.begin(); i != clients.end(); ) { 864e1334f93SIan Lepore if (write(*i, data, len) != len) { 865e1334f93SIan Lepore --num_clients; 866251bedd1SWarner Losh close(*i); 867e1334f93SIan Lepore i = clients.erase(i); 8686d1014a3SAlan Somers devdlog(LOG_WARNING, "notify_clients: write() failed; " 8696d1014a3SAlan Somers "dropping unresponsive client\n"); 870e1334f93SIan Lepore } else 871e1334f93SIan Lepore ++i; 872251bedd1SWarner Losh } 873251bedd1SWarner Losh } 874251bedd1SWarner Losh 875e1334f93SIan Lepore void 876e1334f93SIan Lepore check_clients(void) 877e1334f93SIan Lepore { 878e1334f93SIan Lepore int s; 879e1334f93SIan Lepore struct pollfd pfd; 880e1334f93SIan Lepore list<int>::iterator i; 881e1334f93SIan Lepore 882e1334f93SIan Lepore /* 883e1334f93SIan Lepore * Check all existing clients to see if any of them have disappeared. 884e1334f93SIan Lepore * Normally we reap clients when we get an error trying to send them an 885e1334f93SIan Lepore * event. This check eliminates the problem of an ever-growing list of 886e1334f93SIan Lepore * zombie clients because we're never writing to them on a system 887e1334f93SIan Lepore * without frequent device-change activity. 888e1334f93SIan Lepore */ 889e1334f93SIan Lepore pfd.events = 0; 890e1334f93SIan Lepore for (i = clients.begin(); i != clients.end(); ) { 891e1334f93SIan Lepore pfd.fd = *i; 892e1334f93SIan Lepore s = poll(&pfd, 1, 0); 893e1334f93SIan Lepore if ((s < 0 && s != EINTR ) || 894e1334f93SIan Lepore (s > 0 && (pfd.revents & POLLHUP))) { 895e1334f93SIan Lepore --num_clients; 896e1334f93SIan Lepore close(*i); 897e1334f93SIan Lepore i = clients.erase(i); 8986d1014a3SAlan Somers devdlog(LOG_NOTICE, "check_clients: " 8996d1014a3SAlan Somers "dropping disconnected client\n"); 900e1334f93SIan Lepore } else 901e1334f93SIan Lepore ++i; 902e1334f93SIan Lepore } 903251bedd1SWarner Losh } 904251bedd1SWarner Losh 905251bedd1SWarner Losh void 906251bedd1SWarner Losh new_client(int fd) 907251bedd1SWarner Losh { 908251bedd1SWarner Losh int s; 909b026eddfSAlan Somers int sndbuf_size; 910251bedd1SWarner Losh 911e1334f93SIan Lepore /* 912e1334f93SIan Lepore * First go reap any zombie clients, then accept the connection, and 913e1334f93SIan Lepore * shut down the read side to stop clients from consuming kernel memory 914e1334f93SIan Lepore * by sending large buffers full of data we'll never read. 915e1334f93SIan Lepore */ 916e1334f93SIan Lepore check_clients(); 917251bedd1SWarner Losh s = accept(fd, NULL, NULL); 918e1334f93SIan Lepore if (s != -1) { 919b026eddfSAlan Somers sndbuf_size = CLIENT_BUFSIZE; 920b026eddfSAlan Somers if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, 921b026eddfSAlan Somers sizeof(sndbuf_size))) 922b026eddfSAlan Somers err(1, "setsockopt"); 923e1334f93SIan Lepore shutdown(s, SHUT_RD); 924251bedd1SWarner Losh clients.push_back(s); 925e1334f93SIan Lepore ++num_clients; 926b026eddfSAlan Somers } else 927b026eddfSAlan Somers err(1, "accept"); 928251bedd1SWarner Losh } 929251bedd1SWarner Losh 9303054f218SWarner Losh static void 9313054f218SWarner Losh event_loop(void) 9323054f218SWarner Losh { 9333054f218SWarner Losh int rv; 9343054f218SWarner Losh int fd; 9353054f218SWarner Losh char buffer[DEVCTL_MAXBUF]; 9365a882d77SWarner Losh int once = 0; 937251bedd1SWarner Losh int server_fd, max_fd; 938e1334f93SIan Lepore int accepting; 9395a882d77SWarner Losh timeval tv; 9405a882d77SWarner Losh fd_set fds; 9413054f218SWarner Losh 942901610f4SPawel Jakub Dawidek fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC); 9433054f218SWarner Losh if (fd == -1) 944251bedd1SWarner Losh err(1, "Can't open devctl device %s", PATH_DEVCTL); 945251bedd1SWarner Losh server_fd = create_socket(PIPE); 946e1334f93SIan Lepore accepting = 1; 947251bedd1SWarner Losh max_fd = max(fd, server_fd) + 1; 94867ae3bf1SEitan Adler while (!romeo_must_die) { 9496a2ae0ebSAlan Somers if (!once && !no_daemon && !daemonize_quick) { 9505a882d77SWarner Losh // Check to see if we have any events pending. 9515a882d77SWarner Losh tv.tv_sec = 0; 9525a882d77SWarner Losh tv.tv_usec = 0; 9535a882d77SWarner Losh FD_ZERO(&fds); 9545a882d77SWarner Losh FD_SET(fd, &fds); 9555a882d77SWarner Losh rv = select(fd + 1, &fds, &fds, &fds, &tv); 9565a882d77SWarner Losh // No events -> we've processed all pending events 957f3059505SWarner Losh if (rv == 0) { 9586d1014a3SAlan Somers devdlog(LOG_DEBUG, "Calling daemon\n"); 9591a0cc6b1SPawel Jakub Dawidek cfg.remove_pidfile(); 9601a0cc6b1SPawel Jakub Dawidek cfg.open_pidfile(); 9615a882d77SWarner Losh daemon(0, 0); 9621a0cc6b1SPawel Jakub Dawidek cfg.write_pidfile(); 9635a882d77SWarner Losh once++; 9645a882d77SWarner Losh } 9655a882d77SWarner Losh } 966e1334f93SIan Lepore /* 967e1334f93SIan Lepore * When we've already got the max number of clients, stop 968e1334f93SIan Lepore * accepting new connections (don't put server_fd in the set), 969e1334f93SIan Lepore * shrink the accept() queue to reject connections quickly, and 970e1334f93SIan Lepore * poll the existing clients more often, so that we notice more 971e1334f93SIan Lepore * quickly when any of them disappear to free up client slots. 972e1334f93SIan Lepore */ 973251bedd1SWarner Losh FD_ZERO(&fds); 974251bedd1SWarner Losh FD_SET(fd, &fds); 975e1334f93SIan Lepore if (num_clients < max_clients) { 976e1334f93SIan Lepore if (!accepting) { 977e1334f93SIan Lepore listen(server_fd, max_clients); 978e1334f93SIan Lepore accepting = 1; 979e1334f93SIan Lepore } 980251bedd1SWarner Losh FD_SET(server_fd, &fds); 981e1334f93SIan Lepore tv.tv_sec = 60; 982e1334f93SIan Lepore tv.tv_usec = 0; 983e1334f93SIan Lepore } else { 984e1334f93SIan Lepore if (accepting) { 985e1334f93SIan Lepore listen(server_fd, 0); 986e1334f93SIan Lepore accepting = 0; 987e1334f93SIan Lepore } 988e1334f93SIan Lepore tv.tv_sec = 2; 989e1334f93SIan Lepore tv.tv_usec = 0; 990e1334f93SIan Lepore } 991e1334f93SIan Lepore rv = select(max_fd, &fds, NULL, NULL, &tv); 992be685e01SAlan Somers if (got_siginfo) { 993f0038a8eSAlan Somers devdlog(LOG_NOTICE, "Events received so far=%u\n", 994be685e01SAlan Somers total_events); 995be685e01SAlan Somers got_siginfo = 0; 996be685e01SAlan Somers } 997251bedd1SWarner Losh if (rv == -1) { 998251bedd1SWarner Losh if (errno == EINTR) 999251bedd1SWarner Losh continue; 1000251bedd1SWarner Losh err(1, "select"); 1001e1334f93SIan Lepore } else if (rv == 0) 1002e1334f93SIan Lepore check_clients(); 1003251bedd1SWarner Losh if (FD_ISSET(fd, &fds)) { 10043054f218SWarner Losh rv = read(fd, buffer, sizeof(buffer) - 1); 10053054f218SWarner Losh if (rv > 0) { 1006be685e01SAlan Somers total_events++; 10076d1014a3SAlan Somers if (rv == sizeof(buffer) - 1) { 10086d1014a3SAlan Somers devdlog(LOG_WARNING, "Warning: " 10096d1014a3SAlan Somers "available event data exceeded " 10106d1014a3SAlan Somers "buffer space\n"); 10116d1014a3SAlan Somers } 1012251bedd1SWarner Losh notify_clients(buffer, rv); 10133054f218SWarner Losh buffer[rv] = '\0'; 10143054f218SWarner Losh while (buffer[--rv] == '\n') 10153054f218SWarner Losh buffer[rv] = '\0'; 10163054f218SWarner Losh process_event(buffer); 10173054f218SWarner Losh } else if (rv < 0) { 10183054f218SWarner Losh if (errno != EINTR) 10193054f218SWarner Losh break; 10203054f218SWarner Losh } else { 10213054f218SWarner Losh /* EOF */ 10223054f218SWarner Losh break; 10233054f218SWarner Losh } 10243054f218SWarner Losh } 1025251bedd1SWarner Losh if (FD_ISSET(server_fd, &fds)) 1026251bedd1SWarner Losh new_client(server_fd); 1027251bedd1SWarner Losh } 10283054f218SWarner Losh close(fd); 10293054f218SWarner Losh } 10300285e9b1SAlan Somers 10313054f218SWarner Losh /* 10323054f218SWarner Losh * functions that the parser uses. 10333054f218SWarner Losh */ 10343054f218SWarner Losh void 10353054f218SWarner Losh add_attach(int prio, event_proc *p) 10363054f218SWarner Losh { 10373054f218SWarner Losh cfg.add_attach(prio, p); 10383054f218SWarner Losh } 10393054f218SWarner Losh 10403054f218SWarner Losh void 10413054f218SWarner Losh add_detach(int prio, event_proc *p) 10423054f218SWarner Losh { 10433054f218SWarner Losh cfg.add_detach(prio, p); 10443054f218SWarner Losh } 10453054f218SWarner Losh 10463054f218SWarner Losh void 10473054f218SWarner Losh add_directory(const char *dir) 10483054f218SWarner Losh { 10493054f218SWarner Losh cfg.add_directory(dir); 10503054f218SWarner Losh free(const_cast<char *>(dir)); 10513054f218SWarner Losh } 10523054f218SWarner Losh 10533054f218SWarner Losh void 10543054f218SWarner Losh add_nomatch(int prio, event_proc *p) 10553054f218SWarner Losh { 10563054f218SWarner Losh cfg.add_nomatch(prio, p); 10573054f218SWarner Losh } 10583054f218SWarner Losh 1059842ccec5SWarner Losh void 1060842ccec5SWarner Losh add_notify(int prio, event_proc *p) 1061842ccec5SWarner Losh { 1062842ccec5SWarner Losh cfg.add_notify(prio, p); 1063842ccec5SWarner Losh } 1064842ccec5SWarner Losh 10653054f218SWarner Losh event_proc * 10663054f218SWarner Losh add_to_event_proc(event_proc *ep, eps *eps) 10673054f218SWarner Losh { 10683054f218SWarner Losh if (ep == NULL) 10693054f218SWarner Losh ep = new event_proc(); 10703054f218SWarner Losh ep->add(eps); 10713054f218SWarner Losh return (ep); 10723054f218SWarner Losh } 10733054f218SWarner Losh 10743054f218SWarner Losh eps * 10753054f218SWarner Losh new_action(const char *cmd) 10763054f218SWarner Losh { 10773054f218SWarner Losh eps *e = new action(cmd); 10783054f218SWarner Losh free(const_cast<char *>(cmd)); 10793054f218SWarner Losh return (e); 10803054f218SWarner Losh } 10813054f218SWarner Losh 10823054f218SWarner Losh eps * 10833054f218SWarner Losh new_match(const char *var, const char *re) 10843054f218SWarner Losh { 10855102ef84SWarner Losh eps *e = new match(cfg, var, re); 10863054f218SWarner Losh free(const_cast<char *>(var)); 10873054f218SWarner Losh free(const_cast<char *>(re)); 10883054f218SWarner Losh return (e); 10893054f218SWarner Losh } 10903054f218SWarner Losh 1091cd70782bSWarner Losh eps * 1092cd70782bSWarner Losh new_media(const char *var, const char *re) 1093cd70782bSWarner Losh { 1094cd70782bSWarner Losh eps *e = new media(cfg, var, re); 1095cd70782bSWarner Losh free(const_cast<char *>(var)); 1096cd70782bSWarner Losh free(const_cast<char *>(re)); 1097cd70782bSWarner Losh return (e); 1098cd70782bSWarner Losh } 1099cd70782bSWarner Losh 11003054f218SWarner Losh void 11013054f218SWarner Losh set_pidfile(const char *name) 11023054f218SWarner Losh { 11033054f218SWarner Losh cfg.set_pidfile(name); 11043054f218SWarner Losh free(const_cast<char *>(name)); 11053054f218SWarner Losh } 11063054f218SWarner Losh 11073054f218SWarner Losh void 11083054f218SWarner Losh set_variable(const char *var, const char *val) 11093054f218SWarner Losh { 11103054f218SWarner Losh cfg.set_variable(var, val); 11113054f218SWarner Losh free(const_cast<char *>(var)); 11123054f218SWarner Losh free(const_cast<char *>(val)); 11133054f218SWarner Losh } 11143054f218SWarner Losh 11150285e9b1SAlan Somers 11163054f218SWarner Losh 11173054f218SWarner Losh static void 11183054f218SWarner Losh gensighand(int) 11193054f218SWarner Losh { 1120fcdcaa88SEitan Adler romeo_must_die = 1; 11213054f218SWarner Losh } 11223054f218SWarner Losh 11236d1014a3SAlan Somers /* 1124be685e01SAlan Somers * SIGINFO handler. Will print useful statistics to the syslog or stderr 1125be685e01SAlan Somers * as appropriate 1126be685e01SAlan Somers */ 1127be685e01SAlan Somers static void 1128be685e01SAlan Somers siginfohand(int) 1129be685e01SAlan Somers { 1130be685e01SAlan Somers got_siginfo = 1; 1131be685e01SAlan Somers } 1132be685e01SAlan Somers 1133be685e01SAlan Somers /* 11342e024bc2SAndriy Gapon * Local logging function. Prints to syslog if we're daemonized; stderr 11356d1014a3SAlan Somers * otherwise. 11366d1014a3SAlan Somers */ 11376d1014a3SAlan Somers static void 11386d1014a3SAlan Somers devdlog(int priority, const char* fmt, ...) 11396d1014a3SAlan Somers { 11406d1014a3SAlan Somers va_list argp; 11416d1014a3SAlan Somers 11426d1014a3SAlan Somers va_start(argp, fmt); 11436a2ae0ebSAlan Somers if (no_daemon) 11446d1014a3SAlan Somers vfprintf(stderr, fmt, argp); 11456a2ae0ebSAlan Somers else if ((! quiet_mode) || (priority <= LOG_WARNING)) 11466d1014a3SAlan Somers vsyslog(priority, fmt, argp); 11476d1014a3SAlan Somers va_end(argp); 11486d1014a3SAlan Somers } 11496d1014a3SAlan Somers 11503054f218SWarner Losh static void 11513054f218SWarner Losh usage() 11523054f218SWarner Losh { 11536a2ae0ebSAlan Somers fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n", 1154e1334f93SIan Lepore getprogname()); 11553054f218SWarner Losh exit(1); 11563054f218SWarner Losh } 11573054f218SWarner Losh 11589c180898SWarner Losh static void 11599c180898SWarner Losh check_devd_enabled() 11609c180898SWarner Losh { 11619c180898SWarner Losh int val = 0; 11629c180898SWarner Losh size_t len; 11639c180898SWarner Losh 11649c180898SWarner Losh len = sizeof(val); 116579f92e25SWarner Losh if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0) 11669c180898SWarner Losh errx(1, "devctl sysctl missing from kernel!"); 1167*ee38f2e0SMateusz Guzik if (val == 0) { 1168*ee38f2e0SMateusz Guzik warnx("Setting " SYSCTL " to 1000"); 1169*ee38f2e0SMateusz Guzik val = 1000; 11709c180898SWarner Losh sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)); 11719c180898SWarner Losh } 11729c180898SWarner Losh } 11739c180898SWarner Losh 11743054f218SWarner Losh /* 11753054f218SWarner Losh * main 11763054f218SWarner Losh */ 11773054f218SWarner Losh int 11783054f218SWarner Losh main(int argc, char **argv) 11793054f218SWarner Losh { 11803054f218SWarner Losh int ch; 11813054f218SWarner Losh 11829c180898SWarner Losh check_devd_enabled(); 11836a2ae0ebSAlan Somers while ((ch = getopt(argc, argv, "df:l:nq")) != -1) { 11843054f218SWarner Losh switch (ch) { 11853054f218SWarner Losh case 'd': 11866a2ae0ebSAlan Somers no_daemon = 1; 11873054f218SWarner Losh break; 11888334958aSJoseph Koshy case 'f': 11898334958aSJoseph Koshy configfile = optarg; 11908334958aSJoseph Koshy break; 1191e1334f93SIan Lepore case 'l': 1192e1334f93SIan Lepore max_clients = MAX(1, strtoul(optarg, NULL, 0)); 1193e1334f93SIan Lepore break; 11945a882d77SWarner Losh case 'n': 11956a2ae0ebSAlan Somers daemonize_quick = 1; 11966a2ae0ebSAlan Somers break; 11976a2ae0ebSAlan Somers case 'q': 11986a2ae0ebSAlan Somers quiet_mode = 1; 11995a882d77SWarner Losh break; 12003054f218SWarner Losh default: 12013054f218SWarner Losh usage(); 12023054f218SWarner Losh } 12033054f218SWarner Losh } 12043054f218SWarner Losh 12053054f218SWarner Losh cfg.parse(); 12066a2ae0ebSAlan Somers if (!no_daemon && daemonize_quick) { 12071a0cc6b1SPawel Jakub Dawidek cfg.open_pidfile(); 12083054f218SWarner Losh daemon(0, 0); 12091a0cc6b1SPawel Jakub Dawidek cfg.write_pidfile(); 1210fd6a8f23SWarner Losh } 1211f32a7686SWarner Losh signal(SIGPIPE, SIG_IGN); 12123054f218SWarner Losh signal(SIGHUP, gensighand); 12133054f218SWarner Losh signal(SIGINT, gensighand); 12143054f218SWarner Losh signal(SIGTERM, gensighand); 1215be685e01SAlan Somers signal(SIGINFO, siginfohand); 12163054f218SWarner Losh event_loop(); 12173054f218SWarner Losh return (0); 12183054f218SWarner Losh } 1219