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 {
145 static const char notify = '!';
146 static const char nomatch = '?';
147 static const char attach = '+';
148 static const char detach = '-';
150 static struct pidfh *pfh;
152 static int no_daemon = 0;
153 static int daemonize_quick = 0;
154 static int quiet_mode = 0;
155 static unsigned total_events = 0;
156 static volatile sig_atomic_t got_siginfo = 0;
157 static volatile sig_atomic_t romeo_must_die = 0;
159 static const char *configfile = CF;
161 static void devdlog(int priority, const char* message, ...)
163 static void event_loop(void);
164 static void usage(void) __dead2;
166 template <class T> void
167 delete_and_clear(vector<T *> &v)
169 typename vector<T *>::const_iterator i;
171 for (i = v.begin(); i != v.end(); ++i)
178 event_proc::event_proc() : _prio(-1)
183 event_proc::~event_proc()
185 delete_and_clear(_epsvec);
189 event_proc::add(eps *eps)
191 _epsvec.push_back(eps);
195 event_proc::matches(config &c) const
197 vector<eps *>::const_iterator i;
199 for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
200 if (!(*i)->do_match(c))
206 event_proc::run(config &c) const
208 vector<eps *>::const_iterator i;
210 for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
211 if (!(*i)->do_action(c))
216 action::action(const char *cmd)
228 my_system(const char *command)
232 struct sigaction ign, intact, quitact;
233 sigset_t newsigblock, oldsigblock;
235 if (!command) /* just checking... */
239 * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
240 * existing signal dispositions.
242 ign.sa_handler = SIG_IGN;
243 ::sigemptyset(&ign.sa_mask);
245 ::sigaction(SIGINT, &ign, &intact);
246 ::sigaction(SIGQUIT, &ign, &quitact);
247 ::sigemptyset(&newsigblock);
248 ::sigaddset(&newsigblock, SIGCHLD);
249 ::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
250 switch (pid = ::fork()) {
255 * Restore original signal dispositions and exec the command.
257 ::sigaction(SIGINT, &intact, NULL);
258 ::sigaction(SIGQUIT, &quitact, NULL);
259 ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
261 * Close the PID file, and all other open descriptors.
262 * Inherit std{in,out,err} only.
266 ::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
268 default: /* parent */
271 pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0);
272 } while (pid == -1 && errno == EINTR);
275 ::sigaction(SIGINT, &intact, NULL);
276 ::sigaction(SIGQUIT, &quitact, NULL);
277 ::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
278 return (pid == -1 ? -1 : pstat);
282 action::do_action(config &c)
284 string s = c.expand_string(_cmd.c_str());
285 devdlog(LOG_INFO, "Executing '%s'\n", s.c_str());
286 my_system(s.c_str());
290 match::match(config &c, const char *var, const char *re) :
293 _re(c.expand_string(_inv ? re + 1 : re, "^", "$"))
295 regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE);
304 match::do_match(config &c)
306 const string &value = c.get_variable(_var);
310 * This function gets called WAY too often to justify calling syslog()
311 * each time, even at LOG_DEBUG. Because if syslogd isn't running, it
312 * can consume excessive amounts of systime inside of connect(). Only
313 * log when we're in -d mode.
316 devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n",
317 _var.c_str(), value.c_str(), _re.c_str(), _inv);
320 retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
322 retval = (retval == 0) ? 1 : 0;
327 #include <sys/sockio.h>
329 #include <net/if_media.h>
331 media::media(config &, const char *var, const char *type)
332 : _var(var), _type(-1)
334 static struct ifmedia_description media_types[] = {
335 { IFM_ETHER, "Ethernet" },
336 { IFM_TOKEN, "Tokenring" },
337 { IFM_FDDI, "FDDI" },
338 { IFM_IEEE80211, "802.11" },
343 for (int i = 0; media_types[i].ifmt_string != NULL; ++i)
344 if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
345 _type = media_types[i].ifmt_word;
355 media::do_match(config &c)
358 struct ifmediareq ifmr;
362 // Since we can be called from both a device attach/detach
363 // context where device-name is defined and what we want,
364 // as well as from a link status context, where subsystem is
365 // the name of interest, first try device-name and fall back
366 // to subsystem if none exists.
367 value = c.get_variable("device-name");
369 value = c.get_variable("subsystem");
370 devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n",
371 value.c_str(), _type);
375 s = socket(PF_INET, SOCK_DGRAM, 0);
377 memset(&ifmr, 0, sizeof(ifmr));
378 strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
380 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
381 ifmr.ifm_status & IFM_AVALID) {
382 devdlog(LOG_DEBUG, "%s has media type 0x%x\n",
383 value.c_str(), IFM_TYPE(ifmr.ifm_active));
384 retval = (IFM_TYPE(ifmr.ifm_active) == _type);
385 } else if (_type == -1) {
386 devdlog(LOG_DEBUG, "%s has unknown media type\n",
396 const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
397 const string var_list::nothing = "";
400 var_list::get_variable(const string &var) const
402 map<string, string>::const_iterator i;
405 if (i == _vars.end())
406 return (var_list::bogus);
411 var_list::is_set(const string &var) const
413 return (_vars.find(var) != _vars.end());
418 * Removes quoted characters that have made it this far. \" are
419 * converted to ". For all other characters, both \ and following
420 * character. So the string 'fre\:\"' is translated to 'fred\:"'.
423 var_list::fix_value(const std::string &val) const
426 std::string::size_type pos(0);
428 while ((pos = rv.find("\\\"", pos)) != rv.npos) {
435 var_list::set_variable(const string &var, const string &val)
438 * This function gets called WAY too often to justify calling syslog()
439 * each time, even at LOG_DEBUG. Because if syslogd isn't running, it
440 * can consume excessive amounts of systime inside of connect(). Only
441 * log when we're in -d mode.
443 _vars[var] = fix_value(val);
445 devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str());
452 delete_and_clear(_var_list_table);
453 delete_and_clear(_attach_list);
454 delete_and_clear(_detach_list);
455 delete_and_clear(_nomatch_list);
456 delete_and_clear(_notify_list);
460 config::parse_one_file(const char *fn)
462 devdlog(LOG_DEBUG, "Parsing %s\n", fn);
463 yyin = fopen(fn, "r");
465 err(1, "Cannot open config file %s", fn);
468 errx(1, "Cannot parse %s at line %d", fn, lineno);
473 config::parse_files_in_dir(const char *dirname)
479 devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname);
480 dirp = opendir(dirname);
483 readdir(dirp); /* Skip . */
484 readdir(dirp); /* Skip .. */
485 while ((dp = readdir(dirp)) != NULL) {
486 if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
487 snprintf(path, sizeof(path), "%s/%s",
488 dirname, dp->d_name);
489 parse_one_file(path);
497 int operator()(event_proc *const&l1, event_proc *const&l2) const
499 return (l1->get_priority() > l2->get_priority());
504 config::sort_vector(vector<event_proc *> &v)
506 stable_sort(v.begin(), v.end(), epv_greater());
512 vector<string>::const_iterator i;
514 parse_one_file(configfile);
515 for (i = _dir_list.begin(); i != _dir_list.end(); ++i)
516 parse_files_in_dir((*i).c_str());
517 sort_vector(_attach_list);
518 sort_vector(_detach_list);
519 sort_vector(_nomatch_list);
520 sort_vector(_notify_list);
524 config::open_pidfile()
528 if (_pidfile.empty())
530 pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
533 errx(1, "devd already running, pid: %d", (int)otherpid);
534 warn("cannot open pid file");
539 config::write_pidfile()
546 config::close_pidfile()
553 config::remove_pidfile()
560 config::add_attach(int prio, event_proc *p)
562 p->set_priority(prio);
563 _attach_list.push_back(p);
567 config::add_detach(int prio, event_proc *p)
569 p->set_priority(prio);
570 _detach_list.push_back(p);
574 config::add_directory(const char *dir)
576 _dir_list.push_back(string(dir));
580 config::add_nomatch(int prio, event_proc *p)
582 p->set_priority(prio);
583 _nomatch_list.push_back(p);
587 config::add_notify(int prio, event_proc *p)
589 p->set_priority(prio);
590 _notify_list.push_back(p);
594 config::set_pidfile(const char *fn)
600 config::push_var_table()
605 _var_list_table.push_back(vl);
606 devdlog(LOG_DEBUG, "Pushing table\n");
610 config::pop_var_table()
612 delete _var_list_table.back();
613 _var_list_table.pop_back();
614 devdlog(LOG_DEBUG, "Popping table\n");
618 config::set_variable(const char *var, const char *val)
620 _var_list_table.back()->set_variable(var, val);
624 config::get_variable(const string &var)
626 vector<var_list *>::reverse_iterator i;
628 for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) {
629 if ((*i)->is_set(var))
630 return ((*i)->get_variable(var));
632 return (var_list::nothing);
636 config::is_id_char(char ch) const
638 return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
643 config::expand_one(const char *&src, string &dst)
656 // Not sure if I want to support this or not, so for now we just pass
661 /* If the string ends before ) is matched , return. */
662 while (count > 0 && *src) {
665 else if (*src == '(')
672 // $[^-A-Za-z_*] -> $\1
673 if (!isalpha(*src) && *src != '_' && *src != '-' && *src != '*') {
679 // $var -> replace with value
682 } while (is_id_char(*src));
683 dst.append(get_variable(buffer));
687 config::expand_string(const char *src, const char *prepend, const char *append)
693 * 128 bytes is enough for 2427 of 2438 expansions that happen
694 * while parsing config files, as tested on 2013-01-30.
702 var_at = strchr(src, '$');
703 if (var_at == NULL) {
707 dst.append(src, var_at - src);
709 expand_one(src, dst);
719 config::chop_var(char *&buffer, char *&lhs, char *&rhs) const
725 walker = lhs = buffer;
726 while (is_id_char(*walker))
731 if (*walker == '"') {
734 while (*walker && *walker != '"') {
735 // Skip \" ... We leave it in the string and strip the \ later.
736 // due to the super simplistic parser that we have here.
737 if (*walker == '\\' && walker[1] == '"')
747 while (*walker && !isspace(*walker))
753 while (isspace(*walker))
761 config::set_vars(char *buffer)
767 if (!chop_var(buffer, lhs, rhs))
769 set_variable(lhs, rhs);
775 config::find_and_execute(char type)
777 vector<event_proc *> *l;
778 vector<event_proc *>::const_iterator i;
801 devdlog(LOG_DEBUG, "Processing %s event\n", s);
802 for (i = l->begin(); i != l->end(); ++i) {
803 if ((*i)->matches(*this)) {
813 process_event(char *buffer)
821 devdlog(LOG_INFO, "Processing event '%s'\n", buffer);
823 cfg.push_var_table();
824 // $* is the entire line
825 cfg.set_variable("*", buffer - 1);
826 // $_ is the entire line without the initial character
827 cfg.set_variable("_", buffer);
829 // Save the time this happened (as approximated by when we got
830 // around to processing it).
831 gettimeofday(&tv, NULL);
832 asprintf(×tr, "%jd.%06ld", (uintmax_t)tv.tv_sec, tv.tv_usec);
833 cfg.set_variable("timestamp", timestr);
836 // Match doesn't have a device, and the format is a little
837 // different, so handle it separately.
841 sp = cfg.set_vars(sp);
844 //? at location pnp-info on bus
845 sp = strchr(sp, ' ');
847 return; /* Can't happen? */
851 if (strncmp(sp, "at ", 3) == 0)
853 sp = cfg.set_vars(sp);
856 if (strncmp(sp, "on ", 3) == 0)
857 cfg.set_variable("bus", sp + 3);
859 case attach: /*FALLTHROUGH*/
861 sp = strchr(sp, ' ');
863 return; /* Can't happen? */
865 cfg.set_variable("device-name", buffer);
868 if (strncmp(sp, "at ", 3) == 0)
870 sp = cfg.set_vars(sp);
873 if (strncmp(sp, "on ", 3) == 0)
874 cfg.set_variable("bus", sp + 3);
878 cfg.find_and_execute(type);
883 create_socket(const char *name, int socktype)
886 struct sockaddr_un sun;
888 if ((fd = socket(PF_LOCAL, socktype, 0)) < 0)
890 bzero(&sun, sizeof(sun));
891 sun.sun_family = AF_UNIX;
892 strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
893 slen = SUN_LEN(&sun);
895 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
897 if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
900 if (chown(name, 0, 0)) /* XXX - root.wheel */
902 if (chmod(name, 0666))
907 unsigned int max_clients = 10; /* Default, can be overridden on cmdline. */
908 unsigned int num_clients;
910 list<client_t> clients;
913 notify_clients(const char *data, int len)
915 list<client_t>::iterator i;
918 * Deliver the data to all clients. Throw clients overboard at the
919 * first sign of trouble. This reaps clients who've died or closed
920 * their sockets, and also clients who are alive but failing to keep up
921 * (or who are maliciously not reading, to consume buffer space in
922 * kernel memory or tie up the limited number of available connections).
924 for (i = clients.begin(); i != clients.end(); ) {
926 if (i->socktype == SOCK_SEQPACKET)
931 if (send(i->fd, data, len, flags) != len) {
934 i = clients.erase(i);
935 devdlog(LOG_WARNING, "notify_clients: send() failed; "
936 "dropping unresponsive client\n");
947 list<client_t>::iterator i;
950 * Check all existing clients to see if any of them have disappeared.
951 * Normally we reap clients when we get an error trying to send them an
952 * event. This check eliminates the problem of an ever-growing list of
953 * zombie clients because we're never writing to them on a system
954 * without frequent device-change activity.
957 for (i = clients.begin(); i != clients.end(); ) {
959 s = poll(&pfd, 1, 0);
960 if ((s < 0 && s != EINTR ) ||
961 (s > 0 && (pfd.revents & POLLHUP))) {
964 i = clients.erase(i);
965 devdlog(LOG_NOTICE, "check_clients: "
966 "dropping disconnected client\n");
973 new_client(int fd, int socktype)
979 * First go reap any zombie clients, then accept the connection, and
980 * shut down the read side to stop clients from consuming kernel memory
981 * by sending large buffers full of data we'll never read.
984 s.socktype = socktype;
985 s.fd = accept(fd, NULL, NULL);
987 sndbuf_size = CLIENT_BUFSIZE;
988 if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
989 sizeof(sndbuf_size)))
990 err(1, "setsockopt");
991 shutdown(s.fd, SHUT_RD);
992 clients.push_back(s);
1003 char buffer[DEVCTL_MAXBUF];
1005 int stream_fd, seqpacket_fd, max_fd;
1010 fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC);
1012 err(1, "Can't open devctl device %s", PATH_DEVCTL);
1013 stream_fd = create_socket(STREAMPIPE, SOCK_STREAM);
1014 seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET);
1016 max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1;
1017 while (!romeo_must_die) {
1018 if (!once && !no_daemon && !daemonize_quick) {
1019 // Check to see if we have any events pending.
1024 rv = select(fd + 1, &fds, NULL, NULL, &tv);
1025 // No events -> we've processed all pending events
1027 devdlog(LOG_DEBUG, "Calling daemon\n");
1028 cfg.remove_pidfile();
1031 cfg.write_pidfile();
1036 * When we've already got the max number of clients, stop
1037 * accepting new connections (don't put the listening sockets in
1038 * the set), shrink the accept() queue to reject connections
1039 * quickly, and poll the existing clients more often, so that we
1040 * notice more quickly when any of them disappear to free up
1045 if (num_clients < max_clients) {
1047 listen(stream_fd, max_clients);
1048 listen(seqpacket_fd, max_clients);
1051 FD_SET(stream_fd, &fds);
1052 FD_SET(seqpacket_fd, &fds);
1057 listen(stream_fd, 0);
1058 listen(seqpacket_fd, 0);
1064 rv = select(max_fd, &fds, NULL, NULL, &tv);
1066 devdlog(LOG_NOTICE, "Events received so far=%u\n",
1076 if (FD_ISSET(fd, &fds)) {
1077 rv = read(fd, buffer, sizeof(buffer) - 1);
1080 if (rv == sizeof(buffer) - 1) {
1081 devdlog(LOG_WARNING, "Warning: "
1082 "available event data exceeded "
1085 notify_clients(buffer, rv);
1087 while (buffer[--rv] == '\n')
1090 process_event(buffer);
1092 catch (std::length_error e) {
1093 devdlog(LOG_ERR, "Dropping event %s "
1094 "due to low memory", buffer);
1096 } else if (rv < 0) {
1104 if (FD_ISSET(stream_fd, &fds))
1105 new_client(stream_fd, SOCK_STREAM);
1107 * Aside from the socket type, both sockets use the same
1108 * protocol, so we can process clients the same way.
1110 if (FD_ISSET(seqpacket_fd, &fds))
1111 new_client(seqpacket_fd, SOCK_SEQPACKET);
1113 cfg.remove_pidfile();
1114 close(seqpacket_fd);
1120 * functions that the parser uses.
1123 add_attach(int prio, event_proc *p)
1125 cfg.add_attach(prio, p);
1129 add_detach(int prio, event_proc *p)
1131 cfg.add_detach(prio, p);
1135 add_directory(const char *dir)
1137 cfg.add_directory(dir);
1138 free(const_cast<char *>(dir));
1142 add_nomatch(int prio, event_proc *p)
1144 cfg.add_nomatch(prio, p);
1148 add_notify(int prio, event_proc *p)
1150 cfg.add_notify(prio, p);
1154 add_to_event_proc(event_proc *ep, eps *eps)
1157 ep = new event_proc();
1163 new_action(const char *cmd)
1165 eps *e = new action(cmd);
1166 free(const_cast<char *>(cmd));
1171 new_match(const char *var, const char *re)
1173 eps *e = new match(cfg, var, re);
1174 free(const_cast<char *>(var));
1175 free(const_cast<char *>(re));
1180 new_media(const char *var, const char *re)
1182 eps *e = new media(cfg, var, re);
1183 free(const_cast<char *>(var));
1184 free(const_cast<char *>(re));
1189 set_pidfile(const char *name)
1191 cfg.set_pidfile(name);
1192 free(const_cast<char *>(name));
1196 set_variable(const char *var, const char *val)
1198 cfg.set_variable(var, val);
1199 free(const_cast<char *>(var));
1200 free(const_cast<char *>(val));
1212 * SIGINFO handler. Will print useful statistics to the syslog or stderr
1222 * Local logging function. Prints to syslog if we're daemonized; stderr
1226 devdlog(int priority, const char* fmt, ...)
1230 va_start(argp, fmt);
1232 vfprintf(stderr, fmt, argp);
1233 else if (quiet_mode == 0 || priority <= LOG_WARNING)
1234 vsyslog(priority, fmt, argp);
1241 fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n",
1247 check_devd_enabled()
1253 if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
1254 errx(1, "devctl sysctl missing from kernel!");
1256 warnx("Setting " SYSCTL " to 1000");
1258 if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)))
1259 err(1, "sysctlbyname");
1267 main(int argc, char **argv)
1271 check_devd_enabled();
1272 while ((ch = getopt(argc, argv, "df:l:nq")) != -1) {
1278 configfile = optarg;
1281 max_clients = MAX(1, strtoul(optarg, NULL, 0));
1284 daemonize_quick = 1;
1295 if (!no_daemon && daemonize_quick) {
1298 cfg.write_pidfile();
1300 signal(SIGPIPE, SIG_IGN);
1301 signal(SIGHUP, gensighand);
1302 signal(SIGINT, gensighand);
1303 signal(SIGTERM, gensighand);
1304 signal(SIGINFO, siginfohand);