2 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2010 M. Warner Losh <imp@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * my_system is a variation on lib/libc/stdlib/system.c:
29 * Copyright (c) 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * DEVD control daemon.
62 // o devd.conf and devd man pages need a lot of help:
63 // - devd needs to document the unix domain socket
64 // - devd.conf needs more details on the supported statements.
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
69 #include <sys/param.h>
70 #include <sys/socket.h>
72 #include <sys/sysctl.h>
73 #include <sys/types.h>
102 #include "devd.h" /* C compatible definitions */
103 #include "devd.hh" /* C++ class definitions */
105 #define STREAMPIPE "/var/run/devd.pipe"
106 #define SEQPACKETPIPE "/var/run/devd.seqpacket.pipe"
107 #define CF "/etc/devd.conf"
108 #define SYSCTL "hw.bus.devctl_queue"
111 * Since the client socket is nonblocking, we must increase its send buffer to
112 * handle brief event storms. On FreeBSD, AF_UNIX sockets don't have a receive
113 * buffer, so the client can't increase the buffersize by itself.
115 * For example, when creating a ZFS pool, devd emits one 165 character
116 * resource.fs.zfs.statechange message for each vdev in the pool. The kernel
117 * allocates a 4608B mbuf for each message. Modern technology places a limit of
118 * roughly 450 drives/rack, and it's unlikely that a zpool will ever be larger
121 * 450 drives * 165 bytes / drive = 74250B of data in the sockbuf
122 * 450 drives * 4608B / drive = 2073600B of mbufs in the sockbuf
124 * We can't directly set the sockbuf's mbuf limit, but we can do it indirectly.
125 * The kernel sets it to the minimum of a hard-coded maximum value and sbcc *
126 * kern.ipc.sockbuf_waste_factor, where sbcc is the socket buffer size set by
127 * the user. The default value of kern.ipc.sockbuf_waste_factor is 8. If we
128 * set the bufsize to 256k and use the kern.ipc.sockbuf_waste_factor, then the
129 * kernel will set the mbuf limit to 2MB, which is just large enough for 450
130 * drives. It also happens to be the same as the hardcoded maximum value.
132 #define CLIENT_BUFSIZE 262144
136 typedef struct client {
143 static const char notify = '!';
144 static const char nomatch = '?';
145 static const char attach = '+';
146 static const char detach = '-';
148 static struct pidfh *pfh;
150 static int no_daemon = 0;
151 static int daemonize_quick = 0;
152 static int quiet_mode = 0;
153 static unsigned total_events = 0;
154 static volatile sig_atomic_t got_siginfo = 0;
155 static volatile sig_atomic_t romeo_must_die = 0;
157 static const char *configfile = CF;
159 static void devdlog(int priority, const char* message, ...)
161 static void event_loop(void);
162 static void usage(void) __dead2;
164 template <class T> void
165 delete_and_clear(vector<T *> &v)
167 typename vector<T *>::const_iterator i;
169 for (i = v.begin(); i != v.end(); ++i)
176 event_proc::event_proc() : _prio(-1)
181 event_proc::~event_proc()
183 delete_and_clear(_epsvec);
187 event_proc::add(eps *eps)
189 _epsvec.push_back(eps);
193 event_proc::matches(config &c) const
195 vector<eps *>::const_iterator i;
197 for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
198 if (!(*i)->do_match(c))
204 event_proc::run(config &c) const
206 vector<eps *>::const_iterator i;
208 for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
209 if (!(*i)->do_action(c))
214 action::action(const char *cmd)
226 my_system(const char *command)
230 struct sigaction ign, intact, quitact;
231 sigset_t newsigblock, oldsigblock;
233 if (!command) /* just checking... */
237 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
238 * existing signal dispositions.
240 ign.sa_handler = SIG_IGN;
241 ::sigemptyset(&ign.sa_mask);
243 ::sigaction(SIGINT, &ign, &intact);
244 ::sigaction(SIGQUIT, &ign, &quitact);
245 ::sigemptyset(&newsigblock);
246 ::sigaddset(&newsigblock, SIGCHLD);
247 ::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
248 switch (pid = ::fork()) {
253 * Restore original signal dispositions and exec the command.
255 ::sigaction(SIGINT, &intact, NULL);
256 ::sigaction(SIGQUIT, &quitact, NULL);
257 ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
259 * Close the PID file, and all other open descriptors.
260 * Inherit std{in,out,err} only.
264 ::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
266 default: /* parent */
269 pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0);
270 } while (pid == -1 && errno == EINTR);
273 ::sigaction(SIGINT, &intact, NULL);
274 ::sigaction(SIGQUIT, &quitact, NULL);
275 ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
276 return (pid == -1 ? -1 : pstat);
280 action::do_action(config &c)
282 string s = c.expand_string(_cmd.c_str());
283 devdlog(LOG_INFO, "Executing '%s'\n", s.c_str());
284 my_system(s.c_str());
288 match::match(config &c, const char *var, const char *re) :
291 _re(c.expand_string(_inv ? re + 1 : re, "^", "$"))
293 regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE);
302 match::do_match(config &c)
304 const string &value = c.get_variable(_var);
308 * This function gets called WAY too often to justify calling syslog()
309 * each time, even at LOG_DEBUG. Because if syslogd isn't running, it
310 * can consume excessive amounts of systime inside of connect(). Only
311 * log when we're in -d mode.
314 devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n",
315 _var.c_str(), value.c_str(), _re.c_str(), _inv);
318 retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
320 retval = (retval == 0) ? 1 : 0;
325 #include <sys/sockio.h>
327 #include <net/if_media.h>
329 media::media(config &, const char *var, const char *type)
330 : _var(var), _type(-1)
332 static struct ifmedia_description media_types[] = {
333 { IFM_ETHER, "Ethernet" },
334 { IFM_IEEE80211, "802.11" },
339 for (int i = 0; media_types[i].ifmt_string != NULL; ++i)
340 if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
341 _type = media_types[i].ifmt_word;
351 media::do_match(config &c)
354 struct ifmediareq ifmr;
358 // Since we can be called from both a device attach/detach
359 // context where device-name is defined and what we want,
360 // as well as from a link status context, where subsystem is
361 // the name of interest, first try device-name and fall back
362 // to subsystem if none exists.
363 value = c.get_variable("device-name");
365 value = c.get_variable("subsystem");
366 devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n",
367 value.c_str(), _type);
371 s = socket(PF_INET, SOCK_DGRAM, 0);
373 memset(&ifmr, 0, sizeof(ifmr));
374 strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
376 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
377 ifmr.ifm_status & IFM_AVALID) {
378 devdlog(LOG_DEBUG, "%s has media type 0x%x\n",
379 value.c_str(), IFM_TYPE(ifmr.ifm_active));
380 retval = (IFM_TYPE(ifmr.ifm_active) == _type);
381 } else if (_type == -1) {
382 devdlog(LOG_DEBUG, "%s has unknown media type\n",
392 const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
393 const string var_list::nothing = "";
396 var_list::get_variable(const string &var) const
398 map<string, string>::const_iterator i;
401 if (i == _vars.end())
402 return (var_list::bogus);
407 var_list::is_set(const string &var) const
409 return (_vars.find(var) != _vars.end());
414 * Removes quoted characters that have made it this far. \" are
415 * converted to ". For all other characters, both \ and following
416 * character. So the string 'fre\:\"' is translated to 'fred\:"'.
419 var_list::fix_value(const std::string &val) const
422 std::string::size_type pos(0);
424 while ((pos = rv.find("\\\"", pos)) != rv.npos) {
431 var_list::set_variable(const string &var, const string &val)
434 * This function gets called WAY too often to justify calling syslog()
435 * each time, even at LOG_DEBUG. Because if syslogd isn't running, it
436 * can consume excessive amounts of systime inside of connect(). Only
437 * log when we're in -d mode.
439 _vars[var] = fix_value(val);
441 devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str());
448 delete_and_clear(_var_list_table);
449 delete_and_clear(_attach_list);
450 delete_and_clear(_detach_list);
451 delete_and_clear(_nomatch_list);
452 delete_and_clear(_notify_list);
456 config::parse_one_file(const char *fn)
458 devdlog(LOG_DEBUG, "Parsing %s\n", fn);
459 yyin = fopen(fn, "r");
461 err(1, "Cannot open config file %s", fn);
464 errx(1, "Cannot parse %s at line %d", fn, lineno);
469 config::parse_files_in_dir(const char *dirname)
475 devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname);
476 dirp = opendir(dirname);
479 readdir(dirp); /* Skip . */
480 readdir(dirp); /* Skip .. */
481 while ((dp = readdir(dirp)) != NULL) {
482 if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
483 snprintf(path, sizeof(path), "%s/%s",
484 dirname, dp->d_name);
485 parse_one_file(path);
493 int operator()(event_proc *const&l1, event_proc *const&l2) const
495 return (l1->get_priority() > l2->get_priority());
500 config::sort_vector(vector<event_proc *> &v)
502 stable_sort(v.begin(), v.end(), epv_greater());
508 vector<string>::const_iterator i;
510 parse_one_file(configfile);
511 for (i = _dir_list.begin(); i != _dir_list.end(); ++i)
512 parse_files_in_dir((*i).c_str());
513 sort_vector(_attach_list);
514 sort_vector(_detach_list);
515 sort_vector(_nomatch_list);
516 sort_vector(_notify_list);
520 config::open_pidfile()
524 if (_pidfile.empty())
526 pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
529 errx(1, "devd already running, pid: %d", (int)otherpid);
530 warn("cannot open pid file");
535 config::write_pidfile()
542 config::close_pidfile()
549 config::remove_pidfile()
556 config::add_attach(int prio, event_proc *p)
558 p->set_priority(prio);
559 _attach_list.push_back(p);
563 config::add_detach(int prio, event_proc *p)
565 p->set_priority(prio);
566 _detach_list.push_back(p);
570 config::add_directory(const char *dir)
572 _dir_list.push_back(string(dir));
576 config::add_nomatch(int prio, event_proc *p)
578 p->set_priority(prio);
579 _nomatch_list.push_back(p);
583 config::add_notify(int prio, event_proc *p)
585 p->set_priority(prio);
586 _notify_list.push_back(p);
590 config::set_pidfile(const char *fn)
596 config::push_var_table()
601 _var_list_table.push_back(vl);
602 devdlog(LOG_DEBUG, "Pushing table\n");
606 config::pop_var_table()
608 delete _var_list_table.back();
609 _var_list_table.pop_back();
610 devdlog(LOG_DEBUG, "Popping table\n");
614 config::set_variable(const char *var, const char *val)
616 _var_list_table.back()->set_variable(var, val);
620 config::get_variable(const string &var)
622 vector<var_list *>::reverse_iterator i;
624 for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) {
625 if ((*i)->is_set(var))
626 return ((*i)->get_variable(var));
628 return (var_list::nothing);
632 config::is_id_char(char ch) const
634 return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
639 config::shell_quote(const string &s)
646 * Enclose the string in $' ' with escapes for ' and / characters making
647 * it one argument and ensuring the shell won't be affected by its
648 * usual list of candidates.
650 buffer.reserve(s.length() * 3 / 2);
654 ce = cs + strlen(cs);
655 for (; cs < ce; cs++) {
657 if (c == '\'' || c == '\\') {
668 config::expand_one(const char *&src, string &dst, bool is_shell)
681 // This is the escape hatch for passing down shell subcommands
685 /* If the string ends before ) is matched , return. */
689 else if (*src == '(')
692 } while (count > 0 && *src);
696 // $[^-A-Za-z_*] -> $\1
697 if (!isalpha(*src) && *src != '_' && *src != '-' && *src != '*') {
703 // $var -> replace with value
706 } while (is_id_char(*src));
707 dst.append(is_shell ? shell_quote(get_variable(buffer)) : get_variable(buffer));
711 config::expand_string(const char *src, const char *prepend, const char *append)
717 * 128 bytes is enough for 2427 of 2438 expansions that happen
718 * while parsing config files, as tested on 2013-01-30.
726 var_at = strchr(src, '$');
727 if (var_at == NULL) {
731 dst.append(src, var_at - src);
733 expand_one(src, dst, prepend == NULL);
743 config::chop_var(char *&buffer, char *&lhs, char *&rhs) const
749 walker = lhs = buffer;
750 while (is_id_char(*walker))
755 if (*walker == '"') {
758 while (*walker && *walker != '"') {
759 // Skip \" ... We leave it in the string and strip the \ later.
760 // due to the super simplistic parser that we have here.
761 if (*walker == '\\' && walker[1] == '"')
771 while (*walker && !isspace(*walker))
777 while (isspace(*walker))
785 config::set_vars(char *buffer)
791 if (!chop_var(buffer, lhs, rhs))
793 set_variable(lhs, rhs);
799 config::find_and_execute(char type)
801 vector<event_proc *> *l;
802 vector<event_proc *>::const_iterator i;
825 devdlog(LOG_DEBUG, "Processing %s event\n", s);
826 for (i = l->begin(); i != l->end(); ++i) {
827 if ((*i)->matches(*this)) {
837 process_event(char *buffer)
845 devdlog(LOG_INFO, "Processing event '%s'\n", buffer);
847 cfg.push_var_table();
848 // $* is the entire line
849 cfg.set_variable("*", buffer - 1);
850 // $_ is the entire line without the initial character
851 cfg.set_variable("_", buffer);
853 // Save the time this happened (as approximated by when we got
854 // around to processing it).
855 gettimeofday(&tv, NULL);
856 asprintf(×tr, "%jd.%06ld", (uintmax_t)tv.tv_sec, tv.tv_usec);
857 cfg.set_variable("timestamp", timestr);
860 // Match doesn't have a device, and the format is a little
861 // different, so handle it separately.
865 sp = cfg.set_vars(sp);
868 //? at location pnp-info on bus
869 sp = strchr(sp, ' ');
871 return; /* Can't happen? */
875 if (strncmp(sp, "at ", 3) == 0)
877 sp = cfg.set_vars(sp);
880 if (strncmp(sp, "on ", 3) == 0)
881 cfg.set_variable("bus", sp + 3);
883 case attach: /*FALLTHROUGH*/
885 sp = strchr(sp, ' ');
887 return; /* Can't happen? */
889 cfg.set_variable("device-name", buffer);
892 if (strncmp(sp, "at ", 3) == 0)
894 sp = cfg.set_vars(sp);
897 if (strncmp(sp, "on ", 3) == 0)
898 cfg.set_variable("bus", sp + 3);
902 cfg.find_and_execute(type);
907 create_socket(const char *name, int socktype)
910 struct sockaddr_un sun;
912 if ((fd = socket(PF_LOCAL, socktype, 0)) < 0)
914 bzero(&sun, sizeof(sun));
915 sun.sun_family = AF_UNIX;
916 strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
917 slen = SUN_LEN(&sun);
919 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
921 if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
924 if (chown(name, 0, 0)) /* XXX - root.wheel */
926 if (chmod(name, 0666))
931 static unsigned int max_clients = 10; /* Default, can be overridden on cmdline. */
932 static unsigned int num_clients;
934 static list<client_t> clients;
937 notify_clients(const char *data, int len)
939 list<client_t>::iterator i;
942 * Deliver the data to all clients. Throw clients overboard at the
943 * first sign of trouble. This reaps clients who've died or closed
944 * their sockets, and also clients who are alive but failing to keep up
945 * (or who are maliciously not reading, to consume buffer space in
946 * kernel memory or tie up the limited number of available connections).
948 for (i = clients.begin(); i != clients.end(); ) {
950 if (i->socktype == SOCK_SEQPACKET)
955 if (send(i->fd, data, len, flags) != len) {
958 i = clients.erase(i);
959 devdlog(LOG_WARNING, "notify_clients: send() failed; "
960 "dropping unresponsive client\n");
971 list<client_t>::iterator i;
974 * Check all existing clients to see if any of them have disappeared.
975 * Normally we reap clients when we get an error trying to send them an
976 * event. This check eliminates the problem of an ever-growing list of
977 * zombie clients because we're never writing to them on a system
978 * without frequent device-change activity.
981 for (i = clients.begin(); i != clients.end(); ) {
983 s = poll(&pfd, 1, 0);
984 if ((s < 0 && s != EINTR ) ||
985 (s > 0 && (pfd.revents & POLLHUP))) {
988 i = clients.erase(i);
989 devdlog(LOG_NOTICE, "check_clients: "
990 "dropping disconnected client\n");
997 new_client(int fd, int socktype)
1003 * First go reap any zombie clients, then accept the connection, and
1004 * shut down the read side to stop clients from consuming kernel memory
1005 * by sending large buffers full of data we'll never read.
1008 s.socktype = socktype;
1009 s.fd = accept(fd, NULL, NULL);
1011 sndbuf_size = CLIENT_BUFSIZE;
1012 if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
1013 sizeof(sndbuf_size)))
1014 err(1, "setsockopt");
1015 shutdown(s.fd, SHUT_RD);
1016 clients.push_back(s);
1027 char buffer[DEVCTL_MAXBUF];
1029 int stream_fd, seqpacket_fd, max_fd;
1034 fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC);
1036 err(1, "Can't open devctl device %s", PATH_DEVCTL);
1037 stream_fd = create_socket(STREAMPIPE, SOCK_STREAM);
1038 seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET);
1040 max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1;
1041 while (!romeo_must_die) {
1042 if (!once && !no_daemon && !daemonize_quick) {
1043 // Check to see if we have any events pending.
1048 rv = select(fd + 1, &fds, NULL, NULL, &tv);
1049 // No events -> we've processed all pending events
1051 devdlog(LOG_DEBUG, "Calling daemon\n");
1052 cfg.remove_pidfile();
1055 cfg.write_pidfile();
1060 * When we've already got the max number of clients, stop
1061 * accepting new connections (don't put the listening sockets in
1062 * the set), shrink the accept() queue to reject connections
1063 * quickly, and poll the existing clients more often, so that we
1064 * notice more quickly when any of them disappear to free up
1069 if (num_clients < max_clients) {
1071 listen(stream_fd, max_clients);
1072 listen(seqpacket_fd, max_clients);
1075 FD_SET(stream_fd, &fds);
1076 FD_SET(seqpacket_fd, &fds);
1081 listen(stream_fd, 0);
1082 listen(seqpacket_fd, 0);
1088 rv = select(max_fd, &fds, NULL, NULL, &tv);
1090 devdlog(LOG_NOTICE, "Events received so far=%u\n",
1100 if (FD_ISSET(fd, &fds)) {
1101 rv = read(fd, buffer, sizeof(buffer) - 1);
1104 if (rv == sizeof(buffer) - 1) {
1105 devdlog(LOG_WARNING, "Warning: "
1106 "available event data exceeded "
1109 notify_clients(buffer, rv);
1111 while (buffer[--rv] == '\n')
1114 process_event(buffer);
1116 catch (const std::length_error& e) {
1117 devdlog(LOG_ERR, "Dropping event %s "
1118 "due to low memory", buffer);
1120 } else if (rv < 0) {
1128 if (FD_ISSET(stream_fd, &fds))
1129 new_client(stream_fd, SOCK_STREAM);
1131 * Aside from the socket type, both sockets use the same
1132 * protocol, so we can process clients the same way.
1134 if (FD_ISSET(seqpacket_fd, &fds))
1135 new_client(seqpacket_fd, SOCK_SEQPACKET);
1137 cfg.remove_pidfile();
1138 close(seqpacket_fd);
1144 * functions that the parser uses.
1147 add_attach(int prio, event_proc *p)
1149 cfg.add_attach(prio, p);
1153 add_detach(int prio, event_proc *p)
1155 cfg.add_detach(prio, p);
1159 add_directory(const char *dir)
1161 cfg.add_directory(dir);
1162 free(const_cast<char *>(dir));
1166 add_nomatch(int prio, event_proc *p)
1168 cfg.add_nomatch(prio, p);
1172 add_notify(int prio, event_proc *p)
1174 cfg.add_notify(prio, p);
1178 add_to_event_proc(event_proc *ep, eps *eps)
1181 ep = new event_proc();
1187 new_action(const char *cmd)
1189 eps *e = new action(cmd);
1190 free(const_cast<char *>(cmd));
1195 new_match(const char *var, const char *re)
1197 eps *e = new match(cfg, var, re);
1198 free(const_cast<char *>(var));
1199 free(const_cast<char *>(re));
1204 new_media(const char *var, const char *re)
1206 eps *e = new media(cfg, var, re);
1207 free(const_cast<char *>(var));
1208 free(const_cast<char *>(re));
1213 set_pidfile(const char *name)
1215 cfg.set_pidfile(name);
1216 free(const_cast<char *>(name));
1220 set_variable(const char *var, const char *val)
1222 cfg.set_variable(var, val);
1223 free(const_cast<char *>(var));
1224 free(const_cast<char *>(val));
1236 * SIGINFO handler. Will print useful statistics to the syslog or stderr
1246 * Local logging function. Prints to syslog if we're daemonized; stderr
1250 devdlog(int priority, const char* fmt, ...)
1254 va_start(argp, fmt);
1256 vfprintf(stderr, fmt, argp);
1257 else if (quiet_mode == 0 || priority <= LOG_WARNING)
1258 vsyslog(priority, fmt, argp);
1265 fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n",
1271 check_devd_enabled()
1277 if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
1278 errx(1, "devctl sysctl missing from kernel!");
1280 warnx("Setting " SYSCTL " to 1000");
1282 if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)))
1283 err(1, "sysctlbyname");
1291 main(int argc, char **argv)
1295 check_devd_enabled();
1296 while ((ch = getopt(argc, argv, "df:l:nq")) != -1) {
1302 configfile = optarg;
1305 max_clients = MAX(1, strtoul(optarg, NULL, 0));
1308 daemonize_quick = 1;
1319 if (!no_daemon && daemonize_quick) {
1322 cfg.write_pidfile();
1324 signal(SIGPIPE, SIG_IGN);
1325 signal(SIGHUP, gensighand);
1326 signal(SIGINT, gensighand);
1327 signal(SIGTERM, gensighand);
1328 signal(SIGINFO, siginfohand);