2002-12-07 08:04:36 +00:00
|
|
|
|
/*-
|
2003-04-21 04:00:01 +00:00
|
|
|
|
* Copyright (c) 2002-2003 M. Warner Losh.
|
2002-12-07 08:04:36 +00:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* DEVD control daemon.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// TODO list:
|
|
|
|
|
// o devd.conf and devd man pages need a lot of help:
|
|
|
|
|
// - devd.conf needs to lose the warning about zone files.
|
|
|
|
|
// - devd.conf needs more details on the supported statements.
|
|
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
#include <sys/types.h>
|
2003-04-21 04:30:12 +00:00
|
|
|
|
#include <sys/sysctl.h>
|
2002-12-07 08:04:36 +00:00
|
|
|
|
|
2002-12-18 07:08:01 +00:00
|
|
|
|
#include <ctype.h>
|
2002-12-07 08:04:36 +00:00
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <err.h>
|
|
|
|
|
#include <fcntl.h>
|
2002-12-18 07:08:01 +00:00
|
|
|
|
#include <regex.h>
|
2002-12-07 08:04:36 +00:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
#include <algorithm>
|
2002-12-07 08:04:36 +00:00
|
|
|
|
#include <map>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2003-04-26 20:59:04 +00:00
|
|
|
|
#include "devd.h" /* C compatible definitions */
|
|
|
|
|
#include "devd.hh" /* C++ class definitions */
|
2002-12-07 08:04:36 +00:00
|
|
|
|
|
|
|
|
|
#define CF "/etc/devd.conf"
|
2003-04-21 04:30:12 +00:00
|
|
|
|
#define SYSCTL "hw.bus.devctl_disable"
|
2002-12-07 08:04:36 +00:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
extern FILE *yyin;
|
|
|
|
|
extern int lineno;
|
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
static const char nomatch = '?';
|
|
|
|
|
static const char attach = '+';
|
|
|
|
|
static const char detach = '-';
|
|
|
|
|
|
2003-04-21 06:26:08 +00:00
|
|
|
|
int Dflag;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
int dflag;
|
2003-04-25 02:13:42 +00:00
|
|
|
|
int nflag;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
int romeo_must_die = 0;
|
|
|
|
|
|
|
|
|
|
static void event_loop(void);
|
|
|
|
|
static void usage(void);
|
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
template <class T> void
|
|
|
|
|
delete_and_clear(vector<T *> &v)
|
|
|
|
|
{
|
|
|
|
|
typename vector<T *>::const_iterator i;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
for (i = v.begin(); i != v.end(); i++)
|
|
|
|
|
delete *i;
|
|
|
|
|
v.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
|
config cfg;
|
|
|
|
|
|
|
|
|
|
event_proc::event_proc() : _prio(-1)
|
|
|
|
|
{
|
|
|
|
|
// nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event_proc::~event_proc()
|
|
|
|
|
{
|
|
|
|
|
vector<eps *>::const_iterator i;
|
|
|
|
|
|
|
|
|
|
for (i = _epsvec.begin(); i != _epsvec.end(); i++)
|
|
|
|
|
delete *i;
|
|
|
|
|
_epsvec.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
event_proc::add(eps *eps)
|
|
|
|
|
{
|
|
|
|
|
_epsvec.push_back(eps);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
event_proc::matches(config &c)
|
|
|
|
|
{
|
|
|
|
|
vector<eps *>::const_iterator i;
|
|
|
|
|
|
|
|
|
|
for (i = _epsvec.begin(); i != _epsvec.end(); i++)
|
|
|
|
|
if (!(*i)->do_match(c))
|
|
|
|
|
return (false);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
event_proc::run(config &c)
|
|
|
|
|
{
|
|
|
|
|
vector<eps *>::const_iterator i;
|
|
|
|
|
|
|
|
|
|
for (i = _epsvec.begin(); i != _epsvec.end(); i++)
|
|
|
|
|
if (!(*i)->do_action(c))
|
|
|
|
|
return (false);
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
action::action(const char *cmd)
|
|
|
|
|
: _cmd(cmd)
|
|
|
|
|
{
|
|
|
|
|
// nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
action::~action()
|
|
|
|
|
{
|
|
|
|
|
// nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2002-12-18 07:08:01 +00:00
|
|
|
|
action::do_action(config &c)
|
2002-12-07 08:04:36 +00:00
|
|
|
|
{
|
2003-01-06 08:09:41 +00:00
|
|
|
|
string s = c.expand_string(_cmd);
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
fprintf(stderr, "Executing '%s'\n", s.c_str());
|
|
|
|
|
::system(s.c_str());
|
2002-12-07 08:04:36 +00:00
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-18 07:08:01 +00:00
|
|
|
|
match::match(config &c, const char *var, const char *re)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
: _var(var)
|
2002-12-07 08:04:36 +00:00
|
|
|
|
{
|
2003-01-06 08:09:41 +00:00
|
|
|
|
string pattern = re;
|
|
|
|
|
_re = "^";
|
|
|
|
|
_re.append(c.expand_string(string(re)));
|
|
|
|
|
_re.append("$");
|
|
|
|
|
regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB);
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match::~match()
|
|
|
|
|
{
|
2002-12-18 07:08:01 +00:00
|
|
|
|
regfree(&_regex);
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2002-12-18 07:08:01 +00:00
|
|
|
|
match::do_match(config &c)
|
2002-12-07 08:04:36 +00:00
|
|
|
|
{
|
2002-12-18 07:08:01 +00:00
|
|
|
|
string value = c.get_variable(_var);
|
|
|
|
|
bool retval;
|
|
|
|
|
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
fprintf(stderr, "Testing %s=%s against %s\n", _var.c_str(),
|
|
|
|
|
value.c_str(), _re.c_str());
|
|
|
|
|
|
2002-12-18 07:08:01 +00:00
|
|
|
|
retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
|
|
|
|
|
return retval;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
|
|
|
|
|
const string var_list::nothing = "";
|
|
|
|
|
|
|
|
|
|
const string &
|
|
|
|
|
var_list::get_variable(const string &var) const
|
|
|
|
|
{
|
|
|
|
|
map<string, string>::const_iterator i;
|
|
|
|
|
|
|
|
|
|
i = _vars.find(var);
|
|
|
|
|
if (i == _vars.end())
|
2003-01-06 08:09:41 +00:00
|
|
|
|
return (var_list::bogus);
|
2002-12-07 08:04:36 +00:00
|
|
|
|
return (i->second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
var_list::is_set(const string &var) const
|
|
|
|
|
{
|
|
|
|
|
return (_vars.find(var) != _vars.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
var_list::set_variable(const string &var, const string &val)
|
|
|
|
|
{
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
fprintf(stderr, "%s=%s\n", var.c_str(), val.c_str());
|
2002-12-07 08:04:36 +00:00
|
|
|
|
_vars[var] = val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::reset(void)
|
|
|
|
|
{
|
|
|
|
|
_dir_list.clear();
|
2003-01-06 08:09:41 +00:00
|
|
|
|
delete_and_clear(_var_list_table);
|
|
|
|
|
delete_and_clear(_attach_list);
|
|
|
|
|
delete_and_clear(_detach_list);
|
|
|
|
|
delete_and_clear(_nomatch_list);
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::parse_one_file(const char *fn)
|
|
|
|
|
{
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2002-12-07 08:04:36 +00:00
|
|
|
|
printf("Parsing %s\n", fn);
|
|
|
|
|
yyin = fopen(fn, "r");
|
|
|
|
|
if (yyin == NULL)
|
|
|
|
|
err(1, "Cannot open config file %s", fn);
|
|
|
|
|
if (yyparse() != 0)
|
|
|
|
|
errx(1, "Cannot parse %s at line %d", fn, lineno);
|
|
|
|
|
fclose(yyin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::parse_files_in_dir(const char *dirname)
|
|
|
|
|
{
|
|
|
|
|
DIR *dirp;
|
|
|
|
|
struct dirent *dp;
|
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2002-12-07 08:04:36 +00:00
|
|
|
|
printf("Parsing files in %s\n", dirname);
|
|
|
|
|
dirp = opendir(dirname);
|
|
|
|
|
if (dirp == NULL)
|
|
|
|
|
return;
|
|
|
|
|
readdir(dirp); /* Skip . */
|
|
|
|
|
readdir(dirp); /* Skip .. */
|
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
|
if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s",
|
|
|
|
|
dirname, dp->d_name);
|
|
|
|
|
parse_one_file(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
class epv_greater {
|
|
|
|
|
public:
|
|
|
|
|
int operator()(event_proc *const&l1, event_proc *const&l2)
|
|
|
|
|
{
|
|
|
|
|
return (l1->get_priority() > l2->get_priority());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::sort_vector(vector<event_proc *> &v)
|
|
|
|
|
{
|
|
|
|
|
sort(v.begin(), v.end(), epv_greater());
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
|
void
|
|
|
|
|
config::parse(void)
|
|
|
|
|
{
|
|
|
|
|
vector<string>::const_iterator i;
|
|
|
|
|
|
|
|
|
|
parse_one_file(CF);
|
|
|
|
|
for (i = _dir_list.begin(); i != _dir_list.end(); i++)
|
|
|
|
|
parse_files_in_dir((*i).c_str());
|
2003-01-06 08:09:41 +00:00
|
|
|
|
sort_vector(_attach_list);
|
|
|
|
|
sort_vector(_detach_list);
|
|
|
|
|
sort_vector(_nomatch_list);
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::drop_pidfile()
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
if (_pidfile == "")
|
|
|
|
|
return;
|
|
|
|
|
fp = fopen(_pidfile.c_str(), "w");
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
return;
|
|
|
|
|
fprintf(fp, "%d\n", getpid());
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::add_attach(int prio, event_proc *p)
|
|
|
|
|
{
|
|
|
|
|
p->set_priority(prio);
|
|
|
|
|
_attach_list.push_back(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::add_detach(int prio, event_proc *p)
|
|
|
|
|
{
|
|
|
|
|
p->set_priority(prio);
|
|
|
|
|
_detach_list.push_back(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::add_directory(const char *dir)
|
|
|
|
|
{
|
|
|
|
|
_dir_list.push_back(string(dir));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::add_nomatch(int prio, event_proc *p)
|
|
|
|
|
{
|
|
|
|
|
p->set_priority(prio);
|
|
|
|
|
_nomatch_list.push_back(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::set_pidfile(const char *fn)
|
|
|
|
|
{
|
|
|
|
|
_pidfile = string(fn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::push_var_table()
|
|
|
|
|
{
|
|
|
|
|
var_list *vl;
|
|
|
|
|
|
|
|
|
|
vl = new var_list();
|
|
|
|
|
_var_list_table.push_back(vl);
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
fprintf(stderr, "Pushing table\n");
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::pop_var_table()
|
|
|
|
|
{
|
|
|
|
|
delete _var_list_table.back();
|
|
|
|
|
_var_list_table.pop_back();
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
fprintf(stderr, "Popping table\n");
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::set_variable(const char *var, const char *val)
|
|
|
|
|
{
|
|
|
|
|
_var_list_table.back()->set_variable(var, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string &
|
|
|
|
|
config::get_variable(const string &var)
|
|
|
|
|
{
|
|
|
|
|
vector<var_list *>::reverse_iterator i;
|
|
|
|
|
|
|
|
|
|
for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); i++) {
|
|
|
|
|
if ((*i)->is_set(var))
|
2003-01-06 08:09:41 +00:00
|
|
|
|
return ((*i)->get_variable(var));
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
return (var_list::nothing);
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
bool
|
|
|
|
|
config::is_id_char(char ch)
|
|
|
|
|
{
|
|
|
|
|
return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
|
|
|
|
|
ch == '-'));
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-18 07:08:01 +00:00
|
|
|
|
void
|
2003-04-26 19:44:45 +00:00
|
|
|
|
config::expand_one(const char *&src, string &dst)
|
2002-12-07 08:04:36 +00:00
|
|
|
|
{
|
2002-12-18 07:08:01 +00:00
|
|
|
|
int count;
|
2003-04-26 19:44:45 +00:00
|
|
|
|
string buffer, varstr;
|
2002-12-18 07:08:01 +00:00
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
src++;
|
2002-12-18 07:08:01 +00:00
|
|
|
|
// $$ -> $
|
|
|
|
|
if (*src == '$') {
|
2003-04-26 19:44:45 +00:00
|
|
|
|
dst.append(src++, 1);
|
2002-12-18 07:08:01 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// $(foo) -> $(foo)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
// Not sure if I want to support this or not, so for now we just pass
|
|
|
|
|
// it through.
|
2002-12-18 07:08:01 +00:00
|
|
|
|
if (*src == '(') {
|
2003-04-26 19:44:45 +00:00
|
|
|
|
dst.append("$");
|
2002-12-18 07:08:01 +00:00
|
|
|
|
count = 1;
|
2003-04-26 19:44:45 +00:00
|
|
|
|
/* If the string ends before ) is matched , return. */
|
|
|
|
|
while (count > 0 && *src) {
|
2002-12-18 07:08:01 +00:00
|
|
|
|
if (*src == ')')
|
|
|
|
|
count--;
|
|
|
|
|
else if (*src == '(')
|
|
|
|
|
count++;
|
2003-04-26 19:44:45 +00:00
|
|
|
|
dst.append(src++, 1);
|
2002-12-18 07:08:01 +00:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ${^A-Za-z] -> $\1
|
|
|
|
|
if (!isalpha(*src)) {
|
2003-04-26 19:44:45 +00:00
|
|
|
|
dst.append("$");
|
|
|
|
|
dst.append(src++, 1);
|
2002-12-18 07:08:01 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// $var -> replace with value
|
2003-04-26 19:44:45 +00:00
|
|
|
|
do {
|
|
|
|
|
buffer.append(src++, 1);
|
2003-04-26 20:25:40 +00:00
|
|
|
|
} while (is_id_char(*src));
|
2003-04-26 19:44:45 +00:00
|
|
|
|
buffer.append("", 1);
|
|
|
|
|
varstr = get_variable(buffer.c_str());
|
|
|
|
|
dst.append(varstr);
|
2002-12-18 07:08:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const string
|
|
|
|
|
config::expand_string(const string &s)
|
|
|
|
|
{
|
|
|
|
|
const char *src;
|
2003-04-26 19:44:45 +00:00
|
|
|
|
string dst;
|
2002-12-18 07:08:01 +00:00
|
|
|
|
|
|
|
|
|
src = s.c_str();
|
|
|
|
|
while (*src) {
|
|
|
|
|
if (*src == '$')
|
2003-04-26 19:44:45 +00:00
|
|
|
|
expand_one(src, dst);
|
2002-12-18 07:08:01 +00:00
|
|
|
|
else
|
2003-04-26 19:44:45 +00:00
|
|
|
|
dst.append(src++, 1);
|
2002-12-18 07:08:01 +00:00
|
|
|
|
}
|
2003-04-26 19:44:45 +00:00
|
|
|
|
dst.append("", 1);
|
2002-12-18 07:08:01 +00:00
|
|
|
|
|
2003-04-26 19:44:45 +00:00
|
|
|
|
return (dst);
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
bool
|
|
|
|
|
config::chop_var(char *&buffer, char *&lhs, char *&rhs)
|
|
|
|
|
{
|
|
|
|
|
char *walker;
|
|
|
|
|
|
|
|
|
|
if (*buffer == '\0')
|
|
|
|
|
return (false);
|
|
|
|
|
walker = lhs = buffer;
|
|
|
|
|
while (is_id_char(*walker))
|
|
|
|
|
walker++;
|
|
|
|
|
if (*walker != '=')
|
|
|
|
|
return (false);
|
|
|
|
|
walker++; // skip =
|
|
|
|
|
if (*walker == '"') {
|
|
|
|
|
walker++; // skip "
|
|
|
|
|
rhs = walker;
|
|
|
|
|
while (*walker && *walker != '"')
|
|
|
|
|
walker++;
|
|
|
|
|
if (*walker != '"')
|
|
|
|
|
return (false);
|
|
|
|
|
rhs[-2] = '\0';
|
|
|
|
|
*walker++ = '\0';
|
|
|
|
|
} else {
|
|
|
|
|
rhs = walker;
|
|
|
|
|
while (*walker && !isspace(*walker))
|
|
|
|
|
walker++;
|
|
|
|
|
if (*walker != '\0')
|
|
|
|
|
*walker++ = '\0';
|
|
|
|
|
rhs[-1] = '\0';
|
|
|
|
|
}
|
2003-04-21 04:00:01 +00:00
|
|
|
|
while (isspace(*walker))
|
|
|
|
|
walker++;
|
2003-01-06 08:09:41 +00:00
|
|
|
|
buffer = walker;
|
|
|
|
|
return (true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
config::set_vars(char *buffer)
|
|
|
|
|
{
|
|
|
|
|
char *lhs;
|
|
|
|
|
char *rhs;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
if (!chop_var(buffer, lhs, rhs))
|
|
|
|
|
break;
|
|
|
|
|
set_variable(lhs, rhs);
|
|
|
|
|
}
|
|
|
|
|
return (buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
config::find_and_execute(char type)
|
|
|
|
|
{
|
|
|
|
|
vector<event_proc *> *l;
|
|
|
|
|
vector<event_proc *>::const_iterator i;
|
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
case nomatch:
|
|
|
|
|
l = &_nomatch_list;
|
|
|
|
|
s = "nomatch";
|
|
|
|
|
break;
|
|
|
|
|
case attach:
|
|
|
|
|
l = &_attach_list;
|
|
|
|
|
s = "attach";
|
|
|
|
|
break;
|
|
|
|
|
case detach:
|
|
|
|
|
l = &_detach_list;
|
|
|
|
|
s = "detach";
|
|
|
|
|
break;
|
|
|
|
|
}
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
fprintf(stderr, "Processing %s event\n", s);
|
|
|
|
|
for (i = l->begin(); i != l->end(); i++) {
|
|
|
|
|
if ((*i)->matches(*this)) {
|
|
|
|
|
(*i)->run(*this);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
|
|
|
|
|
|
static void
|
2003-01-06 08:09:41 +00:00
|
|
|
|
process_event(char *buffer)
|
2002-12-07 08:04:36 +00:00
|
|
|
|
{
|
|
|
|
|
char type;
|
|
|
|
|
char *sp;
|
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
sp = buffer + 1;
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
2003-01-06 08:09:41 +00:00
|
|
|
|
fprintf(stderr, "Processing event '%s'\n", buffer);
|
|
|
|
|
type = *buffer++;
|
|
|
|
|
cfg.push_var_table();
|
|
|
|
|
// No match doesn't have a device, and the format is a little
|
|
|
|
|
// different, so handle it separately.
|
|
|
|
|
if (type != nomatch) {
|
|
|
|
|
sp = strchr(sp, ' ');
|
|
|
|
|
if (sp == NULL)
|
|
|
|
|
return; /* Can't happen? */
|
|
|
|
|
*sp++ = '\0';
|
|
|
|
|
cfg.set_variable("device-name", buffer);
|
2003-04-21 04:00:01 +00:00
|
|
|
|
if (strncmp(sp, "at ", 3) == 0)
|
|
|
|
|
sp += 3;
|
|
|
|
|
sp = cfg.set_vars(sp);
|
|
|
|
|
if (strncmp(sp, "on ", 3) == 0)
|
|
|
|
|
cfg.set_variable("bus", sp + 3);
|
|
|
|
|
} else {
|
|
|
|
|
//?vars at location on bus
|
|
|
|
|
sp = cfg.set_vars(sp);
|
|
|
|
|
if (strncmp(sp, "at ", 3) == 0)
|
|
|
|
|
sp += 3;
|
|
|
|
|
sp = cfg.set_vars(sp);
|
|
|
|
|
if (strncmp(sp, "on ", 3) == 0)
|
|
|
|
|
cfg.set_variable("bus", sp + 3);
|
2003-01-06 08:09:41 +00:00
|
|
|
|
}
|
2003-04-21 04:00:01 +00:00
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
|
cfg.find_and_execute(type);
|
|
|
|
|
cfg.pop_var_table();
|
2002-12-07 08:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
event_loop(void)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
int fd;
|
|
|
|
|
char buffer[DEVCTL_MAXBUF];
|
2003-04-21 06:26:08 +00:00
|
|
|
|
int once = 0;
|
|
|
|
|
timeval tv;
|
|
|
|
|
fd_set fds;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
|
|
|
|
|
fd = open(PATH_DEVCTL, O_RDONLY);
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
err(1, "Can't open devctl");
|
|
|
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
|
|
|
|
|
err(1, "Can't set close-on-exec flag");
|
|
|
|
|
while (1) {
|
|
|
|
|
if (romeo_must_die)
|
|
|
|
|
break;
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (!once && !dflag && !nflag) {
|
|
|
|
|
// Check to see if we have any events pending.
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
|
FD_SET(fd, &fds);
|
|
|
|
|
rv = select(fd + 1, &fds, &fds, &fds, &tv);
|
|
|
|
|
// No events -> we've processed all pending events
|
2003-04-25 02:13:42 +00:00
|
|
|
|
// == 2 is a kernel bug, but we hang if we don't
|
|
|
|
|
// make allowances for a while.
|
|
|
|
|
if (rv == 0 || rv == 2) {
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (Dflag)
|
|
|
|
|
fprintf(stderr, "Calling daemon\n");
|
|
|
|
|
daemon(0, 0);
|
|
|
|
|
once++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-12-07 08:04:36 +00:00
|
|
|
|
rv = read(fd, buffer, sizeof(buffer) - 1);
|
|
|
|
|
if (rv > 0) {
|
|
|
|
|
buffer[rv] = '\0';
|
|
|
|
|
while (buffer[--rv] == '\n')
|
|
|
|
|
buffer[rv] = '\0';
|
|
|
|
|
process_event(buffer);
|
|
|
|
|
} else if (rv < 0) {
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
/* EOF */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* functions that the parser uses.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
add_attach(int prio, event_proc *p)
|
|
|
|
|
{
|
|
|
|
|
cfg.add_attach(prio, p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
add_detach(int prio, event_proc *p)
|
|
|
|
|
{
|
|
|
|
|
cfg.add_detach(prio, p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
add_directory(const char *dir)
|
|
|
|
|
{
|
|
|
|
|
cfg.add_directory(dir);
|
|
|
|
|
free(const_cast<char *>(dir));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
add_nomatch(int prio, event_proc *p)
|
|
|
|
|
{
|
|
|
|
|
cfg.add_nomatch(prio, p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event_proc *
|
|
|
|
|
add_to_event_proc(event_proc *ep, eps *eps)
|
|
|
|
|
{
|
|
|
|
|
if (ep == NULL)
|
|
|
|
|
ep = new event_proc();
|
|
|
|
|
ep->add(eps);
|
|
|
|
|
return (ep);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eps *
|
|
|
|
|
new_action(const char *cmd)
|
|
|
|
|
{
|
|
|
|
|
eps *e = new action(cmd);
|
|
|
|
|
free(const_cast<char *>(cmd));
|
|
|
|
|
return (e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eps *
|
|
|
|
|
new_match(const char *var, const char *re)
|
|
|
|
|
{
|
2002-12-18 07:08:01 +00:00
|
|
|
|
eps *e = new match(cfg, var, re);
|
2002-12-07 08:04:36 +00:00
|
|
|
|
free(const_cast<char *>(var));
|
|
|
|
|
free(const_cast<char *>(re));
|
|
|
|
|
return (e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
set_pidfile(const char *name)
|
|
|
|
|
{
|
|
|
|
|
cfg.set_pidfile(name);
|
|
|
|
|
free(const_cast<char *>(name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
set_variable(const char *var, const char *val)
|
|
|
|
|
{
|
|
|
|
|
cfg.set_variable(var, val);
|
|
|
|
|
free(const_cast<char *>(var));
|
|
|
|
|
free(const_cast<char *>(val));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gensighand(int)
|
|
|
|
|
{
|
|
|
|
|
romeo_must_die++;
|
|
|
|
|
_exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
usage()
|
|
|
|
|
{
|
2003-01-06 16:46:25 +00:00
|
|
|
|
fprintf(stderr, "usage: %s [-d]\n", getprogname());
|
2002-12-07 08:04:36 +00:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-21 04:30:12 +00:00
|
|
|
|
static void
|
|
|
|
|
check_devd_enabled()
|
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
len = sizeof(val);
|
2003-05-02 17:38:08 +00:00
|
|
|
|
if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
|
2003-04-21 04:30:12 +00:00
|
|
|
|
errx(1, "devctl sysctl missing from kernel!");
|
|
|
|
|
if (val) {
|
|
|
|
|
warnx("Setting " SYSCTL " to 0");
|
|
|
|
|
val = 0;
|
|
|
|
|
sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
|
/*
|
|
|
|
|
* main
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
int ch;
|
|
|
|
|
|
2003-04-21 04:30:12 +00:00
|
|
|
|
check_devd_enabled();
|
2003-04-21 06:26:08 +00:00
|
|
|
|
while ((ch = getopt(argc, argv, "Ddn")) != -1) {
|
2002-12-07 08:04:36 +00:00
|
|
|
|
switch (ch) {
|
2003-04-21 06:26:08 +00:00
|
|
|
|
case 'D':
|
|
|
|
|
Dflag++;
|
|
|
|
|
break;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
case 'd':
|
|
|
|
|
dflag++;
|
|
|
|
|
break;
|
2003-04-21 06:26:08 +00:00
|
|
|
|
case 'n':
|
|
|
|
|
nflag++;
|
|
|
|
|
break;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
default:
|
|
|
|
|
usage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg.parse();
|
2003-04-21 06:26:08 +00:00
|
|
|
|
if (!dflag && nflag)
|
2002-12-07 08:04:36 +00:00
|
|
|
|
daemon(0, 0);
|
|
|
|
|
cfg.drop_pidfile();
|
|
|
|
|
signal(SIGHUP, gensighand);
|
|
|
|
|
signal(SIGINT, gensighand);
|
|
|
|
|
signal(SIGTERM, gensighand);
|
|
|
|
|
event_loop();
|
|
|
|
|
return (0);
|
|
|
|
|
}
|