udevd.c revision a8389097c0cd91720424b25ee5c6825b4f69cb6a
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Copyright (C) 2009 Canonical Ltd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * This program is free software: you can redistribute it and/or modify
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * it under the terms of the GNU General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * This program is distributed in the hope that it will be useful,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * GNU General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * You should have received a copy of the GNU General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poetteringstatic struct udev_ctrl_connection *udev_ctrl_conn;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool arg_debug = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int arg_daemonize = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering unsigned long long int delaying_seqnum;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unsigned long long int seqnum;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return container_of(node, struct event, node);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state type);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering/* passed from worker to main process */
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstatic void event_free(struct event *event) {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic void worker_free(struct worker *worker) {
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering hashmap_remove(workers, UINT_TO_PTR(worker->pid));
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic void workers_free(void) {
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poetteringstatic int worker_new(struct worker **ret, struct udev *udev, struct udev_monitor *worker_monitor, pid_t pid) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_free_ struct worker *worker = NULL;
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering /* close monitor, but keep address around */
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering worker->monitor = udev_monitor_ref(worker_monitor);
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering r = hashmap_ensure_allocated(&workers, NULL);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering r = hashmap_put(workers, UINT_TO_PTR(pid), worker);
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poetteringstatic void worker_attach_event(struct worker *worker, struct event *event) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->start_usec = now(CLOCK_MONOTONIC);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void worker_spawn(struct event *event) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL;
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* listen for new events */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* allow the main daemon netlink address to send devices to the worker */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_allow_unicast_sender(worker_monitor, monitor);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_monitor_enable_receiving(worker_monitor);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
51323288fc628a5cac50914df915545d685b793eLennart Poettering struct epoll_event ep_signal, ep_monitor;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* take initial device from queue */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = log_error_errno(errno, "error creating signalfd %m");
51323288fc628a5cac50914df915545d685b793eLennart Poettering r = log_error_errno(errno, "error creating epoll fd: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering memzero(&ep_signal, sizeof(struct epoll_event));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd_monitor = udev_monitor_get_fd(worker_monitor);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memzero(&ep_monitor, sizeof(struct epoll_event));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
966c66e34940001a40806142ecebaae61b478444Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering r = log_error_errno(errno, "fail to add fds to epoll: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* request TERM signal if parent exits */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* reset OOM score, we only protect the main daemon */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering write_string_file("/proc/self/oom_score_adj", "0");
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering log_debug("seq %llu running", udev_device_get_seqnum(dev));
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering /* needed for SIGCHLD/SIGTERM in spawn() */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * Take a shared lock on the device node; this establishes
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * a concept of device "ownership" to serialize device
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * access. External processes holding an exclusive lock will
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * cause udev to skip the event handling; in the case udev
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering * acquired the lock, the external process can block until
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * udev has finished its event handling.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!streq_ptr(udev_device_get_action(dev), "remove") &&
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering streq_ptr("block", udev_device_get_subsystem(dev)) &&
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering !startswith(udev_device_get_sysname(dev), "dm-") &&
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering !startswith(udev_device_get_sysname(dev), "md")) {
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering if (streq_ptr("partition", udev_device_get_devtype(d)))
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* needed for renaming netifs */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* apply rules, create node, symlinks */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering arg_event_timeout_usec, arg_event_timeout_warn_usec,
51323288fc628a5cac50914df915545d685b793eLennart Poettering arg_event_timeout_usec, arg_event_timeout_warn_usec,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* in case rtnl was initialized */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* apply/restore inotify watch */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* send processed event back to libudev listeners */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering udev_monitor_send_device(worker_monitor, NULL, dev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("seq %llu processed", udev_device_get_seqnum(dev));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* send udevd the result of the event execution */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memzero(&msg, sizeof(struct worker_message));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error_errno(errno, "failed to send result of seq %llu to main daemon: %m",
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* wait for more device messages from main udevd, or term signal */
51323288fc628a5cac50914df915545d685b793eLennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = log_error_errno(errno, "failed to poll: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (i = 0; i < fdcount; i++) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering dev = udev_monitor_receive_device(worker_monitor);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (size != sizeof(struct signalfd_siginfo))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error_errno(errno, "fork of child failed: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = worker_new(&worker, udev, worker_monitor, pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("seq %llu forked new worker ["PID_FMT"]", udev_device_get_seqnum(event->dev), pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void event_run(struct event *event) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error_errno(errno, "worker ["PID_FMT"] did not accept message %zi (%m), kill it",
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (hashmap_size(workers) >= arg_children_max) {
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering log_debug("maximum number (%i) of children reached", hashmap_size(workers));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* start new worker and pass initial device */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int event_queue_insert(struct udev_device *dev) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->dev_kernel = udev_device_shallow_clone(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_copy_properties(event->dev_kernel, dev);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->seqnum = udev_device_get_seqnum(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devpath = udev_device_get_devpath(dev);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering event->devpath_len = strlen(event->devpath);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->devpath_old = udev_device_get_devpath_old(dev);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->devnum = udev_device_get_devnum(dev);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->is_block = streq("block", udev_device_get_subsystem(dev));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->ifindex = udev_device_get_ifindex(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev),
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering udev_device_get_action(dev), udev_device_get_subsystem(dev));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering udev_list_node_append(&event->node, &event_list);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void worker_kill(void) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering/* lookup event for identical, parent, child device */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic bool is_devpath_busy(struct event *event) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* check if queue contains events we depend on */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering udev_list_node_foreach(loop, &event_list) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering struct event *loop_event = node_to_event(loop);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* we already found a later event, earlier can not block us, no need to check again */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (loop_event->seqnum < event->delaying_seqnum)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* event we checked earlier still exists, no need to check again */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (loop_event->seqnum == event->delaying_seqnum)
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* found ourself, no later event can block us */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* check network device ifindex */
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* check our old name */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering event->delaying_seqnum = loop_event->seqnum;
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering /* compare devpath */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering common = MIN(loop_event->devpath_len, event->devpath_len);
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* one devpath is contained in the other? */
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (memcmp(loop_event->devpath, event->devpath, common) != 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* identical device event found */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (loop_event->devpath_len == event->devpath_len) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* devices names might have changed/swapped in the meantime */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* parent device event found */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
966c66e34940001a40806142ecebaae61b478444Lennart Poettering /* child device event found */
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering if (loop_event->devpath[common] == '/') {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->delaying_seqnum = loop_event->seqnum;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* no matching device */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void event_queue_start(struct udev *udev) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach(loop, &event_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct event *event = node_to_event(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* do not start event if parent or child event is still running */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state match_type) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach_safe(loop, tmp, &event_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct event *event = node_to_event(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (match_type != EVENT_UNDEF && match_type != event->state)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int on_worker(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering size = recvmsg(fd, &msghdr, MSG_DONTWAIT);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return log_error_errno(errno, "failed to receive message: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (size != sizeof(struct worker_message)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning_errno(EIO, "ignoring worker message with invalid size %zi bytes", size);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning_errno(EIO, "ignoring worker message without valid PID");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* lookup worker who sent the signal */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker = hashmap_get(workers, UINT_TO_PTR(ucred->pid));
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering log_debug("worker ["PID_FMT"] returned, but is no longer tracked", ucred->pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* worker returned */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int on_uevent(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_ensure_usec_initialized(dev, NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering/* receive the udevd message from userspace */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int on_ctrl_msg(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_udev_ctrl_connection_unref_ struct udev_ctrl_connection *ctrl_conn = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_udev_ctrl_msg_unref_ struct udev_ctrl_msg *ctrl_msg = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ctrl_conn = udev_ctrl_get_connection(uctrl);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ctrl_msg = udev_ctrl_receive_msg(ctrl_conn);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering i = udev_ctrl_get_set_log_level(ctrl_msg);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", i);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (STOP_EXEC_QUEUE) received");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (START_EXEC_QUEUE) received");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_reload(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (RELOAD) received");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (ENV) received, unset '%s'", key);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_entry_add(&properties_list, key, NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (ENV) received, set '%s=%s'", key, val);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_entry_add(&properties_list, key, val);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering i = udev_ctrl_get_set_children_max(ctrl_msg);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i", i);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (SYNC) received");
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering log_debug("udevd message (EXIT) received");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* keep reference to block the client until we exit */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_ctrl_conn = udev_ctrl_connection_ref(ctrl_conn);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int synthesize_change(struct udev_device *dev) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering streq_ptr("disk", udev_device_get_devtype(dev)) &&
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering !startswith(udev_device_get_sysname(dev), "dm-")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev *udev = udev_device_get_udev(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * Try to re-read the partition table. This only succeeds if
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering * none of the devices is busy. The kernel returns 0 if no
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * partition table is found, and we will not get an event for
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* search for partitions */
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering r = udev_enumerate_add_match_parent(e, dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = udev_enumerate_add_match_subsystem(e, "block");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!streq_ptr("partition", udev_device_get_devtype(d)))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * We have partitions and re-read the table, the kernel already sent
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * out a "change" event for the disk, and "remove/add" for all
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * We have partitions but re-reading the partition table did not
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * work, synthesize "change" for the disk and all partitions.
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!streq_ptr("partition", udev_device_get_devtype(d)))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("device %s closed, synthesising partition '%s' 'change'",
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_get_devnode(dev), udev_device_get_devnode(d));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return log_error_errno(errno, "Failed to read inotify fd: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("inotify event: %x for %s", e->mask, udev_device_get_devnode(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* settle might be waiting on us to determine the queue
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * state. If we just handled an inotify event, we might have
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * generated a "change" event, but we won't have queued up
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * the resultant uevent yet. Do that.
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int on_request_exit(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int on_request_reload(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker = hashmap_get(workers, UINT_TO_PTR(pid));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning("worker ["PID_FMT"] is unknown, ignoring", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("worker ["PID_FMT"] exited", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning("worker ["PID_FMT"] exited with return code %i", pid, WEXITSTATUS(status));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning("worker ["PID_FMT"] terminated by signal %i (%s)", pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_info("worker ["PID_FMT"] stopped", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_info("worker ["PID_FMT"] continued", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning("worker ["PID_FMT"] exit with status 0x%04x", pid, status);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker ["PID_FMT"] failed while handling '%s'", pid, worker->event->devpath);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* delete state from disk */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_delete_db(worker->event->dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_tag_index(worker->event->dev, NULL, false);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* forward kernel event without amending it */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_send_device(monitor, NULL, worker->event->dev_kernel);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void event_queue_update(void) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!udev_list_node_is_empty(&event_list)) {
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poettering log_warning_errno(r, "could not touch /run/udev/queue: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning("could not unlink /run/udev/queue: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("ctrl=%i netlink=%i", ctrl, netlink);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering * read the kernel command line, in case we need to get into debug mode
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering * udev.log-priority=<level> syslog priority
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * udev.children-max=<number of workers> events are fully serialized if set to 1
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * udev.exec-delay=<number of seconds> delay execution of every executed program
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * udev.event-timeout=<number of seconds> seconds to wait before terminating an event
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int parse_proc_cmdline_item(const char *key, const char *value) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning("invalid udev.children-max ignored: %s", value);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning("invalid udev.exec-delay ignored: %s", value);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (streq(key, "event-timeout")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = safe_atou64(value, &arg_event_timeout_usec);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning("invalid udev.event-timeout ignored: %s", value);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void help(void) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering "Manages devices.\n\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " -h --help Print this message\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --version Print version of the program\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --daemon Detach and run in the background\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --debug Enable debug output\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --children-max=INT Set maximum number of workers\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --exec-delay=SECONDS Seconds to wait before executing RUN=\n"
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering " --event-timeout=SECONDS Seconds to wait before terminating an event\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --resolve-names=early|late|never\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " When to resolve users and groups\n"
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering { "children-max", required_argument, NULL, 'c' },
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering { "exec-delay", required_argument, NULL, 'e' },
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering { "event-timeout", required_argument, NULL, 't' },
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering { "resolve-names", required_argument, NULL, 'N' },
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering while ((c = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL)) >= 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering r = safe_atou(optarg, &arg_children_max);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_warning("Invalid --children-max ignored: %s", optarg);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_warning("Invalid --exec-delay ignored: %s", optarg);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering r = safe_atou64(optarg, &arg_event_timeout_usec);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_warning("Invalid --event-timeout ignored: %s", optarg);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("resolve-names must be early, late or never");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering struct epoll_event ep_ctrl = { .events = EPOLLIN };
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering struct epoll_event ep_inotify = { .events = EPOLLIN };
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering struct epoll_event ep_signal = { .events = EPOLLIN };
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering struct epoll_event ep_netlink = { .events = EPOLLIN };
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering struct epoll_event ep_worker = { .events = EPOLLIN };
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering r = log_error_errno(errno, "could not allocate udev context: %m");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering r = parse_proc_cmdline(parse_proc_cmdline_item);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_warning_errno(r, "failed to parse kernel command line, ignoring: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = log_error_errno(EPERM, "root privileges required");
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering log_error_errno(r, "could not initialize labelling: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* set umask before creating any file/directory */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = log_error_errno(errno, "could not change dir to /: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_init(udev, &properties_list, true);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = log_error_errno(errno, "could not create /run/udev: %m");
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering /* before opening new files, make sure std{in,out,err} fds are in a sane state */
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* get control and netlink socket from systemd */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = log_error_errno(EINVAL, "error taking over udev control socket");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = log_error_errno(EINVAL, "error taking over netlink socket");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* get our own cgroup, we regularly kill everything udev has left behind */
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt /* open control and netlink socket */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = log_error_errno(EINVAL, "error initializing udev control socket");
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt monitor = udev_monitor_new_from_netlink(udev, "kernel");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = log_error_errno(EINVAL, "error initializing netlink socket");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd_netlink = udev_monitor_get_fd(monitor);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering if (udev_monitor_enable_receiving(monitor) < 0) {
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering r = log_error_errno(EINVAL, "error binding netlink socket");
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering r = log_error_errno(EINVAL, "error binding udev control socket");
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) {
worker_kill();
if (udev_cgroup)
if (fdcount < 0)
if (fdcount == 0) {
Iterator j;
if (udev_exit) {
worker_kill();
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) {
worker_kill();
reload = false;
if (is_worker)
if (is_netlink)
if (is_signal) {
case SIGINT:
case SIGTERM:
case SIGHUP:
case SIGCHLD:
if (udev_exit)
if (is_inotify)
if (is_ctrl)
exit:
if (fd_ep >= 0)
workers_free();
if (fd_signal >= 0)
log_close();