[Midnightbsd-cvs] src [11233] trunk/sbin/devd: sync devd with freebsd
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Sun Jul 1 17:14:20 EDT 2018
Revision: 11233
http://svnweb.midnightbsd.org/src/?rev=11233
Author: laffer1
Date: 2018-07-01 17:14:19 -0400 (Sun, 01 Jul 2018)
Log Message:
-----------
sync devd with freebsd
Modified Paths:
--------------
trunk/sbin/devd/Makefile
trunk/sbin/devd/devd.8
trunk/sbin/devd/devd.cc
trunk/sbin/devd/devd.conf.5
trunk/sbin/devd/devd.h
trunk/sbin/devd/devd.hh
trunk/sbin/devd/parse.y
trunk/sbin/devd/token.l
Modified: trunk/sbin/devd/Makefile
===================================================================
--- trunk/sbin/devd/Makefile 2018-07-01 21:13:36 UTC (rev 11232)
+++ trunk/sbin/devd/Makefile 2018-07-01 21:14:19 UTC (rev 11233)
@@ -1,5 +1,8 @@
# $MidnightBSD$
+# $FreeBSD: stable/10/sbin/devd/Makefile 270242 2014-08-20 19:30:58Z asomers $
+.include <bsd.own.mk>
+
PROG_CXX=devd
SRCS= devd.cc token.l parse.y y.tab.h
MAN= devd.8 devd.conf.5
@@ -16,4 +19,8 @@
CLEANFILES= y.output
+.if ${MK_TESTS} != "no"
+SUBDIR+= tests
+.endif
+
.include <bsd.prog.mk>
Modified: trunk/sbin/devd/devd.8
===================================================================
--- trunk/sbin/devd/devd.8 2018-07-01 21:13:36 UTC (rev 11232)
+++ trunk/sbin/devd/devd.8 2018-07-01 21:14:19 UTC (rev 11233)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
.\"
.\" Copyright (c) 2002 M. Warner Losh.
.\" All rights reserved.
@@ -23,9 +24,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $MidnightBSD$
+.\" $FreeBSD: stable/10/sbin/devd/devd.8 307431 2016-10-16 23:34:21Z sevan $
.\"
-.Dd January 30, 2013
+.Dd October 5, 2016
.Dt DEVD 8
.Os
.Sh NAME
@@ -33,7 +34,7 @@
.Nd "device state change daemon"
.Sh SYNOPSIS
.Nm
-.Op Fl Ddn
+.Op Fl dnq
.Op Fl f Ar file
.Op Fl l Ar num
.Sh DESCRIPTION
@@ -44,10 +45,8 @@
.Pp
The following options are accepted.
.Bl -tag -width ".Fl f Ar file"
-.It Fl D
-Enable debugging messages.
.It Fl d
-Run in the foreground instead of becoming a daemon.
+Run in the foreground instead of becoming a daemon and log additional information for debugging.
.It Fl f Ar file
Use configuration file
.Ar file
@@ -57,14 +56,15 @@
.Fl f
is specified more than once, the last file specified is used.
.It Fl l Ar num
-Limit concurrent
-.Pa /var/run/devd.pipe
-connections to
+Limit concurrent socket connections to
.Ar num .
The default connection limit is 10.
.It Fl n
Do not process all pending events before becoming a daemon.
Instead, call daemon right away.
+.It Fl q
+Quiet mode.
+Only log messages at priority LOG_WARNING or above.
.El
.Sh IMPLEMENTATION NOTES
The
@@ -130,25 +130,35 @@
system without modifying the user's other
config files.
.Pp
-All messages that
+Since
+.Xr devctl 4
+allows only one active reader,
.Nm
-receives are forwarded to the
+multiplexes it, forwarding all events to any number of connected clients.
+Clients connect by opening the SOCK_SEQPACKET
.Ux
domain socket at
-.Pa /var/run/devd.pipe .
+.Pa /var/run/devd.seqpacket.pipe .
.Sh FILES
-.Bl -tag -width ".Pa /var/run/devd.pipe" -compact
+.Bl -tag -width ".Pa /var/run/devd.seqpacket.pipe" -compact
.It Pa /etc/devd.conf
The default
.Nm
configuration file.
-.It Pa /var/run/devd.pipe
+.It Pa /var/run/devd.seqpacket.pipe
The socket used by
.Nm
to communicate with its clients.
+.It Pa /var/run/devd.pipe
+A deprecated socket retained for use with old clients.
.El
.Sh SEE ALSO
.Xr devctl 4 ,
.Xr devd.conf 5
+.Sh HISTORY
+The
+.Nm
+utility first appeared in
+.Fx 5.0 .
.Sh AUTHORS
.An M. Warner Losh
Modified: trunk/sbin/devd/devd.cc
===================================================================
--- trunk/sbin/devd/devd.cc 2018-07-01 21:13:36 UTC (rev 11232)
+++ trunk/sbin/devd/devd.cc 2018-07-01 21:14:19 UTC (rev 11233)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*-
* Copyright (c) 2002-2010 M. Warner Losh.
* All rights reserved.
@@ -63,7 +64,7 @@
// - devd.conf needs more details on the supported statements.
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sbin/devd/devd.cc 321827 2017-07-31 22:28:33Z asomers $");
#include <sys/param.h>
#include <sys/socket.h>
@@ -73,9 +74,15 @@
#include <sys/wait.h>
#include <sys/un.h>
-#include <ctype.h>
+#include <cctype>
+#include <cerrno>
+#include <cstdlib>
+#include <cstdio>
+#include <csignal>
+#include <cstring>
+#include <cstdarg>
+
#include <dirent.h>
-#include <errno.h>
#include <err.h>
#include <fcntl.h>
#include <libutil.h>
@@ -82,10 +89,7 @@
#include <paths.h>
#include <poll.h>
#include <regex.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+#include <syslog.h>
#include <unistd.h>
#include <algorithm>
@@ -92,17 +96,48 @@
#include <map>
#include <string>
#include <list>
+#include <stdexcept>
#include <vector>
#include "devd.h" /* C compatible definitions */
#include "devd.hh" /* C++ class definitions */
-#define PIPE "/var/run/devd.pipe"
+#define STREAMPIPE "/var/run/devd.pipe"
+#define SEQPACKETPIPE "/var/run/devd.seqpacket.pipe"
#define CF "/etc/devd.conf"
-#define SYSCTL "hw.bus.devctl_disable"
+#define SYSCTL "hw.bus.devctl_queue"
+/*
+ * 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
+ * buffer, so the client can't increase the buffersize by itself.
+ *
+ * For example, when creating a ZFS pool, devd emits one 165 character
+ * 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.
+ */
+#define CLIENT_BUFSIZE 262144
+
using namespace std;
+typedef struct client {
+ int fd;
+ int socktype;
+} client_t;
+
extern FILE *yyin;
extern int lineno;
@@ -113,13 +148,17 @@
static struct pidfh *pfh;
-int Dflag;
-int dflag;
-int nflag;
-int romeo_must_die = 0;
+static int no_daemon = 0;
+static int daemonize_quick = 0;
+static int quiet_mode = 0;
+static unsigned total_events = 0;
+static volatile sig_atomic_t got_siginfo = 0;
+static volatile sig_atomic_t romeo_must_die = 0;
static const char *configfile = CF;
+static void devdlog(int priority, const char* message, ...)
+ __printflike(2, 3);
static void event_loop(void);
static void usage(void);
@@ -166,7 +205,7 @@
event_proc::run(config &c) const
{
vector<eps *>::const_iterator i;
-
+
for (i = _epsvec.begin(); i != _epsvec.end(); ++i)
if (!(*i)->do_action(c))
return (false);
@@ -174,7 +213,7 @@
}
action::action(const char *cmd)
- : _cmd(cmd)
+ : _cmd(cmd)
{
// nothing
}
@@ -193,7 +232,7 @@
sigset_t newsigblock, oldsigblock;
if (!command) /* just checking... */
- return(1);
+ return (1);
/*
* Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save
@@ -242,8 +281,7 @@
action::do_action(config &c)
{
string s = c.expand_string(_cmd.c_str());
- if (Dflag)
- fprintf(stderr, "Executing '%s'\n", s.c_str());
+ devdlog(LOG_NOTICE, "Executing '%s'\n", s.c_str());
my_system(s.c_str());
return (true);
}
@@ -267,15 +305,22 @@
const string &value = c.get_variable(_var);
bool retval;
- if (Dflag)
- fprintf(stderr, "Testing %s=%s against %s, invert=%d\n",
+ /*
+ * 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.
+ */
+ if (no_daemon) {
+ devdlog(LOG_DEBUG, "Testing %s=%s against %s, invert=%d\n",
_var.c_str(), value.c_str(), _re.c_str(), _inv);
+ }
retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
if (_inv == 1)
retval = (retval == 0) ? 1 : 0;
- return retval;
+ return (retval);
}
#include <sys/sockio.h>
@@ -291,7 +336,6 @@
{ IFM_FDDI, "FDDI" },
{ IFM_IEEE80211, "802.11" },
{ IFM_ATM, "ATM" },
- { IFM_CARP, "CARP" },
{ -1, "unknown" },
{ 0, NULL },
};
@@ -320,10 +364,9 @@
// the name of interest, first try device-name and fall back
// to subsystem if none exists.
value = c.get_variable("device-name");
- if (value.length() == 0)
+ if (value.empty())
value = c.get_variable("subsystem");
- if (Dflag)
- fprintf(stderr, "Testing media type of %s against 0x%x\n",
+ devdlog(LOG_DEBUG, "Testing media type of %s against 0x%x\n",
value.c_str(), _type);
retval = false;
@@ -331,17 +374,15 @@
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));
+ strlcpy(ifmr.ifm_name, value.c_str(), sizeof(ifmr.ifm_name));
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0 &&
ifmr.ifm_status & IFM_AVALID) {
- if (Dflag)
- fprintf(stderr, "%s has media type 0x%x\n",
+ devdlog(LOG_DEBUG, "%s has media type 0x%x\n",
value.c_str(), IFM_TYPE(ifmr.ifm_active));
retval = (IFM_TYPE(ifmr.ifm_active) == _type);
} else if (_type == -1) {
- if (Dflag)
- fprintf(stderr, "%s has unknown media type\n",
+ devdlog(LOG_DEBUG, "%s has unknown media type\n",
value.c_str());
retval = true;
}
@@ -348,7 +389,7 @@
close(s);
}
- return retval;
+ return (retval);
}
const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
@@ -374,8 +415,14 @@
void
var_list::set_variable(const string &var, const string &val)
{
- if (Dflag)
- fprintf(stderr, "setting %s=%s\n", var.c_str(), val.c_str());
+ /*
+ * 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.
+ */
+ if (no_daemon)
+ devdlog(LOG_DEBUG, "setting %s=%s\n", var.c_str(), val.c_str());
_vars[var] = val;
}
@@ -393,8 +440,7 @@
void
config::parse_one_file(const char *fn)
{
- if (Dflag)
- fprintf(stderr, "Parsing %s\n", fn);
+ devdlog(LOG_DEBUG, "Parsing %s\n", fn);
yyin = fopen(fn, "r");
if (yyin == NULL)
err(1, "Cannot open config file %s", fn);
@@ -411,8 +457,7 @@
struct dirent *dp;
char path[PATH_MAX];
- if (Dflag)
- fprintf(stderr, "Parsing files in %s\n", dirname);
+ devdlog(LOG_DEBUG, "Parsing files in %s\n", dirname);
dirp = opendir(dirname);
if (dirp == NULL)
return;
@@ -460,8 +505,8 @@
config::open_pidfile()
{
pid_t otherpid;
-
- if (_pidfile == "")
+
+ if (_pidfile.empty())
return;
pfh = pidfile_open(_pidfile.c_str(), 0600, &otherpid);
if (pfh == NULL) {
@@ -474,7 +519,7 @@
void
config::write_pidfile()
{
-
+
pidfile_write(pfh);
}
@@ -481,7 +526,7 @@
void
config::close_pidfile()
{
-
+
pidfile_close(pfh);
}
@@ -488,7 +533,7 @@
void
config::remove_pidfile()
{
-
+
pidfile_remove(pfh);
}
@@ -529,7 +574,7 @@
void
config::set_pidfile(const char *fn)
{
- _pidfile = string(fn);
+ _pidfile = fn;
}
void
@@ -536,11 +581,10 @@
config::push_var_table()
{
var_list *vl;
-
+
vl = new var_list();
_var_list_table.push_back(vl);
- if (Dflag)
- fprintf(stderr, "Pushing table\n");
+ devdlog(LOG_DEBUG, "Pushing table\n");
}
void
@@ -548,8 +592,7 @@
{
delete _var_list_table.back();
_var_list_table.pop_back();
- if (Dflag)
- fprintf(stderr, "Popping table\n");
+ devdlog(LOG_DEBUG, "Popping table\n");
}
void
@@ -573,7 +616,7 @@
bool
config::is_id_char(char ch) const
{
- return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
+ return (ch != '\0' && (isalpha(ch) || isdigit(ch) || ch == '_' ||
ch == '-'));
}
@@ -586,15 +629,15 @@
src++;
// $$ -> $
if (*src == '$') {
- dst.append(src++, 1);
+ dst += *src++;
return;
}
-
+
// $(foo) -> $(foo)
// Not sure if I want to support this or not, so for now we just pass
// it through.
if (*src == '(') {
- dst.append("$");
+ dst += '$';
count = 1;
/* If the string ends before ) is matched , return. */
while (count > 0 && *src) {
@@ -602,23 +645,23 @@
count--;
else if (*src == '(')
count++;
- dst.append(src++, 1);
+ dst += *src++;
}
return;
}
-
- // ${^A-Za-z] -> $\1
+
+ // $[^A-Za-z] -> $\1
if (!isalpha(*src)) {
- dst.append("$");
- dst.append(src++, 1);
+ dst += '$';
+ dst += *src++;
return;
}
// $var -> replace with value
do {
- buffer.append(src++, 1);
+ buffer += *src++;
} while (is_id_char(*src));
- dst.append(get_variable(buffer.c_str()));
+ dst.append(get_variable(buffer));
}
const string
@@ -654,10 +697,10 @@
}
bool
-config::chop_var(char *&buffer, char *&lhs, char *&rhs)
+config::chop_var(char *&buffer, char *&lhs, char *&rhs) const
{
char *walker;
-
+
if (*buffer == '\0')
return (false);
walker = lhs = buffer;
@@ -731,8 +774,7 @@
s = "detach";
break;
}
- if (Dflag)
- fprintf(stderr, "Processing %s event\n", s);
+ devdlog(LOG_DEBUG, "Processing %s event\n", s);
for (i = l->begin(); i != l->end(); ++i) {
if ((*i)->matches(*this)) {
(*i)->run(*this);
@@ -742,7 +784,7 @@
}
-
+
static void
process_event(char *buffer)
{
@@ -750,8 +792,7 @@
char *sp;
sp = buffer + 1;
- if (Dflag)
- fprintf(stderr, "Processing event '%s'\n", buffer);
+ devdlog(LOG_INFO, "Processing event '%s'\n", buffer);
type = *buffer++;
cfg.push_var_table();
// No match doesn't have a device, and the format is a little
@@ -794,18 +835,18 @@
cfg.set_variable("bus", sp + 3);
break;
}
-
+
cfg.find_and_execute(type);
cfg.pop_var_table();
}
int
-create_socket(const char *name)
+create_socket(const char *name, int socktype)
{
int fd, slen;
struct sockaddr_un sun;
- if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
+ if ((fd = socket(PF_LOCAL, socktype, 0)) < 0)
err(1, "socket");
bzero(&sun, sizeof(sun));
sun.sun_family = AF_UNIX;
@@ -817,19 +858,22 @@
if (::bind(fd, (struct sockaddr *) & sun, slen) < 0)
err(1, "bind");
listen(fd, 4);
- chown(name, 0, 0); /* XXX - root.wheel */
- chmod(name, 0666);
+ if (chown(name, 0, 0)) /* XXX - root.wheel */
+ err(1, "chown");
+ if (chmod(name, 0666))
+ err(1, "chmod");
return (fd);
}
-unsigned int max_clients = 10; /* Default, can be overriden on cmdline. */
+unsigned int max_clients = 10; /* Default, can be overridden on cmdline. */
unsigned int num_clients;
-list<int> clients;
+list<client_t> clients;
+
void
notify_clients(const char *data, int len)
{
- list<int>::iterator i;
+ list<client_t>::iterator i;
/*
* Deliver the data to all clients. Throw clients overboard at the
@@ -839,10 +883,18 @@
* kernel memory or tie up the limited number of available connections).
*/
for (i = clients.begin(); i != clients.end(); ) {
- if (write(*i, data, len) != len) {
+ int flags;
+ if (i->socktype == SOCK_SEQPACKET)
+ flags = MSG_EOR;
+ else
+ flags = 0;
+
+ if (send(i->fd, data, len, flags) != len) {
--num_clients;
- close(*i);
+ close(i->fd);
i = clients.erase(i);
+ devdlog(LOG_WARNING, "notify_clients: send() failed; "
+ "dropping unresponsive client\n");
} else
++i;
}
@@ -853,7 +905,7 @@
{
int s;
struct pollfd pfd;
- list<int>::iterator i;
+ list<client_t>::iterator i;
/*
* Check all existing clients to see if any of them have disappeared.
@@ -864,13 +916,15 @@
*/
pfd.events = 0;
for (i = clients.begin(); i != clients.end(); ) {
- pfd.fd = *i;
+ pfd.fd = i->fd;
s = poll(&pfd, 1, 0);
if ((s < 0 && s != EINTR ) ||
(s > 0 && (pfd.revents & POLLHUP))) {
--num_clients;
- close(*i);
+ close(i->fd);
i = clients.erase(i);
+ devdlog(LOG_NOTICE, "check_clients: "
+ "dropping disconnected client\n");
} else
++i;
}
@@ -877,9 +931,10 @@
}
void
-new_client(int fd)
+new_client(int fd, int socktype)
{
- int s;
+ client_t s;
+ int sndbuf_size;
/*
* First go reap any zombie clients, then accept the connection, and
@@ -887,12 +942,18 @@
* by sending large buffers full of data we'll never read.
*/
check_clients();
- s = accept(fd, NULL, NULL);
- if (s != -1) {
- shutdown(s, SHUT_RD);
+ s.socktype = socktype;
+ s.fd = accept(fd, NULL, NULL);
+ if (s.fd != -1) {
+ sndbuf_size = CLIENT_BUFSIZE;
+ if (setsockopt(s.fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size,
+ sizeof(sndbuf_size)))
+ err(1, "setsockopt");
+ shutdown(s.fd, SHUT_RD);
clients.push_back(s);
++num_clients;
- }
+ } else
+ err(1, "accept");
}
static void
@@ -902,23 +963,20 @@
int fd;
char buffer[DEVCTL_MAXBUF];
int once = 0;
- int server_fd, max_fd;
+ int stream_fd, seqpacket_fd, max_fd;
int accepting;
timeval tv;
fd_set fds;
- fd = open(PATH_DEVCTL, O_RDONLY);
+ fd = open(PATH_DEVCTL, O_RDONLY | O_CLOEXEC);
if (fd == -1)
err(1, "Can't open devctl device %s", PATH_DEVCTL);
- if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
- err(1, "Can't set close-on-exec flag on devctl");
- server_fd = create_socket(PIPE);
+ stream_fd = create_socket(STREAMPIPE, SOCK_STREAM);
+ seqpacket_fd = create_socket(SEQPACKETPIPE, SOCK_SEQPACKET);
accepting = 1;
- max_fd = max(fd, server_fd) + 1;
- while (1) {
- if (romeo_must_die)
- break;
- if (!once && !dflag && !nflag) {
+ max_fd = max(fd, max(stream_fd, seqpacket_fd)) + 1;
+ while (!romeo_must_die) {
+ if (!once && !no_daemon && !daemonize_quick) {
// Check to see if we have any events pending.
tv.tv_sec = 0;
tv.tv_usec = 0;
@@ -927,8 +985,7 @@
rv = select(fd + 1, &fds, &fds, &fds, &tv);
// No events -> we've processed all pending events
if (rv == 0) {
- if (Dflag)
- fprintf(stderr, "Calling daemon\n");
+ devdlog(LOG_DEBUG, "Calling daemon\n");
cfg.remove_pidfile();
cfg.open_pidfile();
daemon(0, 0);
@@ -938,24 +995,28 @@
}
/*
* When we've already got the max number of clients, stop
- * accepting new connections (don't put server_fd 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.
+ * 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.
*/
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (num_clients < max_clients) {
if (!accepting) {
- listen(server_fd, max_clients);
+ listen(stream_fd, max_clients);
+ listen(seqpacket_fd, max_clients);
accepting = 1;
}
- FD_SET(server_fd, &fds);
+ FD_SET(stream_fd, &fds);
+ FD_SET(seqpacket_fd, &fds);
tv.tv_sec = 60;
tv.tv_usec = 0;
} else {
if (accepting) {
- listen(server_fd, 0);
+ listen(stream_fd, 0);
+ listen(seqpacket_fd, 0);
accepting = 0;
}
tv.tv_sec = 2;
@@ -962,6 +1023,11 @@
tv.tv_usec = 0;
}
rv = select(max_fd, &fds, NULL, NULL, &tv);
+ if (got_siginfo) {
+ devdlog(LOG_NOTICE, "Events received so far=%u\n",
+ total_events);
+ got_siginfo = 0;
+ }
if (rv == -1) {
if (errno == EINTR)
continue;
@@ -971,11 +1037,23 @@
if (FD_ISSET(fd, &fds)) {
rv = read(fd, buffer, sizeof(buffer) - 1);
if (rv > 0) {
+ total_events++;
+ if (rv == sizeof(buffer) - 1) {
+ devdlog(LOG_WARNING, "Warning: "
+ "available event data exceeded "
+ "buffer space\n");
+ }
notify_clients(buffer, rv);
buffer[rv] = '\0';
while (buffer[--rv] == '\n')
buffer[rv] = '\0';
- process_event(buffer);
+ try {
+ process_event(buffer);
+ }
+ catch (std::length_error e) {
+ devdlog(LOG_ERR, "Dropping event %s "
+ "due to low memory", buffer);
+ }
} else if (rv < 0) {
if (errno != EINTR)
break;
@@ -984,12 +1062,21 @@
break;
}
}
- if (FD_ISSET(server_fd, &fds))
- new_client(server_fd);
+ 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);
}
+ cfg.remove_pidfile();
+ close(seqpacket_fd);
+ close(stream_fd);
close(fd);
}
-
+
/*
* functions that the parser uses.
*/
@@ -1074,19 +1161,45 @@
free(const_cast<char *>(val));
}
-
+
static void
gensighand(int)
{
- romeo_must_die++;
- _exit(0);
+ romeo_must_die = 1;
}
+/*
+ * SIGINFO handler. Will print useful statistics to the syslog or stderr
+ * as appropriate
+ */
static void
+siginfohand(int)
+{
+ got_siginfo = 1;
+}
+
+/*
+ * Local logging function. Prints to syslog if we're daemonized; syslog
+ * otherwise.
+ */
+static void
+devdlog(int priority, const char* fmt, ...)
+{
+ va_list argp;
+
+ va_start(argp, fmt);
+ if (no_daemon)
+ vfprintf(stderr, fmt, argp);
+ else if ((! quiet_mode) || (priority <= LOG_WARNING))
+ vsyslog(priority, fmt, argp);
+ va_end(argp);
+}
+
+static void
usage()
{
- fprintf(stderr, "usage: %s [-Ddn] [-l connlimit] [-f file]\n",
+ fprintf(stderr, "usage: %s [-dnq] [-l connlimit] [-f file]\n",
getprogname());
exit(1);
}
@@ -1100,10 +1213,11 @@
len = sizeof(val);
if (sysctlbyname(SYSCTL, &val, &len, NULL, 0) != 0)
errx(1, "devctl sysctl missing from kernel!");
- if (val) {
- warnx("Setting " SYSCTL " to 0");
- val = 0;
- sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val));
+ if (val == 0) {
+ warnx("Setting " SYSCTL " to 1000");
+ val = 1000;
+ if (sysctlbyname(SYSCTL, NULL, NULL, &val, sizeof(val)))
+ err(1, "sysctlbyname");
}
}
@@ -1116,13 +1230,10 @@
int ch;
check_devd_enabled();
- while ((ch = getopt(argc, argv, "Ddf:l:n")) != -1) {
+ while ((ch = getopt(argc, argv, "df:l:nq")) != -1) {
switch (ch) {
- case 'D':
- Dflag++;
- break;
case 'd':
- dflag++;
+ no_daemon = 1;
break;
case 'f':
configfile = optarg;
@@ -1131,8 +1242,11 @@
max_clients = MAX(1, strtoul(optarg, NULL, 0));
break;
case 'n':
- nflag++;
+ daemonize_quick = 1;
break;
+ case 'q':
+ quiet_mode = 1;
+ break;
default:
usage();
}
@@ -1139,7 +1253,7 @@
}
cfg.parse();
- if (!dflag && nflag) {
+ if (!no_daemon && daemonize_quick) {
cfg.open_pidfile();
daemon(0, 0);
cfg.write_pidfile();
@@ -1148,6 +1262,7 @@
signal(SIGHUP, gensighand);
signal(SIGINT, gensighand);
signal(SIGTERM, gensighand);
+ signal(SIGINFO, siginfohand);
event_loop();
return (0);
}
Modified: trunk/sbin/devd/devd.conf.5
===================================================================
--- trunk/sbin/devd/devd.conf.5 2018-07-01 21:13:36 UTC (rev 11232)
+++ trunk/sbin/devd/devd.conf.5 2018-07-01 21:14:19 UTC (rev 11233)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
.\"
.\" Copyright (c) 2002 M. Warner Losh
.\" All rights reserved.
@@ -22,7 +23,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $MidnightBSD$
+.\" $FreeBSD: stable/10/sbin/devd/devd.conf.5 307446 2016-10-16 23:54:40Z sevan $
.\"
.\" The section on comments was taken from named.conf.5, which has the
.\" following copyright:
@@ -41,7 +42,7 @@
.\" ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
.\" SOFTWARE.
.\"
-.Dd March 4, 2013
+.Dd October 8, 2016
.Dt DEVD.CONF 5
.Os
.Sh NAME
@@ -181,9 +182,8 @@
.Dq Li Tokenring ,
.Dq Li FDDI ,
.Dq Li 802.11 ,
-.Dq Li ATM ,
and
-.Dq Li CARP .
+.Dq Li ATM .
.It Ic subdevice Qq Ar string ;
This is shorthand for
.Dq Ic match Qo Li subdevice Qc Qq Ar string .
@@ -271,6 +271,8 @@
Interface Protocol (USB)
.It Li intsubclass
Interface Sub-class (USB)
+.It Li jail
+Jail name for the process triggering the rule (RCTL)
.It Li manufacturer
Manufacturer ID (pccard).
.It Li mode
@@ -281,6 +283,8 @@
variable.
.It Li parent
Parent device
+.It Li pid
+PID of the process triggering the rule (RCTL)
.It Li port
Hub port number (USB)
.It Li product
@@ -287,6 +291,10 @@
Product ID (pccard/USB).
.It Li release
Hardware revision (USB)
+.It Li ruid
+Real UID of the process triggering the rule (RCTL)
+.It Li rule
+Rule (RCTL)
.It Li sernum
Serial Number (USB).
.It Li slot
@@ -325,10 +333,34 @@
Lid state ($notify=0x00 is closed, 0x01 is open).
.It Li PROCESSOR
Processor state/configuration ($notify=0x81 is a change in available Cx states).
+.It Li Resume
+Resume notification.
+.It Li Suspend
+Suspend notification.
.It Li Thermal
Thermal zone events.
.El
.Pp
+.It Li CARP
+Events related to the
+.Xr carp 8
+protocol.
+.Bl -tag -width ".Sy Subsystem" -compact
+.It Sy Subsystem
+.It Ar vhid at interface
+The
+.Dq subsystem
+contains the actual CARP vhid and the name of the network interface
+on which the event took place.
+.Bl -tag -width ".Li MASTER" -compact
+.It Sy Type
+.It Li MASTER
+Node become the master for a virtual host.
+.It Li BACKUP
+Node become the backup for a virtual host.
+.El
+.El
+.Pp
.It Li IFNET
Events related to the network subsystem.
.Bl -tag -width ".Sy Subsystem" -compact
@@ -350,6 +382,7 @@
The network interface is detached from the system.
.El
.El
+.Pp
.It Li DEVFS
Events related to the
.Xr devfs 5
@@ -369,6 +402,50 @@
node is destroyed.
.El
.El
+.Pp
+.It Li GEOM
+Events related to the
+.Xr geom 4
+framework.
+The difference compared to
+.Li DEVFS
+is that
+.Li GEOM
+only includes disk-like devices.
+.Bl -tag -width ".Sy Subsystem" -compact
+.It Sy Subsystem
+.It Li DEV
+.Bl -tag -width ".Li MEDIACHANGE" -compact
+.It Sy Type
+.It Li CREATE
+A
+.Xr geom 4
+device node is created.
+.It Li DESTROY
+A
+.Xr geom 4
+device node is destroyed.
+.It Li GEOM::physpath
+The physical path of a device has changed.
+.It Li MEDIACHANGE
+Physical media has changed.
+.El
+.El
+.Pp
+.It Li RCTL
+Events related to the
+.Xr rctl 8
+framework.
+.Bl -tag -width ".Sy Subsystem" -compact
+.It Sy Subsystem
+.It Li rule
+.Bl -tag -width ".Li matched" -compact
+.It Sy Type
+.It Li matched
+A rule with action specified as "devctl" was triggered.
+.El
+.El
+.Pp
.It Li USB
Events related to the USB subsystem.
.Bl -tag -width ".Sy Subsystem" -compact
@@ -390,6 +467,7 @@
USB interface is detached from a device.
.El
.El
+.Pp
.It Li coretemp
Events related to the
.Xr coretemp 4
@@ -404,6 +482,7 @@
String containing the temperature of the core that has become too hot.
.El
.El
+.Pp
.It Li kern
Events related to the kernel.
.Bl -tag -width ".Sy Subsystem" -compact
@@ -416,7 +495,24 @@
Notification that the system has woken from the suspended state.
.El
.El
+.Pp
+.It Li CAM
+Events related to the
+.Xr cam 4
+system.
+.Bl -tag -width ".Sy Subsystem" -compact
+.It Sy Subsystem
+.It Li periph
+Events related to peripheral devices.
+.Bl -tag -width ".li timeout" -compact
+.It Sy Type
+.It Li error
+Generic errors.
+.It Li timeout
+Command timeouts.
.El
+.El
+.El
.Pp
A link state change to UP on the interface
.Dq Li fxp0
@@ -542,6 +638,8 @@
.Pa /etc/devd.conf
has many additional examples.
.Sh SEE ALSO
+.Xr cam 4 ,
.Xr coretemp 4 ,
.Xr devfs 5 ,
+.Xr re_format 7 ,
.Xr devd 8
Modified: trunk/sbin/devd/devd.h
===================================================================
--- trunk/sbin/devd/devd.h 2018-07-01 21:13:36 UTC (rev 11232)
+++ trunk/sbin/devd/devd.h 2018-07-01 21:14:19 UTC (rev 11233)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*-
* DEVD (Device action daemon)
*
@@ -25,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sbin/devd/devd.h 249951 2013-04-26 19:49:37Z asomers $
*/
#ifndef DEVD_H
Modified: trunk/sbin/devd/devd.hh
===================================================================
--- trunk/sbin/devd/devd.hh 2018-07-01 21:13:36 UTC (rev 11232)
+++ trunk/sbin/devd/devd.hh 2018-07-01 21:14:19 UTC (rev 11233)
@@ -23,6 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
+ * $FreeBSD: stable/10/sbin/devd/devd.hh 247760 2013-03-04 02:21:24Z eadler $
* $MidnightBSD$
*/
@@ -41,8 +42,6 @@
class var_list
{
public:
- var_list() {}
- virtual ~var_list() {}
/** Set a variable in this var list.
*/
void set_variable(const std::string &var, const std::string &val);
@@ -68,7 +67,6 @@
struct eps
{
public:
- eps() {}
virtual ~eps() {}
/** Does this eps match the current config?
*/
@@ -144,7 +142,7 @@
class config
{
public:
- config() : _pidfile("") { push_var_table(); }
+ config() { push_var_table(); }
virtual ~config() { reset(); }
void add_attach(int, event_proc *);
void add_detach(int, event_proc *);
@@ -172,7 +170,7 @@
void parse_files_in_dir(const char *dirname);
void expand_one(const char *&src, std::string &dst);
bool is_id_char(char) const;
- bool chop_var(char *&buffer, char *&lhs, char *&rhs);
+ bool chop_var(char *&buffer, char *&lhs, char *&rhs) const;
private:
std::vector<std::string> _dir_list;
std::string _pidfile;
Modified: trunk/sbin/devd/parse.y
===================================================================
--- trunk/sbin/devd/parse.y 2018-07-01 21:13:36 UTC (rev 11232)
+++ trunk/sbin/devd/parse.y 2018-07-01 21:14:19 UTC (rev 11233)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
%{
/*-
* DEVD (Device action daemon)
@@ -26,7 +27,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sbin/devd/parse.y 235789 2012-05-22 16:33:10Z bapt $
*/
#include <sys/cdefs.h>
Modified: trunk/sbin/devd/token.l
===================================================================
--- trunk/sbin/devd/token.l 2018-07-01 21:13:36 UTC (rev 11232)
+++ trunk/sbin/devd/token.l 2018-07-01 21:14:19 UTC (rev 11233)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
%{
/*-
* DEVD (Device action daemon)
@@ -26,7 +27,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sbin/devd/token.l 250227 2013-05-03 23:51:32Z jkim $
*/
#include <ctype.h>
More information about the Midnightbsd-cvs
mailing list