13054f218SWarner Losh /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause-FreeBSD 38a16b7a1SPedro F. Giffuni * 4b8f92ce4SWarner Losh * Copyright (c) 2002-2010 M. Warner Losh. 53054f218SWarner Losh * All rights reserved. 63054f218SWarner Losh * 73054f218SWarner Losh * Redistribution and use in source and binary forms, with or without 83054f218SWarner Losh * modification, are permitted provided that the following conditions 93054f218SWarner Losh * are met: 103054f218SWarner Losh * 1. Redistributions of source code must retain the above copyright 113054f218SWarner Losh * notice, this list of conditions and the following disclaimer. 123054f218SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 133054f218SWarner Losh * notice, this list of conditions and the following disclaimer in the 143054f218SWarner Losh * documentation and/or other materials provided with the distribution. 153054f218SWarner Losh * 163054f218SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 173054f218SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 183054f218SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 193054f218SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 203054f218SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 213054f218SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 223054f218SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 233054f218SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 243054f218SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 253054f218SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 263054f218SWarner Losh * SUCH DAMAGE. 27b8f92ce4SWarner Losh * 28b8f92ce4SWarner Losh * my_system is a variation on lib/libc/stdlib/system.c: 29b8f92ce4SWarner Losh * 30b8f92ce4SWarner Losh * Copyright (c) 1988, 1993 31b8f92ce4SWarner Losh * The Regents of the University of California. All rights reserved. 32b8f92ce4SWarner Losh * 33b8f92ce4SWarner Losh * Redistribution and use in source and binary forms, with or without 34b8f92ce4SWarner Losh * modification, are permitted provided that the following conditions 35b8f92ce4SWarner Losh * are met: 36b8f92ce4SWarner Losh * 1. Redistributions of source code must retain the above copyright 37b8f92ce4SWarner Losh * notice, this list of conditions and the following disclaimer. 38b8f92ce4SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 39b8f92ce4SWarner Losh * notice, this list of conditions and the following disclaimer in the 40b8f92ce4SWarner Losh * documentation and/or other materials provided with the distribution. 41fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 42b8f92ce4SWarner Losh * may be used to endorse or promote products derived from this software 43b8f92ce4SWarner Losh * without specific prior written permission. 44b8f92ce4SWarner Losh * 45b8f92ce4SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 46b8f92ce4SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47b8f92ce4SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48b8f92ce4SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 49b8f92ce4SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50b8f92ce4SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51b8f92ce4SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52b8f92ce4SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53b8f92ce4SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54b8f92ce4SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55b8f92ce4SWarner Losh * SUCH DAMAGE. 563054f218SWarner Losh */ 573054f218SWarner Losh 583054f218SWarner Losh /* 593054f218SWarner Losh * DEVD control daemon. 603054f218SWarner Losh */ 613054f218SWarner Losh 623054f218SWarner Losh // TODO list: 633054f218SWarner Losh // o devd.conf and devd man pages need a lot of help: 64251bedd1SWarner Losh // - devd needs to document the unix domain socket 653054f218SWarner Losh // - devd.conf needs more details on the supported statements. 663054f218SWarner Losh 673054f218SWarner Losh #include <sys/cdefs.h> 683054f218SWarner Losh __FBSDID("$FreeBSD$"); 693054f218SWarner Losh 703054f218SWarner Losh #include <sys/param.h> 71251bedd1SWarner Losh #include <sys/socket.h> 72251bedd1SWarner Losh #include <sys/stat.h> 739c180898SWarner Losh #include <sys/sysctl.h> 74251bedd1SWarner Losh #include <sys/types.h> 75b8f92ce4SWarner Losh #include <sys/wait.h> 76251bedd1SWarner Losh #include <sys/un.h> 773054f218SWarner Losh 78d005340fSEitan Adler #include <cctype> 79d005340fSEitan Adler #include <cerrno> 80d005340fSEitan Adler #include <cstdlib> 81d005340fSEitan Adler #include <cstdio> 82d005340fSEitan Adler #include <csignal> 83d005340fSEitan Adler #include <cstring> 84967ecf4aSAlan Somers #include <cstdarg> 85d005340fSEitan Adler 863054f218SWarner Losh #include <dirent.h> 873054f218SWarner Losh #include <err.h> 883054f218SWarner Losh #include <fcntl.h> 891a0cc6b1SPawel Jakub Dawidek #include <libutil.h> 90b8f92ce4SWarner Losh #include <paths.h> 91e1334f93SIan Lepore #include <poll.h> 925102ef84SWarner Losh #include <regex.h> 936d1014a3SAlan Somers #include <syslog.h> 943054f218SWarner Losh #include <unistd.h> 953054f218SWarner Losh 968b823408SWarner Losh #include <algorithm> 973054f218SWarner Losh #include <map> 983054f218SWarner Losh #include <string> 99251bedd1SWarner Losh #include <list> 1003e9662dcSAlan Somers #include <stdexcept> 1013054f218SWarner Losh #include <vector> 1023054f218SWarner Losh 1036aeeca8eSWarner Losh #include "devd.h" /* C compatible definitions */ 1046aeeca8eSWarner Losh #include "devd.hh" /* C++ class definitions */ 1053054f218SWarner Losh 1069102691eSAlan Somers #define STREAMPIPE "/var/run/devd.pipe" 1079102691eSAlan Somers #define SEQPACKETPIPE "/var/run/devd.seqpacket.pipe" 1083054f218SWarner Losh #define CF "/etc/devd.conf" 109ee38f2e0SMateusz Guzik #define SYSCTL "hw.bus.devctl_queue" 1103054f218SWarner Losh 111b026eddfSAlan Somers /* 112b026eddfSAlan Somers * Since the client socket is nonblocking, we must increase its send buffer to 113b026eddfSAlan Somers * handle brief event storms. On FreeBSD, AF_UNIX sockets don't have a receive 114d9ca81daSAlan Somers * buffer, so the client can't increase the buffersize by itself. 115b026eddfSAlan Somers * 116b026eddfSAlan Somers * For example, when creating a ZFS pool, devd emits one 165 character 117d9ca81daSAlan Somers * resource.fs.zfs.statechange message for each vdev in the pool. The kernel 118d9ca81daSAlan Somers * allocates a 4608B mbuf for each message. Modern technology places a limit of 119d9ca81daSAlan Somers * roughly 450 drives/rack, and it's unlikely that a zpool will ever be larger 120d9ca81daSAlan Somers * than that. 121d9ca81daSAlan Somers * 122d9ca81daSAlan Somers * 450 drives * 165 bytes / drive = 74250B of data in the sockbuf 123d9ca81daSAlan Somers * 450 drives * 4608B / drive = 2073600B of mbufs in the sockbuf 124d9ca81daSAlan Somers * 125d9ca81daSAlan Somers * We can't directly set the sockbuf's mbuf limit, but we can do it indirectly. 126d9ca81daSAlan Somers * The kernel sets it to the minimum of a hard-coded maximum value and sbcc * 127d9ca81daSAlan Somers * kern.ipc.sockbuf_waste_factor, where sbcc is the socket buffer size set by 128d9ca81daSAlan Somers * the user. The default value of kern.ipc.sockbuf_waste_factor is 8. If we 129d9ca81daSAlan Somers * set the bufsize to 256k and use the kern.ipc.sockbuf_waste_factor, then the 130d9ca81daSAlan Somers * kernel will set the mbuf limit to 2MB, which is just large enough for 450 131d9ca81daSAlan Somers * drives. It also happens to be the same as the hardcoded maximum value. 132b026eddfSAlan Somers */ 133d9ca81daSAlan Somers #define CLIENT_BUFSIZE 262144 134b026eddfSAlan Somers 1353054f218SWarner Losh using namespace std; 1363054f218SWarner Losh 1379102691eSAlan Somers typedef struct client { 1389102691eSAlan Somers int fd; 1399102691eSAlan Somers int socktype; 1409102691eSAlan Somers } client_t; 1419102691eSAlan Somers 1423054f218SWarner Losh extern FILE *yyin; 1433054f218SWarner Losh 144842ccec5SWarner Losh static const char notify = '!'; 1458b823408SWarner Losh static const char nomatch = '?'; 1468b823408SWarner Losh static const char attach = '+'; 1478b823408SWarner Losh static const char detach = '-'; 1488b823408SWarner Losh 1491a0cc6b1SPawel Jakub Dawidek static struct pidfh *pfh; 1501a0cc6b1SPawel Jakub Dawidek 1516a2ae0ebSAlan Somers static int no_daemon = 0; 1526a2ae0ebSAlan Somers static int daemonize_quick = 0; 1536a2ae0ebSAlan Somers static int quiet_mode = 0; 154be685e01SAlan Somers static unsigned total_events = 0; 155be685e01SAlan Somers static volatile sig_atomic_t got_siginfo = 0; 156fcdcaa88SEitan Adler static volatile sig_atomic_t romeo_must_die = 0; 1573054f218SWarner Losh 1588334958aSJoseph Koshy static const char *configfile = CF; 1598334958aSJoseph Koshy 1603449b15aSAlan Somers static void devdlog(int priority, const char* message, ...) 1613449b15aSAlan Somers __printflike(2, 3); 1623054f218SWarner Losh static void event_loop(void); 163510a8c88SEitan Adler static void usage(void) __dead2; 1643054f218SWarner Losh 1658b823408SWarner Losh template <class T> void 1668b823408SWarner Losh delete_and_clear(vector<T *> &v) 1678b823408SWarner Losh { 1688b823408SWarner Losh typename vector<T *>::const_iterator i; 1693054f218SWarner Losh 170edee691dSEitan Adler for (i = v.begin(); i != v.end(); ++i) 1718b823408SWarner Losh delete *i; 1728b823408SWarner Losh v.clear(); 1738b823408SWarner Losh } 1748b823408SWarner Losh 1750bc60783SEitan Adler static config cfg; 1763054f218SWarner Losh 1773054f218SWarner Losh event_proc::event_proc() : _prio(-1) 1783054f218SWarner Losh { 1795dfc0f6cSIan Lepore _epsvec.reserve(4); 1803054f218SWarner Losh } 1813054f218SWarner Losh 1823054f218SWarner Losh event_proc::~event_proc() 1833054f218SWarner Losh { 18413f8bb71SAlexander Nedotsukov delete_and_clear(_epsvec); 1853054f218SWarner Losh } 1863054f218SWarner Losh 1873054f218SWarner Losh void 1883054f218SWarner Losh event_proc::add(eps *eps) 1893054f218SWarner Losh { 1903054f218SWarner Losh _epsvec.push_back(eps); 1913054f218SWarner Losh } 1923054f218SWarner Losh 1933054f218SWarner Losh bool 194ef370346SEitan Adler event_proc::matches(config &c) const 1953054f218SWarner Losh { 1963054f218SWarner Losh vector<eps *>::const_iterator i; 1973054f218SWarner Losh 198edee691dSEitan Adler for (i = _epsvec.begin(); i != _epsvec.end(); ++i) 1993054f218SWarner Losh if (!(*i)->do_match(c)) 2003054f218SWarner Losh return (false); 2013054f218SWarner Losh return (true); 2023054f218SWarner Losh } 2033054f218SWarner Losh 2043054f218SWarner Losh bool 205ef370346SEitan Adler event_proc::run(config &c) const 2063054f218SWarner Losh { 2073054f218SWarner Losh vector<eps *>::const_iterator i; 2083054f218SWarner Losh 209edee691dSEitan Adler for (i = _epsvec.begin(); i != _epsvec.end(); ++i) 2103054f218SWarner Losh if (!(*i)->do_action(c)) 2113054f218SWarner Losh return (false); 2123054f218SWarner Losh return (true); 2133054f218SWarner Losh } 2143054f218SWarner Losh 2153054f218SWarner Losh action::action(const char *cmd) 2163054f218SWarner Losh : _cmd(cmd) 2173054f218SWarner Losh { 2183054f218SWarner Losh // nothing 2193054f218SWarner Losh } 2203054f218SWarner Losh 2213054f218SWarner Losh action::~action() 2223054f218SWarner Losh { 2233054f218SWarner Losh // nothing 2243054f218SWarner Losh } 2253054f218SWarner Losh 226b8f92ce4SWarner Losh static int 227b8f92ce4SWarner Losh my_system(const char *command) 228b8f92ce4SWarner Losh { 229b8f92ce4SWarner Losh pid_t pid, savedpid; 230b8f92ce4SWarner Losh int pstat; 231b8f92ce4SWarner Losh struct sigaction ign, intact, quitact; 232b8f92ce4SWarner Losh sigset_t newsigblock, oldsigblock; 233b8f92ce4SWarner Losh 234b8f92ce4SWarner Losh if (!command) /* just checking... */ 235b8f92ce4SWarner Losh return (1); 236b8f92ce4SWarner Losh 237b8f92ce4SWarner Losh /* 238b8f92ce4SWarner Losh * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save 239b8f92ce4SWarner Losh * existing signal dispositions. 240b8f92ce4SWarner Losh */ 241b8f92ce4SWarner Losh ign.sa_handler = SIG_IGN; 242b8f92ce4SWarner Losh ::sigemptyset(&ign.sa_mask); 243b8f92ce4SWarner Losh ign.sa_flags = 0; 244b8f92ce4SWarner Losh ::sigaction(SIGINT, &ign, &intact); 245b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &ign, &quitact); 246b8f92ce4SWarner Losh ::sigemptyset(&newsigblock); 247b8f92ce4SWarner Losh ::sigaddset(&newsigblock, SIGCHLD); 248b8f92ce4SWarner Losh ::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); 249b8f92ce4SWarner Losh switch (pid = ::fork()) { 250b8f92ce4SWarner Losh case -1: /* error */ 251b8f92ce4SWarner Losh break; 252b8f92ce4SWarner Losh case 0: /* child */ 253b8f92ce4SWarner Losh /* 254b8f92ce4SWarner Losh * Restore original signal dispositions and exec the command. 255b8f92ce4SWarner Losh */ 256b8f92ce4SWarner Losh ::sigaction(SIGINT, &intact, NULL); 257b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &quitact, NULL); 258b8f92ce4SWarner Losh ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 259b8f92ce4SWarner Losh /* 260b8f92ce4SWarner Losh * Close the PID file, and all other open descriptors. 261b8f92ce4SWarner Losh * Inherit std{in,out,err} only. 262b8f92ce4SWarner Losh */ 263b8f92ce4SWarner Losh cfg.close_pidfile(); 264b8f92ce4SWarner Losh ::closefrom(3); 265b8f92ce4SWarner Losh ::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); 266b8f92ce4SWarner Losh ::_exit(127); 267b8f92ce4SWarner Losh default: /* parent */ 268b8f92ce4SWarner Losh savedpid = pid; 269b8f92ce4SWarner Losh do { 270b8f92ce4SWarner Losh pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0); 271b8f92ce4SWarner Losh } while (pid == -1 && errno == EINTR); 272b8f92ce4SWarner Losh break; 273b8f92ce4SWarner Losh } 274b8f92ce4SWarner Losh ::sigaction(SIGINT, &intact, NULL); 275b8f92ce4SWarner Losh ::sigaction(SIGQUIT, &quitact, NULL); 276b8f92ce4SWarner Losh ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL); 277b8f92ce4SWarner Losh return (pid == -1 ? -1 : pstat); 278b8f92ce4SWarner Losh } 279b8f92ce4SWarner Losh 2803054f218SWarner Losh bool 2815102ef84SWarner Losh action::do_action(config &c) 2823054f218SWarner Losh { 2835dfc0f6cSIan Lepore string s = c.expand_string(_cmd.c_str()); 2841bb0777eSAndriy Gapon devdlog(LOG_INFO, "Executing '%s'\n", s.c_str()); 285b8f92ce4SWarner Losh my_system(s.c_str()); 2863054f218SWarner Losh return (true); 2873054f218SWarner Losh } 2883054f218SWarner Losh 2895dfc0f6cSIan Lepore match::match(config &c, const char *var, const char *re) : 2905dfc0f6cSIan Lepore _inv(re[0] == '!'), 2915dfc0f6cSIan Lepore _var(var), 2925dfc0f6cSIan Lepore _re(c.expand_string(_inv ? re + 1 : re, "^", "$")) 2933054f218SWarner Losh { 2949cb4a7bdSWarner Losh regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE); 2953054f218SWarner Losh } 2963054f218SWarner Losh 2973054f218SWarner Losh match::~match() 2983054f218SWarner Losh { 2995102ef84SWarner Losh regfree(&_regex); 3003054f218SWarner Losh } 3013054f218SWarner Losh 3023054f218SWarner Losh bool 3035102ef84SWarner Losh match::do_match(config &c) 3043054f218SWarner Losh { 3055bf08422SUlf Lilleengen const string &value = c.get_variable(_var); 3065102ef84SWarner Losh bool retval; 3075102ef84SWarner Losh 3086d1014a3SAlan Somers /* 3096d1014a3SAlan Somers * This function gets called WAY too often to justify calling syslog() 3106d1014a3SAlan Somers * each time, even at LOG_DEBUG. Because if syslogd isn't running, it 3116d1014a3SAlan Somers * can consume excessive amounts of systime inside of connect(). Only 3126d1014a3SAlan Somers * log when we're in -d mode. 3136d1014a3SAlan Somers */ 3146a2ae0ebSAlan Somers if (no_daemon) { 3156d1014a3SAlan Somers devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n", 3160321b694SHiroki Sato _var.c_str(), value.c_str(), _re.c_str(), _inv); 3176d1014a3SAlan Somers } 3188b823408SWarner Losh 3195102ef84SWarner Losh retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0); 3200321b694SHiroki Sato if (_inv == 1) 3210321b694SHiroki Sato retval = (retval == 0) ? 1 : 0; 3220321b694SHiroki Sato 3230285e9b1SAlan Somers return (retval); 3243054f218SWarner Losh } 3253054f218SWarner Losh 326cd70782bSWarner Losh #include <sys/sockio.h> 327cd70782bSWarner Losh #include <net/if.h> 328cd70782bSWarner Losh #include <net/if_media.h> 329cd70782bSWarner Losh 33094d2d4ebSBrooks Davis media::media(config &, const char *var, const char *type) 331cd70782bSWarner Losh : _var(var), _type(-1) 332cd70782bSWarner Losh { 333cd70782bSWarner Losh static struct ifmedia_description media_types[] = { 334cd70782bSWarner Losh { IFM_ETHER, "Ethernet" }, 335cd70782bSWarner Losh { IFM_IEEE80211, "802.11" }, 336cd70782bSWarner Losh { IFM_ATM, "ATM" }, 337cd70782bSWarner Losh { -1, "unknown" }, 338cd70782bSWarner Losh { 0, NULL }, 339cd70782bSWarner Losh }; 340edee691dSEitan Adler for (int i = 0; media_types[i].ifmt_string != NULL; ++i) 341cd70782bSWarner Losh if (strcasecmp(type, media_types[i].ifmt_string) == 0) { 342cd70782bSWarner Losh _type = media_types[i].ifmt_word; 343cd70782bSWarner Losh break; 344cd70782bSWarner Losh } 345cd70782bSWarner Losh } 346cd70782bSWarner Losh 347cd70782bSWarner Losh media::~media() 348cd70782bSWarner Losh { 349cd70782bSWarner Losh } 350cd70782bSWarner Losh 351cd70782bSWarner Losh bool 352cd70782bSWarner Losh media::do_match(config &c) 353cd70782bSWarner Losh { 35409c47e76SWarner Losh string value; 355cd70782bSWarner Losh struct ifmediareq ifmr; 356cd70782bSWarner Losh bool retval; 357cd70782bSWarner Losh int s; 358cd70782bSWarner Losh 35909c47e76SWarner Losh // Since we can be called from both a device attach/detach 36009c47e76SWarner Losh // context where device-name is defined and what we want, 36109c47e76SWarner Losh // as well as from a link status context, where subsystem is 36209c47e76SWarner Losh // the name of interest, first try device-name and fall back 36309c47e76SWarner Losh // to subsystem if none exists. 36409c47e76SWarner Losh value = c.get_variable("device-name"); 365a6393aa1SEitan Adler if (value.empty()) 366de02eec8SWarner Losh value = c.get_variable("subsystem"); 3676d1014a3SAlan Somers devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n", 368cd70782bSWarner Losh value.c_str(), _type); 369cd70782bSWarner Losh 370cd70782bSWarner Losh retval = false; 371cd70782bSWarner Losh 372cd70782bSWarner Losh s = socket(PF_INET, SOCK_DGRAM, 0); 373cd70782bSWarner Losh if (s >= 0) { 374cd70782bSWarner Losh memset(&ifmr, 0, sizeof(ifmr)); 375daa0d9ddSAlan Somers strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name)); 376cd70782bSWarner Losh 377cd70782bSWarner Losh if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 && 378cd70782bSWarner Losh ifmr.ifm_status & IFM_AVALID) { 3796d1014a3SAlan Somers devdlog(LOG_DEBUG, "%s has media type 0x%x\n", 380cd70782bSWarner Losh value.c_str(), IFM_TYPE(ifmr.ifm_active)); 381cd70782bSWarner Losh retval = (IFM_TYPE(ifmr.ifm_active) == _type); 382cd70782bSWarner Losh } else if (_type == -1) { 3836d1014a3SAlan Somers devdlog(LOG_DEBUG, "%s has unknown media type\n", 384cd70782bSWarner Losh value.c_str()); 385cd70782bSWarner Losh retval = true; 386cd70782bSWarner Losh } 387cd70782bSWarner Losh close(s); 388cd70782bSWarner Losh } 389cd70782bSWarner Losh 3900285e9b1SAlan Somers return (retval); 391cd70782bSWarner Losh } 392cd70782bSWarner Losh 3933054f218SWarner Losh const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_"; 3943054f218SWarner Losh const string var_list::nothing = ""; 3953054f218SWarner Losh 3963054f218SWarner Losh const string & 3973054f218SWarner Losh var_list::get_variable(const string &var) const 3983054f218SWarner Losh { 3993054f218SWarner Losh map<string, string>::const_iterator i; 4003054f218SWarner Losh 4013054f218SWarner Losh i = _vars.find(var); 4023054f218SWarner Losh if (i == _vars.end()) 4038b823408SWarner Losh return (var_list::bogus); 4043054f218SWarner Losh return (i->second); 4053054f218SWarner Losh } 4063054f218SWarner Losh 4073054f218SWarner Losh bool 4083054f218SWarner Losh var_list::is_set(const string &var) const 4093054f218SWarner Losh { 4103054f218SWarner Losh return (_vars.find(var) != _vars.end()); 4113054f218SWarner Losh } 4123054f218SWarner Losh 413192af3b7SWarner Losh /** fix_value 414192af3b7SWarner Losh * 415192af3b7SWarner Losh * Removes quoted characters that have made it this far. \" are 416192af3b7SWarner Losh * converted to ". For all other characters, both \ and following 417192af3b7SWarner Losh * character. So the string 'fre\:\"' is translated to 'fred\:"'. 418192af3b7SWarner Losh */ 419416823b1SWarner Losh std::string 420192af3b7SWarner Losh var_list::fix_value(const std::string &val) const 421192af3b7SWarner Losh { 422416823b1SWarner Losh std::string rv(val); 423416823b1SWarner Losh std::string::size_type pos(0); 424192af3b7SWarner Losh 425416823b1SWarner Losh while ((pos = rv.find("\\\"", pos)) != rv.npos) { 426416823b1SWarner Losh rv.erase(pos, 1); 427192af3b7SWarner Losh } 428416823b1SWarner Losh return (rv); 429192af3b7SWarner Losh } 430192af3b7SWarner Losh 4313054f218SWarner Losh void 4323054f218SWarner Losh var_list::set_variable(const string &var, const string &val) 4333054f218SWarner Losh { 4346d1014a3SAlan Somers /* 4356d1014a3SAlan Somers * This function gets called WAY too often to justify calling syslog() 4366d1014a3SAlan Somers * each time, even at LOG_DEBUG. Because if syslogd isn't running, it 4376d1014a3SAlan Somers * can consume excessive amounts of systime inside of connect(). Only 4386d1014a3SAlan Somers * log when we're in -d mode. 4396d1014a3SAlan Somers */ 440192af3b7SWarner Losh _vars[var] = fix_value(val); 4416a2ae0ebSAlan Somers if (no_daemon) 4426d1014a3SAlan Somers devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str()); 4433054f218SWarner Losh } 4443054f218SWarner Losh 4453054f218SWarner Losh void 4463054f218SWarner Losh config::reset(void) 4473054f218SWarner Losh { 4483054f218SWarner Losh _dir_list.clear(); 4498b823408SWarner Losh delete_and_clear(_var_list_table); 4508b823408SWarner Losh delete_and_clear(_attach_list); 4518b823408SWarner Losh delete_and_clear(_detach_list); 4528b823408SWarner Losh delete_and_clear(_nomatch_list); 453842ccec5SWarner Losh delete_and_clear(_notify_list); 4543054f218SWarner Losh } 4553054f218SWarner Losh 4563054f218SWarner Losh void 4573054f218SWarner Losh config::parse_one_file(const char *fn) 4583054f218SWarner Losh { 4596d1014a3SAlan Somers devdlog(LOG_DEBUG, "Parsing %s\n", fn); 4603054f218SWarner Losh yyin = fopen(fn, "r"); 4613054f218SWarner Losh if (yyin == NULL) 4623054f218SWarner Losh err(1, "Cannot open config file %s", fn); 4634a3050fcSMaxim Konovalov lineno = 1; 4643054f218SWarner Losh if (yyparse() != 0) 4653054f218SWarner Losh errx(1, "Cannot parse %s at line %d", fn, lineno); 4663054f218SWarner Losh fclose(yyin); 4673054f218SWarner Losh } 4683054f218SWarner Losh 4693054f218SWarner Losh void 4703054f218SWarner Losh config::parse_files_in_dir(const char *dirname) 4713054f218SWarner Losh { 4723054f218SWarner Losh DIR *dirp; 4733054f218SWarner Losh struct dirent *dp; 4743054f218SWarner Losh char path[PATH_MAX]; 4753054f218SWarner Losh 4766d1014a3SAlan Somers devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname); 4773054f218SWarner Losh dirp = opendir(dirname); 4783054f218SWarner Losh if (dirp == NULL) 4793054f218SWarner Losh return; 4803054f218SWarner Losh readdir(dirp); /* Skip . */ 4813054f218SWarner Losh readdir(dirp); /* Skip .. */ 4823054f218SWarner Losh while ((dp = readdir(dirp)) != NULL) { 4833054f218SWarner Losh if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) { 4843054f218SWarner Losh snprintf(path, sizeof(path), "%s/%s", 4853054f218SWarner Losh dirname, dp->d_name); 4863054f218SWarner Losh parse_one_file(path); 4873054f218SWarner Losh } 4883054f218SWarner Losh } 4895983e891SKevin Lo closedir(dirp); 4903054f218SWarner Losh } 4913054f218SWarner Losh 4928b823408SWarner Losh class epv_greater { 4938b823408SWarner Losh public: 494ef370346SEitan Adler int operator()(event_proc *const&l1, event_proc *const&l2) const 4958b823408SWarner Losh { 4968b823408SWarner Losh return (l1->get_priority() > l2->get_priority()); 4978b823408SWarner Losh } 4988b823408SWarner Losh }; 4998b823408SWarner Losh 5008b823408SWarner Losh void 5018b823408SWarner Losh config::sort_vector(vector<event_proc *> &v) 5028b823408SWarner Losh { 5030c04d185SDimitry Andric stable_sort(v.begin(), v.end(), epv_greater()); 5048b823408SWarner Losh } 5058b823408SWarner Losh 5063054f218SWarner Losh void 5073054f218SWarner Losh config::parse(void) 5083054f218SWarner Losh { 5093054f218SWarner Losh vector<string>::const_iterator i; 5103054f218SWarner Losh 5118334958aSJoseph Koshy parse_one_file(configfile); 512edee691dSEitan Adler for (i = _dir_list.begin(); i != _dir_list.end(); ++i) 5133054f218SWarner Losh parse_files_in_dir((*i).c_str()); 5148b823408SWarner Losh sort_vector(_attach_list); 5158b823408SWarner Losh sort_vector(_detach_list); 5168b823408SWarner Losh sort_vector(_nomatch_list); 517842ccec5SWarner Losh sort_vector(_notify_list); 5183054f218SWarner Losh } 5193054f218SWarner Losh 5203054f218SWarner Losh void 5211a0cc6b1SPawel Jakub Dawidek config::open_pidfile() 5223054f218SWarner Losh { 5231a0cc6b1SPawel Jakub Dawidek pid_t otherpid; 5243054f218SWarner Losh 5255a3b1a3dSEitan Adler if (_pidfile.empty()) 5263054f218SWarner Losh return; 5271a0cc6b1SPawel Jakub Dawidek pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid); 5281a0cc6b1SPawel Jakub Dawidek if (pfh == NULL) { 5291a0cc6b1SPawel Jakub Dawidek if (errno == EEXIST) 5301a0cc6b1SPawel Jakub Dawidek errx(1, "devd already running, pid: %d", (int)otherpid); 5311a0cc6b1SPawel Jakub Dawidek warn("cannot open pid file"); 5321a0cc6b1SPawel Jakub Dawidek } 5331a0cc6b1SPawel Jakub Dawidek } 5341a0cc6b1SPawel Jakub Dawidek 5351a0cc6b1SPawel Jakub Dawidek void 5361a0cc6b1SPawel Jakub Dawidek config::write_pidfile() 5371a0cc6b1SPawel Jakub Dawidek { 5381a0cc6b1SPawel Jakub Dawidek 5391a0cc6b1SPawel Jakub Dawidek pidfile_write(pfh); 5401a0cc6b1SPawel Jakub Dawidek } 5411a0cc6b1SPawel Jakub Dawidek 5421a0cc6b1SPawel Jakub Dawidek void 543b8f92ce4SWarner Losh config::close_pidfile() 544b8f92ce4SWarner Losh { 545b8f92ce4SWarner Losh 546b8f92ce4SWarner Losh pidfile_close(pfh); 547b8f92ce4SWarner Losh } 548b8f92ce4SWarner Losh 549b8f92ce4SWarner Losh void 5501a0cc6b1SPawel Jakub Dawidek config::remove_pidfile() 5511a0cc6b1SPawel Jakub Dawidek { 5521a0cc6b1SPawel Jakub Dawidek 5531a0cc6b1SPawel Jakub Dawidek pidfile_remove(pfh); 5543054f218SWarner Losh } 5553054f218SWarner Losh 5563054f218SWarner Losh void 5573054f218SWarner Losh config::add_attach(int prio, event_proc *p) 5583054f218SWarner Losh { 5593054f218SWarner Losh p->set_priority(prio); 5603054f218SWarner Losh _attach_list.push_back(p); 5613054f218SWarner Losh } 5623054f218SWarner Losh 5633054f218SWarner Losh void 5643054f218SWarner Losh config::add_detach(int prio, event_proc *p) 5653054f218SWarner Losh { 5663054f218SWarner Losh p->set_priority(prio); 5673054f218SWarner Losh _detach_list.push_back(p); 5683054f218SWarner Losh } 5693054f218SWarner Losh 5703054f218SWarner Losh void 5713054f218SWarner Losh config::add_directory(const char *dir) 5723054f218SWarner Losh { 5733054f218SWarner Losh _dir_list.push_back(string(dir)); 5743054f218SWarner Losh } 5753054f218SWarner Losh 5763054f218SWarner Losh void 5773054f218SWarner Losh config::add_nomatch(int prio, event_proc *p) 5783054f218SWarner Losh { 5793054f218SWarner Losh p->set_priority(prio); 5803054f218SWarner Losh _nomatch_list.push_back(p); 5813054f218SWarner Losh } 5823054f218SWarner Losh 5833054f218SWarner Losh void 584842ccec5SWarner Losh config::add_notify(int prio, event_proc *p) 585842ccec5SWarner Losh { 586842ccec5SWarner Losh p->set_priority(prio); 587842ccec5SWarner Losh _notify_list.push_back(p); 588842ccec5SWarner Losh } 589842ccec5SWarner Losh 590842ccec5SWarner Losh void 5913054f218SWarner Losh config::set_pidfile(const char *fn) 5923054f218SWarner Losh { 5935a3b1a3dSEitan Adler _pidfile = fn; 5943054f218SWarner Losh } 5953054f218SWarner Losh 5963054f218SWarner Losh void 5973054f218SWarner Losh config::push_var_table() 5983054f218SWarner Losh { 5993054f218SWarner Losh var_list *vl; 6003054f218SWarner Losh 6013054f218SWarner Losh vl = new var_list(); 6023054f218SWarner Losh _var_list_table.push_back(vl); 6036d1014a3SAlan Somers devdlog(LOG_DEBUG, "Pushing table\n"); 6043054f218SWarner Losh } 6053054f218SWarner Losh 6063054f218SWarner Losh void 6073054f218SWarner Losh config::pop_var_table() 6083054f218SWarner Losh { 6093054f218SWarner Losh delete _var_list_table.back(); 6103054f218SWarner Losh _var_list_table.pop_back(); 6116d1014a3SAlan Somers devdlog(LOG_DEBUG, "Popping table\n"); 6123054f218SWarner Losh } 6133054f218SWarner Losh 6143054f218SWarner Losh void 6153054f218SWarner Losh config::set_variable(const char *var, const char *val) 6163054f218SWarner Losh { 6173054f218SWarner Losh _var_list_table.back()->set_variable(var, val); 6183054f218SWarner Losh } 6193054f218SWarner Losh 6203054f218SWarner Losh const string & 6213054f218SWarner Losh config::get_variable(const string &var) 6223054f218SWarner Losh { 6233054f218SWarner Losh vector<var_list *>::reverse_iterator i; 6243054f218SWarner Losh 625edee691dSEitan Adler for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) { 6263054f218SWarner Losh if ((*i)->is_set(var)) 6278b823408SWarner Losh return ((*i)->get_variable(var)); 6283054f218SWarner Losh } 6293054f218SWarner Losh return (var_list::nothing); 6303054f218SWarner Losh } 6313054f218SWarner Losh 6328b823408SWarner Losh bool 633ef370346SEitan Adler config::is_id_char(char ch) const 6348b823408SWarner Losh { 6358b823408SWarner Losh return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' || 6368b823408SWarner Losh ch == '-')); 6378b823408SWarner Losh } 6388b823408SWarner Losh 63954aa4076SWarner Losh string 64054aa4076SWarner Losh config::shell_quote(const string &s) 64154aa4076SWarner Losh { 64254aa4076SWarner Losh string buffer; 64395cbefb3SWarner Losh const char *cs, *ce; 64495cbefb3SWarner Losh char c; 64554aa4076SWarner Losh 64654aa4076SWarner Losh /* 64754aa4076SWarner Losh * Enclose the string in $' ' with escapes for ' and / characters making 64854aa4076SWarner Losh * it one argument and ensuring the shell won't be affected by its 64954aa4076SWarner Losh * usual list of candidates. 65054aa4076SWarner Losh */ 65154aa4076SWarner Losh buffer.reserve(s.length() * 3 / 2); 65254aa4076SWarner Losh buffer += '$'; 65354aa4076SWarner Losh buffer += '\''; 65495cbefb3SWarner Losh cs = s.c_str(); 65595cbefb3SWarner Losh ce = cs + strlen(cs); 65695cbefb3SWarner Losh for (; cs < ce; cs++) { 65795cbefb3SWarner Losh c = *cs; 65854aa4076SWarner Losh if (c == '\'' || c == '\\') { 65954aa4076SWarner Losh buffer += '\\'; 66054aa4076SWarner Losh } 66154aa4076SWarner Losh buffer += c; 66254aa4076SWarner Losh } 66354aa4076SWarner Losh buffer += '\''; 66454aa4076SWarner Losh 66554aa4076SWarner Losh return buffer; 66654aa4076SWarner Losh } 66754aa4076SWarner Losh 6685102ef84SWarner Losh void 6696cabd675SWarner Losh config::expand_one(const char *&src, string &dst) 6703054f218SWarner Losh { 6715102ef84SWarner Losh int count; 6725bf08422SUlf Lilleengen string buffer; 6735102ef84SWarner Losh 6748b823408SWarner Losh src++; 6755102ef84SWarner Losh // $$ -> $ 6765102ef84SWarner Losh if (*src == '$') { 6777d9e9c60SEitan Adler dst += *src++; 6785102ef84SWarner Losh return; 6795102ef84SWarner Losh } 6805102ef84SWarner Losh 6815102ef84SWarner Losh // $(foo) -> $(foo) 68254aa4076SWarner Losh // This is the escape hatch for passing down shell subcommands 6835102ef84SWarner Losh if (*src == '(') { 6847d9e9c60SEitan Adler dst += '$'; 6855102ef84SWarner Losh count = 1; 6866cabd675SWarner Losh /* If the string ends before ) is matched , return. */ 6876cabd675SWarner Losh while (count > 0 && *src) { 6885102ef84SWarner Losh if (*src == ')') 6895102ef84SWarner Losh count--; 6905102ef84SWarner Losh else if (*src == '(') 6915102ef84SWarner Losh count++; 6927d9e9c60SEitan Adler dst += *src++; 6935102ef84SWarner Losh } 6945102ef84SWarner Losh return; 6955102ef84SWarner Losh } 6965102ef84SWarner Losh 697b3d32292SWarner Losh // $[^-A-Za-z_*] -> $\1 698b3d32292SWarner Losh if (!isalpha(*src) && *src != '_' && *src != '-' && *src != '*') { 6997d9e9c60SEitan Adler dst += '$'; 7007d9e9c60SEitan Adler dst += *src++; 7015102ef84SWarner Losh return; 7025102ef84SWarner Losh } 7035102ef84SWarner Losh 7045102ef84SWarner Losh // $var -> replace with value 7056cabd675SWarner Losh do { 7067d9e9c60SEitan Adler buffer += *src++; 70766ff6821SWarner Losh } while (is_id_char(*src)); 70854aa4076SWarner Losh dst.append(shell_quote(get_variable(buffer))); 7095102ef84SWarner Losh } 7105102ef84SWarner Losh 7115102ef84SWarner Losh const string 7125dfc0f6cSIan Lepore config::expand_string(const char *src, const char *prepend, const char *append) 7135102ef84SWarner Losh { 7145dfc0f6cSIan Lepore const char *var_at; 7156cabd675SWarner Losh string dst; 7165102ef84SWarner Losh 7175dfc0f6cSIan Lepore /* 7185dfc0f6cSIan Lepore * 128 bytes is enough for 2427 of 2438 expansions that happen 7195dfc0f6cSIan Lepore * while parsing config files, as tested on 2013-01-30. 7205dfc0f6cSIan Lepore */ 7215dfc0f6cSIan Lepore dst.reserve(128); 7225dfc0f6cSIan Lepore 7235dfc0f6cSIan Lepore if (prepend != NULL) 7245dfc0f6cSIan Lepore dst = prepend; 7255dfc0f6cSIan Lepore 7265dfc0f6cSIan Lepore for (;;) { 7275dfc0f6cSIan Lepore var_at = strchr(src, '$'); 7285dfc0f6cSIan Lepore if (var_at == NULL) { 7295dfc0f6cSIan Lepore dst.append(src); 7305dfc0f6cSIan Lepore break; 7315102ef84SWarner Losh } 7325dfc0f6cSIan Lepore dst.append(src, var_at - src); 7335dfc0f6cSIan Lepore src = var_at; 7345dfc0f6cSIan Lepore expand_one(src, dst); 7355dfc0f6cSIan Lepore } 7365dfc0f6cSIan Lepore 7375dfc0f6cSIan Lepore if (append != NULL) 7385dfc0f6cSIan Lepore dst.append(append); 7395102ef84SWarner Losh 7406cabd675SWarner Losh return (dst); 7413054f218SWarner Losh } 7423054f218SWarner Losh 7438b823408SWarner Losh bool 74411fd1366SEitan Adler config::chop_var(char *&buffer, char *&lhs, char *&rhs) const 7458b823408SWarner Losh { 7468b823408SWarner Losh char *walker; 7478b823408SWarner Losh 7488b823408SWarner Losh if (*buffer == '\0') 7498b823408SWarner Losh return (false); 7508b823408SWarner Losh walker = lhs = buffer; 7518b823408SWarner Losh while (is_id_char(*walker)) 7528b823408SWarner Losh walker++; 7538b823408SWarner Losh if (*walker != '=') 7548b823408SWarner Losh return (false); 7558b823408SWarner Losh walker++; // skip = 7568b823408SWarner Losh if (*walker == '"') { 7578b823408SWarner Losh walker++; // skip " 7588b823408SWarner Losh rhs = walker; 759192af3b7SWarner Losh while (*walker && *walker != '"') { 760192af3b7SWarner Losh // Skip \" ... We leave it in the string and strip the \ later. 761192af3b7SWarner Losh // due to the super simplistic parser that we have here. 762192af3b7SWarner Losh if (*walker == '\\' && walker[1] == '"') 7638b823408SWarner Losh walker++; 764192af3b7SWarner Losh walker++; 765192af3b7SWarner Losh } 7668b823408SWarner Losh if (*walker != '"') 7678b823408SWarner Losh return (false); 7688b823408SWarner Losh rhs[-2] = '\0'; 7698b823408SWarner Losh *walker++ = '\0'; 7708b823408SWarner Losh } else { 7718b823408SWarner Losh rhs = walker; 7728b823408SWarner Losh while (*walker && !isspace(*walker)) 7738b823408SWarner Losh walker++; 7748b823408SWarner Losh if (*walker != '\0') 7758b823408SWarner Losh *walker++ = '\0'; 7768b823408SWarner Losh rhs[-1] = '\0'; 7778b823408SWarner Losh } 778b1ee04b1SWarner Losh while (isspace(*walker)) 779b1ee04b1SWarner Losh walker++; 7808b823408SWarner Losh buffer = walker; 7818b823408SWarner Losh return (true); 7828b823408SWarner Losh } 7838b823408SWarner Losh 7848b823408SWarner Losh 7858b823408SWarner Losh char * 7868b823408SWarner Losh config::set_vars(char *buffer) 7878b823408SWarner Losh { 7888b823408SWarner Losh char *lhs; 7898b823408SWarner Losh char *rhs; 7908b823408SWarner Losh 7918b823408SWarner Losh while (1) { 7928b823408SWarner Losh if (!chop_var(buffer, lhs, rhs)) 7938b823408SWarner Losh break; 7948b823408SWarner Losh set_variable(lhs, rhs); 7958b823408SWarner Losh } 7968b823408SWarner Losh return (buffer); 7978b823408SWarner Losh } 7988b823408SWarner Losh 7998b823408SWarner Losh void 8008b823408SWarner Losh config::find_and_execute(char type) 8018b823408SWarner Losh { 8028b823408SWarner Losh vector<event_proc *> *l; 8038b823408SWarner Losh vector<event_proc *>::const_iterator i; 80494d2d4ebSBrooks Davis const char *s; 8058b823408SWarner Losh 8068b823408SWarner Losh switch (type) { 8078b823408SWarner Losh default: 8088b823408SWarner Losh return; 809842ccec5SWarner Losh case notify: 810842ccec5SWarner Losh l = &_notify_list; 811842ccec5SWarner Losh s = "notify"; 812842ccec5SWarner Losh break; 8138b823408SWarner Losh case nomatch: 8148b823408SWarner Losh l = &_nomatch_list; 8158b823408SWarner Losh s = "nomatch"; 8168b823408SWarner Losh break; 8178b823408SWarner Losh case attach: 8188b823408SWarner Losh l = &_attach_list; 8198b823408SWarner Losh s = "attach"; 8208b823408SWarner Losh break; 8218b823408SWarner Losh case detach: 8228b823408SWarner Losh l = &_detach_list; 8238b823408SWarner Losh s = "detach"; 8248b823408SWarner Losh break; 8258b823408SWarner Losh } 8266d1014a3SAlan Somers devdlog(LOG_DEBUG, "Processing %s event\n", s); 827edee691dSEitan Adler for (i = l->begin(); i != l->end(); ++i) { 8288b823408SWarner Losh if ((*i)->matches(*this)) { 8298b823408SWarner Losh (*i)->run(*this); 8308b823408SWarner Losh break; 8318b823408SWarner Losh } 8328b823408SWarner Losh } 8338b823408SWarner Losh 8348b823408SWarner Losh } 8358b823408SWarner Losh 8360285e9b1SAlan Somers 8373054f218SWarner Losh static void 8388b823408SWarner Losh process_event(char *buffer) 8393054f218SWarner Losh { 8403054f218SWarner Losh char type; 8413054f218SWarner Losh char *sp; 8423b336ac3SWarner Losh struct timeval tv; 8433b336ac3SWarner Losh char *timestr; 8443054f218SWarner Losh 8458b823408SWarner Losh sp = buffer + 1; 846f0038a8eSAlan Somers devdlog(LOG_INFO, "Processing event '%s'\n", buffer); 8473054f218SWarner Losh type = *buffer++; 8488b823408SWarner Losh cfg.push_var_table(); 849b3d32292SWarner Losh // $* is the entire line 850b3d32292SWarner Losh cfg.set_variable("*", buffer - 1); 851b3d32292SWarner Losh // $_ is the entire line without the initial character 852535595dbSWarner Losh cfg.set_variable("_", buffer); 8533b336ac3SWarner Losh 8543b336ac3SWarner Losh // Save the time this happened (as approximated by when we got 8553b336ac3SWarner Losh // around to processing it). 8563b336ac3SWarner Losh gettimeofday(&tv, NULL); 8573b336ac3SWarner Losh asprintf(×tr, "%jd.%06ld", (uintmax_t)tv.tv_sec, tv.tv_usec); 8583b336ac3SWarner Losh cfg.set_variable("timestamp", timestr); 8593b336ac3SWarner Losh free(timestr); 8603b336ac3SWarner Losh 8613b336ac3SWarner Losh // Match doesn't have a device, and the format is a little 8628b823408SWarner Losh // different, so handle it separately. 863842ccec5SWarner Losh switch (type) { 864842ccec5SWarner Losh case notify: 865b3d32292SWarner Losh //! (k=v)* 866842ccec5SWarner Losh sp = cfg.set_vars(sp); 867842ccec5SWarner Losh break; 868842ccec5SWarner Losh case nomatch: 869528d3f79SWarner Losh //? at location pnp-info on bus 870528d3f79SWarner Losh sp = strchr(sp, ' '); 871528d3f79SWarner Losh if (sp == NULL) 872528d3f79SWarner Losh return; /* Can't happen? */ 873528d3f79SWarner Losh *sp++ = '\0'; 8749b6cb520SWarner Losh while (isspace(*sp)) 8759b6cb520SWarner Losh sp++; 876842ccec5SWarner Losh if (strncmp(sp, "at ", 3) == 0) 877842ccec5SWarner Losh sp += 3; 878842ccec5SWarner Losh sp = cfg.set_vars(sp); 8799b6cb520SWarner Losh while (isspace(*sp)) 8809b6cb520SWarner Losh sp++; 881842ccec5SWarner Losh if (strncmp(sp, "on ", 3) == 0) 882842ccec5SWarner Losh cfg.set_variable("bus", sp + 3); 883842ccec5SWarner Losh break; 884842ccec5SWarner Losh case attach: /*FALLTHROUGH*/ 885842ccec5SWarner Losh case detach: 8868b823408SWarner Losh sp = strchr(sp, ' '); 8873054f218SWarner Losh if (sp == NULL) 8883054f218SWarner Losh return; /* Can't happen? */ 8898b823408SWarner Losh *sp++ = '\0'; 8908b823408SWarner Losh cfg.set_variable("device-name", buffer); 8919b6cb520SWarner Losh while (isspace(*sp)) 8929b6cb520SWarner Losh sp++; 8938b823408SWarner Losh if (strncmp(sp, "at ", 3) == 0) 8948b823408SWarner Losh sp += 3; 8958b823408SWarner Losh sp = cfg.set_vars(sp); 8969b6cb520SWarner Losh while (isspace(*sp)) 8979b6cb520SWarner Losh sp++; 8988b823408SWarner Losh if (strncmp(sp, "on ", 3) == 0) 8998b823408SWarner Losh cfg.set_variable("bus", sp + 3); 900842ccec5SWarner Losh break; 901b1ee04b1SWarner Losh } 902b1ee04b1SWarner Losh 9038b823408SWarner Losh cfg.find_and_execute(type); 9048b823408SWarner Losh cfg.pop_var_table(); 9053054f218SWarner Losh } 9063054f218SWarner Losh 907*514e2dbfSEitan Adler static int 9089102691eSAlan Somers create_socket(const char *name, int socktype) 909251bedd1SWarner Losh { 910251bedd1SWarner Losh int fd, slen; 911251bedd1SWarner Losh struct sockaddr_un sun; 912251bedd1SWarner Losh 9139102691eSAlan Somers if ((fd = socket(PF_LOCAL, socktype, 0)) < 0) 914251bedd1SWarner Losh err(1, "socket"); 915251bedd1SWarner Losh bzero(&sun, sizeof(sun)); 916251bedd1SWarner Losh sun.sun_family = AF_UNIX; 917251bedd1SWarner Losh strlcpy(sun.sun_path, name, sizeof(sun.sun_path)); 918251bedd1SWarner Losh slen = SUN_LEN(&sun); 919251bedd1SWarner Losh unlink(name); 920e60fa014SJoe Marcus Clarke if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) 921e60fa014SJoe Marcus Clarke err(1, "fcntl"); 9225d586d29SDimitry Andric if (::bind(fd, (struct sockaddr *) & sun, slen) < 0) 923251bedd1SWarner Losh err(1, "bind"); 924251bedd1SWarner Losh listen(fd, 4); 925daa0d9ddSAlan Somers if (chown(name, 0, 0)) /* XXX - root.wheel */ 926daa0d9ddSAlan Somers err(1, "chown"); 927daa0d9ddSAlan Somers if (chmod(name, 0666)) 928daa0d9ddSAlan Somers err(1, "chmod"); 929251bedd1SWarner Losh return (fd); 930251bedd1SWarner Losh } 931251bedd1SWarner Losh 9320bc60783SEitan Adler static unsigned int max_clients = 10; /* Default, can be overridden on cmdline. */ 9330bc60783SEitan Adler static unsigned int num_clients; 9349102691eSAlan Somers 9350bc60783SEitan Adler static list<client_t> clients; 936251bedd1SWarner Losh 937*514e2dbfSEitan Adler static void 938251bedd1SWarner Losh notify_clients(const char *data, int len) 939251bedd1SWarner Losh { 9409102691eSAlan Somers list<client_t>::iterator i; 941251bedd1SWarner Losh 942e1334f93SIan Lepore /* 943e1334f93SIan Lepore * Deliver the data to all clients. Throw clients overboard at the 944e1334f93SIan Lepore * first sign of trouble. This reaps clients who've died or closed 945e1334f93SIan Lepore * their sockets, and also clients who are alive but failing to keep up 946e1334f93SIan Lepore * (or who are maliciously not reading, to consume buffer space in 947e1334f93SIan Lepore * kernel memory or tie up the limited number of available connections). 948e1334f93SIan Lepore */ 949e1334f93SIan Lepore for (i = clients.begin(); i != clients.end(); ) { 9509102691eSAlan Somers int flags; 9519102691eSAlan Somers if (i->socktype == SOCK_SEQPACKET) 9529102691eSAlan Somers flags = MSG_EOR; 9539102691eSAlan Somers else 9549102691eSAlan Somers flags = 0; 9559102691eSAlan Somers 9569102691eSAlan Somers if (send(i->fd, data, len, flags) != len) { 957e1334f93SIan Lepore --num_clients; 9589102691eSAlan Somers close(i->fd); 959e1334f93SIan Lepore i = clients.erase(i); 9609102691eSAlan Somers devdlog(LOG_WARNING, "notify_clients: send() failed; " 9616d1014a3SAlan Somers "dropping unresponsive client\n"); 962e1334f93SIan Lepore } else 963e1334f93SIan Lepore ++i; 964251bedd1SWarner Losh } 965251bedd1SWarner Losh } 966251bedd1SWarner Losh 967*514e2dbfSEitan Adler static void 968e1334f93SIan Lepore check_clients(void) 969e1334f93SIan Lepore { 970e1334f93SIan Lepore int s; 971e1334f93SIan Lepore struct pollfd pfd; 9729102691eSAlan Somers list<client_t>::iterator i; 973e1334f93SIan Lepore 974e1334f93SIan Lepore /* 975e1334f93SIan Lepore * Check all existing clients to see if any of them have disappeared. 976e1334f93SIan Lepore * Normally we reap clients when we get an error trying to send them an 977e1334f93SIan Lepore * event. This check eliminates the problem of an ever-growing list of 978e1334f93SIan Lepore * zombie clients because we're never writing to them on a system 979e1334f93SIan Lepore * without frequent device-change activity. 980e1334f93SIan Lepore */ 981e1334f93SIan Lepore pfd.events = 0; 982e1334f93SIan Lepore for (i = clients.begin(); i != clients.end(); ) { 9839102691eSAlan Somers pfd.fd = i->fd; 984e1334f93SIan Lepore s = poll(&pfd, 1, 0); 985e1334f93SIan Lepore if ((s < 0 && s != EINTR ) || 986e1334f93SIan Lepore (s > 0 && (pfd.revents & POLLHUP))) { 987e1334f93SIan Lepore --num_clients; 9889102691eSAlan Somers close(i->fd); 989e1334f93SIan Lepore i = clients.erase(i); 9906d1014a3SAlan Somers devdlog(LOG_NOTICE, "check_clients: " 9916d1014a3SAlan Somers "dropping disconnected client\n"); 992e1334f93SIan Lepore } else 993e1334f93SIan Lepore ++i; 994e1334f93SIan Lepore } 995251bedd1SWarner Losh } 996251bedd1SWarner Losh 997*514e2dbfSEitan Adler static void 9989102691eSAlan Somers new_client(int fd, int socktype) 999251bedd1SWarner Losh { 10009102691eSAlan Somers client_t s; 1001b026eddfSAlan Somers int sndbuf_size; 1002251bedd1SWarner Losh 1003e1334f93SIan Lepore /* 1004e1334f93SIan Lepore * First go reap any zombie clients, then accept the connection, and 1005e1334f93SIan Lepore * shut down the read side to stop clients from consuming kernel memory 1006e1334f93SIan Lepore * by sending large buffers full of data we'll never read. 1007e1334f93SIan Lepore */ 1008e1334f93SIan Lepore check_clients(); 10099102691eSAlan Somers s.socktype = socktype; 10109102691eSAlan Somers s.fd = accept(fd, NULL, NULL); 10119102691eSAlan Somers if (s.fd != -1) { 1012b026eddfSAlan Somers sndbuf_size = CLIENT_BUFSIZE; 10139102691eSAlan Somers if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, 1014b026eddfSAlan Somers sizeof(sndbuf_size))) 1015b026eddfSAlan Somers err(1, "setsockopt"); 10169102691eSAlan Somers shutdown(s.fd, SHUT_RD); 1017251bedd1SWarner Losh clients.push_back(s); 1018e1334f93SIan Lepore ++num_clients; 1019b026eddfSAlan Somers } else 1020b026eddfSAlan Somers err(1, "accept"); 1021251bedd1SWarner Losh } 1022251bedd1SWarner Losh 10233054f218SWarner Losh static void 10243054f218SWarner Losh event_loop(void) 10253054f218SWarner Losh { 10263054f218SWarner Losh int rv; 10273054f218SWarner Losh int fd; 10283054f218SWarner Losh char buffer[DEVCTL_MAXBUF]; 10295a882d77SWarner Losh int once = 0; 10309102691eSAlan Somers int stream_fd, seqpacket_fd, max_fd; 1031e1334f93SIan Lepore int accepting; 10325a882d77SWarner Losh timeval tv; 10335a882d77SWarner Losh fd_set fds; 10343054f218SWarner Losh 1035901610f4SPawel Jakub Dawidek fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC); 10363054f218SWarner Losh if (fd == -1) 1037251bedd1SWarner Losh err(1, "Can't open devctl device %s", PATH_DEVCTL); 10389102691eSAlan Somers stream_fd = create_socket(STREAMPIPE, SOCK_STREAM); 10399102691eSAlan Somers seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET); 1040e1334f93SIan Lepore accepting = 1; 10419102691eSAlan Somers max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1; 104267ae3bf1SEitan Adler while (!romeo_must_die) { 10436a2ae0ebSAlan Somers if (!once && !no_daemon && !daemonize_quick) { 10445a882d77SWarner Losh // Check to see if we have any events pending. 10455a882d77SWarner Losh tv.tv_sec = 0; 10465a882d77SWarner Losh tv.tv_usec = 0; 10475a882d77SWarner Losh FD_ZERO(&fds); 10485a882d77SWarner Losh FD_SET(fd, &fds); 1049dacbef68SEitan Adler rv = select(fd + 1, &fds, NULL, NULL, &tv); 10505a882d77SWarner Losh // No events -> we've processed all pending events 1051f3059505SWarner Losh if (rv == 0) { 10526d1014a3SAlan Somers devdlog(LOG_DEBUG, "Calling daemon\n"); 10531a0cc6b1SPawel Jakub Dawidek cfg.remove_pidfile(); 10541a0cc6b1SPawel Jakub Dawidek cfg.open_pidfile(); 10555a882d77SWarner Losh daemon(0, 0); 10561a0cc6b1SPawel Jakub Dawidek cfg.write_pidfile(); 10575a882d77SWarner Losh once++; 10585a882d77SWarner Losh } 10595a882d77SWarner Losh } 1060e1334f93SIan Lepore /* 1061e1334f93SIan Lepore * When we've already got the max number of clients, stop 10629102691eSAlan Somers * accepting new connections (don't put the listening sockets in 10639102691eSAlan Somers * the set), shrink the accept() queue to reject connections 10649102691eSAlan Somers * quickly, and poll the existing clients more often, so that we 10659102691eSAlan Somers * notice more quickly when any of them disappear to free up 10669102691eSAlan Somers * client slots. 1067e1334f93SIan Lepore */ 1068251bedd1SWarner Losh FD_ZERO(&fds); 1069251bedd1SWarner Losh FD_SET(fd, &fds); 1070e1334f93SIan Lepore if (num_clients < max_clients) { 1071e1334f93SIan Lepore if (!accepting) { 10729102691eSAlan Somers listen(stream_fd, max_clients); 10739102691eSAlan Somers listen(seqpacket_fd, max_clients); 1074e1334f93SIan Lepore accepting = 1; 1075e1334f93SIan Lepore } 10769102691eSAlan Somers FD_SET(stream_fd, &fds); 10779102691eSAlan Somers FD_SET(seqpacket_fd, &fds); 1078e1334f93SIan Lepore tv.tv_sec = 60; 1079e1334f93SIan Lepore tv.tv_usec = 0; 1080e1334f93SIan Lepore } else { 1081e1334f93SIan Lepore if (accepting) { 10829102691eSAlan Somers listen(stream_fd, 0); 10839102691eSAlan Somers listen(seqpacket_fd, 0); 1084e1334f93SIan Lepore accepting = 0; 1085e1334f93SIan Lepore } 1086e1334f93SIan Lepore tv.tv_sec = 2; 1087e1334f93SIan Lepore tv.tv_usec = 0; 1088e1334f93SIan Lepore } 1089e1334f93SIan Lepore rv = select(max_fd, &fds, NULL, NULL, &tv); 1090be685e01SAlan Somers if (got_siginfo) { 1091f0038a8eSAlan Somers devdlog(LOG_NOTICE, "Events received so far=%u\n", 1092be685e01SAlan Somers total_events); 1093be685e01SAlan Somers got_siginfo = 0; 1094be685e01SAlan Somers } 1095251bedd1SWarner Losh if (rv == -1) { 1096251bedd1SWarner Losh if (errno == EINTR) 1097251bedd1SWarner Losh continue; 1098251bedd1SWarner Losh err(1, "select"); 1099e1334f93SIan Lepore } else if (rv == 0) 1100e1334f93SIan Lepore check_clients(); 1101251bedd1SWarner Losh if (FD_ISSET(fd, &fds)) { 11023054f218SWarner Losh rv = read(fd, buffer, sizeof(buffer) - 1); 11033054f218SWarner Losh if (rv > 0) { 1104be685e01SAlan Somers total_events++; 11056d1014a3SAlan Somers if (rv == sizeof(buffer) - 1) { 11066d1014a3SAlan Somers devdlog(LOG_WARNING, "Warning: " 11076d1014a3SAlan Somers "available event data exceeded " 11086d1014a3SAlan Somers "buffer space\n"); 11096d1014a3SAlan Somers } 1110251bedd1SWarner Losh notify_clients(buffer, rv); 11113054f218SWarner Losh buffer[rv] = '\0'; 11123054f218SWarner Losh while (buffer[--rv] == '\n') 11133054f218SWarner Losh buffer[rv] = '\0'; 1114daa0d9ddSAlan Somers try { 11153054f218SWarner Losh process_event(buffer); 1116daa0d9ddSAlan Somers } 1117958160f3SEitan Adler catch (const std::length_error& e) { 1118daa0d9ddSAlan Somers devdlog(LOG_ERR, "Dropping event %s " 1119daa0d9ddSAlan Somers "due to low memory", buffer); 1120daa0d9ddSAlan Somers } 11213054f218SWarner Losh } else if (rv < 0) { 11223054f218SWarner Losh if (errno != EINTR) 11233054f218SWarner Losh break; 11243054f218SWarner Losh } else { 11253054f218SWarner Losh /* EOF */ 11263054f218SWarner Losh break; 11273054f218SWarner Losh } 11283054f218SWarner Losh } 11299102691eSAlan Somers if (FD_ISSET(stream_fd, &fds)) 11309102691eSAlan Somers new_client(stream_fd, SOCK_STREAM); 11319102691eSAlan Somers /* 11329102691eSAlan Somers * Aside from the socket type, both sockets use the same 11339102691eSAlan Somers * protocol, so we can process clients the same way. 11349102691eSAlan Somers */ 11359102691eSAlan Somers if (FD_ISSET(seqpacket_fd, &fds)) 11369102691eSAlan Somers new_client(seqpacket_fd, SOCK_SEQPACKET); 1137251bedd1SWarner Losh } 113891cfa6b8SAlan Somers cfg.remove_pidfile(); 1139daa0d9ddSAlan Somers close(seqpacket_fd); 1140daa0d9ddSAlan Somers close(stream_fd); 11413054f218SWarner Losh close(fd); 11423054f218SWarner Losh } 11430285e9b1SAlan Somers 11443054f218SWarner Losh /* 11453054f218SWarner Losh * functions that the parser uses. 11463054f218SWarner Losh */ 11473054f218SWarner Losh void 11483054f218SWarner Losh add_attach(int prio, event_proc *p) 11493054f218SWarner Losh { 11503054f218SWarner Losh cfg.add_attach(prio, p); 11513054f218SWarner Losh } 11523054f218SWarner Losh 11533054f218SWarner Losh void 11543054f218SWarner Losh add_detach(int prio, event_proc *p) 11553054f218SWarner Losh { 11563054f218SWarner Losh cfg.add_detach(prio, p); 11573054f218SWarner Losh } 11583054f218SWarner Losh 11593054f218SWarner Losh void 11603054f218SWarner Losh add_directory(const char *dir) 11613054f218SWarner Losh { 11623054f218SWarner Losh cfg.add_directory(dir); 11633054f218SWarner Losh free(const_cast<char *>(dir)); 11643054f218SWarner Losh } 11653054f218SWarner Losh 11663054f218SWarner Losh void 11673054f218SWarner Losh add_nomatch(int prio, event_proc *p) 11683054f218SWarner Losh { 11693054f218SWarner Losh cfg.add_nomatch(prio, p); 11703054f218SWarner Losh } 11713054f218SWarner Losh 1172842ccec5SWarner Losh void 1173842ccec5SWarner Losh add_notify(int prio, event_proc *p) 1174842ccec5SWarner Losh { 1175842ccec5SWarner Losh cfg.add_notify(prio, p); 1176842ccec5SWarner Losh } 1177842ccec5SWarner Losh 11783054f218SWarner Losh event_proc * 11793054f218SWarner Losh add_to_event_proc(event_proc *ep, eps *eps) 11803054f218SWarner Losh { 11813054f218SWarner Losh if (ep == NULL) 11823054f218SWarner Losh ep = new event_proc(); 11833054f218SWarner Losh ep->add(eps); 11843054f218SWarner Losh return (ep); 11853054f218SWarner Losh } 11863054f218SWarner Losh 11873054f218SWarner Losh eps * 11883054f218SWarner Losh new_action(const char *cmd) 11893054f218SWarner Losh { 11903054f218SWarner Losh eps *e = new action(cmd); 11913054f218SWarner Losh free(const_cast<char *>(cmd)); 11923054f218SWarner Losh return (e); 11933054f218SWarner Losh } 11943054f218SWarner Losh 11953054f218SWarner Losh eps * 11963054f218SWarner Losh new_match(const char *var, const char *re) 11973054f218SWarner Losh { 11985102ef84SWarner Losh eps *e = new match(cfg, var, re); 11993054f218SWarner Losh free(const_cast<char *>(var)); 12003054f218SWarner Losh free(const_cast<char *>(re)); 12013054f218SWarner Losh return (e); 12023054f218SWarner Losh } 12033054f218SWarner Losh 1204cd70782bSWarner Losh eps * 1205cd70782bSWarner Losh new_media(const char *var, const char *re) 1206cd70782bSWarner Losh { 1207cd70782bSWarner Losh eps *e = new media(cfg, var, re); 1208cd70782bSWarner Losh free(const_cast<char *>(var)); 1209cd70782bSWarner Losh free(const_cast<char *>(re)); 1210cd70782bSWarner Losh return (e); 1211cd70782bSWarner Losh } 1212cd70782bSWarner Losh 12133054f218SWarner Losh void 12143054f218SWarner Losh set_pidfile(const char *name) 12153054f218SWarner Losh { 12163054f218SWarner Losh cfg.set_pidfile(name); 12173054f218SWarner Losh free(const_cast<char *>(name)); 12183054f218SWarner Losh } 12193054f218SWarner Losh 12203054f218SWarner Losh void 12213054f218SWarner Losh set_variable(const char *var, const char *val) 12223054f218SWarner Losh { 12233054f218SWarner Losh cfg.set_variable(var, val); 12243054f218SWarner Losh free(const_cast<char *>(var)); 12253054f218SWarner Losh free(const_cast<char *>(val)); 12263054f218SWarner Losh } 12273054f218SWarner Losh 12280285e9b1SAlan Somers 12293054f218SWarner Losh 12303054f218SWarner Losh static void 12313054f218SWarner Losh gensighand(int) 12323054f218SWarner Losh { 1233fcdcaa88SEitan Adler romeo_must_die = 1; 12343054f218SWarner Losh } 12353054f218SWarner Losh 12366d1014a3SAlan Somers /* 1237be685e01SAlan Somers * SIGINFO handler. Will print useful statistics to the syslog or stderr 1238be685e01SAlan Somers * as appropriate 1239be685e01SAlan Somers */ 1240be685e01SAlan Somers static void 1241be685e01SAlan Somers siginfohand(int) 1242be685e01SAlan Somers { 1243be685e01SAlan Somers got_siginfo = 1; 1244be685e01SAlan Somers } 1245be685e01SAlan Somers 1246be685e01SAlan Somers /* 12472e024bc2SAndriy Gapon * Local logging function. Prints to syslog if we're daemonized; stderr 12486d1014a3SAlan Somers * otherwise. 12496d1014a3SAlan Somers */ 12506d1014a3SAlan Somers static void 12516d1014a3SAlan Somers devdlog(int priority, const char* fmt, ...) 12526d1014a3SAlan Somers { 12536d1014a3SAlan Somers va_list argp; 12546d1014a3SAlan Somers 12556d1014a3SAlan Somers va_start(argp, fmt); 12566a2ae0ebSAlan Somers if (no_daemon) 12576d1014a3SAlan Somers vfprintf(stderr, fmt, argp); 12584d00d13cSWarner Losh else if (quiet_mode == 0 || priority <= LOG_WARNING) 12596d1014a3SAlan Somers vsyslog(priority, fmt, argp); 12606d1014a3SAlan Somers va_end(argp); 12616d1014a3SAlan Somers } 12626d1014a3SAlan Somers 12633054f218SWarner Losh static void 12643054f218SWarner Losh usage() 12653054f218SWarner Losh { 12666a2ae0ebSAlan Somers fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n", 1267e1334f93SIan Lepore getprogname()); 12683054f218SWarner Losh exit(1); 12693054f218SWarner Losh } 12703054f218SWarner Losh 12719c180898SWarner Losh static void 12729c180898SWarner Losh check_devd_enabled() 12739c180898SWarner Losh { 12749c180898SWarner Losh int val = 0; 12759c180898SWarner Losh size_t len; 12769c180898SWarner Losh 12779c180898SWarner Losh len = sizeof(val); 127879f92e25SWarner Losh if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0) 12799c180898SWarner Losh errx(1, "devctl sysctl missing from kernel!"); 1280ee38f2e0SMateusz Guzik if (val == 0) { 1281ee38f2e0SMateusz Guzik warnx("Setting " SYSCTL " to 1000"); 1282ee38f2e0SMateusz Guzik val = 1000; 1283daa0d9ddSAlan Somers if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val))) 1284daa0d9ddSAlan Somers err(1, "sysctlbyname"); 12859c180898SWarner Losh } 12869c180898SWarner Losh } 12879c180898SWarner Losh 12883054f218SWarner Losh /* 12893054f218SWarner Losh * main 12903054f218SWarner Losh */ 12913054f218SWarner Losh int 12923054f218SWarner Losh main(int argc, char **argv) 12933054f218SWarner Losh { 12943054f218SWarner Losh int ch; 12953054f218SWarner Losh 12969c180898SWarner Losh check_devd_enabled(); 12976a2ae0ebSAlan Somers while ((ch = getopt(argc, argv, "df:l:nq")) != -1) { 12983054f218SWarner Losh switch (ch) { 12993054f218SWarner Losh case 'd': 13006a2ae0ebSAlan Somers no_daemon = 1; 13013054f218SWarner Losh break; 13028334958aSJoseph Koshy case 'f': 13038334958aSJoseph Koshy configfile = optarg; 13048334958aSJoseph Koshy break; 1305e1334f93SIan Lepore case 'l': 1306e1334f93SIan Lepore max_clients = MAX(1, strtoul(optarg, NULL, 0)); 1307e1334f93SIan Lepore break; 13085a882d77SWarner Losh case 'n': 13096a2ae0ebSAlan Somers daemonize_quick = 1; 13106a2ae0ebSAlan Somers break; 13116a2ae0ebSAlan Somers case 'q': 13126a2ae0ebSAlan Somers quiet_mode = 1; 13135a882d77SWarner Losh break; 13143054f218SWarner Losh default: 13153054f218SWarner Losh usage(); 13163054f218SWarner Losh } 13173054f218SWarner Losh } 13183054f218SWarner Losh 13193054f218SWarner Losh cfg.parse(); 13206a2ae0ebSAlan Somers if (!no_daemon && daemonize_quick) { 13211a0cc6b1SPawel Jakub Dawidek cfg.open_pidfile(); 13223054f218SWarner Losh daemon(0, 0); 13231a0cc6b1SPawel Jakub Dawidek cfg.write_pidfile(); 1324fd6a8f23SWarner Losh } 1325f32a7686SWarner Losh signal(SIGPIPE, SIG_IGN); 13263054f218SWarner Losh signal(SIGHUP, gensighand); 13273054f218SWarner Losh signal(SIGINT, gensighand); 13283054f218SWarner Losh signal(SIGTERM, gensighand); 1329be685e01SAlan Somers signal(SIGINFO, siginfohand); 13303054f218SWarner Losh event_loop(); 13313054f218SWarner Losh return (0); 13323054f218SWarner Losh } 1333