udevd.c revision f6fd40d16adbd0f9ddbb1f24b4b8734fd8790ad0
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * Copyright (C) 2009 Canonical Ltd.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * This program is free software: you can redistribute it and/or modify
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * it under the terms of the GNU General Public License as published by
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * the Free Software Foundation, either version 2 of the License, or
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * (at your option) any later version.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * This program is distributed in the hope that it will be useful,
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * but WITHOUT ANY WARRANTY; without even the implied warranty of
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * GNU General Public License for more details.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * You should have received a copy of the GNU General Public License
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt * along with this program. If not, see <http://www.gnu.org/licenses/>.
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic bool arg_debug = false;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic int arg_daemonize = false;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic unsigned arg_children_max;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykttypedef struct Manager {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt pid_t pid; /* the process that originally allocated the manager object */
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt struct udev_ctrl_connection *ctrl_conn_blocking;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt unsigned long long int delaying_seqnum;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt unsigned long long int seqnum;
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flyktstatic inline struct event *node_to_event(struct udev_list_node *node) {
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flyktstatic void event_queue_cleanup(Manager *manager, enum event_state type);
f12ed3bf0b315fc88d5fbdf5bdca14b218c86e0cPatrik Flykt/* passed from worker to main process */
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt if (udev_list_node_is_empty(&event->manager->events)) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt /* only clean up the queue from the process that created it */
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
947527f8326d3591f252c48fee5426a563f03544Patrik Flyktstatic void worker_free(struct worker *worker) {
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt hashmap_remove(worker->manager->workers, UINT_TO_PTR(worker->pid));
947527f8326d3591f252c48fee5426a563f03544Patrik Flyktstatic void manager_workers_free(Manager *manager) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt manager->workers = hashmap_free(manager->workers);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic int worker_new(struct worker **ret, Manager *manager, struct udev_monitor *worker_monitor, pid_t pid) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt /* close monitor, but keep address around */
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt worker->monitor = udev_monitor_ref(worker_monitor);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = hashmap_ensure_allocated(&manager->workers, NULL);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = hashmap_put(manager->workers, UINT_TO_PTR(pid), worker);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt log_error("seq %llu '%s' killed", udev_device_get_seqnum(event->dev), event->devpath);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt log_warning("seq %llu '%s' is taking a long time", udev_device_get_seqnum(event->dev), event->devpath);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flyktstatic void worker_attach_event(struct worker *worker, struct event *event) {
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt r = sd_event_now(e, clock_boottime_or_monotonic(), &usec);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt (void) sd_event_add_time(e, &event->timeout_warning, clock_boottime_or_monotonic(),
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt usec + arg_event_timeout_warn_usec, USEC_PER_SEC, on_event_timeout_warning, event);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt (void) sd_event_add_time(e, &event->timeout, clock_boottime_or_monotonic(),
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt usec + arg_event_timeout_usec, USEC_PER_SEC, on_event_timeout, event);
859cca44f834ab1cc3e41fa6b94744f1856ab027Patrik Flykt udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik FlyktDEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt return loop_write(fd, &message, sizeof(message), false);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic void worker_spawn(Manager *manager, struct event *event) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL;
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* listen for new events */
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt /* allow the main daemon netlink address to send devices to the worker */
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt udev_monitor_allow_unicast_sender(worker_monitor, manager->monitor);
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt _cleanup_close_ int fd_signal = -1, fd_ep = -1;
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt struct epoll_event ep_signal = { .events = EPOLLIN };
947527f8326d3591f252c48fee5426a563f03544Patrik Flykt struct epoll_event ep_monitor = { .events = EPOLLIN };
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt /* take initial device from queue */
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->monitor = udev_monitor_unref(manager->monitor);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->ctrl = udev_ctrl_unref(manager->ctrl);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->ctrl_event = sd_event_source_unref(manager->ctrl_event);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->uevent_event = sd_event_source_unref(manager->uevent_event);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->inotify_event = sd_event_source_unref(manager->inotify_event);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt manager->event = sd_event_unref(manager->event);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt r = log_error_errno(errno, "error creating signalfd %m");
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt fd_monitor = udev_monitor_get_fd(worker_monitor);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt r = log_error_errno(errno, "error creating epoll fd: %m");
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt r = log_error_errno(errno, "fail to add fds to epoll: %m");
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt /* request TERM signal if parent exits */
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt /* reset OOM score, we only protect the main daemon */
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt write_string_file("/proc/self/oom_score_adj", "0");
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt log_debug("seq %llu running", udev_device_get_seqnum(dev));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt * Take a shared lock on the device node; this establishes
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt * a concept of device "ownership" to serialize device
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt * access. External processes holding an exclusive lock will
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt * cause udev to skip the event handling; in the case udev
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt * acquired the lock, the external process can block until
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt * udev has finished its event handling.
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (!streq_ptr(udev_device_get_action(dev), "remove") &&
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt streq_ptr("block", udev_device_get_subsystem(dev)) &&
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt !startswith(udev_device_get_sysname(dev), "dm-") &&
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt !startswith(udev_device_get_sysname(dev), "md")) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (streq_ptr("partition", udev_device_get_devtype(d)))
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt /* needed for renaming netifs */
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* apply rules, create node, symlinks */
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt arg_event_timeout_usec, arg_event_timeout_warn_usec,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt arg_event_timeout_usec, arg_event_timeout_warn_usec,
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* in case rtnl was initialized */
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt /* apply/restore inotify watch */
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* send processed event back to libudev listeners */
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt udev_monitor_send_device(worker_monitor, NULL, dev);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt log_debug("seq %llu processed", udev_device_get_seqnum(dev));
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* send udevd the result of the event execution */
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt r = worker_send_message(manager->worker_watch[WRITE_END]);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_error_errno(r, "failed to send result of seq %llu to main daemon: %m",
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* wait for more device messages from main udevd, or term signal */
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt r = log_error_errno(errno, "failed to poll: %m");
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt for (i = 0; i < fdcount; i++) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt dev = udev_monitor_receive_device(worker_monitor);
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
5e256ea7d3d556b3a1fb5c1faa94ec6a8833e53ePatrik Flykt size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_error_errno(errno, "fork of child failed: %m");
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt r = worker_new(&worker, manager, worker_monitor, pid);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_debug("seq %llu forked new worker ["PID_FMT"]", udev_device_get_seqnum(event->dev), pid);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flyktstatic void event_run(Manager *manager, struct event *event) {
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt count = udev_monitor_send_device(manager->monitor, worker->monitor, event->dev);
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt log_error_errno(errno, "worker ["PID_FMT"] did not accept message %zi (%m), kill it",
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt if (hashmap_size(manager->workers) >= arg_children_max) {
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flykt log_debug("maximum number (%i) of children reached", hashmap_size(manager->workers));
2ea8857effb833615b16d10fc7a19a7104c19e13Patrik Flykt /* start new worker and pass initial device */
813e3a6ffcd094696001716480bbd68008cc54c8Patrik Flyktstatic int event_queue_insert(Manager *manager, struct udev_device *dev) {
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 >= 0)
if (netlink >= 0)
* 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->inotify_event, manager->fd_inotify, EPOLLIN, on_inotify, manager);
r = sd_event_add_io(manager->event, &manager->uevent_event, fd_uevent, 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();