2 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2010 M. Warner Losh.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * my_system is a variation on lib/libc/stdlib/system.c:
30 * Copyright (c) 1988, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * DEVD control daemon.
63 // o devd.conf and devd man pages need a lot of help:
64 // - devd needs to document the unix domain socket
65 // - devd.conf needs more details on the supported statements.
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
71 #include <sys/socket.h>
73 #include <sys/sysctl.h>
74 #include <sys/types.h>
103 #include "devd.h" /* C compatible definitions */
104 #include "devd.hh" /* C++ class definitions */
106 #define STREAMPIPE "/var/run/devd.pipe"
107 #define SEQPACKETPIPE "/var/run/devd.seqpacket.pipe"
108 #define CF "/etc/devd.conf"
109 #define SYSCTL "hw.bus.devctl_queue"
112 * Since the client socket is nonblocking, we must increase its send buffer to
113 * handle brief event storms. On FreeBSD, AF_UNIX sockets don't have a receive
114 * buffer, so the client can't increase the buffersize by itself.
116 * For example, when creating a ZFS pool, devd emits one 165 character
117 * resource.fs.zfs.statechange message for each vdev in the pool. The kernel
118 * allocates a 4608B mbuf for each message. Modern technology places a limit of
119 * roughly 450 drives/rack, and it's unlikely that a zpool will ever be larger
122 * 450 drives * 165 bytes / drive = 74250B of data in the sockbuf
123 * 450 drives * 4608B / drive = 2073600B of mbufs in the sockbuf
125 * We can't directly set the sockbuf's mbuf limit, but we can do it indirectly.
126 * The kernel sets it to the minimum of a hard-coded maximum value and sbcc *
127 * kern.ipc.sockbuf_waste_factor, where sbcc is the socket buffer size set by
128 * the user. The default value of kern.ipc.sockbuf_waste_factor is 8. If we
129 * set the bufsize to 256k and use the kern.ipc.sockbuf_waste_factor, then the
130 * kernel will set the mbuf limit to 2MB, which is just large enough for 450
131 * drives. It also happens to be the same as the hardcoded maximum value.
133 #define CLIENT_BUFSIZE 262144
137 typedef struct client {
144 static const char notify = '!';
145 static const char nomatch = '?';
146 static const char attach = '+';
147 static const char detach = '-';
149 static struct pidfh *pfh;
151 static int no_daemon = 0;
152 static int daemonize_quick = 0;
153 static int quiet_mode = 0;
154 static unsigned total_events = 0;
155 static volatile sig_atomic_t got_siginfo = 0;
156 static volatile sig_atomic_t romeo_must_die = 0;
158 static const char *configfile = CF;
160 static void devdlog(int priority, const char* message, ...)
162 static void event_loop(void);
163 static void usage(void) __dead2;
165 template <class T> void
166 delete_and_clear(vector<T *> &v)
168 typename vector<T *>::const_iterator i;
170 for (i = v.begin(); i != v.end(); ++i)
177 event_proc::event_proc() : _prio(-1)
182 event_proc::~event_proc()
184 delete_and_clear(_epsvec);
188 event_proc::add(eps *eps)
190 _epsvec.push_back(eps);
194 event_proc::matches(config &c) const
196 vector<eps *>::const_iterator i;
198 for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
199 if (!(*i)->do_match(c))
205 event_proc::run(config &c) const
207 vector<eps *>::const_iterator i;
209 for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
210 if (!(*i)->do_action(c))
215 action::action(const char *cmd)
227 my_system(const char *command)
231 struct sigaction ign, intact, quitact;
232 sigset_t newsigblock, oldsigblock;
234 if (!command) /* just checking... */
238 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
239 * existing signal dispositions.
241 ign.sa_handler = SIG_IGN;
242 ::sigemptyset(&ign.sa_mask);
244 ::sigaction(SIGINT, &ign, &intact);
245 ::sigaction(SIGQUIT, &ign, &quitact);
246 ::sigemptyset(&newsigblock);
247 ::sigaddset(&newsigblock, SIGCHLD);
248 ::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
249 switch (pid = ::fork()) {
254 * Restore original signal dispositions and exec the command.
256 ::sigaction(SIGINT, &intact, NULL);
257 ::sigaction(SIGQUIT, &quitact, NULL);
258 ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
260 * Close the PID file, and all other open descriptors.
261 * Inherit std{in,out,err} only.
265 ::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
267 default: /* parent */
270 pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0);
271 } while (pid == -1 && errno == EINTR);
274 ::sigaction(SIGINT, &intact, NULL);
275 ::sigaction(SIGQUIT, &quitact, NULL);
276 ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
277 return (pid == -1 ? -1 : pstat);
281 action::do_action(config &c)
283 string s = c.expand_string(_cmd.c_str());
284 devdlog(LOG_INFO, "Executing '%s'\n", s.c_str());
285 my_system(s.c_str());
289 match::match(config &c, const char *var, const char *re) :
292 _re(c.expand_string(_inv ? re + 1 : re, "^", "$"))
294 regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE);
303 match::do_match(config &c)
305 const string &value = c.get_variable(_var);
309 * This function gets called WAY too often to justify calling syslog()
310 * each time, even at LOG_DEBUG. Because if syslogd isn't running, it
311 * can consume excessive amounts of systime inside of connect(). Only
312 * log when we're in -d mode.
315 devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n",
316 _var.c_str(), value.c_str(), _re.c_str(), _inv);
319 retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
321 retval = (retval == 0) ? 1 : 0;
326 #include <sys/sockio.h>
328 #include <net/if_media.h>
330 media::media(config &, const char *var, const char *type)
331 : _var(var), _type(-1)
333 static struct ifmedia_description media_types[] = {
334 { IFM_ETHER, "Ethernet" },
335 { IFM_IEEE80211, "802.11" },
340 for (int i = 0; media_types[i].ifmt_string != NULL; ++i)
341 if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
342 _type = media_types[i].ifmt_word;
352 media::do_match(config &c)
355 struct ifmediareq ifmr;
359 // Since we can be called from both a device attach/detach
360 // context where device-name is defined and what we want,
361 // as well as from a link status context, where subsystem is
362 // the name of interest, first try device-name and fall back
363 // to subsystem if none exists.
364 value = c.get_variable("device-name");
366 value = c.get_variable("subsystem");
367 devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n",
368 value.c_str(), _type);
372 s = socket(PF_INET, SOCK_DGRAM, 0);
374 memset(&ifmr, 0, sizeof(ifmr));
375 strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
377 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
378 ifmr.ifm_status & IFM_AVALID) {
379 devdlog(LOG_DEBUG, "%s has media type 0x%x\n",
380 value.c_str(), IFM_TYPE(ifmr.ifm_active));
381 retval = (IFM_TYPE(ifmr.ifm_active) == _type);
382 } else if (_type == -1) {
383 devdlog(LOG_DEBUG, "%s has unknown media type\n",
393 const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
394 const string var_list::nothing = "";
397 var_list::get_variable(const string &var) const
399 map<string, string>::const_iterator i;
402 if (i == _vars.end())
403 return (var_list::bogus);
408 var_list::is_set(const string &var) const
410 return (_vars.find(var) != _vars.end());
415 * Removes quoted characters that have made it this far. \" are
416 * converted to ". For all other characters, both \ and following
417 * character. So the string 'fre\:\"' is translated to 'fred\:"'.
420 var_list::fix_value(const std::string &val) const
423 std::string::size_type pos(0);
425 while ((pos = rv.find("\\\"", pos)) != rv.npos) {
432 var_list::set_variable(const string &var, const string &val)
435 * This function gets called WAY too often to justify calling syslog()
436 * each time, even at LOG_DEBUG. Because if syslogd isn't running, it
437 * can consume excessive amounts of systime inside of connect(). Only
438 * log when we're in -d mode.
440 _vars[var] = fix_value(val);
442 devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str());
449 delete_and_clear(_var_list_table);
450 delete_and_clear(_attach_list);
451 delete_and_clear(_detach_list);
452 delete_and_clear(_nomatch_list);
453 delete_and_clear(_notify_list);
457 config::parse_one_file(const char *fn)
459 devdlog(LOG_DEBUG, "Parsing %s\n", fn);
460 yyin = fopen(fn, "r");
462 err(1, "Cannot open config file %s", fn);
465 errx(1, "Cannot parse %s at line %d", fn, lineno);
470 config::parse_files_in_dir(const char *dirname)
476 devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname);
477 dirp = opendir(dirname);
480 readdir(dirp); /* Skip . */
481 readdir(dirp); /* Skip .. */
482 while ((dp = readdir(dirp)) != NULL) {
483 if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
484 snprintf(path, sizeof(path), "%s/%s",
485 dirname, dp->d_name);
486 parse_one_file(path);
494 int operator()(event_proc *const&l1, event_proc *const&l2) const
496 return (l1->get_priority() > l2->get_priority());
501 config::sort_vector(vector<event_proc *> &v)
503 stable_sort(v.begin(), v.end(), epv_greater());
509 vector<string>::const_iterator i;
511 parse_one_file(configfile);
512 for (i = _dir_list.begin(); i != _dir_list.end(); ++i)
513 parse_files_in_dir((*i).c_str());
514 sort_vector(_attach_list);
515 sort_vector(_detach_list);
516 sort_vector(_nomatch_list);
517 sort_vector(_notify_list);
521 config::open_pidfile()
525 if (_pidfile.empty())
527 pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
530 errx(1, "devd already running, pid: %d", (int)otherpid);
531 warn("cannot open pid file");
536 config::write_pidfile()
543 config::close_pidfile()
550 config::remove_pidfile()
557 config::add_attach(int prio, event_proc *p)
559 p->set_priority(prio);
560 _attach_list.push_back(p);
564 config::add_detach(int prio, event_proc *p)
566 p->set_priority(prio);
567 _detach_list.push_back(p);
571 config::add_directory(const char *dir)
573 _dir_list.push_back(string(dir));
577 config::add_nomatch(int prio, event_proc *p)
579 p->set_priority(prio);
580 _nomatch_list.push_back(p);
584 config::add_notify(int prio, event_proc *p)
586 p->set_priority(prio);
587 _notify_list.push_back(p);
591 config::set_pidfile(const char *fn)
597 config::push_var_table()
602 _var_list_table.push_back(vl);
603 devdlog(LOG_DEBUG, "Pushing table\n");
607 config::pop_var_table()
609 delete _var_list_table.back();
610 _var_list_table.pop_back();
611 devdlog(LOG_DEBUG, "Popping table\n");
615 config::set_variable(const char *var, const char *val)
617 _var_list_table.back()->set_variable(var, val);
621 config::get_variable(const string &var)
623 vector<var_list *>::reverse_iterator i;
625 for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) {
626 if ((*i)->is_set(var))
627 return ((*i)->get_variable(var));
629 return (var_list::nothing);
633 config::is_id_char(char ch) const
635 return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
640 config::shell_quote(const string &s)
647 * Enclose the string in $' ' with escapes for ' and / characters making
648 * it one argument and ensuring the shell won't be affected by its
649 * usual list of candidates.
651 buffer.reserve(s.length() * 3 / 2);
655 ce = cs + strlen(cs);
656 for (; cs < ce; cs++) {
658 if (c == '\'' || c == '\\') {
669 config::expand_one(const char *&src, string &dst, bool is_shell)
682 // This is the escape hatch for passing down shell subcommands
686 /* If the string ends before ) is matched , return. */
687 while (count > 0 && *src) {
690 else if (*src == '(')
697 // $[^-A-Za-z_*] -> $\1
698 if (!isalpha(*src) && *src != '_' && *src != '-' && *src != '*') {
704 // $var -> replace with value
707 } while (is_id_char(*src));
708 dst.append(is_shell ? shell_quote(get_variable(buffer)) : get_variable(buffer));
712 config::expand_string(const char *src, const char *prepend, const char *append)
718 * 128 bytes is enough for 2427 of 2438 expansions that happen
719 * while parsing config files, as tested on 2013-01-30.
727 var_at = strchr(src, '$');
728 if (var_at == NULL) {
732 dst.append(src, var_at - src);
734 expand_one(src, dst, prepend == NULL);
744 config::chop_var(char *&buffer, char *&lhs, char *&rhs) const
750 walker = lhs = buffer;
751 while (is_id_char(*walker))
756 if (*walker == '"') {
759 while (*walker && *walker != '"') {
760 // Skip \" ... We leave it in the string and strip the \ later.
761 // due to the super simplistic parser that we have here.
762 if (*walker == '\\' && walker[1] == '"')
772 while (*walker && !isspace(*walker))
778 while (isspace(*walker))
786 config::set_vars(char *buffer)
792 if (!chop_var(buffer, lhs, rhs))
794 set_variable(lhs, rhs);
800 config::find_and_execute(char type)
802 vector<event_proc *> *l;
803 vector<event_proc *>::const_iterator i;
826 devdlog(LOG_DEBUG, "Processing %s event\n", s);
827 for (i = l->begin(); i != l->end(); ++i) {
828 if ((*i)->matches(*this)) {
838 process_event(char *buffer)
846 devdlog(LOG_INFO, "Processing event '%s'\n", buffer);
848 cfg.push_var_table();
849 // $* is the entire line
850 cfg.set_variable("*", buffer - 1);
851 // $_ is the entire line without the initial character
852 cfg.set_variable("_", buffer);
854 // Save the time this happened (as approximated by when we got
855 // around to processing it).
856 gettimeofday(&tv, NULL);
857 asprintf(×tr, "%jd.%06ld", (uintmax_t)tv.tv_sec, tv.tv_usec);
858 cfg.set_variable("timestamp", timestr);
861 // Match doesn't have a device, and the format is a little
862 // different, so handle it separately.
866 sp = cfg.set_vars(sp);
869 //? at location pnp-info on bus
870 sp = strchr(sp, ' ');
872 return; /* Can't happen? */
876 if (strncmp(sp, "at ", 3) == 0)
878 sp = cfg.set_vars(sp);
881 if (strncmp(sp, "on ", 3) == 0)
882 cfg.set_variable("bus", sp + 3);
884 case attach: /*FALLTHROUGH*/
886 sp = strchr(sp, ' ');
888 return; /* Can't happen? */
890 cfg.set_variable("device-name", buffer);
893 if (strncmp(sp, "at ", 3) == 0)
895 sp = cfg.set_vars(sp);
898 if (strncmp(sp, "on ", 3) == 0)
899 cfg.set_variable("bus", sp + 3);
903 cfg.find_and_execute(type);
908 create_socket(const char *name, int socktype)
911 struct sockaddr_un sun;
913 if ((fd = socket(PF_LOCAL, socktype, 0)) < 0)
915 bzero(&sun, sizeof(sun));
916 sun.sun_family = AF_UNIX;
917 strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
918 slen = SUN_LEN(&sun);
920 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
922 if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
925 if (chown(name, 0, 0)) /* XXX - root.wheel */
927 if (chmod(name, 0666))
932 static unsigned int max_clients = 10; /* Default, can be overridden on cmdline. */
933 static unsigned int num_clients;
935 static list<client_t> clients;
938 notify_clients(const char *data, int len)
940 list<client_t>::iterator i;
943 * Deliver the data to all clients. Throw clients overboard at the
944 * first sign of trouble. This reaps clients who've died or closed
945 * their sockets, and also clients who are alive but failing to keep up
946 * (or who are maliciously not reading, to consume buffer space in
947 * kernel memory or tie up the limited number of available connections).
949 for (i = clients.begin(); i != clients.end(); ) {
951 if (i->socktype == SOCK_SEQPACKET)
956 if (send(i->fd, data, len, flags) != len) {
959 i = clients.erase(i);
960 devdlog(LOG_WARNING, "notify_clients: send() failed; "
961 "dropping unresponsive client\n");
972 list<client_t>::iterator i;
975 * Check all existing clients to see if any of them have disappeared.
976 * Normally we reap clients when we get an error trying to send them an
977 * event. This check eliminates the problem of an ever-growing list of
978 * zombie clients because we're never writing to them on a system
979 * without frequent device-change activity.
982 for (i = clients.begin(); i != clients.end(); ) {
984 s = poll(&pfd, 1, 0);
985 if ((s < 0 && s != EINTR ) ||
986 (s > 0 && (pfd.revents & POLLHUP))) {
989 i = clients.erase(i);
990 devdlog(LOG_NOTICE, "check_clients: "
991 "dropping disconnected client\n");
998 new_client(int fd, int socktype)
1004 * First go reap any zombie clients, then accept the connection, and
1005 * shut down the read side to stop clients from consuming kernel memory
1006 * by sending large buffers full of data we'll never read.
1009 s.socktype = socktype;
1010 s.fd = accept(fd, NULL, NULL);
1012 sndbuf_size = CLIENT_BUFSIZE;
1013 if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
1014 sizeof(sndbuf_size)))
1015 err(1, "setsockopt");
1016 shutdown(s.fd, SHUT_RD);
1017 clients.push_back(s);
1028 char buffer[DEVCTL_MAXBUF];
1030 int stream_fd, seqpacket_fd, max_fd;
1035 fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC);
1037 err(1, "Can't open devctl device %s", PATH_DEVCTL);
1038 stream_fd = create_socket(STREAMPIPE, SOCK_STREAM);
1039 seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET);
1041 max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1;
1042 while (!romeo_must_die) {
1043 if (!once && !no_daemon && !daemonize_quick) {
1044 // Check to see if we have any events pending.
1049 rv = select(fd + 1, &fds, NULL, NULL, &tv);
1050 // No events -> we've processed all pending events
1052 devdlog(LOG_DEBUG, "Calling daemon\n");
1053 cfg.remove_pidfile();
1056 cfg.write_pidfile();
1061 * When we've already got the max number of clients, stop
1062 * accepting new connections (don't put the listening sockets in
1063 * the set), shrink the accept() queue to reject connections
1064 * quickly, and poll the existing clients more often, so that we
1065 * notice more quickly when any of them disappear to free up
1070 if (num_clients < max_clients) {
1072 listen(stream_fd, max_clients);
1073 listen(seqpacket_fd, max_clients);
1076 FD_SET(stream_fd, &fds);
1077 FD_SET(seqpacket_fd, &fds);
1082 listen(stream_fd, 0);
1083 listen(seqpacket_fd, 0);
1089 rv = select(max_fd, &fds, NULL, NULL, &tv);
1091 devdlog(LOG_NOTICE, "Events received so far=%u\n",
1101 if (FD_ISSET(fd, &fds)) {
1102 rv = read(fd, buffer, sizeof(buffer) - 1);
1105 if (rv == sizeof(buffer) - 1) {
1106 devdlog(LOG_WARNING, "Warning: "
1107 "available event data exceeded "
1110 notify_clients(buffer, rv);
1112 while (buffer[--rv] == '\n')
1115 process_event(buffer);
1117 catch (const std::length_error& e) {
1118 devdlog(LOG_ERR, "Dropping event %s "
1119 "due to low memory", buffer);
1121 } else if (rv < 0) {
1129 if (FD_ISSET(stream_fd, &fds))
1130 new_client(stream_fd, SOCK_STREAM);
1132 * Aside from the socket type, both sockets use the same
1133 * protocol, so we can process clients the same way.
1135 if (FD_ISSET(seqpacket_fd, &fds))
1136 new_client(seqpacket_fd, SOCK_SEQPACKET);
1138 cfg.remove_pidfile();
1139 close(seqpacket_fd);
1145 * functions that the parser uses.
1148 add_attach(int prio, event_proc *p)
1150 cfg.add_attach(prio, p);
1154 add_detach(int prio, event_proc *p)
1156 cfg.add_detach(prio, p);
1160 add_directory(const char *dir)
1162 cfg.add_directory(dir);
1163 free(const_cast<char *>(dir));
1167 add_nomatch(int prio, event_proc *p)
1169 cfg.add_nomatch(prio, p);
1173 add_notify(int prio, event_proc *p)
1175 cfg.add_notify(prio, p);
1179 add_to_event_proc(event_proc *ep, eps *eps)
1182 ep = new event_proc();
1188 new_action(const char *cmd)
1190 eps *e = new action(cmd);
1191 free(const_cast<char *>(cmd));
1196 new_match(const char *var, const char *re)
1198 eps *e = new match(cfg, var, re);
1199 free(const_cast<char *>(var));
1200 free(const_cast<char *>(re));
1205 new_media(const char *var, const char *re)
1207 eps *e = new media(cfg, var, re);
1208 free(const_cast<char *>(var));
1209 free(const_cast<char *>(re));
1214 set_pidfile(const char *name)
1216 cfg.set_pidfile(name);
1217 free(const_cast<char *>(name));
1221 set_variable(const char *var, const char *val)
1223 cfg.set_variable(var, val);
1224 free(const_cast<char *>(var));
1225 free(const_cast<char *>(val));
1237 * SIGINFO handler. Will print useful statistics to the syslog or stderr
1247 * Local logging function. Prints to syslog if we're daemonized; stderr
1251 devdlog(int priority, const char* fmt, ...)
1255 va_start(argp, fmt);
1257 vfprintf(stderr, fmt, argp);
1258 else if (quiet_mode == 0 || priority <= LOG_WARNING)
1259 vsyslog(priority, fmt, argp);
1266 fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n",
1272 check_devd_enabled()
1278 if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
1279 errx(1, "devctl sysctl missing from kernel!");
1281 warnx("Setting " SYSCTL " to 1000");
1283 if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)))
1284 err(1, "sysctlbyname");
1292 main(int argc, char **argv)
1296 check_devd_enabled();
1297 while ((ch = getopt(argc, argv, "df:l:nq")) != -1) {
1303 configfile = optarg;
1306 max_clients = MAX(1, strtoul(optarg, NULL, 0));
1309 daemonize_quick = 1;
1320 if (!no_daemon && daemonize_quick) {
1323 cfg.write_pidfile();
1325 signal(SIGPIPE, SIG_IGN);
1326 signal(SIGHUP, gensighand);
1327 signal(SIGINT, gensighand);
1328 signal(SIGTERM, gensighand);
1329 signal(SIGINFO, siginfohand);