udevd.c revision 4c1fc3e404d648c70bd2f50ac50aeac6ece8872e
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * Copyright (C) 2009 Canonical Ltd.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * This program is free software: you can redistribute it and/or modify
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * it under the terms of the GNU General Public License as published by
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * (at your option) any later version.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * This program is distributed in the hope that it will be useful,
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * GNU General Public License for more details.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * You should have received a copy of the GNU General Public License
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic bool arg_debug = false;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic int arg_daemonize = false;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringtypedef struct Manager {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering pid_t pid; /* the process that originally allocated the manager object */
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering struct udev_ctrl_connection *ctrl_conn_blocking;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering unsigned long long int delaying_seqnum;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering unsigned long long int seqnum;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering return container_of(node, struct event, node);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic void event_queue_cleanup(Manager *manager, enum event_state type);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering/* passed from worker to main process */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic void event_free(struct event *event) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering sd_event_source_unref(event->timeout_warning);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (udev_list_node_is_empty(&event->manager->events)) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* only clean up the queue from the process that created it */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic void worker_free(struct worker *worker) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering hashmap_remove(worker->manager->workers, UINT_TO_PTR(worker->pid));
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic void manager_workers_free(Manager *manager) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering HASHMAP_FOREACH(worker, manager->workers, i)
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering manager->workers = hashmap_free(manager->workers);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic int worker_new(struct worker **ret, Manager *manager, struct udev_monitor *worker_monitor, pid_t pid) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_free_ struct worker *worker = NULL;
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering /* close monitor, but keep address around */
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering worker->monitor = udev_monitor_ref(worker_monitor);
a34faf579d2be139b0b9e8cd0c73ad4d918ef736Lukas Nykryn r = hashmap_ensure_allocated(&manager->workers, NULL);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering r = hashmap_put(manager->workers, UINT_TO_PTR(pid), worker);
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering kill_and_sigcont(event->worker->pid, SIGKILL);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_error("seq %llu '%s' killed", udev_device_get_seqnum(event->dev), event->devpath);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_warning("seq %llu '%s' is taking a long time", udev_device_get_seqnum(event->dev), event->devpath);
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic void worker_attach_event(struct worker *worker, struct event *event) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering r = sd_event_now(e, clock_boottime_or_monotonic(), &usec);
d2e54fae5ca7a0f71b5ac8b356a589ff0a09ea0aKay Sievers (void) sd_event_add_time(e, &event->timeout_warning, clock_boottime_or_monotonic(),
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering usec + arg_event_timeout_warn_usec, USEC_PER_SEC, on_event_timeout_warning, event);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering (void) sd_event_add_time(e, &event->timeout, clock_boottime_or_monotonic(),
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering usec + arg_event_timeout_usec, USEC_PER_SEC, on_event_timeout, event);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic void manager_free(Manager *manager) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering sd_event_source_unref(manager->ctrl_event);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering sd_event_source_unref(manager->uevent_event);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering sd_event_source_unref(manager->inotify_event);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering event_queue_cleanup(manager, EVENT_UNDEF);
718db96199eb307751264e4163555662c9a389faLennart Poettering udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return loop_write(fd, &message, sizeof(message), false);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic void worker_spawn(Manager *manager, struct event *event) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* listen for new events */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* allow the main daemon netlink address to send devices to the worker */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering udev_monitor_allow_unicast_sender(worker_monitor, manager->monitor);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering r = udev_monitor_enable_receiving(worker_monitor);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_error_errno(r, "worker: could not enable receiving of device: %m");
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering _cleanup_netlink_unref_ sd_netlink *rtnl = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_close_ int fd_signal = -1, fd_ep = -1;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering struct epoll_event ep_signal = { .events = EPOLLIN };
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering struct epoll_event ep_monitor = { .events = EPOLLIN };
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering /* take initial device from queue */
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering event_queue_cleanup(manager, EVENT_UNDEF);
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering manager->monitor = udev_monitor_unref(manager->monitor);
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
2c4f86c1298f402220965682ab0e7729e150a562Lennart Poettering manager->ctrl = udev_ctrl_unref(manager->ctrl);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering manager->ctrl_event = sd_event_source_unref(manager->ctrl_event);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering manager->uevent_event = sd_event_source_unref(manager->uevent_event);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering manager->inotify_event = sd_event_source_unref(manager->inotify_event);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering manager->event = sd_event_unref(manager->event);
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering r = log_error_errno(errno, "error creating signalfd %m");
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering fd_monitor = udev_monitor_get_fd(worker_monitor);
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering r = log_error_errno(errno, "error creating epoll fd: %m");
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering r = log_error_errno(errno, "fail to add fds to epoll: %m");
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering /* request TERM signal if parent exits */
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering /* reset OOM score, we only protect the main daemon */
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering write_string_file("/proc/self/oom_score_adj", "0", WRITE_STRING_FILE_CREATE);
c7b5eb98e8eeafe63a079ee3c51e9670872437aeLennart Poettering log_debug("seq %llu running", udev_device_get_seqnum(dev));
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * Take a shared lock on the device node; this establishes
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * a concept of device "ownership" to serialize device
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering * access. External processes holding an exclusive lock will
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering * cause udev to skip the event handling; in the case udev
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering * acquired the lock, the external process can block until
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering * udev has finished its event handling.
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (!streq_ptr(udev_device_get_action(dev), "remove") &&
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering streq_ptr("block", udev_device_get_subsystem(dev)) &&
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering !startswith(udev_device_get_sysname(dev), "dm-") &&
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering !startswith(udev_device_get_sysname(dev), "md")) {
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering if (streq_ptr("partition", udev_device_get_devtype(d)))
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* needed for renaming netifs */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* apply rules, create node, symlinks */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering arg_event_timeout_usec, arg_event_timeout_warn_usec,
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen arg_event_timeout_usec, arg_event_timeout_warn_usec);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering /* in case rtnl was initialized */
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen rtnl = sd_netlink_ref(udev_event->rtnl);
641906e9366891e0ad3e6e38b7396a427678c4cfThomas Hindoe Paaboel Andersen /* apply/restore inotify watch */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* send processed event back to libudev listeners */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering udev_monitor_send_device(worker_monitor, NULL, dev);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering log_debug("seq %llu processed", udev_device_get_seqnum(dev));
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering /* send udevd the result of the event execution */
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering r = worker_send_message(manager->worker_watch[WRITE_END]);
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, r;
n = sd_listen_fds(true);
if (ctrl_fd >= 0)
return -EINVAL;
if (netlink_fd >= 0)
return -EINVAL;
return -EINVAL;
if (ctrl_fd < 0) {
if (!udev)
return -ENOMEM;
if (!ctrl)
if (fd < 0)
if (ctrl_fd < 0)
if (netlink_fd < 0) {
if (!udev) {
if (!udev)
return -ENOMEM;
if (!monitor)
if (fd < 0)
if (ctrl_fd < 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);
goto exit;
(void) sd_notify(false,
goto exit;
exit:
sd_notify(false,
if (manager)
log_open();
goto exit;
if (arg_debug) {
if (getuid() != 0) {
goto exit;
if (arg_children_max == 0) {
goto exit;
goto exit;
goto exit;
if (r == -ENOENT)
goto exit;
if (arg_daemonize) {
(void) make_null_stdio();
switch (pid) {
goto exit;
log_close();
setsid();
exit:
log_close();