2 * Copyright (c) 2002-2003 M. Warner Losh.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * DEVD control daemon.
32 // o devd.conf and devd man pages need a lot of help:
33 // - devd needs to document the unix domain socket
34 // - devd.conf needs more details on the supported statements.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 #include <sys/types.h>
64 #include "devd.h" /* C compatible definitions */
65 #include "devd.hh" /* C++ class definitions */
67 #define PIPE "/var/run/devd.pipe"
68 #define CF "/etc/devd.conf"
69 #define SYSCTL "hw.bus.devctl_disable"
76 static const char notify = '!';
77 static const char nomatch = '?';
78 static const char attach = '+';
79 static const char detach = '-';
84 int romeo_must_die = 0;
86 static void event_loop(void);
87 static void usage(void);
89 template <class T> void
90 delete_and_clear(vector<T *> &v)
92 typename vector<T *>::const_iterator i;
94 for (i = v.begin(); i != v.end(); i++)
101 event_proc::event_proc() : _prio(-1)
106 event_proc::~event_proc()
108 vector<eps *>::const_iterator i;
110 for (i = _epsvec.begin(); i != _epsvec.end(); i++)
116 event_proc::add(eps *eps)
118 _epsvec.push_back(eps);
122 event_proc::matches(config &c)
124 vector<eps *>::const_iterator i;
126 for (i = _epsvec.begin(); i != _epsvec.end(); i++)
127 if (!(*i)->do_match(c))
133 event_proc::run(config &c)
135 vector<eps *>::const_iterator i;
137 for (i = _epsvec.begin(); i != _epsvec.end(); i++)
138 if (!(*i)->do_action(c))
143 action::action(const char *cmd)
155 action::do_action(config &c)
157 string s = c.expand_string(_cmd);
159 fprintf(stderr, "Executing '%s'\n", s.c_str());
164 match::match(config &c, const char *var, const char *re)
169 _re.append(c.expand_string(string(re)));
171 regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB);
180 match::do_match(config &c)
182 string value = c.get_variable(_var);
186 fprintf(stderr, "Testing %s=%s against %s\n", _var.c_str(),
187 value.c_str(), _re.c_str());
189 retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
193 const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
194 const string var_list::nothing = "";
197 var_list::get_variable(const string &var) const
199 map<string, string>::const_iterator i;
202 if (i == _vars.end())
203 return (var_list::bogus);
208 var_list::is_set(const string &var) const
210 return (_vars.find(var) != _vars.end());
214 var_list::set_variable(const string &var, const string &val)
217 fprintf(stderr, "setting %s=%s\n", var.c_str(), val.c_str());
225 delete_and_clear(_var_list_table);
226 delete_and_clear(_attach_list);
227 delete_and_clear(_detach_list);
228 delete_and_clear(_nomatch_list);
229 delete_and_clear(_notify_list);
233 config::parse_one_file(const char *fn)
236 printf("Parsing %s\n", fn);
237 yyin = fopen(fn, "r");
239 err(1, "Cannot open config file %s", fn);
241 errx(1, "Cannot parse %s at line %d", fn, lineno);
246 config::parse_files_in_dir(const char *dirname)
253 printf("Parsing files in %s\n", dirname);
254 dirp = opendir(dirname);
257 readdir(dirp); /* Skip . */
258 readdir(dirp); /* Skip .. */
259 while ((dp = readdir(dirp)) != NULL) {
260 if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
261 snprintf(path, sizeof(path), "%s/%s",
262 dirname, dp->d_name);
263 parse_one_file(path);
270 int operator()(event_proc *const&l1, event_proc *const&l2)
272 return (l1->get_priority() > l2->get_priority());
277 config::sort_vector(vector<event_proc *> &v)
279 sort(v.begin(), v.end(), epv_greater());
285 vector<string>::const_iterator i;
288 for (i = _dir_list.begin(); i != _dir_list.end(); i++)
289 parse_files_in_dir((*i).c_str());
290 sort_vector(_attach_list);
291 sort_vector(_detach_list);
292 sort_vector(_nomatch_list);
293 sort_vector(_notify_list);
297 config::drop_pidfile()
303 fp = fopen(_pidfile.c_str(), "w");
306 fprintf(fp, "%d\n", getpid());
311 config::add_attach(int prio, event_proc *p)
313 p->set_priority(prio);
314 _attach_list.push_back(p);
318 config::add_detach(int prio, event_proc *p)
320 p->set_priority(prio);
321 _detach_list.push_back(p);
325 config::add_directory(const char *dir)
327 _dir_list.push_back(string(dir));
331 config::add_nomatch(int prio, event_proc *p)
333 p->set_priority(prio);
334 _nomatch_list.push_back(p);
338 config::add_notify(int prio, event_proc *p)
340 p->set_priority(prio);
341 _notify_list.push_back(p);
345 config::set_pidfile(const char *fn)
347 _pidfile = string(fn);
351 config::push_var_table()
356 _var_list_table.push_back(vl);
358 fprintf(stderr, "Pushing table\n");
362 config::pop_var_table()
364 delete _var_list_table.back();
365 _var_list_table.pop_back();
367 fprintf(stderr, "Popping table\n");
371 config::set_variable(const char *var, const char *val)
373 _var_list_table.back()->set_variable(var, val);
377 config::get_variable(const string &var)
379 vector<var_list *>::reverse_iterator i;
381 for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); i++) {
382 if ((*i)->is_set(var))
383 return ((*i)->get_variable(var));
385 return (var_list::nothing);
389 config::is_id_char(char ch)
391 return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
396 config::expand_one(const char *&src, string &dst)
399 string buffer, varstr;
404 dst.append(src++, 1);
409 // Not sure if I want to support this or not, so for now we just pass
414 /* If the string ends before ) is matched , return. */
415 while (count > 0 && *src) {
418 else if (*src == '(')
420 dst.append(src++, 1);
426 if (!isalpha(*src)) {
428 dst.append(src++, 1);
432 // $var -> replace with value
434 buffer.append(src++, 1);
435 } while (is_id_char(*src));
436 buffer.append("", 1);
437 varstr = get_variable(buffer.c_str());
442 config::expand_string(const string &s)
450 expand_one(src, dst);
452 dst.append(src++, 1);
460 config::chop_var(char *&buffer, char *&lhs, char *&rhs)
466 walker = lhs = buffer;
467 while (is_id_char(*walker))
472 if (*walker == '"') {
475 while (*walker && *walker != '"')
483 while (*walker && !isspace(*walker))
489 while (isspace(*walker))
497 config::set_vars(char *buffer)
503 if (!chop_var(buffer, lhs, rhs))
505 set_variable(lhs, rhs);
511 config::find_and_execute(char type)
513 vector<event_proc *> *l;
514 vector<event_proc *>::const_iterator i;
538 fprintf(stderr, "Processing %s event\n", s);
539 for (i = l->begin(); i != l->end(); i++) {
540 if ((*i)->matches(*this)) {
550 process_event(char *buffer)
557 fprintf(stderr, "Processing event '%s'\n", buffer);
559 cfg.push_var_table();
560 // No match doesn't have a device, and the format is a little
561 // different, so handle it separately.
564 sp = cfg.set_vars(sp);
567 //? at location pnp-info on bus
568 sp = strchr(sp, ' ');
570 return; /* Can't happen? */
572 if (strncmp(sp, "at ", 3) == 0)
574 sp = cfg.set_vars(sp);
575 if (strncmp(sp, "on ", 3) == 0)
576 cfg.set_variable("bus", sp + 3);
578 case attach: /*FALLTHROUGH*/
580 sp = strchr(sp, ' ');
582 return; /* Can't happen? */
584 cfg.set_variable("device-name", buffer);
585 if (strncmp(sp, "at ", 3) == 0)
587 sp = cfg.set_vars(sp);
588 if (strncmp(sp, "on ", 3) == 0)
589 cfg.set_variable("bus", sp + 3);
593 cfg.find_and_execute(type);
598 create_socket(const char *name)
601 struct sockaddr_un sun;
603 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
605 bzero(&sun, sizeof(sun));
606 sun.sun_family = AF_UNIX;
607 strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
608 slen = SUN_LEN(&sun);
610 if (bind(fd, (struct sockaddr *) & sun, slen) < 0)
613 fchown(fd, 0, 0); /* XXX - root.wheel */
621 notify_clients(const char *data, int len)
624 list<int>::const_iterator i;
626 for (i = clients.begin(); i != clients.end(); i++) {
627 if (write(*i, data, len) <= 0) {
633 for (i = bad.begin(); i != bad.end(); i++)
634 clients.erase(find(clients.begin(), clients.end(), *i));
642 s = accept(fd, NULL, NULL);
644 clients.push_back(s);
652 char buffer[DEVCTL_MAXBUF];
654 int server_fd, max_fd;
658 fd = open(PATH_DEVCTL, O_RDONLY);
660 err(1, "Can't open devctl device %s", PATH_DEVCTL);
661 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
662 err(1, "Can't set close-on-exec flag on devctl");
663 server_fd = create_socket(PIPE);
664 max_fd = max(fd, server_fd) + 1;
668 if (!once && !dflag && !nflag) {
669 // Check to see if we have any events pending.
674 rv = select(fd + 1, &fds, &fds, &fds, &tv);
675 // No events -> we've processed all pending events
678 fprintf(stderr, "Calling daemon\n");
686 FD_SET(server_fd, &fds);
687 rv = select(max_fd, &fds, NULL, NULL, NULL);
693 if (FD_ISSET(fd, &fds)) {
694 rv = read(fd, buffer, sizeof(buffer) - 1);
696 notify_clients(buffer, rv);
698 while (buffer[--rv] == '\n')
700 process_event(buffer);
709 if (FD_ISSET(server_fd, &fds))
710 new_client(server_fd);
716 * functions that the parser uses.
719 add_attach(int prio, event_proc *p)
721 cfg.add_attach(prio, p);
725 add_detach(int prio, event_proc *p)
727 cfg.add_detach(prio, p);
731 add_directory(const char *dir)
733 cfg.add_directory(dir);
734 free(const_cast<char *>(dir));
738 add_nomatch(int prio, event_proc *p)
740 cfg.add_nomatch(prio, p);
744 add_notify(int prio, event_proc *p)
746 cfg.add_notify(prio, p);
750 add_to_event_proc(event_proc *ep, eps *eps)
753 ep = new event_proc();
759 new_action(const char *cmd)
761 eps *e = new action(cmd);
762 free(const_cast<char *>(cmd));
767 new_match(const char *var, const char *re)
769 eps *e = new match(cfg, var, re);
770 free(const_cast<char *>(var));
771 free(const_cast<char *>(re));
776 set_pidfile(const char *name)
778 cfg.set_pidfile(name);
779 free(const_cast<char *>(name));
783 set_variable(const char *var, const char *val)
785 cfg.set_variable(var, val);
786 free(const_cast<char *>(var));
787 free(const_cast<char *>(val));
802 fprintf(stderr, "usage: %s [-Ddn]\n", getprogname());
813 if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
814 errx(1, "devctl sysctl missing from kernel!");
816 warnx("Setting " SYSCTL " to 0");
818 sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
826 main(int argc, char **argv)
830 check_devd_enabled();
831 while ((ch = getopt(argc, argv, "Ddn")) != -1) {
848 if (!dflag && nflag) {
852 signal(SIGPIPE, SIG_IGN);
853 signal(SIGHUP, gensighand);
854 signal(SIGINT, gensighand);
855 signal(SIGTERM, gensighand);