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