udevd.c revision a505965d955a5b90a5199b1be953e9707b19e3b5
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * Copyright (C) 2009 Canonical Ltd.
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * This program is free software: you can redistribute it and/or modify
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * it under the terms of the GNU General Public License as published by
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * the Free Software Foundation, either version 2 of the License, or
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * (at your option) any later version.
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * This program is distributed in the hope that it will be useful,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * GNU General Public License for more details.
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * You should have received a copy of the GNU General Public License
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmekstatic bool arg_debug = false;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic int arg_daemonize = false;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned long long int delaying_seqnum;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned long long int seqnum;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return container_of(node, struct event, node);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state type);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering/* passed from worker to main process */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic inline struct worker *node_to_worker(struct udev_list_node *node) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return container_of(node, struct worker, node);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic void event_free(struct event *event) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic void worker_free(struct worker *worker) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering hashmap_remove(workers, UINT_TO_PTR(worker->pid));
507f22bd0172bff5e5d98145b1419bd472a2c57fZbigniew Jędrzejewski-Szmek udev_monitor_unref(worker->monitor);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic void workers_free(void) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic int worker_new(struct worker **ret, struct udev *udev, struct udev_monitor *worker_monitor, pid_t pid) {
e9f600f2fb4b0df55c7a8fb4b4d09f9979997223Lennart Poettering _cleanup_free_ struct worker *worker = NULL;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* close monitor, but keep address around */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering worker->monitor = udev_monitor_ref(worker_monitor);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = hashmap_ensure_allocated(&workers, NULL);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = hashmap_put(workers, UINT_TO_PTR(pid), worker);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic void worker_attach_event(struct worker *worker, struct event *event) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering event->start_usec = now(CLOCK_MONOTONIC);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringstatic void worker_spawn(struct event *event) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* listen for new events */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* allow the main daemon netlink address to send devices to the worker */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering udev_monitor_allow_unicast_sender(worker_monitor, monitor);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering udev_monitor_enable_receiving(worker_monitor);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering struct epoll_event ep_signal, ep_monitor;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* take initial device from queue */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = log_error_errno(errno, "error creating signalfd %m");
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = log_error_errno(errno, "error creating epoll fd: %m");
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering memzero(&ep_signal, sizeof(struct epoll_event));
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering fd_monitor = udev_monitor_get_fd(worker_monitor);
36dd072cdf03dcac0fcd2d6b42f261444dc7ac88Michal Sekletar memzero(&ep_monitor, sizeof(struct epoll_event));
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
e88baee88fad8bc59d33b55a7a2d640ef9e16cd6Zbigniew Jędrzejewski-Szmek r = log_error_errno(errno, "fail to add fds to epoll: %m");
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* request TERM signal if parent exits */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* reset OOM score, we only protect the main daemon */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering write_string_file("/proc/self/oom_score_adj", "0");
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering log_debug("seq %llu running", udev_device_get_seqnum(dev));
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* needed for SIGCHLD/SIGTERM in spawn() */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * Take a shared lock on the device node; this establishes
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * a concept of device "ownership" to serialize device
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * access. External processes holding an exclusive lock will
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * cause udev to skip the event handling; in the case udev
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * acquired the lock, the external process can block until
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering * udev has finished its event handling.
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!streq_ptr(udev_device_get_action(dev), "remove") &&
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering streq_ptr("block", udev_device_get_subsystem(dev)) &&
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering !startswith(udev_device_get_sysname(dev), "dm-") &&
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering !startswith(udev_device_get_sysname(dev), "md")) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (streq_ptr("partition", udev_device_get_devtype(d)))
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* needed for renaming netifs */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* apply rules, create node, symlinks */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering arg_event_timeout_usec, arg_event_timeout_warn_usec,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering arg_event_timeout_usec, arg_event_timeout_warn_usec,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* in case rtnl was initialized */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* apply/restore inotify watch */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* send processed event back to libudev listeners */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering udev_monitor_send_device(worker_monitor, NULL, dev);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering log_debug("seq %llu processed", udev_device_get_seqnum(dev));
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* send udevd the result of the event execution */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering memzero(&msg, sizeof(struct worker_message));
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering log_error_errno(errno, "failed to send result of seq %llu to main daemon: %m",
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering /* wait for more device messages from main udevd, or term signal */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = log_error_errno(errno, "failed to poll: %m");
445ea9be520b9549aee45d0b6427cf48b446987fLennart Poettering for (i = 0; i < fdcount; i++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering dev = udev_monitor_receive_device(worker_monitor);
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
c0f71f469fef3f3a0822e0021085e6d165df2b46Lennart Poettering if (size != sizeof(struct signalfd_siginfo))
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering log_error_errno(errno, "fork of child failed: %m");
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = worker_new(&worker, udev, worker_monitor, pid);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering log_debug("seq %llu forked new worker ["PID_FMT"]", udev_device_get_seqnum(event->dev), pid);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering log_error_errno(errno, "worker ["PID_FMT"] did not accept message %zi (%m), kill it",
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) {
const char *str;
goto out;
goto out;
stop_exec_queue = true;
stop_exec_queue = false;
reload = true;
char *key;
char *val;
arg_children_max = i;
udev_exit = true;
out:
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)
switch (signo) {
case SIGINT:
case SIGTERM:
udev_exit = true;
case SIGCHLD:
int status;
if (pid <= 0)
if (!worker) {
case SIGHUP:
reload = true;
static void event_queue_update(void) {
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 (!udev) {
goto exit;
log_open();
goto exit;
if (arg_debug)
if (getuid() != 0) {
goto exit;
goto exit;
goto exit;
goto exit;
if (arg_daemonize) {
int fd;
if (fd >= 0) {
if (!udev_ctrl) {
goto exit;
if (!monitor) {
goto exit;
if (!udev_ctrl) {
goto exit;
if (!monitor) {
goto exit;
goto exit;
goto exit;
if (!rules) {
goto exit;
if (arg_daemonize) {
switch (pid) {
goto exit;
goto exit_daemonize;
setsid();
if (arg_children_max <= 0) {
if (fd_inotify < 0) {
goto exit;
if (fd_signal < 0) {
goto exit;
goto exit;
if (fd_ep < 0) {
goto exit;
goto exit;
int fdcount;
int timeout;
if (udev_exit) {
if (fd_ctrl >= 0) {
if (fd_inotify >= 0) {
if (udev_cgroup)
if (fdcount < 0)
if (fdcount == 0) {
Iterator j;
if (udev_exit) {
for (i = 0; i < fdcount; i++) {
is_worker = true;
is_netlink = true;
is_signal = true;
is_inotify = true;
is_ctrl = true;
reload = true;
reload = true;
if (reload) {
reload = false;
if (is_worker)
if (is_netlink) {
if (dev) {
if (is_signal) {
if (udev_exit)
if (is_inotify) {
if (is_ctrl)
exit:
if (fd_ep >= 0)
workers_free();
if (fd_signal >= 0)
log_close();