udevd.c revision 689a97f52383110bf0da049e8f6294993f4020dd
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/>.
b2b796b8ab5565fbe60b544d2579e2bfca31bf6aLennart Poetteringvoid udev_main_log(struct udev *udev, int priority,
b2b796b8ab5565fbe60b544d2579e2bfca31bf6aLennart Poettering const char *file, int line, const char *fn,
91adc4db33f69606aabd332813a5d7d5751c859fLennart Poettering log_metav(priority, file, line, fn, format, args);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic struct udev_queue_export *udev_queue_export;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering unsigned long long int delaying_seqnum;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering unsigned long long int seqnum;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node)
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return container_of(node, struct event, node);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state type);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering/* passed from worker to main process */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline struct worker *node_to_worker(struct udev_list_node *node)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return container_of(node, struct worker, node);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic void event_queue_delete(struct event *event, bool export)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_queue_export_device_finished(udev_queue_export, event->dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic struct worker *worker_ref(struct worker *worker)
51323288fc628a5cac50914df915545d685b793eLennart Poetteringstatic void worker_cleanup(struct worker *worker)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringstatic void worker_unref(struct worker *worker)
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering log_debug("worker [%u] cleaned up\n", worker->pid);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void worker_list_cleanup(struct udev *udev)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_foreach_safe(loop, tmp, &worker_list) {
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering struct worker *worker = node_to_worker(loop);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void worker_new(struct event *event)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* listen for new events */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering /* allow the main daemon netlink address to send devices to the worker */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_allow_unicast_sender(worker_monitor, monitor);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_enable_receiving(worker_monitor);
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering worker = calloc(1, sizeof(struct worker));
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* worker + event reference */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct epoll_event ep_signal, ep_monitor;
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* take initial device from queue */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_queue_export_unref(udev_queue_export);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
51323288fc628a5cac50914df915545d685b793eLennart Poettering log_error("error creating signalfd %m\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error creating epoll fd: %m\n");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memset(&ep_signal, 0, sizeof(struct epoll_event));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd_monitor = udev_monitor_get_fd(worker_monitor);
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering memset(&ep_monitor, 0, sizeof(struct epoll_event));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering log_error("fail to add fds to epoll: %m\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* request TERM signal if parent exits */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* reset OOM score, we only protect the main daemon */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering write_string_file("/proc/self/oom_score_adj", "0");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("seq %llu running\n", udev_device_get_seqnum(dev));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* needed for SIGCHLD/SIGTERM in spawn() */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* apply rules, create node, symlinks */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering err = udev_event_execute_rules(udev_event, rules, &sigmask_orig);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_event_execute_run(udev_event, &sigmask_orig);
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering /* apply/restore inotify watch */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (err == 0 && udev_event->inotify_watch) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* send processed event back to libudev listeners */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_monitor_send_device(worker_monitor, NULL, dev);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* send udevd the result of the event execution */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering memset(&msg, 0, sizeof(struct worker_message));
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen log_debug("seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* wait for more device messages from main udevd, or term signal */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering for (i = 0; i < fdcount; i++) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering dev = udev_monitor_receive_device(worker_monitor);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (size != sizeof(struct signalfd_siginfo))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* close monitor, but keep address around */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->event_start_usec = now(CLOCK_MONOTONIC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_append(&worker->node, &worker_list);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void event_run(struct event *event)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_foreach(loop, &worker_list) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering struct worker *worker = node_to_worker(loop);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->event_start_usec = now(CLOCK_MONOTONIC);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("maximum number (%i) of children reached\n", children);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* start new worker and pass initial device */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int event_queue_insert(struct udev_device *dev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->seqnum = udev_device_get_seqnum(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devpath = udev_device_get_devpath(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devpath_len = strlen(event->devpath);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devpath_old = udev_device_get_devpath_old(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devnum = udev_device_get_devnum(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->is_block = streq("block", udev_device_get_subsystem(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->ifindex = udev_device_get_ifindex(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (streq(udev_device_get_subsystem(dev), "firmware"))
4e2d538f33df8a425487aaa4facc23065a9bdaf7Lennart Poettering udev_queue_export_device_queued(udev_queue_export, dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_get_action(dev), udev_device_get_subsystem(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_append(&event->node, &event_list);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void worker_kill(struct udev *udev)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_list_node_foreach(loop, &worker_list) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering struct worker *worker = node_to_worker(loop);
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 */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach(loop, &event_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct event *loop_event = node_to_event(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* we already found a later event, earlier can not block us, no need to check again */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* found ourself, no later event can block us */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* check network device ifindex */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* check our old name */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* compare devpath */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering common = MIN(loop_event->devpath_len, event->devpath_len);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* one devpath is contained in the other? */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart 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) {
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering /* devices names might have changed/swapped in the meantime */
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* allow to bypass the dependency tracking */
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering /* parent device event found */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* child device event found */
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (loop_event->devpath[common] == '/') {
51323288fc628a5cac50914df915545d685b793eLennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* no matching device */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void event_queue_start(struct udev *udev)
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering udev_list_node_foreach(loop, &event_list) {
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering struct event *event = node_to_event(loop);
c463eb783e5ad999d400180c69b912c54fa07ee1Lennart Poettering /* do not start event if parent or child event is still running */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state match_type)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering udev_list_node_foreach_safe(loop, tmp, &event_list) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering struct event *event = node_to_event(loop);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (match_type != EVENT_UNDEF && match_type != event->state)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void worker_returned(int fd_worker)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering size = recv(fd_worker, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (size != sizeof(struct worker_message))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* lookup worker who sent the signal */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach(loop, &worker_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct worker *worker = node_to_worker(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* worker returned */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering/* receive the udevd message from userspace */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering struct udev *udev = udev_ctrl_get_udev(uctrl);
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_PRIORITY) received, log_priority=%i\n", i);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (STOP_EXEC_QUEUE) received\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (START_EXEC_QUEUE) received\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_reload(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (RELOAD) received\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (ENV) received, unset '%s'\n", key);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (ENV) received, set '%s=%s'\n", key, val);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("wrong key format '%s'\n", key);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering i = udev_ctrl_get_set_children_max(ctrl_msg);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i\n", i);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (SYNC) received\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (EXIT) received\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* keep reference to block the client until we exit */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return udev_ctrl_connection_unref(ctrl_conn);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering/* read inotify messages */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int handle_inotify(struct udev *udev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if ((ioctl(fd_inotify, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error getting buffer for inotify\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ev = (struct inotify_event *)(buf + pos);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void handle_signal(struct udev *udev, int signo)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach_safe(loop, tmp, &worker_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct worker *worker = node_to_worker(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] exit with return code %i\n", pid, WEXITSTATUS(status));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] terminated by signal %i (%s)\n",
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] continued\n", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] exit with status 0x%04x\n", pid, status);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] failed while handling '%s'\n",
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* drop reference taken for state 'running' */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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)) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("ctrl=%i netlink=%i\n", ctrl, netlink);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * read the kernel commandline, in case we need to get into debug mode
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * udev.log-priority=<level> syslog priority
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * udev.children-max=<number of workers> events are fully serialized if set to 1
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * udev.exec-delay=<number of seconds> delay execution of every executed program
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void kernel_cmdline_options(struct udev *udev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (read_one_line_file("/proc/cmdline", &line) < 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* accept the same options for the initrd, prefixed with "rd." */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (startswith(opt, "udev.log-priority=")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (startswith(opt, "udev.children-max=")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering children_max = strtoul(opt + 18, NULL, 0);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (startswith(opt, "udev.exec-delay=")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "children-max", required_argument, NULL, 'c' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "exec-delay", required_argument, NULL, 'e' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "resolve-names", required_argument, NULL, 'N' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct epoll_event ep_ctrl, ep_inotify, ep_signal, ep_netlink, ep_worker;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_ctrl_connection *ctrl_conn = NULL;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_set_max_level(udev_get_log_priority(udev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering option = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fprintf(stderr, "resolve-names must be early, late or never\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("resolve-names must be early, late or never\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --children-max=<maximum number of workers>\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --exec-delay=<seconds to wait before executing RUN=>\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --resolve-names=early|late|never\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --version\n"
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fprintf(stderr, "root privileges required\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* set umask before creating any file/directory */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* before opening new files, make sure std{in,out,err} fds are in a sane state */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fprintf(stderr, "cannot open /dev/null\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* get control and netlink socket from systemd */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error taking over udev control socket");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error taking over netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* get our own cgroup, we regularly kill everything udev has left behind */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* open control and netlink socket */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fprintf(stderr, "error initializing udev control socket");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("error initializing udev control socket");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering monitor = udev_monitor_new_from_netlink(udev, "kernel");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fprintf(stderr, "error initializing netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error initializing netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd_netlink = udev_monitor_get_fd(monitor);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_monitor_enable_receiving(monitor) < 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fprintf(stderr, "error binding netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error binding netlink socket\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fprintf(stderr, "error binding udev control socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error binding udev control socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* create queue file before signalling 'ready', to make sure we block 'settle' */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_queue_export = udev_queue_export_new(udev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error creating queue file\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("fork of daemon failed: %m\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering write_string_file("/proc/self/oom_score_adj", "-1000");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering print_kmsg("starting version " VERSION "\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error initializing inotify\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error initializing inotify\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* block and listen to all signals on signalfd */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error creating signalfd\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* unnamed socket from workers to the main daemon */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error creating socketpair\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error creating socketpair\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rules = udev_rules_new(udev, resolve_names);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_ctrl, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_inotify, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_signal, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_netlink, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_worker, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error creating epoll fd: %m\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 ||
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 ||
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 ||
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("fail to add fds to epoll: %m\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_debug("set children_max to %u\n", children_max);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = udev_rules_apply_static_dev_perms(rules);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("failed to apply permissions on static device nodes - %s\n", strerror(-rc));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* close sources of new events and discard buffered events */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* discard queued events and kill workers */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* exit after all has cleaned up */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (udev_list_node_is_empty(&event_list) && udev_list_node_is_empty(&worker_list))
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* timeout at exit for workers to finish */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering } else if (udev_list_node_is_empty(&event_list) && !children) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* we are idle */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* cleanup possible left-over processes in our cgroup */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* kill idle or hanging workers */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("timeout, giving up waiting for workers to finish\n");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* kill idle workers */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (udev_list_node_is_empty(&event_list)) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* check for hanging events */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering udev_list_node_foreach(loop, &worker_list) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering struct worker *worker = node_to_worker(loop);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if ((now(CLOCK_MONOTONIC) - worker->event_start_usec) > 30 * 1000 * 1000) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("worker [%u] %s timeout; kill it\n", worker->pid,
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker->event ? worker->event->devpath : "<idle>");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* drop reference taken for state 'running' */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering is_worker = is_signal = is_inotify = is_netlink = is_ctrl = false;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering for (i = 0; i < fdcount; i++) {
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering if (ev[i].data.fd == fd_worker && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering else if (ev[i].data.fd == fd_netlink && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering else if (ev[i].data.fd == fd_inotify && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering else if (ev[i].data.fd == fd_ctrl && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering /* check for changed config, every 3 seconds at most */
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * 1000 * 1000) {
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering /* reload requested, HUP signal received, rules changed, builtin changed */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* event has finished */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering dev = udev_monitor_receive_device(monitor);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC));
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering /* start new events */
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) {
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering rules = udev_rules_new(udev, resolve_names);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (size == sizeof(struct signalfd_siginfo))
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering /* we are shutting down, the events below are not handled anymore */
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering /* device node watch */
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * This needs to be after the inotify handling, to make sure,
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * that the ping is send back after the possibly generated
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * "change" events by the inotify device node watch.
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * A single time we may receive a client connection which we need to
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * keep open to block the client. It will be closed right before we
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_queue_export_cleanup(udev_queue_export);