2002-12-07 08:04:36 +00:00
|
|
|
/*-
|
2010-06-29 03:52:12 +00:00
|
|
|
* Copyright (c) 2002-2010 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.
|
2010-06-29 03:52:12 +00:00
|
|
|
*
|
|
|
|
* my_system is a variation on lib/libc/stdlib/system.c:
|
|
|
|
*
|
|
|
|
* Copyright (c) 1988, 1993
|
|
|
|
* The Regents of the University of California. 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.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
2002-12-07 08:04:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DEVD control daemon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TODO list:
|
|
|
|
// o devd.conf and devd man pages need a lot of help:
|
2004-07-01 07:24:18 +00:00
|
|
|
// - devd needs to document the unix domain socket
|
2002-12-07 08:04:36 +00:00
|
|
|
// - devd.conf needs more details on the supported statements.
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2004-07-01 07:24:18 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
2003-04-21 04:30:12 +00:00
|
|
|
#include <sys/sysctl.h>
|
2004-07-01 07:24:18 +00:00
|
|
|
#include <sys/types.h>
|
2010-06-29 03:52:12 +00:00
|
|
|
#include <sys/wait.h>
|
2004-07-01 07:24:18 +00:00
|
|
|
#include <sys/un.h>
|
2002-12-07 08:04:36 +00:00
|
|
|
|
2013-05-02 17:02:50 +00:00
|
|
|
#include <cctype>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <csignal>
|
|
|
|
#include <cstring>
|
2013-07-02 14:48:39 +00:00
|
|
|
#include <cstdarg>
|
2013-05-02 17:02:50 +00:00
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
2006-01-30 22:50:13 +00:00
|
|
|
#include <libutil.h>
|
2010-06-29 03:52:12 +00:00
|
|
|
#include <paths.h>
|
2013-01-30 15:21:18 +00:00
|
|
|
#include <poll.h>
|
2002-12-18 07:08:01 +00:00
|
|
|
#include <regex.h>
|
2013-07-01 21:20:17 +00:00
|
|
|
#include <syslog.h>
|
2002-12-07 08:04:36 +00:00
|
|
|
#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>
|
2004-07-01 07:24:18 +00:00
|
|
|
#include <list>
|
2002-12-07 08:04:36 +00:00
|
|
|
#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
|
|
|
|
2014-08-14 22:33:56 +00:00
|
|
|
#define STREAMPIPE "/var/run/devd.pipe"
|
|
|
|
#define SEQPACKETPIPE "/var/run/devd.seqpacket.pipe"
|
2002-12-07 08:04:36 +00:00
|
|
|
#define CF "/etc/devd.conf"
|
2014-03-26 02:25:40 +00:00
|
|
|
#define SYSCTL "hw.bus.devctl_queue"
|
2002-12-07 08:04:36 +00:00
|
|
|
|
2013-12-13 21:49:41 +00:00
|
|
|
/*
|
|
|
|
* Since the client socket is nonblocking, we must increase its send buffer to
|
|
|
|
* handle brief event storms. On FreeBSD, AF_UNIX sockets don't have a receive
|
2015-12-09 18:55:25 +00:00
|
|
|
* buffer, so the client can't increase the buffersize by itself.
|
2013-12-13 21:49:41 +00:00
|
|
|
*
|
|
|
|
* For example, when creating a ZFS pool, devd emits one 165 character
|
2015-12-09 18:55:25 +00:00
|
|
|
* resource.fs.zfs.statechange message for each vdev in the pool. The kernel
|
|
|
|
* allocates a 4608B mbuf for each message. Modern technology places a limit of
|
|
|
|
* roughly 450 drives/rack, and it's unlikely that a zpool will ever be larger
|
|
|
|
* than that.
|
|
|
|
*
|
|
|
|
* 450 drives * 165 bytes / drive = 74250B of data in the sockbuf
|
|
|
|
* 450 drives * 4608B / drive = 2073600B of mbufs in the sockbuf
|
|
|
|
*
|
|
|
|
* We can't directly set the sockbuf's mbuf limit, but we can do it indirectly.
|
|
|
|
* The kernel sets it to the minimum of a hard-coded maximum value and sbcc *
|
|
|
|
* kern.ipc.sockbuf_waste_factor, where sbcc is the socket buffer size set by
|
|
|
|
* the user. The default value of kern.ipc.sockbuf_waste_factor is 8. If we
|
|
|
|
* set the bufsize to 256k and use the kern.ipc.sockbuf_waste_factor, then the
|
|
|
|
* kernel will set the mbuf limit to 2MB, which is just large enough for 450
|
|
|
|
* drives. It also happens to be the same as the hardcoded maximum value.
|
2013-12-13 21:49:41 +00:00
|
|
|
*/
|
2015-12-09 18:55:25 +00:00
|
|
|
#define CLIENT_BUFSIZE 262144
|
2013-12-13 21:49:41 +00:00
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2014-08-14 22:33:56 +00:00
|
|
|
typedef struct client {
|
|
|
|
int fd;
|
|
|
|
int socktype;
|
|
|
|
} client_t;
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
extern FILE *yyin;
|
|
|
|
extern int lineno;
|
|
|
|
|
2003-10-24 22:02:29 +00:00
|
|
|
static const char notify = '!';
|
2003-01-06 08:09:41 +00:00
|
|
|
static const char nomatch = '?';
|
|
|
|
static const char attach = '+';
|
|
|
|
static const char detach = '-';
|
|
|
|
|
2006-01-30 22:50:13 +00:00
|
|
|
static struct pidfh *pfh;
|
|
|
|
|
2014-03-07 23:30:48 +00:00
|
|
|
static int no_daemon = 0;
|
|
|
|
static int daemonize_quick = 0;
|
|
|
|
static int quiet_mode = 0;
|
2013-07-01 21:33:05 +00:00
|
|
|
static unsigned total_events = 0;
|
|
|
|
static volatile sig_atomic_t got_siginfo = 0;
|
2013-03-04 02:21:08 +00:00
|
|
|
static volatile sig_atomic_t romeo_must_die = 0;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
2005-11-24 14:39:41 +00:00
|
|
|
static const char *configfile = CF;
|
|
|
|
|
2013-07-08 21:10:30 +00:00
|
|
|
static void devdlog(int priority, const char* message, ...)
|
|
|
|
__printflike(2, 3);
|
2002-12-07 08:04:36 +00:00
|
|
|
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
|
|
|
|
2012-12-06 01:25:17 +00:00
|
|
|
for (i = v.begin(); i != v.end(); ++i)
|
2003-01-06 08:09:41 +00:00
|
|
|
delete *i;
|
|
|
|
v.clear();
|
|
|
|
}
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
config cfg;
|
|
|
|
|
|
|
|
event_proc::event_proc() : _prio(-1)
|
|
|
|
{
|
2013-01-30 23:37:35 +00:00
|
|
|
_epsvec.reserve(4);
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event_proc::~event_proc()
|
|
|
|
{
|
2005-11-14 02:01:10 +00:00
|
|
|
delete_and_clear(_epsvec);
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
event_proc::add(eps *eps)
|
|
|
|
{
|
|
|
|
_epsvec.push_back(eps);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-06 01:25:14 +00:00
|
|
|
event_proc::matches(config &c) const
|
2002-12-07 08:04:36 +00:00
|
|
|
{
|
|
|
|
vector<eps *>::const_iterator i;
|
|
|
|
|
2012-12-06 01:25:17 +00:00
|
|
|
for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
|
2002-12-07 08:04:36 +00:00
|
|
|
if (!(*i)->do_match(c))
|
|
|
|
return (false);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-06 01:25:14 +00:00
|
|
|
event_proc::run(config &c) const
|
2002-12-07 08:04:36 +00:00
|
|
|
{
|
|
|
|
vector<eps *>::const_iterator i;
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2012-12-06 01:25:17 +00:00
|
|
|
for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
|
2002-12-07 08:04:36 +00:00
|
|
|
if (!(*i)->do_action(c))
|
|
|
|
return (false);
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
action::action(const char *cmd)
|
2013-07-01 21:49:17 +00:00
|
|
|
: _cmd(cmd)
|
2002-12-07 08:04:36 +00:00
|
|
|
{
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
action::~action()
|
|
|
|
{
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
|
2010-06-29 03:52:12 +00:00
|
|
|
static int
|
|
|
|
my_system(const char *command)
|
|
|
|
{
|
|
|
|
pid_t pid, savedpid;
|
|
|
|
int pstat;
|
|
|
|
struct sigaction ign, intact, quitact;
|
|
|
|
sigset_t newsigblock, oldsigblock;
|
|
|
|
|
|
|
|
if (!command) /* just checking... */
|
2013-07-01 21:49:17 +00:00
|
|
|
return (1);
|
2010-06-29 03:52:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
|
|
|
|
* existing signal dispositions.
|
|
|
|
*/
|
|
|
|
ign.sa_handler = SIG_IGN;
|
|
|
|
::sigemptyset(&ign.sa_mask);
|
|
|
|
ign.sa_flags = 0;
|
|
|
|
::sigaction(SIGINT, &ign, &intact);
|
|
|
|
::sigaction(SIGQUIT, &ign, &quitact);
|
|
|
|
::sigemptyset(&newsigblock);
|
|
|
|
::sigaddset(&newsigblock, SIGCHLD);
|
|
|
|
::sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
|
|
|
|
switch (pid = ::fork()) {
|
|
|
|
case -1: /* error */
|
|
|
|
break;
|
|
|
|
case 0: /* child */
|
|
|
|
/*
|
|
|
|
* Restore original signal dispositions and exec the command.
|
|
|
|
*/
|
|
|
|
::sigaction(SIGINT, &intact, NULL);
|
|
|
|
::sigaction(SIGQUIT, &quitact, NULL);
|
|
|
|
::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
|
|
|
|
/*
|
|
|
|
* Close the PID file, and all other open descriptors.
|
|
|
|
* Inherit std{in,out,err} only.
|
|
|
|
*/
|
|
|
|
cfg.close_pidfile();
|
|
|
|
::closefrom(3);
|
|
|
|
::execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
|
|
|
|
::_exit(127);
|
|
|
|
default: /* parent */
|
|
|
|
savedpid = pid;
|
|
|
|
do {
|
|
|
|
pid = ::wait4(savedpid, &pstat, 0, (struct rusage *)0);
|
|
|
|
} while (pid == -1 && errno == EINTR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
::sigaction(SIGINT, &intact, NULL);
|
|
|
|
::sigaction(SIGQUIT, &quitact, NULL);
|
|
|
|
::sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
|
|
|
|
return (pid == -1 ? -1 : pstat);
|
|
|
|
}
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
bool
|
2002-12-18 07:08:01 +00:00
|
|
|
action::do_action(config &c)
|
2002-12-07 08:04:36 +00:00
|
|
|
{
|
2013-01-30 23:37:35 +00:00
|
|
|
string s = c.expand_string(_cmd.c_str());
|
2013-11-07 16:24:31 +00:00
|
|
|
devdlog(LOG_INFO, "Executing '%s'\n", s.c_str());
|
2010-06-29 03:52:12 +00:00
|
|
|
my_system(s.c_str());
|
2002-12-07 08:04:36 +00:00
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2013-01-30 23:37:35 +00:00
|
|
|
match::match(config &c, const char *var, const char *re) :
|
|
|
|
_inv(re[0] == '!'),
|
|
|
|
_var(var),
|
|
|
|
_re(c.expand_string(_inv ? re + 1 : re, "^", "$"))
|
2002-12-07 08:04:36 +00:00
|
|
|
{
|
2006-01-08 05:18:36 +00:00
|
|
|
regcomp(&_regex, _re.c_str(), REG_EXTENDED | REG_NOSUB | REG_ICASE);
|
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
|
|
|
{
|
2010-07-29 16:06:40 +00:00
|
|
|
const string &value = c.get_variable(_var);
|
2002-12-18 07:08:01 +00:00
|
|
|
bool retval;
|
|
|
|
|
2013-07-01 21:49:17 +00:00
|
|
|
/*
|
2013-07-01 21:20:17 +00:00
|
|
|
* This function gets called WAY too often to justify calling syslog()
|
|
|
|
* each time, even at LOG_DEBUG. Because if syslogd isn't running, it
|
|
|
|
* can consume excessive amounts of systime inside of connect(). Only
|
|
|
|
* log when we're in -d mode.
|
|
|
|
*/
|
2014-03-07 23:30:48 +00:00
|
|
|
if (no_daemon) {
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n",
|
2011-10-26 02:11:28 +00:00
|
|
|
_var.c_str(), value.c_str(), _re.c_str(), _inv);
|
2013-07-01 21:20:17 +00:00
|
|
|
}
|
2003-01-06 08:09:41 +00:00
|
|
|
|
2002-12-18 07:08:01 +00:00
|
|
|
retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
|
2011-10-26 02:11:28 +00:00
|
|
|
if (_inv == 1)
|
|
|
|
retval = (retval == 0) ? 1 : 0;
|
|
|
|
|
2013-07-01 21:49:17 +00:00
|
|
|
return (retval);
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
2005-07-10 03:37:15 +00:00
|
|
|
#include <sys/sockio.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_media.h>
|
|
|
|
|
2005-10-19 21:21:22 +00:00
|
|
|
media::media(config &, const char *var, const char *type)
|
2005-07-10 03:37:15 +00:00
|
|
|
: _var(var), _type(-1)
|
|
|
|
{
|
|
|
|
static struct ifmedia_description media_types[] = {
|
|
|
|
{ IFM_ETHER, "Ethernet" },
|
|
|
|
{ IFM_TOKEN, "Tokenring" },
|
|
|
|
{ IFM_FDDI, "FDDI" },
|
|
|
|
{ IFM_IEEE80211, "802.11" },
|
|
|
|
{ IFM_ATM, "ATM" },
|
|
|
|
{ -1, "unknown" },
|
|
|
|
{ 0, NULL },
|
|
|
|
};
|
2012-12-06 01:25:17 +00:00
|
|
|
for (int i = 0; media_types[i].ifmt_string != NULL; ++i)
|
2005-07-10 03:37:15 +00:00
|
|
|
if (strcasecmp(type, media_types[i].ifmt_string) == 0) {
|
|
|
|
_type = media_types[i].ifmt_word;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
media::~media()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
media::do_match(config &c)
|
|
|
|
{
|
2005-10-04 22:22:51 +00:00
|
|
|
string value;
|
2005-07-10 03:37:15 +00:00
|
|
|
struct ifmediareq ifmr;
|
|
|
|
bool retval;
|
|
|
|
int s;
|
|
|
|
|
2005-10-04 22:22:51 +00:00
|
|
|
// Since we can be called from both a device attach/detach
|
|
|
|
// context where device-name is defined and what we want,
|
|
|
|
// as well as from a link status context, where subsystem is
|
|
|
|
// the name of interest, first try device-name and fall back
|
|
|
|
// to subsystem if none exists.
|
|
|
|
value = c.get_variable("device-name");
|
2013-03-04 02:21:26 +00:00
|
|
|
if (value.empty())
|
2005-10-19 18:10:34 +00:00
|
|
|
value = c.get_variable("subsystem");
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n",
|
2005-07-10 03:37:15 +00:00
|
|
|
value.c_str(), _type);
|
|
|
|
|
|
|
|
retval = false;
|
|
|
|
|
|
|
|
s = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s >= 0) {
|
|
|
|
memset(&ifmr, 0, sizeof(ifmr));
|
|
|
|
strncpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
|
|
|
|
|
|
|
|
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
|
|
|
|
ifmr.ifm_status & IFM_AVALID) {
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "%s has media type 0x%x\n",
|
2005-07-10 03:37:15 +00:00
|
|
|
value.c_str(), IFM_TYPE(ifmr.ifm_active));
|
|
|
|
retval = (IFM_TYPE(ifmr.ifm_active) == _type);
|
|
|
|
} else if (_type == -1) {
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "%s has unknown media type\n",
|
2005-07-10 03:37:15 +00:00
|
|
|
value.c_str());
|
|
|
|
retval = true;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:49:17 +00:00
|
|
|
return (retval);
|
2005-07-10 03:37:15 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2013-07-01 21:20:17 +00:00
|
|
|
/*
|
|
|
|
* This function gets called WAY too often to justify calling syslog()
|
|
|
|
* each time, even at LOG_DEBUG. Because if syslogd isn't running, it
|
|
|
|
* can consume excessive amounts of systime inside of connect(). Only
|
|
|
|
* log when we're in -d mode.
|
|
|
|
*/
|
2014-03-07 23:30:48 +00:00
|
|
|
if (no_daemon)
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "setting %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);
|
2003-10-24 22:02:29 +00:00
|
|
|
delete_and_clear(_notify_list);
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
config::parse_one_file(const char *fn)
|
|
|
|
{
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "Parsing %s\n", fn);
|
2002-12-07 08:04:36 +00:00
|
|
|
yyin = fopen(fn, "r");
|
|
|
|
if (yyin == NULL)
|
|
|
|
err(1, "Cannot open config file %s", fn);
|
2006-04-14 07:20:42 +00:00
|
|
|
lineno = 1;
|
2002-12-07 08:04:36 +00:00
|
|
|
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];
|
|
|
|
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname);
|
2002-12-07 08:04:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2010-11-21 04:02:37 +00:00
|
|
|
closedir(dirp);
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
class epv_greater {
|
|
|
|
public:
|
2012-12-06 01:25:14 +00:00
|
|
|
int operator()(event_proc *const&l1, event_proc *const&l2) const
|
2003-01-06 08:09:41 +00:00
|
|
|
{
|
|
|
|
return (l1->get_priority() > l2->get_priority());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
config::sort_vector(vector<event_proc *> &v)
|
|
|
|
{
|
2012-12-05 20:50:40 +00:00
|
|
|
stable_sort(v.begin(), v.end(), epv_greater());
|
2003-01-06 08:09:41 +00:00
|
|
|
}
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
void
|
|
|
|
config::parse(void)
|
|
|
|
{
|
|
|
|
vector<string>::const_iterator i;
|
|
|
|
|
2005-11-24 14:39:41 +00:00
|
|
|
parse_one_file(configfile);
|
2012-12-06 01:25:17 +00:00
|
|
|
for (i = _dir_list.begin(); i != _dir_list.end(); ++i)
|
2002-12-07 08:04:36 +00:00
|
|
|
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);
|
2003-10-24 22:02:29 +00:00
|
|
|
sort_vector(_notify_list);
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-01-30 22:50:13 +00:00
|
|
|
config::open_pidfile()
|
2002-12-07 08:04:36 +00:00
|
|
|
{
|
2006-01-30 22:50:13 +00:00
|
|
|
pid_t otherpid;
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2013-03-04 02:21:19 +00:00
|
|
|
if (_pidfile.empty())
|
2002-12-07 08:04:36 +00:00
|
|
|
return;
|
2006-01-30 22:50:13 +00:00
|
|
|
pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
|
|
|
|
if (pfh == NULL) {
|
|
|
|
if (errno == EEXIST)
|
|
|
|
errx(1, "devd already running, pid: %d", (int)otherpid);
|
|
|
|
warn("cannot open pid file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
config::write_pidfile()
|
|
|
|
{
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2006-01-30 22:50:13 +00:00
|
|
|
pidfile_write(pfh);
|
|
|
|
}
|
|
|
|
|
2010-06-29 03:52:12 +00:00
|
|
|
void
|
|
|
|
config::close_pidfile()
|
|
|
|
{
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2010-06-29 03:52:12 +00:00
|
|
|
pidfile_close(pfh);
|
|
|
|
}
|
|
|
|
|
2006-01-30 22:50:13 +00:00
|
|
|
void
|
|
|
|
config::remove_pidfile()
|
|
|
|
{
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2006-01-30 22:50:13 +00:00
|
|
|
pidfile_remove(pfh);
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-10-24 22:02:29 +00:00
|
|
|
void
|
|
|
|
config::add_notify(int prio, event_proc *p)
|
|
|
|
{
|
|
|
|
p->set_priority(prio);
|
|
|
|
_notify_list.push_back(p);
|
|
|
|
}
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
void
|
|
|
|
config::set_pidfile(const char *fn)
|
|
|
|
{
|
2013-03-04 02:21:19 +00:00
|
|
|
_pidfile = fn;
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
config::push_var_table()
|
|
|
|
{
|
|
|
|
var_list *vl;
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
vl = new var_list();
|
|
|
|
_var_list_table.push_back(vl);
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "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();
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "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;
|
|
|
|
|
2012-12-06 01:25:17 +00:00
|
|
|
for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); ++i) {
|
2002-12-07 08:04:36 +00:00
|
|
|
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
|
2012-12-06 01:25:14 +00:00
|
|
|
config::is_id_char(char ch) const
|
2003-01-06 08:09:41 +00:00
|
|
|
{
|
2013-07-01 21:49:17 +00:00
|
|
|
return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
|
2003-01-06 08:09:41 +00:00
|
|
|
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;
|
2010-07-29 16:06:40 +00:00
|
|
|
string buffer;
|
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 == '$') {
|
2013-03-04 02:21:29 +00:00
|
|
|
dst += *src++;
|
2002-12-18 07:08:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2002-12-18 07:08:01 +00:00
|
|
|
// $(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 == '(') {
|
2013-03-04 02:21:29 +00:00
|
|
|
dst += '$';
|
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++;
|
2013-03-04 02:21:29 +00:00
|
|
|
dst += *src++;
|
2002-12-18 07:08:01 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2013-03-04 02:21:31 +00:00
|
|
|
// $[^A-Za-z] -> $\1
|
2002-12-18 07:08:01 +00:00
|
|
|
if (!isalpha(*src)) {
|
2013-03-04 02:21:29 +00:00
|
|
|
dst += '$';
|
|
|
|
dst += *src++;
|
2002-12-18 07:08:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $var -> replace with value
|
2003-04-26 19:44:45 +00:00
|
|
|
do {
|
2013-03-04 02:21:29 +00:00
|
|
|
buffer += *src++;
|
2003-04-26 20:25:40 +00:00
|
|
|
} while (is_id_char(*src));
|
2013-03-04 02:21:19 +00:00
|
|
|
dst.append(get_variable(buffer));
|
2002-12-18 07:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const string
|
2013-01-30 23:37:35 +00:00
|
|
|
config::expand_string(const char *src, const char *prepend, const char *append)
|
2002-12-18 07:08:01 +00:00
|
|
|
{
|
2013-01-30 23:37:35 +00:00
|
|
|
const char *var_at;
|
2003-04-26 19:44:45 +00:00
|
|
|
string dst;
|
2002-12-18 07:08:01 +00:00
|
|
|
|
2013-01-30 23:37:35 +00:00
|
|
|
/*
|
|
|
|
* 128 bytes is enough for 2427 of 2438 expansions that happen
|
|
|
|
* while parsing config files, as tested on 2013-01-30.
|
|
|
|
*/
|
|
|
|
dst.reserve(128);
|
|
|
|
|
|
|
|
if (prepend != NULL)
|
|
|
|
dst = prepend;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
var_at = strchr(src, '$');
|
|
|
|
if (var_at == NULL) {
|
|
|
|
dst.append(src);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dst.append(src, var_at - src);
|
|
|
|
src = var_at;
|
|
|
|
expand_one(src, dst);
|
2002-12-18 07:08:01 +00:00
|
|
|
}
|
2013-01-30 23:37:35 +00:00
|
|
|
|
|
|
|
if (append != NULL)
|
|
|
|
dst.append(append);
|
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
|
2013-03-04 02:19:55 +00:00
|
|
|
config::chop_var(char *&buffer, char *&lhs, char *&rhs) const
|
2003-01-06 08:09:41 +00:00
|
|
|
{
|
|
|
|
char *walker;
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2003-01-06 08:09:41 +00:00
|
|
|
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;
|
2005-10-19 21:21:22 +00:00
|
|
|
const char *s;
|
2003-01-06 08:09:41 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
default:
|
|
|
|
return;
|
2003-10-24 22:02:29 +00:00
|
|
|
case notify:
|
|
|
|
l = &_notify_list;
|
|
|
|
s = "notify";
|
|
|
|
break;
|
2003-01-06 08:09:41 +00:00
|
|
|
case nomatch:
|
|
|
|
l = &_nomatch_list;
|
|
|
|
s = "nomatch";
|
|
|
|
break;
|
|
|
|
case attach:
|
|
|
|
l = &_attach_list;
|
|
|
|
s = "attach";
|
|
|
|
break;
|
|
|
|
case detach:
|
|
|
|
l = &_detach_list;
|
|
|
|
s = "detach";
|
|
|
|
break;
|
|
|
|
}
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "Processing %s event\n", s);
|
2012-12-06 01:25:17 +00:00
|
|
|
for (i = l->begin(); i != l->end(); ++i) {
|
2003-01-06 08:09:41 +00:00
|
|
|
if ((*i)->matches(*this)) {
|
|
|
|
(*i)->run(*this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:49:17 +00:00
|
|
|
|
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;
|
2013-12-13 22:58:57 +00:00
|
|
|
devdlog(LOG_INFO, "Processing event '%s'\n", buffer);
|
2003-01-06 08:09:41 +00:00
|
|
|
type = *buffer++;
|
|
|
|
cfg.push_var_table();
|
|
|
|
// No match doesn't have a device, and the format is a little
|
|
|
|
// different, so handle it separately.
|
2003-10-24 22:02:29 +00:00
|
|
|
switch (type) {
|
|
|
|
case notify:
|
|
|
|
sp = cfg.set_vars(sp);
|
|
|
|
break;
|
|
|
|
case nomatch:
|
2005-04-18 02:54:07 +00:00
|
|
|
//? at location pnp-info on bus
|
|
|
|
sp = strchr(sp, ' ');
|
|
|
|
if (sp == NULL)
|
|
|
|
return; /* Can't happen? */
|
|
|
|
*sp++ = '\0';
|
2010-10-09 07:29:13 +00:00
|
|
|
while (isspace(*sp))
|
|
|
|
sp++;
|
2003-04-21 04:00:01 +00:00
|
|
|
if (strncmp(sp, "at ", 3) == 0)
|
|
|
|
sp += 3;
|
|
|
|
sp = cfg.set_vars(sp);
|
2010-10-09 07:29:13 +00:00
|
|
|
while (isspace(*sp))
|
|
|
|
sp++;
|
2003-04-21 04:00:01 +00:00
|
|
|
if (strncmp(sp, "on ", 3) == 0)
|
|
|
|
cfg.set_variable("bus", sp + 3);
|
2003-10-24 22:02:29 +00:00
|
|
|
break;
|
|
|
|
case attach: /*FALLTHROUGH*/
|
|
|
|
case detach:
|
|
|
|
sp = strchr(sp, ' ');
|
|
|
|
if (sp == NULL)
|
|
|
|
return; /* Can't happen? */
|
|
|
|
*sp++ = '\0';
|
|
|
|
cfg.set_variable("device-name", buffer);
|
2010-10-09 07:29:13 +00:00
|
|
|
while (isspace(*sp))
|
|
|
|
sp++;
|
2003-04-21 04:00:01 +00:00
|
|
|
if (strncmp(sp, "at ", 3) == 0)
|
|
|
|
sp += 3;
|
|
|
|
sp = cfg.set_vars(sp);
|
2010-10-09 07:29:13 +00:00
|
|
|
while (isspace(*sp))
|
|
|
|
sp++;
|
2003-04-21 04:00:01 +00:00
|
|
|
if (strncmp(sp, "on ", 3) == 0)
|
|
|
|
cfg.set_variable("bus", sp + 3);
|
2003-10-24 22:02:29 +00:00
|
|
|
break;
|
2003-01-06 08:09:41 +00:00
|
|
|
}
|
2013-07-01 21:49:17 +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
|
|
|
}
|
|
|
|
|
2004-07-01 07:24:18 +00:00
|
|
|
int
|
2014-08-14 22:33:56 +00:00
|
|
|
create_socket(const char *name, int socktype)
|
2004-07-01 07:24:18 +00:00
|
|
|
{
|
|
|
|
int fd, slen;
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
|
2014-08-14 22:33:56 +00:00
|
|
|
if ((fd = socket(PF_LOCAL, socktype, 0)) < 0)
|
2004-07-01 07:24:18 +00:00
|
|
|
err(1, "socket");
|
|
|
|
bzero(&sun, sizeof(sun));
|
|
|
|
sun.sun_family = AF_UNIX;
|
|
|
|
strlcpy(sun.sun_path, name, sizeof(sun.sun_path));
|
|
|
|
slen = SUN_LEN(&sun);
|
|
|
|
unlink(name);
|
2005-07-13 17:28:11 +00:00
|
|
|
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
err(1, "fcntl");
|
2012-06-01 06:56:35 +00:00
|
|
|
if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
|
2004-07-01 07:24:18 +00:00
|
|
|
err(1, "bind");
|
|
|
|
listen(fd, 4);
|
2005-07-13 17:10:47 +00:00
|
|
|
chown(name, 0, 0); /* XXX - root.wheel */
|
2005-07-13 17:28:11 +00:00
|
|
|
chmod(name, 0666);
|
2004-07-01 07:24:18 +00:00
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
2015-10-21 05:37:09 +00:00
|
|
|
unsigned int max_clients = 10; /* Default, can be overridden on cmdline. */
|
2013-01-30 15:21:18 +00:00
|
|
|
unsigned int num_clients;
|
2014-08-14 22:33:56 +00:00
|
|
|
|
|
|
|
list<client_t> clients;
|
2004-07-01 07:24:18 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
notify_clients(const char *data, int len)
|
|
|
|
{
|
2014-08-14 22:33:56 +00:00
|
|
|
list<client_t>::iterator i;
|
2004-07-01 07:24:18 +00:00
|
|
|
|
2013-01-30 15:21:18 +00:00
|
|
|
/*
|
|
|
|
* Deliver the data to all clients. Throw clients overboard at the
|
|
|
|
* first sign of trouble. This reaps clients who've died or closed
|
|
|
|
* their sockets, and also clients who are alive but failing to keep up
|
|
|
|
* (or who are maliciously not reading, to consume buffer space in
|
|
|
|
* kernel memory or tie up the limited number of available connections).
|
|
|
|
*/
|
|
|
|
for (i = clients.begin(); i != clients.end(); ) {
|
2014-08-14 22:33:56 +00:00
|
|
|
int flags;
|
|
|
|
if (i->socktype == SOCK_SEQPACKET)
|
|
|
|
flags = MSG_EOR;
|
|
|
|
else
|
|
|
|
flags = 0;
|
|
|
|
|
|
|
|
if (send(i->fd, data, len, flags) != len) {
|
2013-01-30 15:21:18 +00:00
|
|
|
--num_clients;
|
2014-08-14 22:33:56 +00:00
|
|
|
close(i->fd);
|
2013-01-30 15:21:18 +00:00
|
|
|
i = clients.erase(i);
|
2014-08-14 22:33:56 +00:00
|
|
|
devdlog(LOG_WARNING, "notify_clients: send() failed; "
|
2013-07-01 21:20:17 +00:00
|
|
|
"dropping unresponsive client\n");
|
2013-01-30 15:21:18 +00:00
|
|
|
} else
|
|
|
|
++i;
|
2004-07-01 07:24:18 +00:00
|
|
|
}
|
2013-01-30 15:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
check_clients(void)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct pollfd pfd;
|
2014-08-14 22:33:56 +00:00
|
|
|
list<client_t>::iterator i;
|
2004-07-01 07:24:18 +00:00
|
|
|
|
2013-01-30 15:21:18 +00:00
|
|
|
/*
|
|
|
|
* Check all existing clients to see if any of them have disappeared.
|
|
|
|
* Normally we reap clients when we get an error trying to send them an
|
|
|
|
* event. This check eliminates the problem of an ever-growing list of
|
|
|
|
* zombie clients because we're never writing to them on a system
|
|
|
|
* without frequent device-change activity.
|
|
|
|
*/
|
|
|
|
pfd.events = 0;
|
|
|
|
for (i = clients.begin(); i != clients.end(); ) {
|
2014-08-14 22:33:56 +00:00
|
|
|
pfd.fd = i->fd;
|
2013-01-30 15:21:18 +00:00
|
|
|
s = poll(&pfd, 1, 0);
|
|
|
|
if ((s < 0 && s != EINTR ) ||
|
|
|
|
(s > 0 && (pfd.revents & POLLHUP))) {
|
|
|
|
--num_clients;
|
2014-08-14 22:33:56 +00:00
|
|
|
close(i->fd);
|
2013-01-30 15:21:18 +00:00
|
|
|
i = clients.erase(i);
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_NOTICE, "check_clients: "
|
|
|
|
"dropping disconnected client\n");
|
2013-01-30 15:21:18 +00:00
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
}
|
2004-07-01 07:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-14 22:33:56 +00:00
|
|
|
new_client(int fd, int socktype)
|
2004-07-01 07:24:18 +00:00
|
|
|
{
|
2014-08-14 22:33:56 +00:00
|
|
|
client_t s;
|
2013-12-13 21:49:41 +00:00
|
|
|
int sndbuf_size;
|
2004-07-01 07:24:18 +00:00
|
|
|
|
2013-01-30 15:21:18 +00:00
|
|
|
/*
|
|
|
|
* First go reap any zombie clients, then accept the connection, and
|
|
|
|
* shut down the read side to stop clients from consuming kernel memory
|
|
|
|
* by sending large buffers full of data we'll never read.
|
|
|
|
*/
|
|
|
|
check_clients();
|
2014-08-14 22:33:56 +00:00
|
|
|
s.socktype = socktype;
|
|
|
|
s.fd = accept(fd, NULL, NULL);
|
|
|
|
if (s.fd != -1) {
|
2013-12-13 21:49:41 +00:00
|
|
|
sndbuf_size = CLIENT_BUFSIZE;
|
2014-08-14 22:33:56 +00:00
|
|
|
if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
|
2013-12-13 21:49:41 +00:00
|
|
|
sizeof(sndbuf_size)))
|
|
|
|
err(1, "setsockopt");
|
2014-08-14 22:33:56 +00:00
|
|
|
shutdown(s.fd, SHUT_RD);
|
2004-07-01 07:24:18 +00:00
|
|
|
clients.push_back(s);
|
2013-01-30 15:21:18 +00:00
|
|
|
++num_clients;
|
2013-12-13 21:49:41 +00:00
|
|
|
} else
|
|
|
|
err(1, "accept");
|
2004-07-01 07:24:18 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2014-08-14 22:33:56 +00:00
|
|
|
int stream_fd, seqpacket_fd, max_fd;
|
2013-01-30 15:21:18 +00:00
|
|
|
int accepting;
|
2003-04-21 06:26:08 +00:00
|
|
|
timeval tv;
|
|
|
|
fd_set fds;
|
2002-12-07 08:04:36 +00:00
|
|
|
|
2012-09-22 12:42:51 +00:00
|
|
|
fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC);
|
2002-12-07 08:04:36 +00:00
|
|
|
if (fd == -1)
|
2004-07-01 07:24:18 +00:00
|
|
|
err(1, "Can't open devctl device %s", PATH_DEVCTL);
|
2014-08-14 22:33:56 +00:00
|
|
|
stream_fd = create_socket(STREAMPIPE, SOCK_STREAM);
|
|
|
|
seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET);
|
2013-01-30 15:21:18 +00:00
|
|
|
accepting = 1;
|
2014-08-14 22:33:56 +00:00
|
|
|
max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1;
|
2013-03-04 02:21:15 +00:00
|
|
|
while (!romeo_must_die) {
|
2014-03-07 23:30:48 +00:00
|
|
|
if (!once && !no_daemon && !daemonize_quick) {
|
2003-04-21 06:26:08 +00:00
|
|
|
// 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-07-23 23:50:00 +00:00
|
|
|
if (rv == 0) {
|
2013-07-01 21:20:17 +00:00
|
|
|
devdlog(LOG_DEBUG, "Calling daemon\n");
|
2006-01-30 22:50:13 +00:00
|
|
|
cfg.remove_pidfile();
|
|
|
|
cfg.open_pidfile();
|
2003-04-21 06:26:08 +00:00
|
|
|
daemon(0, 0);
|
2006-01-30 22:50:13 +00:00
|
|
|
cfg.write_pidfile();
|
2003-04-21 06:26:08 +00:00
|
|
|
once++;
|
|
|
|
}
|
|
|
|
}
|
2013-01-30 15:21:18 +00:00
|
|
|
/*
|
|
|
|
* When we've already got the max number of clients, stop
|
2014-08-14 22:33:56 +00:00
|
|
|
* accepting new connections (don't put the listening sockets in
|
|
|
|
* the set), shrink the accept() queue to reject connections
|
|
|
|
* quickly, and poll the existing clients more often, so that we
|
|
|
|
* notice more quickly when any of them disappear to free up
|
|
|
|
* client slots.
|
2013-01-30 15:21:18 +00:00
|
|
|
*/
|
2004-07-01 07:24:18 +00:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
2013-01-30 15:21:18 +00:00
|
|
|
if (num_clients < max_clients) {
|
|
|
|
if (!accepting) {
|
2014-08-14 22:33:56 +00:00
|
|
|
listen(stream_fd, max_clients);
|
|
|
|
listen(seqpacket_fd, max_clients);
|
2013-01-30 15:21:18 +00:00
|
|
|
accepting = 1;
|
|
|
|
}
|
2014-08-14 22:33:56 +00:00
|
|
|
FD_SET(stream_fd, &fds);
|
|
|
|
FD_SET(seqpacket_fd, &fds);
|
2013-01-30 15:21:18 +00:00
|
|
|
tv.tv_sec = 60;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
} else {
|
|
|
|
if (accepting) {
|
2014-08-14 22:33:56 +00:00
|
|
|
listen(stream_fd, 0);
|
|
|
|
listen(seqpacket_fd, 0);
|
2013-01-30 15:21:18 +00:00
|
|
|
accepting = 0;
|
|
|
|
}
|
|
|
|
tv.tv_sec = 2;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
}
|
|
|
|
rv = select(max_fd, &fds, NULL, NULL, &tv);
|
2013-07-01 21:33:05 +00:00
|
|
|
if (got_siginfo) {
|
2013-12-13 22:58:57 +00:00
|
|
|
devdlog(LOG_NOTICE, "Events received so far=%u\n",
|
2013-07-01 21:33:05 +00:00
|
|
|
total_events);
|
|
|
|
got_siginfo = 0;
|
|
|
|
}
|
2004-07-01 07:24:18 +00:00
|
|
|
if (rv == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
err(1, "select");
|
2013-01-30 15:21:18 +00:00
|
|
|
} else if (rv == 0)
|
|
|
|
check_clients();
|
2004-07-01 07:24:18 +00:00
|
|
|
if (FD_ISSET(fd, &fds)) {
|
|
|
|
rv = read(fd, buffer, sizeof(buffer) - 1);
|
|
|
|
if (rv > 0) {
|
2013-07-01 21:33:05 +00:00
|
|
|
total_events++;
|
2013-07-01 21:20:17 +00:00
|
|
|
if (rv == sizeof(buffer) - 1) {
|
|
|
|
devdlog(LOG_WARNING, "Warning: "
|
|
|
|
"available event data exceeded "
|
|
|
|
"buffer space\n");
|
|
|
|
}
|
2004-07-01 07:24:18 +00:00
|
|
|
notify_clients(buffer, rv);
|
2002-12-07 08:04:36 +00:00
|
|
|
buffer[rv] = '\0';
|
2004-07-01 07:24:18 +00:00
|
|
|
while (buffer[--rv] == '\n')
|
|
|
|
buffer[rv] = '\0';
|
|
|
|
process_event(buffer);
|
|
|
|
} else if (rv < 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* EOF */
|
2002-12-07 08:04:36 +00:00
|
|
|
break;
|
2004-07-01 07:24:18 +00:00
|
|
|
}
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
2014-08-14 22:33:56 +00:00
|
|
|
if (FD_ISSET(stream_fd, &fds))
|
|
|
|
new_client(stream_fd, SOCK_STREAM);
|
|
|
|
/*
|
|
|
|
* Aside from the socket type, both sockets use the same
|
|
|
|
* protocol, so we can process clients the same way.
|
|
|
|
*/
|
|
|
|
if (FD_ISSET(seqpacket_fd, &fds))
|
|
|
|
new_client(seqpacket_fd, SOCK_SEQPACKET);
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2003-10-24 22:02:29 +00:00
|
|
|
void
|
|
|
|
add_notify(int prio, event_proc *p)
|
|
|
|
{
|
|
|
|
cfg.add_notify(prio, p);
|
|
|
|
}
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2005-07-10 03:37:15 +00:00
|
|
|
eps *
|
|
|
|
new_media(const char *var, const char *re)
|
|
|
|
{
|
|
|
|
eps *e = new media(cfg, var, re);
|
|
|
|
free(const_cast<char *>(var));
|
|
|
|
free(const_cast<char *>(re));
|
|
|
|
return (e);
|
|
|
|
}
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:49:17 +00:00
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gensighand(int)
|
|
|
|
{
|
2013-03-04 02:21:08 +00:00
|
|
|
romeo_must_die = 1;
|
2002-12-07 08:04:36 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 21:33:05 +00:00
|
|
|
/*
|
|
|
|
* SIGINFO handler. Will print useful statistics to the syslog or stderr
|
|
|
|
* as appropriate
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
siginfohand(int)
|
|
|
|
{
|
|
|
|
got_siginfo = 1;
|
|
|
|
}
|
|
|
|
|
2013-07-01 21:20:17 +00:00
|
|
|
/*
|
2013-11-07 16:22:04 +00:00
|
|
|
* Local logging function. Prints to syslog if we're daemonized; stderr
|
2013-07-01 21:20:17 +00:00
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
devdlog(int priority, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list argp;
|
|
|
|
|
|
|
|
va_start(argp, fmt);
|
2014-03-07 23:30:48 +00:00
|
|
|
if (no_daemon)
|
2013-07-01 21:20:17 +00:00
|
|
|
vfprintf(stderr, fmt, argp);
|
2014-03-07 23:30:48 +00:00
|
|
|
else if ((! quiet_mode) || (priority <= LOG_WARNING))
|
2013-07-01 21:20:17 +00:00
|
|
|
vsyslog(priority, fmt, argp);
|
|
|
|
va_end(argp);
|
|
|
|
}
|
|
|
|
|
2002-12-07 08:04:36 +00:00
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
2014-03-07 23:30:48 +00:00
|
|
|
fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n",
|
2013-01-30 15:21:18 +00:00
|
|
|
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!");
|
2014-03-26 02:25:40 +00:00
|
|
|
if (val == 0) {
|
|
|
|
warnx("Setting " SYSCTL " to 1000");
|
|
|
|
val = 1000;
|
2003-04-21 04:30:12 +00:00
|
|
|
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();
|
2014-03-07 23:30:48 +00:00
|
|
|
while ((ch = getopt(argc, argv, "df:l:nq")) != -1) {
|
2002-12-07 08:04:36 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
2014-03-07 23:30:48 +00:00
|
|
|
no_daemon = 1;
|
2002-12-07 08:04:36 +00:00
|
|
|
break;
|
2005-11-24 14:39:41 +00:00
|
|
|
case 'f':
|
|
|
|
configfile = optarg;
|
|
|
|
break;
|
2013-01-30 15:21:18 +00:00
|
|
|
case 'l':
|
|
|
|
max_clients = MAX(1, strtoul(optarg, NULL, 0));
|
|
|
|
break;
|
2003-04-21 06:26:08 +00:00
|
|
|
case 'n':
|
2014-03-07 23:30:48 +00:00
|
|
|
daemonize_quick = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet_mode = 1;
|
2003-04-21 06:26:08 +00:00
|
|
|
break;
|
2002-12-07 08:04:36 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.parse();
|
2014-03-07 23:30:48 +00:00
|
|
|
if (!no_daemon && daemonize_quick) {
|
2006-01-30 22:50:13 +00:00
|
|
|
cfg.open_pidfile();
|
2002-12-07 08:04:36 +00:00
|
|
|
daemon(0, 0);
|
2006-01-30 22:50:13 +00:00
|
|
|
cfg.write_pidfile();
|
2003-07-05 00:43:50 +00:00
|
|
|
}
|
2005-05-16 20:51:46 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2002-12-07 08:04:36 +00:00
|
|
|
signal(SIGHUP, gensighand);
|
|
|
|
signal(SIGINT, gensighand);
|
|
|
|
signal(SIGTERM, gensighand);
|
2013-07-01 21:33:05 +00:00
|
|
|
signal(SIGINFO, siginfohand);
|
2002-12-07 08:04:36 +00:00
|
|
|
event_loop();
|
|
|
|
return (0);
|
|
|
|
}
|