udevd.c revision 523c620bcd657f22cb52e6b78881cbb1f7d6832a
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) 2009 Canonical Ltd.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This program is free software: you can redistribute it and/or modify
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * it under the terms of the GNU General Public License as published by
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * the Free Software Foundation, either version 2 of the License, or
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * (at your option) any later version.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This program is distributed in the hope that it will be useful,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * but WITHOUT ANY WARRANTY; without even the implied warranty of
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * GNU General Public License for more details.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * You should have received a copy of the GNU General Public License
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * along with this program. If not, see <http://www.gnu.org/licenses/>.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic bool arg_debug = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int arg_daemonize = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned long long int delaying_seqnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned long long int seqnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline struct event *node_to_event(struct udev_list_node *node) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return container_of(node, struct event, node);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_queue_cleanup(struct udev *udev, enum event_state type);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* passed from worker to main process */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline struct worker *node_to_worker(struct udev_list_node *node) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return container_of(node, struct worker, node);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_queue_delete(struct event *event) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic struct worker *worker_ref(struct worker *worker) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void worker_cleanup(struct worker *worker) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void worker_unref(struct worker *worker) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("worker [%u] cleaned up", worker->pid);
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersenstatic void worker_list_cleanup(struct udev *udev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach_safe(loop, tmp, &worker_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* listen for new events */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* allow the main daemon netlink address to send devices to the worker */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_allow_unicast_sender(worker_monitor, monitor);
52d629010db73a9466c359201916494bd55186d1Tom Gundersen udev_monitor_enable_receiving(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* worker + event reference */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* take initial device from queue */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "error creating signalfd %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "error creating epoll fd: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memzero(&ep_signal, sizeof(struct epoll_event));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_monitor = udev_monitor_get_fd(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memzero(&ep_monitor, sizeof(struct epoll_event));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "fail to add fds to epoll: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* request TERM signal if parent exits */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* reset OOM score, we only protect the main daemon */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen write_string_file("/proc/self/oom_score_adj", "0");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("seq %llu running", udev_device_get_seqnum(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* needed for SIGCHLD/SIGTERM in spawn() */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Take a shared lock on the device node; this establishes
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * a concept of device "ownership" to serialize device
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * access. External processes holding an exclusive lock will
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * cause udev to skip the event handling; in the case udev
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * acquired the lock, the external process can block until
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * udev has finished its event handling.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!streq_ptr(udev_device_get_action(dev), "remove") &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen streq_ptr("block", udev_device_get_subsystem(dev)) &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !startswith(udev_device_get_sysname(dev), "dm-") &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !startswith(udev_device_get_sysname(dev), "md")) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (streq_ptr("partition", udev_device_get_devtype(d)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* needed for renaming netifs */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* apply rules, create node, symlinks */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_usec, arg_event_timeout_warn_usec,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_usec, arg_event_timeout_warn_usec,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* in case rtnl was initialized */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* apply/restore inotify watch */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* send processed event back to libudev listeners */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_send_device(worker_monitor, NULL, dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* send udevd the result of the event execution */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("seq %llu processed with %i", udev_device_get_seqnum(dev), err);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* wait for more device messages from main udevd, or term signal */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; i < fdcount; i++) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev = udev_monitor_receive_device(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "fork of child failed: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* close monitor, but keep address around */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event_start_usec = now(CLOCK_MONOTONIC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_append(&worker->node, &worker_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("seq %llu forked new worker [%u]", udev_device_get_seqnum(event->dev), pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "worker [%u] did not accept message %zi (%m), kill it", worker->pid, count);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event_start_usec = now(CLOCK_MONOTONIC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("maximum number (%i) of children reached", children);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* start new worker and pass initial device */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int event_queue_insert(struct udev_device *dev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->devpath = udev_device_get_devpath(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->devpath_old = udev_device_get_devpath_old(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->is_block = streq("block", udev_device_get_subsystem(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->ifindex = udev_device_get_ifindex(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev),
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_get_action(dev), udev_device_get_subsystem(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_append(&event->node, &event_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* lookup event for identical, parent, child device */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic bool is_devpath_busy(struct event *event) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check if queue contains events we depend on */
7141e4f62c3f220872df3114c42d9e4b9525e43eTom Gundersen struct event *loop_event = node_to_event(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* we already found a later event, earlier can not block us, no need to check again */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (loop_event->seqnum < event->delaying_seqnum)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* event we checked earlier still exists, no need to check again */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (loop_event->seqnum == event->delaying_seqnum)
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen /* found ourself, no later event can block us */
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check network device ifindex */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check our old name */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* compare devpath */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen common = MIN(loop_event->devpath_len, event->devpath_len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* one devpath is contained in the other? */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (memcmp(loop_event->devpath, event->devpath, common) != 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* identical device event found */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (loop_event->devpath_len == event->devpath_len) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* devices names might have changed/swapped in the meantime */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* parent device event found */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* child device event found */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* no matching device */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_queue_start(struct udev *udev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* do not start event if parent or child event is still running */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_queue_cleanup(struct udev *udev, enum event_state match_type) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach_safe(loop, tmp, &event_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (match_type != EVENT_UNDEF && match_type != event->state)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size = recv(fd_worker, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* lookup worker who sent the signal */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* worker returned */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* receive the udevd message from userspace */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev *udev = udev_ctrl_get_udev(uctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *str;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (STOP_EXEC_QUEUE) received");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (START_EXEC_QUEUE) received");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (ENV) received, unset '%s'", key);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_entry_add(&properties_list, key, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (ENV) received, set '%s=%s'", key, val);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_entry_add(&properties_list, key, val);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i", i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* keep reference to block the client until we exit */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int synthesize_change(struct udev_device *dev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen streq_ptr("disk", udev_device_get_devtype(dev)) &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !startswith(udev_device_get_sysname(dev), "dm-")) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev *udev = udev_device_get_udev(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Try to re-read the partition table. This only succeeds if
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * none of the devices is busy. The kernel returns 0 if no
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * partition table is found, and we will not get an event for
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* search for partitions */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = udev_enumerate_add_match_subsystem(e, "block");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _cleanup_udev_device_unref_ struct udev_device *d = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!streq_ptr("partition", udev_device_get_devtype(d)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * We have partitions and re-read the table, the kernel already sent
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * out a "change" event for the disk, and "remove/add" for all
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * partitions.
85091685af65831f379580c75b40776c20e245eeTom Gundersen * We have partitions but re-reading the partition table did not
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * work, synthesize "change" for the disk and all partitions.
5a917c064b23c1b8a12d6abd2f9f31c575ddebc6Tom Gundersen log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann _cleanup_udev_device_unref_ struct udev_device *d = NULL;
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!streq_ptr("partition", udev_device_get_devtype(d)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("device %s closed, synthesising partition '%s' 'change'",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_get_devnode(dev), udev_device_get_devnode(d));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0 || nbytes <= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error getting buffer for inotify");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("inotify event: %x for %s", ev->mask, udev_device_get_devnode(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void handle_signal(struct udev *udev, int signo) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach_safe(loop, tmp, &worker_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] exit with return code %i",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] terminated by signal %i (%s)",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] exit with status 0x%04x", pid, status);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] failed while handling '%s'",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* drop reference taken for state 'running' */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("ctrl=%i netlink=%i", ctrl, netlink);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * read the kernel command line, in case we need to get into debug mode
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * udev.log-priority=<level> syslog priority
afcac065c0f649ebcf0f450475a8d7c3bc776d14Tom Gundersen * udev.children-max=<number of workers> events are fully serialized if set to 1
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * udev.exec-delay=<number of seconds> delay execution of every executed program
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void kernel_cmdline_options(struct udev *udev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* accept the same options for the initrd, prefixed with "rd." */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((value = startswith(opt, "udev.log-priority="))) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if ((value = startswith(opt, "udev.children-max="))) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid udev.children-max ignored: %s", value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if ((value = startswith(opt, "udev.exec-delay="))) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid udev.exec-delay ignored: %s", value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if ((value = startswith(opt, "udev.event-timeout="))) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = safe_atou64(value, &arg_event_timeout_usec);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid udev.event-timeout ignored: %s", value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmannstatic void help(void) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen "Manages devices.\n\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --daemon\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --children-max=<maximum number of workers>\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --exec-delay=<seconds to wait before executing RUN=>\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --event-timeout=<seconds to wait before terminating an event>\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --resolve-names=early|late|never\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --version\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int parse_argv(int argc, char *argv[]) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "children-max", required_argument, NULL, 'c' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "exec-delay", required_argument, NULL, 'e' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "event-timeout", required_argument, NULL, 't' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "resolve-names", required_argument, NULL, 'N' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen while ((c = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL)) >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid --children-max ignored: %s", optarg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid --exec-delay ignored: %s", optarg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = safe_atou64(optarg, &arg_event_timeout_usec);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid --event-timeout ignored: %s", optarg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("resolve-names must be early, late or never");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_ctrl = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_inotify = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_signal = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_netlink = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_worker = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_ctrl_connection *ctrl_conn = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (getuid() != 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(r, "could not initialize labelling: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* set umask before creating any file/directory */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "could not change dir to /: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "could not create /run/udev: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* before opening new files, make sure std{in,out,err} fds are in a sane state */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* get control and netlink socket from systemd */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error taking over udev control socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error taking over netlink socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* get our own cgroup, we regularly kill everything udev has left behind */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* open control and netlink socket */
4189708ad0cde8e211e38d27de943579772f8869Tom Gundersen log_error("error initializing udev control socket");
53fae771bcf1623cd28791c48fa60d9d5e5086e4Zbigniew Jędrzejewski-Szmek monitor = udev_monitor_new_from_netlink(udev, "kernel");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error initializing netlink socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_monitor_enable_receiving(monitor) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error binding udev control socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rules = udev_rules_new(udev, arg_resolve_names);
107f2e2526d476c6cc9b81a690391c111027d641Tom Gundersen rc = udev_rules_apply_static_dev_perms(rules);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(rc, "failed to apply permissions on static device nodes - %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "fork of daemon failed: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen write_string_file("/proc/self/oom_score_adj", "-1000");
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering arg_children_max += CPU_COUNT(&cpu_set) * 2;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("set children_max to %u", arg_children_max);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* block and listen to all signals on signalfd */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* unnamed socket from workers to the main daemon */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "error creating epoll fd: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "fail to add fds to epoll: %m");
107f2e2526d476c6cc9b81a690391c111027d641Tom Gundersen bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* close sources of new events and discard buffered events */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* discard queued events and kill workers */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* exit after all has cleaned up */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_list_node_is_empty(&event_list) && children == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* timeout at exit for workers to finish */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (udev_list_node_is_empty(&event_list) && children == 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* we are idle */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* cleanup possible left-over processes in our cgroup */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* kill idle or hanging workers */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* tell settle that we are busy or idle */
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann fd = open("/run/udev/queue", O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann /* timeout */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("timeout, giving up waiting for workers to finish");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* kill idle workers */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check for hanging events */
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann struct worker *worker = node_to_worker(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((ts - worker->event_start_usec) > arg_event_timeout_warn_usec) {
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann if ((ts - worker->event_start_usec) > arg_event_timeout_usec) {
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann log_error("worker [%u] %s timeout; kill it", worker->pid, worker->event->devpath);
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann /* drop reference taken for state 'running' */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("seq %llu '%s' killed", udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("worker [%u] %s is taking a long time", worker->pid, worker->event->devpath);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen is_worker = is_signal = is_inotify = is_netlink = is_ctrl = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; i < fdcount; i++) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ev[i].data.fd == fd_worker && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev[i].data.fd == fd_netlink && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev[i].data.fd == fd_inotify && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev[i].data.fd == fd_ctrl && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check for changed config, every 3 seconds at most */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * USEC_PER_SEC) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* reload requested, HUP signal received, rules changed, builtin changed */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* event has finished */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* start new events */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rules = udev_rules_new(udev, arg_resolve_names);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* we are shutting down, the events below are not handled anymore */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* device node watch */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This needs to be after the inotify handling, to make sure,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * that the ping is send back after the possibly generated
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * "change" events by the inotify device node watch.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * A single time we may receive a client connection which we need to
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * keep open to block the client. It will be closed right before we