udevd.c revision 523c620bcd657f22cb52e6b78881cbb1f7d6832a
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) 2009 Canonical Ltd.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This program is free software: you can redistribute it and/or modify
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * it under the terms of the GNU General Public License as published by
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * the Free Software Foundation, either version 2 of the License, or
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * (at your option) any later version.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This program is distributed in the hope that it will be useful,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * but WITHOUT ANY WARRANTY; without even the implied warranty of
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * GNU General Public License for more details.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * You should have received a copy of the GNU General Public License
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * along with this program. If not, see <http://www.gnu.org/licenses/>.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stddef.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <signal.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <unistd.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <errno.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdio.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdlib.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <stdbool.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <string.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <ctype.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <fcntl.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <time.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <getopt.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <dirent.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/file.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/time.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/prctl.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/socket.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/un.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/signalfd.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/epoll.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/mount.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/poll.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/wait.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/stat.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/ioctl.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/inotify.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include <sys/utsname.h>
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "udev.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "udev-util.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "rtnl-util.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "sd-daemon.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "cgroup-util.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "dev-setup.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen#include "fileio.h"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic struct udev_rules *rules;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic struct udev_ctrl *udev_ctrl;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic struct udev_monitor *monitor;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int worker_watch[2] = { -1, -1 };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int fd_signal = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int fd_ep = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int fd_inotify = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic bool stop_exec_queue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic bool reload;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int children;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic bool arg_debug = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int arg_daemonize = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int arg_resolve_names = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int arg_children_max;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int arg_exec_delay;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic sigset_t sigmask_orig;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic UDEV_LIST(event_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic UDEV_LIST(worker_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic char *udev_cgroup;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic struct udev_list properties_list;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic bool udev_exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenenum event_state {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen EVENT_UNDEF,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen EVENT_QUEUED,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen EVENT_RUNNING,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstruct event {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node node;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev *udev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_device *dev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen enum event_state state;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int exitcode;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned long long int delaying_seqnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unsigned long long int seqnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *devpath;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size_t devpath_len;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *devpath_old;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_t devnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int ifindex;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen bool is_block;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline struct event *node_to_event(struct udev_list_node *node) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return container_of(node, struct event, node);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_queue_cleanup(struct udev *udev, enum event_state type);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenenum worker_state {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen WORKER_UNDEF,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen WORKER_RUNNING,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen WORKER_IDLE,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen WORKER_KILLED,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstruct worker {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node node;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev *udev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int refcount;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid_t pid;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_monitor *monitor;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen enum worker_state state;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct event *event;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen usec_t event_start_usec;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen bool event_warned;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* passed from worker to main process */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstruct worker_message {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid_t pid;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int exitcode;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen};
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic inline struct worker *node_to_worker(struct udev_list_node *node) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return container_of(node, struct worker, node);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_queue_delete(struct event *event) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_remove(&event->node);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_unref(event->dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic struct worker *worker_ref(struct worker *worker) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->refcount++;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return worker;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void worker_cleanup(struct worker *worker) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_remove(&worker->node);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_unref(worker->monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen children--;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(worker);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void worker_unref(struct worker *worker) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->refcount--;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->refcount > 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("worker [%u] cleaned up", worker->pid);
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen worker_cleanup(worker);
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen}
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersenstatic void worker_list_cleanup(struct udev *udev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop, *tmp;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach_safe(loop, tmp, &worker_list) {
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen struct worker *worker = node_to_worker(loop);
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen worker_cleanup(worker);
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen }
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersenstatic void worker_new(struct event *event) {
23446f01480e1d6f72a03d71f9c67cbf34ddaf3bMichal Schmidt struct udev *udev = event->udev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct worker *worker;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_monitor *worker_monitor;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid_t pid;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* listen for new events */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker_monitor == NULL)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* allow the main daemon netlink address to send devices to the worker */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_allow_unicast_sender(worker_monitor, monitor);
52d629010db73a9466c359201916494bd55186d1Tom Gundersen udev_monitor_enable_receiving(worker_monitor);
52d629010db73a9466c359201916494bd55186d1Tom Gundersen
52d629010db73a9466c359201916494bd55186d1Tom Gundersen worker = new0(struct worker, 1);
52d629010db73a9466c359201916494bd55186d1Tom Gundersen if (worker == NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_unref(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* worker + event reference */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->refcount = 2;
52d629010db73a9466c359201916494bd55186d1Tom Gundersen worker->udev = udev;
52d629010db73a9466c359201916494bd55186d1Tom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid = fork();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen switch (pid) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 0: {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_device *dev = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd_monitor;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_signal, ep_monitor;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sigset_t mask;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int rc = EXIT_SUCCESS;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* take initial device from queue */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev = event->dev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->dev = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(worker);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_list_cleanup(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_queue_cleanup(udev, EVENT_UNDEF);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_unref(monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_ctrl_unref(udev_ctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd_signal);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd_ep);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(worker_watch[READ_END]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sigfillset(&mask);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_signal < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "error creating signalfd %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 2;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_ep = epoll_create1(EPOLL_CLOEXEC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_ep < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "error creating epoll fd: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 3;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memzero(&ep_signal, sizeof(struct epoll_event));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_signal.events = EPOLLIN;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_signal.data.fd = fd_signal;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_monitor = udev_monitor_get_fd(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memzero(&ep_monitor, sizeof(struct epoll_event));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_monitor.events = EPOLLIN;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_monitor.data.fd = fd_monitor;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "fail to add fds to epoll: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 4;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* request TERM signal if parent exits */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen prctl(PR_SET_PDEATHSIG, SIGTERM);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* reset OOM score, we only protect the main daemon */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen write_string_file("/proc/self/oom_score_adj", "0");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (;;) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_event *udev_event;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct worker_message msg;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd_lock = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int err = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("seq %llu running", udev_device_get_seqnum(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_event = udev_event_new(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_event == NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 5;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* needed for SIGCHLD/SIGTERM in spawn() */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_event->fd_signal = fd_signal;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (arg_exec_delay > 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_event->exec_delay = arg_exec_delay;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Take a shared lock on the device node; this establishes
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * a concept of device "ownership" to serialize device
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * access. External processes holding an exclusive lock will
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * cause udev to skip the event handling; in the case udev
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * acquired the lock, the external process can block until
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * udev has finished its event handling.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!streq_ptr(udev_device_get_action(dev), "remove") &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen streq_ptr("block", udev_device_get_subsystem(dev)) &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !startswith(udev_device_get_sysname(dev), "dm-") &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !startswith(udev_device_get_sysname(dev), "md")) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_device *d = dev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (streq_ptr("partition", udev_device_get_devtype(d)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen d = udev_device_get_parent(d);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (d) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen err = -EWOULDBLOCK;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_lock = safe_close(fd_lock);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto skip;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* needed for renaming netifs */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_event->rtnl = rtnl;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* apply rules, create node, symlinks */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_event_execute_rules(udev_event,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_usec, arg_event_timeout_warn_usec,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen &properties_list,
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen rules,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen &sigmask_orig);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_event_execute_run(udev_event,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_usec, arg_event_timeout_warn_usec,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen &sigmask_orig);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_event->rtnl)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* in case rtnl was initialized */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rtnl = sd_rtnl_ref(udev_event->rtnl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* apply/restore inotify watch */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_event->inotify_watch) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_watch_begin(udev, dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_update_db(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen safe_close(fd_lock);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* send processed event back to libudev listeners */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_send_device(worker_monitor, NULL, dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenskip:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* send udevd the result of the event execution */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen memzero(&msg, sizeof(struct worker_message));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen msg.exitcode = err;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen msg.pid = getpid();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("seq %llu processed with %i", udev_device_get_seqnum(dev), err);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_unref(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_event->sigterm) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_event_unref(udev_event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_event_unref(udev_event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* wait for more device messages from main udevd, or term signal */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen while (dev == NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ev[4];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fdcount;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int i;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fdcount < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (errno == EINTR)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "failed to poll: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; i < fdcount; i++) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev = udev_monitor_receive_device(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct signalfd_siginfo fdsi;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ssize_t size;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (size != sizeof(struct signalfd_siginfo))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen switch (fdsi.ssi_signo) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case SIGTERM:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenout:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_unref(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen safe_close(fd_signal);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen safe_close(fd_ep);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd_inotify);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(worker_watch[WRITE_END]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_rules_unref(rules);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_builtin_exit(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_unref(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_unref(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_close();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen exit(rc);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case -1:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_unref(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->state = EVENT_QUEUED;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(worker);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "fork of child failed: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen default:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* close monitor, but keep address around */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_disconnect(worker_monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->monitor = worker_monitor;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->pid = pid;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->state = WORKER_RUNNING;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event_start_usec = now(CLOCK_MONOTONIC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event_warned = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event = event;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->state = EVENT_RUNNING;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_append(&worker->node, &worker_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen children++;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("seq %llu forked new worker [%u]", udev_device_get_seqnum(event->dev), pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_run(struct event *event) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach(loop, &worker_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct worker *worker = node_to_worker(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ssize_t count;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->state != WORKER_IDLE)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (count < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "worker [%u] did not accept message %zi (%m), kill it", worker->pid, count);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen kill(worker->pid, SIGKILL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->state = WORKER_KILLED;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_ref(worker);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event = event;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->state = WORKER_RUNNING;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event_start_usec = now(CLOCK_MONOTONIC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event_warned = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->state = EVENT_RUNNING;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (children >= arg_children_max) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (arg_children_max > 1)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("maximum number (%i) of children reached", children);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* start new worker and pass initial device */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_new(event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int event_queue_insert(struct udev_device *dev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct event *event;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event = new0(struct event, 1);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event == NULL)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->udev = udev_device_get_udev(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->dev = dev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->seqnum = udev_device_get_seqnum(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->devpath = udev_device_get_devpath(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->devpath_len = strlen(event->devpath);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->devpath_old = udev_device_get_devpath_old(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->devnum = udev_device_get_devnum(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->is_block = streq("block", udev_device_get_subsystem(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->ifindex = udev_device_get_ifindex(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev),
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_get_action(dev), udev_device_get_subsystem(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->state = EVENT_QUEUED;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_append(&event->node, &event_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void worker_kill(struct udev *udev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach(loop, &worker_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct worker *worker = node_to_worker(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->state == WORKER_KILLED)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->state = WORKER_KILLED;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen kill(worker->pid, SIGTERM);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* lookup event for identical, parent, child device */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic bool is_devpath_busy(struct event *event) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size_t common;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check if queue contains events we depend on */
7141e4f62c3f220872df3114c42d9e4b9525e43eTom Gundersen udev_list_node_foreach(loop, &event_list) {
7141e4f62c3f220872df3114c42d9e4b9525e43eTom Gundersen struct event *loop_event = node_to_event(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* we already found a later event, earlier can not block us, no need to check again */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (loop_event->seqnum < event->delaying_seqnum)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* event we checked earlier still exists, no need to check again */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (loop_event->seqnum == event->delaying_seqnum)
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen return true;
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen /* found ourself, no later event can block us */
2a2137401b3aef20618308d2b2694e21b0124f89Tom Gundersen if (loop_event->seqnum >= event->seqnum)
2a2137401b3aef20618308d2b2694e21b0124f89Tom Gundersen break;
2a2137401b3aef20618308d2b2694e21b0124f89Tom Gundersen
bba061662b0f759abb43bad60c9733305c191045Tom Gundersen /* check major/minor */
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check network device ifindex */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check our old name */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->delaying_seqnum = loop_event->seqnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* compare devpath */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen common = MIN(loop_event->devpath_len, event->devpath_len);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* one devpath is contained in the other? */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (memcmp(loop_event->devpath, event->devpath, common) != 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* identical device event found */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (loop_event->devpath_len == event->devpath_len) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* devices names might have changed/swapped in the meantime */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->delaying_seqnum = loop_event->seqnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* parent device event found */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event->devpath[common] == '/') {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event->delaying_seqnum = loop_event->seqnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* child device event found */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (loop_event->devpath[common] == '/') {
19c29853804f8806866695d743fd4e3586996dbbMichal Schmidt event->delaying_seqnum = loop_event->seqnum;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* no matching device */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_queue_start(struct udev *udev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
23446f01480e1d6f72a03d71f9c67cbf34ddaf3bMichal Schmidt udev_list_node_foreach(loop, &event_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct event *event = node_to_event(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event->state != EVENT_QUEUED)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* do not start event if parent or child event is still running */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (is_devpath_busy(event))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_run(event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void event_queue_cleanup(struct udev *udev, enum event_state match_type) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop, *tmp;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach_safe(loop, tmp, &event_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct event *event = node_to_event(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (match_type != EVENT_UNDEF && match_type != event->state)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_queue_delete(event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void worker_returned(int fd_worker) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (;;) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct worker_message msg;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ssize_t size;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size = recv(fd_worker, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (size != sizeof(struct worker_message))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* lookup worker who sent the signal */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach(loop, &worker_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct worker *worker = node_to_worker(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->pid != msg.pid)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* worker returned */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->event) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event->exitcode = msg.exitcode;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_queue_delete(worker->event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->state != WORKER_KILLED)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->state = WORKER_IDLE;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_unref(worker);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/* receive the udevd message from userspace */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev *udev = udev_ctrl_get_udev(uctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_ctrl_connection *ctrl_conn;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_ctrl_msg *ctrl_msg = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *str;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int i;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ctrl_conn = udev_ctrl_get_connection(uctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ctrl_conn == NULL)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ctrl_msg = udev_ctrl_receive_msg(ctrl_conn);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ctrl_msg == NULL)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto out;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen i = udev_ctrl_get_set_log_level(ctrl_msg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_set_max_level(i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_kill(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (STOP_EXEC_QUEUE) received");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen stop_exec_queue = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (START_EXEC_QUEUE) received");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen stop_exec_queue = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_get_reload(ctrl_msg) > 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (RELOAD) received");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen reload = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen str = udev_ctrl_get_set_env(ctrl_msg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (str != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *key;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen key = strdup(str);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (key != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *val;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen val = strchr(key, '=');
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (val != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen val[0] = '\0';
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen val = &val[1];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (val[0] == '\0') {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (ENV) received, unset '%s'", key);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_entry_add(&properties_list, key, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (ENV) received, set '%s=%s'", key, val);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_entry_add(&properties_list, key, val);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("wrong key format '%s'", key);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(key);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_kill(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen i = udev_ctrl_get_set_children_max(ctrl_msg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (i >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i", i);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_children_max = i;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_get_ping(ctrl_msg) > 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (SYNC) received");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_get_exit(ctrl_msg) > 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("udevd message (EXIT) received");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_exit = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* keep reference to block the client until we exit */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_ctrl_connection_ref(ctrl_conn);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenout:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_ctrl_msg_unref(ctrl_msg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return udev_ctrl_connection_unref(ctrl_conn);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
08232a020bd2571088d3ee06dda07732c5e963d1Tom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int synthesize_change(struct udev_device *dev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char filename[UTIL_PATH_SIZE];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen streq_ptr("disk", udev_device_get_devtype(dev)) &&
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen !startswith(udev_device_get_sysname(dev), "dm-")) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen bool part_table_read = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen bool has_partitions = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev *udev = udev_device_get_udev(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_entry *item;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * Try to re-read the partition table. This only succeeds if
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * none of the devices is busy. The kernel returns 0 if no
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * partition table is found, and we will not get an event for
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * the disk.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = flock(fd, LOCK_EX|LOCK_NB);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = ioctl(fd, BLKRRPART, 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen part_table_read = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* search for partitions */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen e = udev_enumerate_new(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!e)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -ENOMEM;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = udev_enumerate_add_match_parent(e, dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = udev_enumerate_add_match_subsystem(e, "block");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = udev_enumerate_scan_devices(e);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _cleanup_udev_device_unref_ struct udev_device *d = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!d)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!streq_ptr("partition", udev_device_get_devtype(d)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen has_partitions = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * We have partitions and re-read the table, the kernel already sent
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * out a "change" event for the disk, and "remove/add" for all
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * partitions.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (part_table_read && has_partitions)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
85091685af65831f379580c75b40776c20e245eeTom Gundersen * We have partitions but re-reading the partition table did not
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * work, synthesize "change" for the disk and all partitions.
4189708ad0cde8e211e38d27de943579772f8869Tom Gundersen */
5a917c064b23c1b8a12d6abd2f9f31c575ddebc6Tom Gundersen log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen write_string_file(filename, "change");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann _cleanup_udev_device_unref_ struct udev_device *d = NULL;
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!d)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!streq_ptr("partition", udev_device_get_devtype(d)))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("device %s closed, synthesising partition '%s' 'change'",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_get_devnode(dev), udev_device_get_devnode(d));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen write_string_file(filename, "change");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen write_string_file(filename, "change");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int handle_inotify(struct udev *udev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int nbytes, pos;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *buf;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct inotify_event *ev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = ioctl(fd_inotify, FIONREAD, &nbytes);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0 || nbytes <= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -errno;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen buf = malloc(nbytes);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!buf) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error getting buffer for inotify");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -ENOMEM;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen nbytes = read(fd_inotify, buf, nbytes);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_device *dev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ev = (struct inotify_event *)(buf + pos);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev = udev_watch_lookup(udev, ev->wd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!dev)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("inotify event: %x for %s", ev->mask, udev_device_get_devnode(dev));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ev->mask & IN_CLOSE_WRITE)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen synthesize_change(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev->mask & IN_IGNORED)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_watch_end(udev, dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_unref(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(buf);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void handle_signal(struct udev *udev, int signo) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen switch (signo) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case SIGINT:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case SIGTERM:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_exit = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case SIGCHLD:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (;;) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid_t pid;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int status;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop, *tmp;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid = waitpid(-1, &status, WNOHANG);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (pid <= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach_safe(loop, tmp, &worker_list) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct worker *worker = node_to_worker(loop);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->pid != pid)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("worker [%u] exit", pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (WIFEXITED(status)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (WEXITSTATUS(status) != 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] exit with return code %i",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid, WEXITSTATUS(status));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (WIFSIGNALED(status)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] terminated by signal %i (%s)",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (WIFSTOPPED(status)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] stopped", pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (WIFCONTINUED(status)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] continued", pid);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] exit with status 0x%04x", pid, status);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->event) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("worker [%u] failed while handling '%s'",
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid, worker->event->devpath);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event->exitcode = -32;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_queue_delete(worker->event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* drop reference taken for state 'running' */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_unref(worker);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen worker_unref(worker);
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen break;
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen }
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen }
7283a80d10217b929acf58899f016ca06dacdc53Tom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case SIGHUP:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen reload = true;
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann break;
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann }
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int ctrl = -1, netlink = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd, n;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen n = sd_listen_fds(true);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (n <= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ctrl >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ctrl = fd;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (netlink >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen netlink = fd;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ctrl < 0 || netlink < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("ctrl=%i netlink=%i", ctrl, netlink);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *rctrl = ctrl;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *rnetlink = netlink;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen/*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * read the kernel command line, in case we need to get into debug mode
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * udev.log-priority=<level> syslog priority
afcac065c0f649ebcf0f450475a8d7c3bc776d14Tom Gundersen * udev.children-max=<number of workers> events are fully serialized if set to 1
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * udev.exec-delay=<number of seconds> delay execution of every executed program
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic void kernel_cmdline_options(struct udev *udev) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen _cleanup_free_ char *line = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen const char *word, *state;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size_t l;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = proc_cmdline(&line);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen FOREACH_WORD_QUOTED(word, l, line, state) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen char *s, *opt, *value;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen s = strndup(word, l);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!s)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* accept the same options for the initrd, prefixed with "rd." */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (in_initrd() && startswith(s, "rd."))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen opt = s + 3;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen opt = s;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((value = startswith(opt, "udev.log-priority="))) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int prio;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen prio = util_log_priority(value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_set_max_level(prio);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if ((value = startswith(opt, "udev.children-max="))) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = safe_atoi(value, &arg_children_max);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid udev.children-max ignored: %s", value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if ((value = startswith(opt, "udev.exec-delay="))) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = safe_atoi(value, &arg_exec_delay);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid udev.exec-delay ignored: %s", value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if ((value = startswith(opt, "udev.event-timeout="))) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = safe_atou64(value, &arg_event_timeout_usec);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid udev.event-timeout ignored: %s", value);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_usec *= USEC_PER_SEC;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen free(s);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmannstatic void help(void) {
bf4c113e179650c07a8f95a14d71f365dbfe66e4David Herrmann printf("%s [OPTIONS...]\n\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen "Manages devices.\n\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --daemon\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --debug\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --children-max=<maximum number of workers>\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --exec-delay=<seconds to wait before executing RUN=>\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --event-timeout=<seconds to wait before terminating an event>\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --resolve-names=early|late|never\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --version\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen " --help\n"
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen , program_invocation_short_name);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenstatic int parse_argv(int argc, char *argv[]) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen static const struct option options[] = {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "daemon", no_argument, NULL, 'd' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "debug", no_argument, NULL, 'D' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "children-max", required_argument, NULL, 'c' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "exec-delay", required_argument, NULL, 'e' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "event-timeout", required_argument, NULL, 't' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "resolve-names", required_argument, NULL, 'N' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "help", no_argument, NULL, 'h' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen { "version", no_argument, NULL, 'V' },
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen {}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int c;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(argc >= 0);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert(argv);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen while ((c = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL)) >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen switch (c) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 'd':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_daemonize = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 'c':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = safe_atoi(optarg, &arg_children_max);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid --children-max ignored: %s", optarg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 'e':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = safe_atoi(optarg, &arg_exec_delay);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid --exec-delay ignored: %s", optarg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 't':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = safe_atou64(optarg, &arg_event_timeout_usec);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("Invalid --event-timeout ignored: %s", optarg);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_usec *= USEC_PER_SEC;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 'D':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_debug = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 'N':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (streq(optarg, "early")) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_resolve_names = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (streq(optarg, "late")) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_resolve_names = 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (streq(optarg, "never")) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_resolve_names = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("resolve-names must be early, late or never");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 'h':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen help();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 'V':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen printf("%s\n", VERSION);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 0;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case '?':
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return -EINVAL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen default:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen assert_not_reached("Unhandled option");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenint main(int argc, char *argv[]) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev *udev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sigset_t mask;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd_ctrl = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd_netlink = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd_worker = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_ctrl = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_inotify = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_signal = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_netlink = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ep_worker = { .events = EPOLLIN };
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_ctrl_connection *ctrl_conn = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int rc = 1, r;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev = udev_new();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev == NULL)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_set_target(LOG_TARGET_AUTO);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_parse_environment();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_open();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = parse_argv(argc, argv);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r <= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen kernel_cmdline_options(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (arg_debug)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_set_max_level(LOG_DEBUG);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (getuid() != 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("root privileges required");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = mac_selinux_init("/dev");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(r, "could not initialize labelling: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* set umask before creating any file/directory */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = chdir("/");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "could not change dir to /: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen umask(022);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_init(udev, &properties_list, true);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen r = mkdir("/run/udev", 0755);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (r < 0 && errno != EEXIST) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "could not create /run/udev: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev_setup(NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* before opening new files, make sure std{in,out,err} fds are in a sane state */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (arg_daemonize) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd = open("/dev/null", O_RDWR);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (write(STDOUT_FILENO, 0, 0) < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dup2(fd, STDOUT_FILENO);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (write(STDERR_FILENO, 0, 0) < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dup2(fd, STDERR_FILENO);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd > STDERR_FILENO)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("cannot open /dev/null");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* get control and netlink socket from systemd */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl == NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error taking over udev control socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (monitor == NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error taking over netlink socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 3;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* get our own cgroup, we regularly kill everything udev has left behind */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_cgroup = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* open control and netlink socket */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_ctrl = udev_ctrl_new(udev);
4189708ad0cde8e211e38d27de943579772f8869Tom Gundersen if (udev_ctrl == NULL) {
4189708ad0cde8e211e38d27de943579772f8869Tom Gundersen log_error("error initializing udev control socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_ctrl = udev_ctrl_get_fd(udev_ctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
53fae771bcf1623cd28791c48fa60d9d5e5086e4Zbigniew Jędrzejewski-Szmek monitor = udev_monitor_new_from_netlink(udev, "kernel");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (monitor == NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error initializing netlink socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 3;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
53fae771bcf1623cd28791c48fa60d9d5e5086e4Zbigniew Jędrzejewski-Szmek }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_netlink = udev_monitor_get_fd(monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_monitor_enable_receiving(monitor) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error binding netlink socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 3;
4189708ad0cde8e211e38d27de943579772f8869Tom Gundersen goto exit;
4189708ad0cde8e211e38d27de943579772f8869Tom Gundersen }
4189708ad0cde8e211e38d27de943579772f8869Tom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error binding udev control socket");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
53fae771bcf1623cd28791c48fa60d9d5e5086e4Zbigniew Jędrzejewski-Szmek }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_info("starting version " VERSION);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_builtin_init(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rules = udev_rules_new(udev, arg_resolve_names);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (rules == NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error reading rules");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
107f2e2526d476c6cc9b81a690391c111027d641Tom Gundersen rc = udev_rules_apply_static_dev_perms(rules);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (rc < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(rc, "failed to apply permissions on static device nodes - %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (arg_daemonize) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid_t pid;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen pid = fork();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen switch (pid) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case 0:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen case -1:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "fork of daemon failed: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 4;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen default:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = EXIT_SUCCESS;
107f2e2526d476c6cc9b81a690391c111027d641Tom Gundersen goto exit_daemonize;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
7141e4f62c3f220872df3114c42d9e4b9525e43eTom Gundersen setsid();
7141e4f62c3f220872df3114c42d9e4b9525e43eTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen write_string_file("/proc/self/oom_score_adj", "-1000");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sd_notify(1, "READY=1");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (arg_children_max <= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen cpu_set_t cpu_set;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen arg_children_max = 8;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering arg_children_max += CPU_COUNT(&cpu_set) * 2;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("set children_max to %u", arg_children_max);
7e518afab9fb55b8052f68888210927259275560Thomas Hindoe Paaboel Andersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_init(&event_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_init(&worker_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_inotify = udev_watch_init(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_inotify < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error initializing inotify");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 4;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_watch_restore(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* block and listen to all signals on signalfd */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sigfillset(&mask);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_signal < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error creating signalfd");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 5;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* unnamed socket from workers to the main daemon */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("error creating socketpair");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = 6;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_worker = worker_watch[READ_END];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_ctrl.data.fd = fd_ctrl;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_inotify.data.fd = fd_inotify;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_signal.data.fd = fd_signal;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_netlink.data.fd = fd_netlink;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ep_worker.data.fd = fd_worker;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_ep = epoll_create1(EPOLL_CLOEXEC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_ep < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "error creating epoll fd: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
19c29853804f8806866695d743fd4e3586996dbbMichal Schmidt }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 ||
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error_errno(errno, "fail to add fds to epoll: %m");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen goto exit;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (;;) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen static usec_t last_usec;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct epoll_event ev[8];
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fdcount;
107f2e2526d476c6cc9b81a690391c111027d641Tom Gundersen int timeout;
107f2e2526d476c6cc9b81a690391c111027d641Tom Gundersen bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl;
107f2e2526d476c6cc9b81a690391c111027d641Tom Gundersen int i;
107f2e2526d476c6cc9b81a690391c111027d641Tom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_exit) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* close sources of new events and discard buffered events */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_ctrl >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_ctrl = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (monitor != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_unref(monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen monitor = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_inotify >= 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd_inotify);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fd_inotify = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* discard queued events and kill workers */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_queue_cleanup(udev, EVENT_QUEUED);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_kill(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* exit after all has cleaned up */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_list_node_is_empty(&event_list) && children == 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* timeout at exit for workers to finish */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen timeout = 30 * MSEC_PER_SEC;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (udev_list_node_is_empty(&event_list) && children == 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* we are idle */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen timeout = -1;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* cleanup possible left-over processes in our cgroup */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_cgroup)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* kill idle or hanging workers */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen timeout = 3 * MSEC_PER_SEC;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* tell settle that we are busy or idle */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!udev_list_node_is_empty(&event_list)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen int fd;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann fd = open("/run/udev/queue", O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann if (fd >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unlink("/run/udev/queue");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fdcount < 0)
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann continue;
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fdcount == 0) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_list_node *loop;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann /* timeout */
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann if (udev_exit) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("timeout, giving up waiting for workers to finish");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen break;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* kill idle workers */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_list_node_is_empty(&event_list)) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_debug("cleanup idle workers");
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann worker_kill(udev);
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check for hanging events */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_node_foreach(loop, &worker_list) {
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann struct worker *worker = node_to_worker(loop);
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann usec_t ts;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker->state != WORKER_RUNNING)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ts = now(CLOCK_MONOTONIC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((ts - worker->event_start_usec) > arg_event_timeout_warn_usec) {
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann if ((ts - worker->event_start_usec) > arg_event_timeout_usec) {
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann log_error("worker [%u] %s timeout; kill it", worker->pid, worker->event->devpath);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen kill(worker->pid, SIGKILL);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->state = WORKER_KILLED;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann /* drop reference taken for state 'running' */
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann worker_unref(worker);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_error("seq %llu '%s' killed", udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event->exitcode = -64;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_queue_delete(worker->event);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event = NULL;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen } else if (!worker->event_warned) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_warning("worker [%u] %s is taking a long time", worker->pid, worker->event->devpath);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker->event_warned = true;
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann }
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen is_worker = is_signal = is_inotify = is_netlink = is_ctrl = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen for (i = 0; i < fdcount; i++) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (ev[i].data.fd == fd_worker && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen is_worker = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev[i].data.fd == fd_netlink && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen is_netlink = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen is_signal = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev[i].data.fd == fd_inotify && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen is_inotify = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen else if (ev[i].data.fd == fd_ctrl && ev[i].events & EPOLLIN)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen is_ctrl = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* check for changed config, every 3 seconds at most */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * USEC_PER_SEC) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_rules_check_timestamp(rules))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen reload = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (udev_builtin_validate(udev))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen reload = true;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen last_usec = now(CLOCK_MONOTONIC);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* reload requested, HUP signal received, rules changed, builtin changed */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (reload) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_kill(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rules = udev_rules_unref(rules);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_builtin_exit(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen reload = false;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* event has finished */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (is_worker)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_returned(fd_worker);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (is_netlink) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct udev_device *dev;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen dev = udev_monitor_receive_device(monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (dev != NULL) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (event_queue_insert(dev) < 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_device_unref(dev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* start new events */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_builtin_init(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (rules == NULL)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rules = udev_rules_new(udev, arg_resolve_names);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (rules != NULL)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_queue_start(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (is_signal) {
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen struct signalfd_siginfo fdsi;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ssize_t size;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (size == sizeof(struct signalfd_siginfo))
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen handle_signal(udev, fdsi.ssi_signo);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* we are shutting down, the events below are not handled anymore */
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann if (udev_exit)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen continue;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /* device node watch */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (is_inotify)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen handle_inotify(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen /*
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * This needs to be after the inotify handling, to make sure,
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * that the ping is send back after the possibly generated
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * "change" events by the inotify device node watch.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen *
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * A single time we may receive a client connection which we need to
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * keep open to block the client. It will be closed right before we
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen * exit.
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen */
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (is_ctrl)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen ctrl_conn = handle_ctrl_msg(udev_ctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen }
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen rc = EXIT_SUCCESS;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenexit:
8927b1dad2d4a7330174cb924090b4635a2547fbDavid Herrmann udev_ctrl_cleanup(udev_ctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen unlink("/run/udev/queue");
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersenexit_daemonize:
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_ep >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd_ep);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen worker_list_cleanup(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen event_queue_cleanup(udev, EVENT_UNDEF);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_rules_unref(rules);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_builtin_exit(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (fd_signal >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(fd_signal);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker_watch[READ_END] >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(worker_watch[READ_END]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen if (worker_watch[WRITE_END] >= 0)
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen close(worker_watch[WRITE_END]);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_monitor_unref(monitor);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_ctrl_connection_unref(ctrl_conn);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_ctrl_unref(udev_ctrl);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_list_cleanup(&properties_list);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen mac_selinux_finish();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen udev_unref(udev);
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen log_close();
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen return rc;
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen}
57fa1d094cd2c5ac68970526ad0a0754c548e75dTom Gundersen