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 static const char *curr_cf = NULL;
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_IEEE80211, "802.11" },
341 for (int i = 0; media_types[i].ifmt_string != NULL; ++i)
342 if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
343 _type = media_types[i].ifmt_word;
353 media::do_match(config &c)
356 struct ifmediareq ifmr;
360 // Since we can be called from both a device attach/detach
361 // context where device-name is defined and what we want,
362 // as well as from a link status context, where subsystem is
363 // the name of interest, first try device-name and fall back
364 // to subsystem if none exists.
365 value = c.get_variable("device-name");
367 value = c.get_variable("subsystem");
368 devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n",
369 value.c_str(), _type);
373 s = socket(PF_INET, SOCK_DGRAM, 0);
375 memset(&ifmr, 0, sizeof(ifmr));
376 strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
378 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
379 ifmr.ifm_status & IFM_AVALID) {
380 devdlog(LOG_DEBUG, "%s has media type 0x%x\n",
381 value.c_str(), IFM_TYPE(ifmr.ifm_active));
382 retval = (IFM_TYPE(ifmr.ifm_active) == _type);
383 } else if (_type == -1) {
384 devdlog(LOG_DEBUG, "%s has unknown media type\n",
394 const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
395 const string var_list::nothing = "";
398 var_list::get_variable(const string &var) const
400 map<string, string>::const_iterator i;
403 if (i == _vars.end())
404 return (var_list::bogus);
409 var_list::is_set(const string &var) const
411 return (_vars.find(var) != _vars.end());
416 * Removes quoted characters that have made it this far. \" are
417 * converted to ". For all other characters, both \ and following
418 * character. So the string 'fre\:\"' is translated to 'fred\:"'.
421 var_list::fix_value(const std::string &val) const
424 std::string::size_type pos(0);
426 while ((pos = rv.find("\\\"", pos)) != rv.npos) {
433 var_list::set_variable(const string &var, const string &val)
436 * This function gets called WAY too often to justify calling syslog()
437 * each time, even at LOG_DEBUG. Because if syslogd isn't running, it
438 * can consume excessive amounts of systime inside of connect(). Only
439 * log when we're in -d mode.
441 _vars[var] = fix_value(val);
443 devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str());
450 delete_and_clear(_var_list_table);
451 delete_and_clear(_attach_list);
452 delete_and_clear(_detach_list);
453 delete_and_clear(_nomatch_list);
454 delete_and_clear(_notify_list);
458 * Called recursively as new files are included, so current stack of old names
459 * saved in each instance of 'old' on the call stack. Called single threaded
460 * so global varaibles curr_cf and lineno (and all of yacc's parser state)
461 * are safe to access w/o a lock.
464 config::parse_one_file(const char *fn)
468 devdlog(LOG_DEBUG, "Parsing %s\n", fn);
469 yyin = fopen(fn, "r");
473 err(1, "Cannot open config file %s", fn);
476 errx(1, "Cannot parse %s at line %d", fn, lineno);
482 config::parse_files_in_dir(const char *dirname)
488 devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname);
489 dirp = opendir(dirname);
492 readdir(dirp); /* Skip . */
493 readdir(dirp); /* Skip .. */
494 while ((dp = readdir(dirp)) != NULL) {
495 if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
496 snprintf(path, sizeof(path), "%s/%s",
497 dirname, dp->d_name);
498 parse_one_file(path);
506 int operator()(event_proc *const&l1, event_proc *const&l2) const
508 return (l1->get_priority() > l2->get_priority());
513 config::sort_vector(vector<event_proc *> &v)
515 stable_sort(v.begin(), v.end(), epv_greater());
521 vector<string>::const_iterator i;
523 parse_one_file(configfile);
524 for (i = _dir_list.begin(); i != _dir_list.end(); ++i)
525 parse_files_in_dir((*i).c_str());
526 sort_vector(_attach_list);
527 sort_vector(_detach_list);
528 sort_vector(_nomatch_list);
529 sort_vector(_notify_list);
533 config::open_pidfile()
537 if (_pidfile.empty())
539 pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
542 errx(1, "devd already running, pid: %d", (int)otherpid);
543 warn("cannot open pid file");
548 config::write_pidfile()
555 config::close_pidfile()
562 config::remove_pidfile()
569 config::add_attach(int prio, event_proc *p)
571 p->set_priority(prio);
572 _attach_list.push_back(p);
576 config::add_detach(int prio, event_proc *p)
578 p->set_priority(prio);
579 _detach_list.push_back(p);
583 config::add_directory(const char *dir)
585 _dir_list.push_back(string(dir));
589 config::add_nomatch(int prio, event_proc *p)
591 p->set_priority(prio);
592 _nomatch_list.push_back(p);
596 config::add_notify(int prio, event_proc *p)
598 p->set_priority(prio);
599 _notify_list.push_back(p);
603 config::set_pidfile(const char *fn)
609 config::push_var_table()
614 _var_list_table.push_back(vl);
615 devdlog(LOG_DEBUG, "Pushing table\n");
619 config::pop_var_table()
621 delete _var_list_table.back();
622 _var_list_table.pop_back();
623 devdlog(LOG_DEBUG, "Popping table\n");
627 config::set_variable(const char *var, const char *val)
629 _var_list_table.back()->set_variable(var, val);
633 config::get_variable(const string &var)
635 vector<var_list *>::reverse_iterator i;
637 for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) {
638 if ((*i)->is_set(var))
639 return ((*i)->get_variable(var));
641 return (var_list::nothing);
645 config::is_id_char(char ch) const
647 return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
652 config::shell_quote(const string &s)
659 * Enclose the string in $' ' with escapes for ' and / characters making
660 * it one argument and ensuring the shell won't be affected by its
661 * usual list of candidates.
663 buffer.reserve(s.length() * 3 / 2);
667 ce = cs + strlen(cs);
668 for (; cs < ce; cs++) {
670 if (c == '\'' || c == '\\') {
681 config::expand_one(const char *&src, string &dst, bool is_shell)
694 // This is the escape hatch for passing down shell subcommands
698 /* If the string ends before ) is matched , return. */
702 else if (*src == '(')
705 } while (count > 0 && *src);
709 // $[^-A-Za-z_*] -> $\1
710 if (!isalpha(*src) && *src != '_' && *src != '-' && *src != '*') {
716 // $var -> replace with value
719 } while (is_id_char(*src));
720 dst.append(is_shell ? shell_quote(get_variable(buffer)) : get_variable(buffer));
724 config::expand_string(const char *src, const char *prepend, const char *append)
730 * 128 bytes is enough for 2427 of 2438 expansions that happen
731 * while parsing config files, as tested on 2013-01-30.
739 var_at = strchr(src, '$');
740 if (var_at == NULL) {
744 dst.append(src, var_at - src);
746 expand_one(src, dst, prepend == NULL);
756 config::chop_var(char *&buffer, char *&lhs, char *&rhs) const
762 walker = lhs = buffer;
763 while (is_id_char(*walker))
768 if (*walker == '"') {
771 while (*walker && *walker != '"') {
772 // Skip \" ... We leave it in the string and strip the \ later.
773 // due to the super simplistic parser that we have here.
774 if (*walker == '\\' && walker[1] == '"')
784 while (*walker && !isspace(*walker))
790 while (isspace(*walker))
798 config::set_vars(char *buffer)
804 if (!chop_var(buffer, lhs, rhs))
806 set_variable(lhs, rhs);
812 config::find_and_execute(char type)
814 vector<event_proc *> *l;
815 vector<event_proc *>::const_iterator i;
838 devdlog(LOG_DEBUG, "Processing %s event\n", s);
839 for (i = l->begin(); i != l->end(); ++i) {
840 if ((*i)->matches(*this)) {
850 process_event(char *buffer)
858 devdlog(LOG_INFO, "Processing event '%s'\n", buffer);
860 cfg.push_var_table();
861 // $* is the entire line
862 cfg.set_variable("*", buffer - 1);
863 // $_ is the entire line without the initial character
864 cfg.set_variable("_", buffer);
866 // Save the time this happened (as approximated by when we got
867 // around to processing it).
868 gettimeofday(&tv, NULL);
869 asprintf(×tr, "%jd.%06ld", (uintmax_t)tv.tv_sec, tv.tv_usec);
870 cfg.set_variable("timestamp", timestr);
873 // Match doesn't have a device, and the format is a little
874 // different, so handle it separately.
878 sp = cfg.set_vars(sp);
881 //? at location pnp-info on bus
882 sp = strchr(sp, ' ');
884 return; /* Can't happen? */
888 if (strncmp(sp, "at ", 3) == 0)
890 sp = cfg.set_vars(sp);
893 if (strncmp(sp, "on ", 3) == 0)
894 cfg.set_variable("bus", sp + 3);
896 case attach: /*FALLTHROUGH*/
898 sp = strchr(sp, ' ');
900 return; /* Can't happen? */
902 cfg.set_variable("device-name", buffer);
905 if (strncmp(sp, "at ", 3) == 0)
907 sp = cfg.set_vars(sp);
910 if (strncmp(sp, "on ", 3) == 0)
911 cfg.set_variable("bus", sp + 3);
915 cfg.find_and_execute(type);
920 create_socket(const char *name, int socktype)
923 struct sockaddr_un sun;
925 if ((fd = socket(PF_LOCAL, socktype, 0)) < 0)
927 bzero(&sun, sizeof(sun));
928 sun.sun_family = AF_UNIX;
929 strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
930 slen = SUN_LEN(&sun);
932 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
934 if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
937 if (chown(name, 0, 0)) /* XXX - root.wheel */
939 if (chmod(name, 0666))
944 static unsigned int max_clients = 10; /* Default, can be overridden on cmdline. */
945 static unsigned int num_clients;
947 static list<client_t> clients;
950 notify_clients(const char *data, int len)
952 list<client_t>::iterator i;
955 * Deliver the data to all clients. Throw clients overboard at the
956 * first sign of trouble. This reaps clients who've died or closed
957 * their sockets, and also clients who are alive but failing to keep up
958 * (or who are maliciously not reading, to consume buffer space in
959 * kernel memory or tie up the limited number of available connections).
961 for (i = clients.begin(); i != clients.end(); ) {
963 if (i->socktype == SOCK_SEQPACKET)
968 if (send(i->fd, data, len, flags) != len) {
971 i = clients.erase(i);
972 devdlog(LOG_WARNING, "notify_clients: send() failed; "
973 "dropping unresponsive client\n");
984 list<client_t>::iterator i;
987 * Check all existing clients to see if any of them have disappeared.
988 * Normally we reap clients when we get an error trying to send them an
989 * event. This check eliminates the problem of an ever-growing list of
990 * zombie clients because we're never writing to them on a system
991 * without frequent device-change activity.
994 for (i = clients.begin(); i != clients.end(); ) {
996 s = poll(&pfd, 1, 0);
997 if ((s < 0 && s != EINTR ) ||
998 (s > 0 && (pfd.revents & POLLHUP))) {
1001 i = clients.erase(i);
1002 devdlog(LOG_NOTICE, "check_clients: "
1003 "dropping disconnected client\n");
1010 new_client(int fd, int socktype)
1016 * First go reap any zombie clients, then accept the connection, and
1017 * shut down the read side to stop clients from consuming kernel memory
1018 * by sending large buffers full of data we'll never read.
1021 s.socktype = socktype;
1022 s.fd = accept(fd, NULL, NULL);
1024 sndbuf_size = CLIENT_BUFSIZE;
1025 if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
1026 sizeof(sndbuf_size)))
1027 err(1, "setsockopt");
1028 shutdown(s.fd, SHUT_RD);
1029 clients.push_back(s);
1040 char buffer[DEVCTL_MAXBUF];
1042 int stream_fd, seqpacket_fd, max_fd;
1047 fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC);
1049 err(1, "Can't open devctl device %s", PATH_DEVCTL);
1050 stream_fd = create_socket(STREAMPIPE, SOCK_STREAM);
1051 seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET);
1053 max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1;
1054 while (!romeo_must_die) {
1055 if (!once && !no_daemon && !daemonize_quick) {
1056 // Check to see if we have any events pending.
1061 rv = select(fd + 1, &fds, NULL, NULL, &tv);
1062 // No events -> we've processed all pending events
1064 devdlog(LOG_DEBUG, "Calling daemon\n");
1065 cfg.remove_pidfile();
1068 cfg.write_pidfile();
1073 * When we've already got the max number of clients, stop
1074 * accepting new connections (don't put the listening sockets in
1075 * the set), shrink the accept() queue to reject connections
1076 * quickly, and poll the existing clients more often, so that we
1077 * notice more quickly when any of them disappear to free up
1082 if (num_clients < max_clients) {
1084 listen(stream_fd, max_clients);
1085 listen(seqpacket_fd, max_clients);
1088 FD_SET(stream_fd, &fds);
1089 FD_SET(seqpacket_fd, &fds);
1094 listen(stream_fd, 0);
1095 listen(seqpacket_fd, 0);
1101 rv = select(max_fd, &fds, NULL, NULL, &tv);
1103 devdlog(LOG_NOTICE, "Events received so far=%u\n",
1113 if (FD_ISSET(fd, &fds)) {
1114 rv = read(fd, buffer, sizeof(buffer) - 1);
1117 if (rv == sizeof(buffer) - 1) {
1118 devdlog(LOG_WARNING, "Warning: "
1119 "available event data exceeded "
1122 notify_clients(buffer, rv);
1124 while (buffer[--rv] == '\n')
1127 process_event(buffer);
1129 catch (const std::length_error& e) {
1130 devdlog(LOG_ERR, "Dropping event %s "
1131 "due to low memory", buffer);
1133 } else if (rv < 0) {
1141 if (FD_ISSET(stream_fd, &fds))
1142 new_client(stream_fd, SOCK_STREAM);
1144 * Aside from the socket type, both sockets use the same
1145 * protocol, so we can process clients the same way.
1147 if (FD_ISSET(seqpacket_fd, &fds))
1148 new_client(seqpacket_fd, SOCK_SEQPACKET);
1150 cfg.remove_pidfile();
1151 close(seqpacket_fd);
1157 * functions that the parser uses.
1160 add_attach(int prio, event_proc *p)
1162 cfg.add_attach(prio, p);
1166 add_detach(int prio, event_proc *p)
1168 cfg.add_detach(prio, p);
1172 add_directory(const char *dir)
1174 cfg.add_directory(dir);
1175 free(const_cast<char *>(dir));
1179 add_nomatch(int prio, event_proc *p)
1181 cfg.add_nomatch(prio, p);
1185 add_notify(int prio, event_proc *p)
1187 cfg.add_notify(prio, p);
1191 add_to_event_proc(event_proc *ep, eps *eps)
1194 ep = new event_proc();
1200 new_action(const char *cmd)
1202 eps *e = new action(cmd);
1203 free(const_cast<char *>(cmd));
1208 new_match(const char *var, const char *re)
1211 * In FreeBSD 14, we changed the system=kern to system=kernel for the
1212 * resume message to match all the other 'kernel' messages. Generate a
1213 * warning for the life of 14.x that we've 'fixed' the file on the fly,
1214 * but make it a fatal error in 15.x and newer.
1216 if (strcmp(var, "kern") == 0) {
1217 #if __FreeBSD_version < 1500000
1218 devdlog(LOG_WARNING,
1219 "Changing deprecated system='kern' to new name 'kernel' in %s line %d.",
1221 free(const_cast<char *>(var));
1222 var = strdup("kernel");
1223 #elif __FreeBSD_version < 1600000
1224 errx(1, "Encountered deprecated system=\"kern\" rule in %s line %d",
1227 #error "Remove this gross hack"
1231 eps *e = new match(cfg, var, re);
1232 free(const_cast<char *>(var));
1233 free(const_cast<char *>(re));
1238 new_media(const char *var, const char *re)
1240 eps *e = new media(cfg, var, re);
1241 free(const_cast<char *>(var));
1242 free(const_cast<char *>(re));
1247 set_pidfile(const char *name)
1249 cfg.set_pidfile(name);
1250 free(const_cast<char *>(name));
1254 set_variable(const char *var, const char *val)
1256 cfg.set_variable(var, val);
1257 free(const_cast<char *>(var));
1258 free(const_cast<char *>(val));
1270 * SIGINFO handler. Will print useful statistics to the syslog or stderr
1280 * Local logging function. Prints to syslog if we're daemonized; stderr
1284 devdlog(int priority, const char* fmt, ...)
1288 va_start(argp, fmt);
1290 vfprintf(stderr, fmt, argp);
1291 else if (quiet_mode == 0 || priority <= LOG_WARNING)
1292 vsyslog(priority, fmt, argp);
1299 fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n",
1305 check_devd_enabled()
1311 if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
1312 errx(1, "devctl sysctl missing from kernel!");
1314 warnx("Setting " SYSCTL " to 1000");
1316 if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)))
1317 err(1, "sysctlbyname");
1325 main(int argc, char **argv)
1329 check_devd_enabled();
1330 while ((ch = getopt(argc, argv, "df:l:nq")) != -1) {
1336 configfile = optarg;
1339 max_clients = MAX(1, strtoul(optarg, NULL, 0));
1342 daemonize_quick = 1;
1353 if (!no_daemon && daemonize_quick) {
1356 cfg.write_pidfile();
1358 signal(SIGPIPE, SIG_IGN);
1359 signal(SIGHUP, gensighand);
1360 signal(SIGINT, gensighand);
1361 signal(SIGTERM, gensighand);
1362 signal(SIGINFO, siginfohand);