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>
63 #include "devd.h" /* C compatible definitions */
64 #include "devd.hh" /* C++ class definitions */
66 #define PIPE "/var/run/devd.pipe"
67 #define CF "/etc/devd.conf"
68 #define SYSCTL "hw.bus.devctl_disable"
75 static const char notify = '!';
76 static const char nomatch = '?';
77 static const char attach = '+';
78 static const char detach = '-';
83 int romeo_must_die = 0;
85 static void event_loop(void);
86 static void usage(void);
88 template <class T> void
89 delete_and_clear(vector<T *> &v)
91 typename vector<T *>::const_iterator i;
93 for (i = v.begin(); i != v.end(); i++)
100 event_proc::event_proc() : _prio(-1)
105 event_proc::~event_proc()
107 vector<eps *>::const_iterator i;
109 for (i = _epsvec.begin(); i != _epsvec.end(); i++)
115 event_proc::add(eps *eps)
117 _epsvec.push_back(eps);
121 event_proc::matches(config &c)
123 vector<eps *>::const_iterator i;
125 for (i = _epsvec.begin(); i != _epsvec.end(); i++)
126 if (!(*i)->do_match(c))
132 event_proc::run(config &c)
134 vector<eps *>::const_iterator i;
136 for (i = _epsvec.begin(); i != _epsvec.end(); i++)
137 if (!(*i)->do_action(c))
142 action::action(const char *cmd)
154 action::do_action(config &c)
156 string s = c.expand_string(_cmd);
158 fprintf(stderr, "Executing '%s'\n", s.c_str());
163 match::match(config &c, const char *var, const char *re)
168 _re.append(c.expand_string(string(re)));
170 regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB);
179 match::do_match(config &c)
181 string value = c.get_variable(_var);
185 fprintf(stderr, "Testing %s=%s against %s\n", _var.c_str(),
186 value.c_str(), _re.c_str());
188 retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
192 const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
193 const string var_list::nothing = "";
196 var_list::get_variable(const string &var) const
198 map<string, string>::const_iterator i;
201 if (i == _vars.end())
202 return (var_list::bogus);
207 var_list::is_set(const string &var) const
209 return (_vars.find(var) != _vars.end());
213 var_list::set_variable(const string &var, const string &val)
216 fprintf(stderr, "setting %s=%s\n", var.c_str(), val.c_str());
224 delete_and_clear(_var_list_table);
225 delete_and_clear(_attach_list);
226 delete_and_clear(_detach_list);
227 delete_and_clear(_nomatch_list);
228 delete_and_clear(_notify_list);
232 config::parse_one_file(const char *fn)
235 printf("Parsing %s\n", fn);
236 yyin = fopen(fn, "r");
238 err(1, "Cannot open config file %s", fn);
240 errx(1, "Cannot parse %s at line %d", fn, lineno);
245 config::parse_files_in_dir(const char *dirname)
252 printf("Parsing files in %s\n", dirname);
253 dirp = opendir(dirname);
256 readdir(dirp); /* Skip . */
257 readdir(dirp); /* Skip .. */
258 while ((dp = readdir(dirp)) != NULL) {
259 if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
260 snprintf(path, sizeof(path), "%s/%s",
261 dirname, dp->d_name);
262 parse_one_file(path);
269 int operator()(event_proc *const&l1, event_proc *const&l2)
271 return (l1->get_priority() > l2->get_priority());
276 config::sort_vector(vector<event_proc *> &v)
278 sort(v.begin(), v.end(), epv_greater());
284 vector<string>::const_iterator i;
287 for (i = _dir_list.begin(); i != _dir_list.end(); i++)
288 parse_files_in_dir((*i).c_str());
289 sort_vector(_attach_list);
290 sort_vector(_detach_list);
291 sort_vector(_nomatch_list);
292 sort_vector(_notify_list);
296 config::drop_pidfile()
302 fp = fopen(_pidfile.c_str(), "w");
305 fprintf(fp, "%d\n", getpid());
310 config::add_attach(int prio, event_proc *p)
312 p->set_priority(prio);
313 _attach_list.push_back(p);
317 config::add_detach(int prio, event_proc *p)
319 p->set_priority(prio);
320 _detach_list.push_back(p);
324 config::add_directory(const char *dir)
326 _dir_list.push_back(string(dir));
330 config::add_nomatch(int prio, event_proc *p)
332 p->set_priority(prio);
333 _nomatch_list.push_back(p);
337 config::add_notify(int prio, event_proc *p)
339 p->set_priority(prio);
340 _notify_list.push_back(p);
344 config::set_pidfile(const char *fn)
346 _pidfile = string(fn);
350 config::push_var_table()
355 _var_list_table.push_back(vl);
357 fprintf(stderr, "Pushing table\n");
361 config::pop_var_table()
363 delete _var_list_table.back();
364 _var_list_table.pop_back();
366 fprintf(stderr, "Popping table\n");
370 config::set_variable(const char *var, const char *val)
372 _var_list_table.back()->set_variable(var, val);
376 config::get_variable(const string &var)
378 vector<var_list *>::reverse_iterator i;
380 for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); i++) {
381 if ((*i)->is_set(var))
382 return ((*i)->get_variable(var));
384 return (var_list::nothing);
388 config::is_id_char(char ch)
390 return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
395 config::expand_one(const char *&src, string &dst)
398 string buffer, varstr;
403 dst.append(src++, 1);
408 // Not sure if I want to support this or not, so for now we just pass
413 /* If the string ends before ) is matched , return. */
414 while (count > 0 && *src) {
417 else if (*src == '(')
419 dst.append(src++, 1);
425 if (!isalpha(*src)) {
427 dst.append(src++, 1);
431 // $var -> replace with value
433 buffer.append(src++, 1);
434 } while (is_id_char(*src));
435 buffer.append("", 1);
436 varstr = get_variable(buffer.c_str());
441 config::expand_string(const string &s)
449 expand_one(src, dst);
451 dst.append(src++, 1);
459 config::chop_var(char *&buffer, char *&lhs, char *&rhs)
465 walker = lhs = buffer;
466 while (is_id_char(*walker))
471 if (*walker == '"') {
474 while (*walker && *walker != '"')
482 while (*walker && !isspace(*walker))
488 while (isspace(*walker))
496 config::set_vars(char *buffer)
502 if (!chop_var(buffer, lhs, rhs))
504 set_variable(lhs, rhs);
510 config::find_and_execute(char type)
512 vector<event_proc *> *l;
513 vector<event_proc *>::const_iterator i;
537 fprintf(stderr, "Processing %s event\n", s);
538 for (i = l->begin(); i != l->end(); i++) {
539 if ((*i)->matches(*this)) {
549 process_event(char *buffer)
556 fprintf(stderr, "Processing event '%s'\n", buffer);
558 cfg.push_var_table();
559 // No match doesn't have a device, and the format is a little
560 // different, so handle it separately.
563 sp = cfg.set_vars(sp);
566 //? at location pnp-info on bus
567 sp = strchr(sp, ' ');
569 return; /* Can't happen? */
571 if (strncmp(sp, "at ", 3) == 0)
573 sp = cfg.set_vars(sp);
574 if (strncmp(sp, "on ", 3) == 0)
575 cfg.set_variable("bus", sp + 3);
577 case attach: /*FALLTHROUGH*/
579 sp = strchr(sp, ' ');
581 return; /* Can't happen? */
583 cfg.set_variable("device-name", buffer);
584 if (strncmp(sp, "at ", 3) == 0)
586 sp = cfg.set_vars(sp);
587 if (strncmp(sp, "on ", 3) == 0)
588 cfg.set_variable("bus", sp + 3);
592 cfg.find_and_execute(type);
597 create_socket(const char *name)
600 struct sockaddr_un sun;
602 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
604 bzero(&sun, sizeof(sun));
605 sun.sun_family = AF_UNIX;
606 strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
607 slen = SUN_LEN(&sun);
609 if (bind(fd, (struct sockaddr *) & sun, slen) < 0)
612 fchown(fd, 0, 0); /* XXX - root.wheel */
620 notify_clients(const char *data, int len)
623 list<int>::const_iterator i;
625 for (i = clients.begin(); i != clients.end(); i++) {
626 if (write(*i, data, len) <= 0) {
632 for (i = bad.begin(); i != bad.end(); i++)
633 clients.erase(find(clients.begin(), clients.end(), *i));
641 s = accept(fd, NULL, NULL);
643 clients.push_back(s);
651 char buffer[DEVCTL_MAXBUF];
653 int server_fd, max_fd;
657 fd = open(PATH_DEVCTL, O_RDONLY);
659 err(1, "Can't open devctl device %s", PATH_DEVCTL);
660 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
661 err(1, "Can't set close-on-exec flag on devctl");
662 server_fd = create_socket(PIPE);
663 max_fd = max(fd, server_fd) + 1;
667 if (!once && !dflag && !nflag) {
668 // Check to see if we have any events pending.
673 rv = select(fd + 1, &fds, &fds, &fds, &tv);
674 // No events -> we've processed all pending events
677 fprintf(stderr, "Calling daemon\n");
685 FD_SET(server_fd, &fds);
686 rv = select(max_fd, &fds, NULL, NULL, NULL);
692 if (FD_ISSET(fd, &fds)) {
693 rv = read(fd, buffer, sizeof(buffer) - 1);
695 notify_clients(buffer, rv);
697 while (buffer[--rv] == '\n')
699 process_event(buffer);
708 if (FD_ISSET(server_fd, &fds))
709 new_client(server_fd);
715 * functions that the parser uses.
718 add_attach(int prio, event_proc *p)
720 cfg.add_attach(prio, p);
724 add_detach(int prio, event_proc *p)
726 cfg.add_detach(prio, p);
730 add_directory(const char *dir)
732 cfg.add_directory(dir);
733 free(const_cast<char *>(dir));
737 add_nomatch(int prio, event_proc *p)
739 cfg.add_nomatch(prio, p);
743 add_notify(int prio, event_proc *p)
745 cfg.add_notify(prio, p);
749 add_to_event_proc(event_proc *ep, eps *eps)
752 ep = new event_proc();
758 new_action(const char *cmd)
760 eps *e = new action(cmd);
761 free(const_cast<char *>(cmd));
766 new_match(const char *var, const char *re)
768 eps *e = new match(cfg, var, re);
769 free(const_cast<char *>(var));
770 free(const_cast<char *>(re));
775 set_pidfile(const char *name)
777 cfg.set_pidfile(name);
778 free(const_cast<char *>(name));
782 set_variable(const char *var, const char *val)
784 cfg.set_variable(var, val);
785 free(const_cast<char *>(var));
786 free(const_cast<char *>(val));
801 fprintf(stderr, "usage: %s [-Ddn]\n", getprogname());
812 if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
813 errx(1, "devctl sysctl missing from kernel!");
815 warnx("Setting " SYSCTL " to 0");
817 sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
825 main(int argc, char **argv)
829 check_devd_enabled();
830 while ((ch = getopt(argc, argv, "Ddn")) != -1) {
847 if (!dflag && nflag) {
851 signal(SIGHUP, gensighand);
852 signal(SIGINT, gensighand);
853 signal(SIGTERM, gensighand);