udevd.c revision 0f9963a8b8c1d60a467c0cdc04d5e7bfce9d7c75
/*
* Copyright (C) 2004-2012 Kay Sievers <kay.sievers@vrfy.org>
* Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
* Copyright (C) 2009 Canonical Ltd.
* Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stddef.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
#include <getopt.h>
#include <dirent.h>
#include <sys/signalfd.h>
#include "udev.h"
#include "sd-daemon.h"
#include "cgroup-util.h"
#include "dev-setup.h"
static bool debug;
{
}
static struct udev_rules *rules;
static struct udev_queue_export *udev_queue_export;
static struct udev_monitor *monitor;
static int fd_signal = -1;
static int fd_ep = -1;
static int fd_inotify = -1;
static bool stop_exec_queue;
static bool reload;
static int children;
static int children_max;
static int exec_delay;
static sigset_t sigmask_orig;
static UDEV_LIST(event_list);
static UDEV_LIST(worker_list);
char *udev_cgroup;
static bool udev_exit;
enum event_state {
};
struct event {
struct udev_list_node node;
struct udev_device *dev;
enum event_state state;
int exitcode;
unsigned long long int delaying_seqnum;
unsigned long long int seqnum;
const char *devpath;
const char *devpath_old;
bool is_block;
int ifindex;
};
{
}
enum worker_state {
};
struct worker {
struct udev_list_node node;
int refcount;
struct udev_monitor *monitor;
enum worker_state state;
unsigned long long event_start_usec;
};
/* passed from worker to main process */
struct worker_message {
int exitcode;
};
{
}
{
if (export) {
}
}
{
return worker;
}
{
children--;
}
{
return;
}
{
}
}
{
struct udev_monitor *worker_monitor;
/* listen for new events */
if (worker_monitor == NULL)
return;
/* allow the main daemon netlink address to send devices to the worker */
return;
}
/* worker + event reference */
switch (pid) {
case 0: {
int fd_monitor;
int rc = EXIT_SUCCESS;
/* take initial device from queue */
sigfillset(&mask);
if (fd_signal < 0) {
log_error("error creating signalfd %m\n");
rc = 2;
goto out;
}
if (fd_ep < 0) {
log_error("error creating epoll fd: %m\n");
rc = 3;
goto out;
}
log_error("fail to add fds to epoll: %m\n");
rc = 4;
goto out;
}
/* request TERM signal if parent exits */
for (;;) {
struct udev_event *udev_event;
struct worker_message msg;
int err;
if (udev_event == NULL) {
rc = 5;
goto out;
}
if (exec_delay > 0)
/* apply rules, create node, symlinks */
if (err == 0)
}
/* send processed event back to libudev listeners */
/* send udevd the result of the event execution */
if (err != 0)
if (udev_event->sigterm) {
goto out;
}
/* wait for more device messages from main udevd, or term signal */
int fdcount;
int i;
if (fdcount < 0) {
continue;
log_error("failed to poll: %m\n");
goto out;
}
for (i = 0; i < fdcount; i++) {
break;
struct signalfd_siginfo fdsi;
if (size != sizeof(struct signalfd_siginfo))
continue;
case SIGTERM:
goto out;
}
}
}
}
}
out:
if (fd_signal >= 0)
if (fd_ep >= 0)
log_close();
}
case -1:
log_error("fork of child failed: %m\n");
break;
default:
/* close monitor, but keep address around */
children++;
break;
}
}
{
struct udev_list_node *loop;
continue;
if (count < 0) {
continue;
}
return;
}
if (children >= children_max) {
if (children_max > 1)
return;
}
/* start new worker and pass initial device */
}
{
return -1;
return 0;
}
{
struct udev_list_node *loop;
continue;
}
}
/* lookup event for identical, parent, child device */
{
struct udev_list_node *loop;
/* check if queue contains events we depend on */
/* we already found a later event, earlier can not block us, no need to check again */
continue;
/* event we checked earlier still exists, no need to check again */
return true;
/* found ourself, no later event can block us */
break;
if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
return true;
/* check network device ifindex */
return true;
/* check our old name */
return true;
}
/* compare devpath */
/* one devpath is contained in the other? */
continue;
/* identical device event found */
if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
continue;
continue;
return true;
}
/* parent device event found */
return true;
}
/* child device event found */
return true;
}
/* no matching device */
continue;
}
return false;
}
{
struct udev_list_node *loop;
continue;
/* do not start event if parent or child event is still running */
if (is_devpath_busy(event))
continue;
}
}
{
continue;
event_queue_delete(event, false);
}
}
static void worker_returned(int fd_worker)
{
for (;;) {
struct worker_message msg;
struct udev_list_node *loop;
if (size != sizeof(struct worker_message))
break;
/* lookup worker who sent the signal */
continue;
/* worker returned */
}
break;
}
}
}
/* receive the udevd message from userspace */
{
struct udev_ctrl_connection *ctrl_conn;
const char *str;
int i;
goto out;
goto out;
if (i >= 0) {
log_debug("udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
}
if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
log_debug("udevd message (STOP_EXEC_QUEUE) received\n");
stop_exec_queue = true;
}
if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
log_debug("udevd message (START_EXEC_QUEUE) received\n");
stop_exec_queue = false;
}
if (udev_ctrl_get_reload(ctrl_msg) > 0) {
log_debug("udevd message (RELOAD) received\n");
reload = true;
}
char *key;
char *val;
val[0] = '\0';
if (val[0] == '\0') {
} else {
}
} else {
}
}
}
if (i >= 0) {
log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i\n", i);
children_max = i;
}
if (udev_ctrl_get_ping(ctrl_msg) > 0)
log_debug("udevd message (SYNC) received\n");
if (udev_ctrl_get_exit(ctrl_msg) > 0) {
log_debug("udevd message (EXIT) received\n");
udev_exit = true;
/* keep reference to block the client until we exit */
}
out:
return udev_ctrl_connection_unref(ctrl_conn);
}
/* read inotify messages */
{
char *buf;
struct inotify_event *ev;
return 0;
log_error("error getting buffer for inotify\n");
return -1;
}
struct udev_device *dev;
char filename[UTIL_PATH_SIZE];
int fd;
if (fd >= 0) {
log_debug("error writing uevent: %m\n");
}
}
}
}
return 0;
}
{
switch (signo) {
case SIGINT:
case SIGTERM:
udev_exit = true;
break;
case SIGCHLD:
for (;;) {
int status;
if (pid <= 0)
break;
continue;
if (WEXITSTATUS(status) != 0)
} else if (WIFSIGNALED(status)) {
log_error("worker [%u] terminated by signal %i (%s)\n",
} else if (WIFSTOPPED(status)) {
} else if (WIFCONTINUED(status)) {
} else {
}
log_error("worker [%u] failed while handling '%s'\n",
/* drop reference taken for state 'running' */
}
}
break;
}
}
break;
case SIGHUP:
reload = true;
break;
}
}
{
char modules[UTIL_PATH_SIZE];
char buf[4096];
FILE *f;
if (f == NULL)
return;
char *s;
const char *modname;
const char *devname;
const char *devno;
char type;
char filename[UTIL_PATH_SIZE];
if (buf[0] == '#')
continue;
if (s == NULL)
continue;
s[0] = '\0';
devname = &s[1];
if (s == NULL)
continue;
s[0] = '\0';
devno = &s[1];
if (s == NULL)
if (s != NULL)
s[0] = '\0';
continue;
if (type == 'c')
else if (type == 'b')
else
continue;
}
fclose(f);
}
static int mem_size_mb(void)
{
FILE *f;
char buf[4096];
long int memsize = -1;
if (f == NULL)
return -1;
long int value;
break;
}
}
fclose(f);
return memsize;
}
{
char filename[UTIL_PATH_SIZE];
FILE *f;
struct udev_enumerate *udev_enumerate;
struct udev_list_entry *list_entry;
/* current database */
return 0;
/* make sure we do not get here again */
/* old database */
return 0;
if (f != NULL) {
fclose(f);
}
if (udev_enumerate == NULL)
return -1;
struct udev_device *device;
continue;
/* try to find the old database for devices without a current one */
bool have_db;
const char *id;
char devpath[UTIL_PATH_SIZE];
char from[UTIL_PATH_SIZE];
have_db = false;
/* find database in old location */
if (!have_db) {
have_db = true;
}
}
/* find old database with $subsys:$sysname name */
if (!have_db) {
have_db = true;
}
}
/* find old database with the encoded devpath name */
if (!have_db) {
have_db = true;
}
}
/* write out new database */
if (have_db)
}
}
return 0;
}
{
int fd, n;
n = sd_listen_fds(true);
if (n <= 0)
return -1;
if (ctrl >= 0)
return -1;
continue;
}
if (netlink >= 0)
return -1;
continue;
}
return -1;
}
return -1;
return 0;
}
{
FILE *f;
int daemonize = false;
int resolve_names = 1;
{}
};
int fd_ctrl = -1;
int fd_netlink = -1;
int fd_worker = -1;
int rc = 1;
goto exit;
log_open();
label_init("/dev");
for (;;) {
int option;
if (option == -1)
break;
switch (option) {
case 'd':
daemonize = true;
break;
case 'c':
break;
case 'e':
break;
case 'D':
debug = true;
break;
case 'N':
resolve_names = 1;
resolve_names = 0;
resolve_names = -1;
} else {
log_error("resolve-names must be early, late or never\n");
goto exit;
}
break;
case 'h':
printf("Usage: udevd OPTIONS\n"
" --daemon\n"
" --debug\n"
" --children-max=<maximum number of workers>\n"
" --exec-delay=<seconds to wait before executing RUN=>\n"
" --resolve-names=early|late|never\n"
" --version\n"
" --help\n"
"\n");
goto exit;
case 'V':
goto exit;
default:
goto exit;
}
}
/*
* read the kernel commandline, in case we need to get into debug mode
* udev.log-priority=<level> syslog priority
* udev.children-max=<number of workers> events are fully serialized if set to 1
*
*/
if (f != NULL) {
char cmdline[4096];
char *pos;
}
}
}
}
fclose(f);
}
if (getuid() != 0) {
log_error("root privileges required\n");
goto exit;
}
chdir("/");
umask(022);
dev_setup();
/* before opening new files, make sure std{in,out,err} fds are in a sane state */
if (daemonize) {
int fd;
if (fd >= 0) {
if (write(STDOUT_FILENO, 0, 0) < 0)
if (write(STDERR_FILENO, 0, 0) < 0)
if (fd > STDERR_FILENO)
} else {
}
}
/* get control and netlink socket from from systemd */
log_error("error taking over udev control socket");
rc = 1;
goto exit;
}
log_error("error taking over netlink socket\n");
rc = 3;
goto exit;
}
/* get our own cgroup, we regularly kill everything udev has left behind */
udev_cgroup = NULL;
} else {
/* open control and netlink socket */
log_error("error initializing udev control socket");
rc = 1;
goto exit;
}
log_error("error initializing netlink socket\n");
rc = 3;
goto exit;
}
}
if (udev_monitor_enable_receiving(monitor) < 0) {
log_error("error binding netlink socket\n");
rc = 3;
goto exit;
}
if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
log_error("error binding udev control socket\n");
rc = 1;
goto exit;
}
/* create queue file before signalling 'ready', to make sure we block 'settle' */
if (udev_queue_export == NULL) {
log_error("error creating queue file\n");
goto exit;
}
if (daemonize) {
int fd;
switch (pid) {
case 0:
break;
case -1:
log_error("fork of daemon failed: %m\n");
rc = 4;
goto exit;
default:
rc = EXIT_SUCCESS;
goto exit_daemonize;
}
setsid();
if (fd >= 0) {
}
} else {
}
if (f != NULL) {
fclose(f);
}
if (!debug) {
int fd;
if (fd >= 0) {
}
}
if (fd_inotify < 0) {
log_error("error initializing inotify\n");
rc = 4;
goto exit;
}
/* block and listen to all signals on signalfd */
sigfillset(&mask);
if (fd_signal < 0) {
log_error("error creating signalfd\n");
rc = 5;
goto exit;
}
/* unnamed socket from workers to the main daemon */
log_error("error creating socketpair\n");
rc = 6;
goto exit;
}
log_error("error reading rules\n");
goto exit;
}
if (fd_ep < 0) {
log_error("error creating epoll fd: %m\n");
goto exit;
}
log_error("fail to add fds to epoll: %m\n");
goto exit;
}
/* if needed, convert old database from earlier udev version */
if (children_max <= 0) {
int memsize = mem_size_mb();
/* set value depending on the amount of RAM */
if (memsize > 0)
else
children_max = 128;
}
for (;;) {
static unsigned long long last_usec;
int fdcount;
int timeout;
int i;
if (udev_exit) {
/* close sources of new events and discard buffered events */
if (fd_ctrl >= 0) {
fd_ctrl = -1;
}
}
if (fd_inotify >= 0) {
fd_inotify = -1;
}
/* discard queued events and kill workers */
/* exit after all has cleaned up */
break;
/* timeout at exit for workers to finish */
/* we are idle */
timeout = -1;
/* cleanup possible left-over processes in our cgroup */
if (udev_cgroup)
} else {
/* kill idle or hanging workers */
}
if (fdcount < 0)
continue;
if (fdcount == 0) {
struct udev_list_node *loop;
/* timeout */
if (udev_exit) {
log_error("timeout, giving up waiting for workers to finish\n");
break;
}
/* kill idle workers */
if (udev_list_node_is_empty(&event_list)) {
log_debug("cleanup idle workers\n");
}
/* check for hanging events */
continue;
/* drop reference taken for state 'running' */
log_error("seq %llu '%s' killed\n",
}
}
}
}
for (i = 0; i < fdcount; i++) {
is_worker = true;
is_netlink = true;
is_signal = true;
is_inotify = true;
is_ctrl = true;
}
/* check for changed config, every 3 seconds at most */
reload = true;
if (udev_builtin_validate(udev))
reload = true;
}
/* reload requested, HUP signal received, rules changed, builtin changed */
if (reload) {
reload = 0;
}
/* event has finished */
if (is_worker)
if (is_netlink) {
struct udev_device *dev;
if (event_queue_insert(dev) < 0)
}
}
/* start new events */
}
if (is_signal) {
struct signalfd_siginfo fdsi;
if (size == sizeof(struct signalfd_siginfo))
}
/* we are shutting down, the events below are not handled anymore */
if (udev_exit)
continue;
/* device node watch */
if (is_inotify)
/*
* This needs to be after the inotify handling, to make sure,
* that the ping is send back after the possibly generated
* "change" events by the inotify device node watch.
*
* A single time we may receive a client connection which we need to
* keep open to block the client. It will be closed right before we
* exit.
*/
if (is_ctrl)
}
rc = EXIT_SUCCESS;
exit:
if (fd_ep >= 0)
if (fd_signal >= 0)
if (worker_watch[READ_END] >= 0)
if (worker_watch[WRITE_END] >= 0)
label_finish();
log_close();
return rc;
}