udevd.c revision e859aa9e993453be321450148d45d08fcc55c3f5
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * Copyright (C) 2009 Canonical Ltd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * This program is free software: you can redistribute it and/or modify
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * it under the terms of the GNU General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * the Free Software Foundation, either version 2 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * This program is distributed in the hope that it will be useful,
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * but WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * GNU General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * You should have received a copy of the GNU General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen * along with this program. If not, see <http://www.gnu.org/licenses/>.
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic bool arg_debug = false;
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic int arg_daemonize = false;
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
99634696183dfabae20104e58157c69029a11594Tom Gundersenstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
99634696183dfabae20104e58157c69029a11594Tom Gundersentypedef struct Manager {
99634696183dfabae20104e58157c69029a11594Tom Gundersen pid_t pid; /* the process that originally allocated the manager object */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen struct udev_ctrl_connection *ctrl_conn_blocking;
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering unsigned long long int delaying_seqnum;
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering unsigned long long int seqnum;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersenstatic inline struct event *node_to_event(struct udev_list_node *node) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return container_of(node, struct event, node);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersenstatic void event_queue_cleanup(Manager *manager, enum event_state type);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt/* passed from worker to main process */
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering sd_event_source_unref(event->timeout_warning);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (udev_list_node_is_empty(&event->manager->events)) {
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering /* only clean up the queue from the process that created it */
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poetteringstatic void worker_free(struct worker *worker) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen hashmap_remove(worker->manager->workers, UINT_TO_PTR(worker->pid));
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersenstatic void manager_workers_free(Manager *manager) {
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering HASHMAP_FOREACH(worker, manager->workers, i)
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen manager->workers = hashmap_free(manager->workers);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidtstatic int worker_new(struct worker **ret, Manager *manager, struct udev_monitor *worker_monitor, pid_t pid) {
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering _cleanup_free_ struct worker *worker = NULL;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen /* close monitor, but keep address around */
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen worker->monitor = udev_monitor_ref(worker_monitor);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen r = hashmap_ensure_allocated(&manager->workers, NULL);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen r = hashmap_put(manager->workers, UINT_TO_PTR(pid), worker);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenstatic int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen kill_and_sigcont(event->worker->pid, SIGKILL);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen log_error("seq %llu '%s' killed", udev_device_get_seqnum(event->dev), event->devpath);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersenstatic int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen log_warning("seq %llu '%s' is taking a long time", udev_device_get_seqnum(event->dev), event->devpath);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersenstatic void worker_attach_event(struct worker *worker, struct event *event) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen assert_se(sd_event_now(e, clock_boottime_or_monotonic(), &usec) >= 0);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen (void) sd_event_add_time(e, &event->timeout_warning, clock_boottime_or_monotonic(),
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen usec + arg_event_timeout_warn_usec, USEC_PER_SEC, on_event_timeout_warning, event);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen (void) sd_event_add_time(e, &event->timeout, clock_boottime_or_monotonic(),
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen usec + arg_event_timeout_usec, USEC_PER_SEC, on_event_timeout, event);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen sd_event_source_unref(manager->inotify_event);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom GundersenDEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen return loop_write(fd, &message, sizeof(message), false);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenstatic void worker_spawn(Manager *manager, struct event *event) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* listen for new events */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* allow the main daemon netlink address to send devices to the worker */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen udev_monitor_allow_unicast_sender(worker_monitor, manager->monitor);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = udev_monitor_enable_receiving(worker_monitor);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen log_error_errno(r, "worker: could not enable receiving of device: %m");
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_netlink_unref_ sd_netlink *rtnl = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_close_ int fd_signal = -1, fd_ep = -1;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen struct epoll_event ep_signal = { .events = EPOLLIN };
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen struct epoll_event ep_monitor = { .events = EPOLLIN };
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* take initial device from queue */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->monitor = udev_monitor_unref(manager->monitor);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->ctrl = udev_ctrl_unref(manager->ctrl);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->ctrl_conn_blocking = udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->ctrl_event = sd_event_source_unref(manager->ctrl_event);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->uevent_event = sd_event_source_unref(manager->uevent_event);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->inotify_event = sd_event_source_unref(manager->inotify_event);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen manager->event = sd_event_unref(manager->event);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = log_error_errno(errno, "error creating signalfd %m");
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen fd_monitor = udev_monitor_get_fd(worker_monitor);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = log_error_errno(errno, "error creating epoll fd: %m");
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen r = log_error_errno(errno, "fail to add fds to epoll: %m");
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* request TERM signal if parent exits */
d6bd972d061af306ede2affd2c9340a1660f7996Tom Gundersen /* reset OOM score, we only protect the main daemon */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen write_string_file("/proc/self/oom_score_adj", "0", 0);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen log_debug("seq %llu running", udev_device_get_seqnum(dev));
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen * Take a shared lock on the device node; this establishes
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen * a concept of device "ownership" to serialize device
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen * access. External processes holding an exclusive lock will
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen * cause udev to skip the event handling; in the case udev
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen * acquired the lock, the external process can block until
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen * udev has finished its event handling.
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (!streq_ptr(udev_device_get_action(dev), "remove") &&
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen streq_ptr("block", udev_device_get_subsystem(dev)) &&
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen !startswith(udev_device_get_sysname(dev), "dm-") &&
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen !startswith(udev_device_get_sysname(dev), "md")) {
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams if (streq_ptr("partition", udev_device_get_devtype(d)))
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen /* needed for renaming netifs */
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen /* apply rules, create node, symlinks */
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen arg_event_timeout_usec, arg_event_timeout_warn_usec,
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen arg_event_timeout_usec, arg_event_timeout_warn_usec);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen /* in case rtnl was initialized */
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen /* apply/restore inotify watch */
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen /* send processed event back to libudev listeners */
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen udev_monitor_send_device(worker_monitor, NULL, dev);
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen log_debug("seq %llu processed", udev_device_get_seqnum(dev));
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen /* send udevd the result of the event execution */
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen r = worker_send_message(manager->worker_watch[WRITE_END]);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen log_error_errno(r, "failed to send result of seq %llu to main daemon: %m",
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen /* wait for more device messages from main udevd, or term signal */
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = log_error_errno(errno, "failed to poll: %m");
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen for (i = 0; i < fdcount; i++) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen dev = udev_monitor_receive_device(worker_monitor);
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
59b8f6b628145586e87b8a4f6e29c755ad7d61edTom Gundersen size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering log_error_errno(errno, "fork of child failed: %m");
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering r = worker_new(&worker, manager, worker_monitor, pid);
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering log_debug("seq %llu forked new worker ["PID_FMT"]", udev_device_get_seqnum(event->dev), pid);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersenstatic void event_run(Manager *manager, struct event *event) {
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen HASHMAP_FOREACH(worker, manager->workers, i) {
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen count = udev_monitor_send_device(manager->monitor, worker->monitor, event->dev);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen log_error_errno(errno, "worker ["PID_FMT"] did not accept message %zi (%m), kill it",
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen if (hashmap_size(manager->workers) >= arg_children_max) {
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen log_debug("maximum number (%i) of children reached", hashmap_size(manager->workers));
76253e73f9c9c24fec755e485516f3b55d0707b4Dan Williams /* start new worker and pass initial device */
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersenstatic int event_queue_insert(Manager *manager, struct udev_device *dev) {
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen /* only one process can add events to the queue */
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen event->dev_kernel = udev_device_shallow_clone(dev);
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen udev_device_copy_properties(event->dev_kernel, dev);
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen event->devpath = udev_device_get_devpath(dev);
c7d9ffe6d629cb5b34dd749e4a88b190b11a0f48Tom Gundersen event->devpath_old = udev_device_get_devpath_old(dev);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen event->is_block = streq("block", udev_device_get_subsystem(dev));
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen event->ifindex = udev_device_get_ifindex(dev);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev),
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen udev_device_get_action(dev), udev_device_get_subsystem(dev));
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (udev_list_node_is_empty(&manager->events)) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen log_warning_errno(r, "could not touch /run/udev/queue: %m");
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen udev_list_node_append(&event->node, &manager->events);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersenstatic void manager_kill_workers(Manager *manager) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen HASHMAP_FOREACH(worker, manager->workers, i) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen/* lookup event for identical, parent, child device */
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersenstatic bool is_devpath_busy(Manager *manager, struct event *event) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* check if queue contains events we depend on */
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen udev_list_node_foreach(loop, &manager->events) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen struct event *loop_event = node_to_event(loop);
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* we already found a later event, earlier can not block us, no need to check again */
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (loop_event->seqnum < event->delaying_seqnum)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* event we checked earlier still exists, no need to check again */
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (loop_event->seqnum == event->delaying_seqnum)
e2acdb6b0f68d9b4152708a9f21bf9e11f8b9e7eTorstein Husebø /* found ourself, no later event can block us */
9a0f246fcdb1238e6b3397169a10095f4df89210Lennart Poettering if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* check network device ifindex */
9a0f246fcdb1238e6b3397169a10095f4df89210Lennart Poettering if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* check our old name */
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* compare devpath */
9a0f246fcdb1238e6b3397169a10095f4df89210Lennart Poettering common = MIN(loop_event->devpath_len, event->devpath_len);
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering /* one devpath is contained in the other? */
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering if (memcmp(loop_event->devpath, event->devpath, common) != 0)
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* identical device event found */
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen if (loop_event->devpath_len == event->devpath_len) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* devices names might have changed/swapped in the meantime */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
99634696183dfabae20104e58157c69029a11594Tom Gundersen /* parent device event found */
83cedf7ae28925e37931e7e92d22be9c936a1defTom Gundersen /* child device event found */
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen /* no matching device */
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen return false;
be077570f779664ed87b50f60608df9fbe258821Tom Gundersenstatic int on_exit_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
816e2e7af96886e4a43194042ef61ba9fec2c77dTom Gundersen log_error_errno(ETIMEDOUT, "giving up waiting for workers to finish");
9a0f246fcdb1238e6b3397169a10095f4df89210Lennart Poettering "STOPPING=1\n"
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen "STATUS=Starting shutdown...");
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen /* close sources of new events and discard buffered events */
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen manager->ctrl_event = sd_event_source_unref(manager->ctrl_event);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen manager->ctrl = udev_ctrl_unref(manager->ctrl);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen manager->inotify_event = sd_event_source_unref(manager->inotify_event);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen manager->fd_inotify = safe_close(manager->fd_inotify);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen manager->uevent_event = sd_event_source_unref(manager->uevent_event);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen manager->monitor = udev_monitor_unref(manager->monitor);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen /* discard queued events and kill workers */
99634696183dfabae20104e58157c69029a11594Tom Gundersen assert_se(sd_event_now(manager->event, clock_boottime_or_monotonic(), &usec) >= 0);
83cedf7ae28925e37931e7e92d22be9c936a1defTom Gundersen r = sd_event_add_time(manager->event, NULL, clock_boottime_or_monotonic(),
83cedf7ae28925e37931e7e92d22be9c936a1defTom Gundersen usec + 30 * USEC_PER_SEC, USEC_PER_SEC, on_exit_timeout, manager);
933f9caeeb2b3c1b951d330e04beb04226e5a890Daniel Mack/* reload requested, HUP signal received, rules changed, builtin changed */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen "RELOADING=1\n"
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen "STATUS=Flushing configuration...");
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen manager->rules = udev_rules_unref(manager->rules);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen "STATUS=Processing...");
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersenstatic void event_queue_start(Manager *manager) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (udev_list_node_is_empty(&manager->events) ||
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen assert_se(sd_event_now(manager->event, clock_boottime_or_monotonic(), &usec) >= 0);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen /* check for changed config, every 3 seconds at most */
f693e9b38f13575b988335e3324a50dcc8772d48Tom Gundersen (usec - manager->last_usec) > 3 * USEC_PER_SEC) {
5b34277c2015e32e51d10cfa076df2c7106b4537Tom Gundersen if (udev_rules_check_timestamp(manager->rules) ||
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen manager->rules = udev_rules_new(manager->udev, arg_resolve_names);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen udev_list_node_foreach(loop, &manager->events) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen /* do not start event if parent or child event is still running */
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersenstatic void event_queue_cleanup(Manager *manager, enum event_state match_type) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen udev_list_node_foreach_safe(loop, tmp, &manager->events) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (match_type != EVENT_UNDEF && match_type != event->state)
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersenstatic int on_worker(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen /* nothing more to read */
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen return log_error_errno(errno, "failed to receive message: %m");
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen } else if (size != sizeof(struct worker_message)) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen log_warning_errno(EIO, "ignoring worker message with invalid size %zi bytes", size);
bff92d2c2f913c368d80066ee89855665818edc8Susant Sahani cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen log_warning_errno(EIO, "ignoring worker message without valid PID");
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen /* lookup worker who sent the signal */
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen worker = hashmap_get(manager->workers, UINT_TO_PTR(ucred->pid));
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen log_debug("worker ["PID_FMT"] returned, but is no longer tracked", ucred->pid);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen /* worker returned */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen /* we have free workers, try to schedule events */
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersenstatic int on_uevent(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen dev = udev_monitor_receive_device(manager->monitor);
bd57b45029ff25067704c9538e79f31e71c10045Tom Gundersen udev_device_ensure_usec_initialized(dev, NULL);
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen /* we have fresh events, try to schedule them */
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen/* receive the udevd message from userspace */
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersenstatic int on_ctrl_msg(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen _cleanup_udev_ctrl_connection_unref_ struct udev_ctrl_connection *ctrl_conn = NULL;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen _cleanup_udev_ctrl_msg_unref_ struct udev_ctrl_msg *ctrl_msg = NULL;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen const char *str;
500792d8180c9a11d65f107cdc79dea21b2964c4Tom Gundersen ctrl_conn = udev_ctrl_get_connection(manager->ctrl);
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen if (i >= 0) {
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", i);
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen log_debug("udevd message (STOP_EXEC_QUEUE) received");
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen log_debug("udevd message (START_EXEC_QUEUE) received");
966d74c043098e12d4d5b101aa7650244c14b815Lennart Poettering if (udev_ctrl_get_reload(ctrl_msg) > 0) {
0d43d2fcb7ac5264c739dc2f67f93ed0985a418aTom Gundersen log_debug("udevd message (ENV) received, unset '%s'", key);
0d43d2fcb7ac5264c739dc2f67f93ed0985a418aTom Gundersen udev_list_entry_add(&manager->properties, key, NULL);
0d43d2fcb7ac5264c739dc2f67f93ed0985a418aTom Gundersen log_debug("udevd message (ENV) received, set '%s=%s'", key, val);
0d43d2fcb7ac5264c739dc2f67f93ed0985a418aTom Gundersen udev_list_entry_add(&manager->properties, key, val);
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen if (i >= 0) {
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i", i);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen /* keep reference to block the client until we exit
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen TODO: deal with several blocking exit requests */
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen manager->ctrl_conn_blocking = udev_ctrl_connection_ref(ctrl_conn);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersenstatic int synthesize_change(struct udev_device *dev) {
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen streq_ptr("disk", udev_device_get_devtype(dev)) &&
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen !startswith(udev_device_get_sysname(dev), "dm-")) {
8de4a226c71ef43e652274b33b5d19211a44ac7bTom Gundersen struct udev *udev = udev_device_get_udev(dev);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen * Try to re-read the partition table. This only succeeds if
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen * none of the devices is busy. The kernel returns 0 if no
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen * partition table is found, and we will not get an event for
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen /* search for partitions */
621ac3d2cc8f37169166df9c7f379b0cb6b17e36Thomas Hindoe Paaboel Andersen r = udev_enumerate_add_match_subsystem(e, "block");
99634696183dfabae20104e58157c69029a11594Tom Gundersen udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen _cleanup_udev_device_unref_ struct udev_device *d = NULL;
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
527503444ef24ae03c73cf85128c7acbb1146f3cTom Gundersen if (!streq_ptr("partition", udev_device_get_devtype(d)))
64d6c22905e94b02e760747c6c143dc9893083d9Thomas Hindoe Paaboel Andersen * We have partitions and re-read the table, the kernel already sent
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering * out a "change" event for the disk, and "remove/add" for all
64d6c22905e94b02e760747c6c143dc9893083d9Thomas Hindoe Paaboel Andersen if (part_table_read && has_partitions)
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering * We have partitions but re-reading the partition table did not
64d6c22905e94b02e760747c6c143dc9893083d9Thomas Hindoe Paaboel Andersen * work, synthesize "change" for the disk and all partitions.
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
8eb9058dc1f99a5eb9b8726a978fcc0720837a10Lennart Poettering write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering if (!streq_ptr("partition", udev_device_get_devtype(d)))
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering log_debug("device %s closed, synthesising partition '%s' 'change'",
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering udev_device_get_devnode(dev), udev_device_get_devnode(d));
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poetteringstatic int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering return log_error_errno(errno, "Failed to read inotify fd: %m");
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering dev = udev_watch_lookup(manager->udev, e->wd);
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering log_debug("inotify event: %x for %s", e->mask, udev_device_get_devnode(dev));
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering /* settle might be waiting on us to determine the queue
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering * state. If we just handled an inotify event, we might have
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering * generated a "change" event, but we won't have queued up
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poettering * the resultant uevent yet. Do that.
1a04db0fc9d08fffe80d6d7b5b60459295922b11Lennart Poetteringstatic int on_sigterm(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
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;
if (prio < 0)
goto invalid;
goto invalid;
goto invalid;
goto invalid;
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;
goto exit;
if (arg_daemonize) {
(void) make_null_stdio();
switch (pid) {
goto exit;
log_close();
setsid();
exit:
log_close();