udevd.c revision 712cebf146b65136f02b49eb0f64a14f4628aec9
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 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) {
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
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);
7e518afab9fb55b8052f68888210927259275560Thomas Hindoe Paaboel Andersen _cleanup_close_ int fd_signal = -1, fd_ep = -1;
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* take initial device from queue */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->monitor = udev_monitor_unref(manager->monitor);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->ctrl = udev_ctrl_unref(manager->ctrl);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->fd_signal = safe_close(manager->fd_signal);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen r = log_error_errno(errno, "error creating signalfd %m");
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen r = log_error_errno(errno, "error creating epoll fd: %m");
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen memzero(&ep_signal, sizeof(struct epoll_event));
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen fd_monitor = udev_monitor_get_fd(worker_monitor);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen memzero(&ep_monitor, sizeof(struct epoll_event));
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 /* needed for SIGCHLD/SIGTERM in spawn() */
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 */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* apply/restore inotify watch */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* send processed event back to libudev listeners */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen udev_monitor_send_device(worker_monitor, NULL, dev);
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen log_debug("seq %llu processed", udev_device_get_seqnum(dev));
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* send udevd the result of the event execution */
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen r = worker_send_message(manager->worker_watch[WRITE_END]);
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann log_error_errno(r, "failed to send result of seq %llu to main daemon: %m",
23fbe14f503c1e98292efc4ba1238adb7dc38d80Tom Gundersen /* wait for more device messages from main udevd, or term signal */
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))
} 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();
log_open();
goto exit;
if (arg_debug)
if (getuid() != 0) {
goto exit;
if (arg_children_max == 0) {
if (arg_daemonize) {
int fd;
if (fd < 0)
goto exit;
goto exit;
goto exit;
goto exit;
if (arg_daemonize) {
switch (pid) {
goto exit;
goto exit_daemonize;
setsid();
int fdcount;
int timeout;
if (fdcount < 0)
if (fdcount == 0) {
Iterator j;
for (i = 0; i < fdcount; i++) {
is_worker = true;
is_uevent = true;
is_signal = true;
is_inotify = true;
is_ctrl = true;
if (is_worker)
if (is_uevent)
if (is_signal) {
case SIGINT:
case SIGTERM:
case SIGHUP:
case SIGCHLD:
if (is_inotify)
if (is_ctrl)
exit:
if (manager)
log_close();