udevd.c revision fcff1e7241377b44f1d6e2a68ed55940b154ed4e
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * Copyright (C) 2009 Canonical Ltd.
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * This program is free software: you can redistribute it and/or modify
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * it under the terms of the GNU General Public License as published by
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * the Free Software Foundation, either version 2 of the License, or
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * (at your option) any later version.
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * This program is distributed in the hope that it will be useful,
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * but WITHOUT ANY WARRANTY; without even the implied warranty of
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * GNU General Public License for more details.
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * You should have received a copy of the GNU General Public License
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * along with this program. If not, see <http://www.gnu.org/licenses/>.
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic bool arg_debug = false;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic int arg_daemonize = false;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersentypedef struct Manager {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen pid_t pid; /* the process that originally allocated the manager object */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen struct udev_ctrl_connection *ctrl_conn_blocking;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen unsigned long long int delaying_seqnum;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen unsigned long long int seqnum;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic inline struct event *node_to_event(struct udev_list_node *node) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen return container_of(node, struct event, node);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic void event_queue_cleanup(Manager *manager, enum event_state type);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen/* passed from worker to main process */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen sd_event_source_unref(event->timeout_warning);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen if (udev_list_node_is_empty(&event->manager->events)) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* only clean up the queue from the process that created it */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic void worker_free(struct worker *worker) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen hashmap_remove(worker->manager->workers, UINT_TO_PTR(worker->pid));
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic void manager_workers_free(Manager *manager) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->workers = hashmap_free(manager->workers);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic int worker_new(struct worker **ret, Manager *manager, struct udev_monitor *worker_monitor, pid_t pid) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* close monitor, but keep address around */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen worker->monitor = udev_monitor_ref(worker_monitor);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen r = hashmap_ensure_allocated(&manager->workers, NULL);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen r = hashmap_put(manager->workers, UINT_TO_PTR(pid), worker);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen kill_and_sigcont(event->worker->pid, SIGKILL);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen log_error("seq %llu '%s' killed", udev_device_get_seqnum(event->dev), event->devpath);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen log_warning("seq %llu '%s' is taking a long time", udev_device_get_seqnum(event->dev), event->devpath);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic void worker_attach_event(struct worker *worker, struct event *event) {
52efd56a6369e19c2400a42981a197cd2eef924aLennart Poettering r = sd_event_now(e, clock_boottime_or_monotonic(), &usec);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen (void) sd_event_add_time(e, &event->timeout_warning, clock_boottime_or_monotonic(),
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen usec + arg_event_timeout_warn_usec, USEC_PER_SEC, on_event_timeout_warning, event);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen (void) sd_event_add_time(e, &event->timeout, clock_boottime_or_monotonic(),
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen usec + arg_event_timeout_usec, USEC_PER_SEC, on_event_timeout, event);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen sd_event_source_unref(manager->inotify_event);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
7e518afab9fb55b8052f68888210927259275560Thomas Hindoe Paaboel Andersenstatic int worker_send_message(int fd) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen return loop_write(fd, &message, sizeof(message), false);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersenstatic void worker_spawn(Manager *manager, struct event *event) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* listen for new events */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* allow the main daemon netlink address to send devices to the worker */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen udev_monitor_allow_unicast_sender(worker_monitor, manager->monitor);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen udev_monitor_enable_receiving(worker_monitor);
f0c4b1c3fd827b429ba36aa45fd39e0a023cbf2cTom Gundersen _cleanup_close_ int fd_signal = -1, fd_ep = -1;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen struct epoll_event ep_signal = { .events = EPOLLIN };
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen struct epoll_event ep_monitor = { .events = EPOLLIN };
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* take initial device from queue */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->monitor = udev_monitor_unref(manager->monitor);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->ctrl = udev_ctrl_unref(manager->ctrl);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->ctrl_event = sd_event_source_unref(manager->ctrl_event);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->uevent_event = sd_event_source_unref(manager->uevent_event);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->inotify_event = sd_event_source_unref(manager->inotify_event);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->event = sd_event_unref(manager->event);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen r = log_error_errno(errno, "error creating signalfd %m");
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen fd_monitor = udev_monitor_get_fd(worker_monitor);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen r = log_error_errno(errno, "error creating epoll fd: %m");
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen r = log_error_errno(errno, "fail to add fds to epoll: %m");
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* request TERM signal if parent exits */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* reset OOM score, we only protect the main daemon */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen write_string_file("/proc/self/oom_score_adj", "0");
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen log_debug("seq %llu running", udev_device_get_seqnum(dev));
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * Take a shared lock on the device node; this establishes
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * a concept of device "ownership" to serialize device
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * access. External processes holding an exclusive lock will
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * cause udev to skip the event handling; in the case udev
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * acquired the lock, the external process can block until
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen * udev has finished its event handling.
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen if (!streq_ptr(udev_device_get_action(dev), "remove") &&
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen streq_ptr("block", udev_device_get_subsystem(dev)) &&
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen !startswith(udev_device_get_sysname(dev), "dm-") &&
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen !startswith(udev_device_get_sysname(dev), "md")) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen if (streq_ptr("partition", udev_device_get_devtype(d)))
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* needed for renaming netifs */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* apply rules, create node, symlinks */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen arg_event_timeout_usec, arg_event_timeout_warn_usec,
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen arg_event_timeout_usec, arg_event_timeout_warn_usec);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* in case rtnl was initialized */
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann /* apply/restore inotify watch */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* send processed event back to libudev listeners */
skip:
int fdcount;
if (fdcount < 0) {
goto out;
for (i = 0; i < fdcount; i++) {
case SIGTERM:
goto out;
out:
log_close();
Iterator i;
if (count < 0) {
if (!event)
return -ENOMEM;
Iterator i;
if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
sd_notify(false,
sd_notify(false,
sd_notify(false,
} control = {};
if (size < 0) {
if (!worker) {
if (dev) {
const char *str;
if (!ctrl_conn)
if (!ctrl_msg)
if (key) {
char *val;
arg_children_max = i;
bool part_table_read = false;
bool has_partitions = false;
int fd;
if (fd >= 0) {
part_table_read = true;
return -ENOMEM;
r = udev_enumerate_scan_devices(e);
has_partitions = true;
struct inotify_event *e;
ssize_t l;
if (!dev)
int status;
if (pid <= 0)
if (!worker) {
log_warning("worker ["PID_FMT"] terminated by signal %i (%s)", pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
int fd, n;
n = sd_listen_fds(true);
if (ctrl_fd >= 0)
return -EINVAL;
if (netlink_fd >= 0)
return -EINVAL;
return -EINVAL;
return -EINVAL;
* udev.children-max=<number of workers> events are fully serialized if set to 1
if (!value)
int prio;
static void help(void) {
arg_daemonize = true;
arg_debug = true;
arg_resolve_names = 0;
help();
return -EINVAL;
if (!manager)
return log_oom();
r = sd_event_add_io(manager->event, &manager->ctrl_event, udev_ctrl_get_fd(manager->ctrl), EPOLLIN, on_ctrl_msg, manager);
r = sd_event_add_io(manager->event, &manager->inotify_event, manager->fd_inotify, EPOLLIN, on_inotify, manager);
r = sd_event_add_io(manager->event, &manager->uevent_event, udev_monitor_get_fd(manager->monitor), EPOLLIN, on_uevent, manager);
log_open();
goto exit;
if (arg_debug)
if (getuid() != 0) {
goto exit;
if (arg_children_max == 0) {
goto exit;
goto exit;
goto exit;
goto exit;
if (arg_daemonize) {
switch (pid) {
goto exit;
log_close();
setsid();
sd_notify(false,
goto exit;
exit:
sd_notify(false,
if (manager)
log_close();