udevd.c revision 005e945cc454c07fac381d59a6e0fe810eb1107b
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Copyright (C) 2009 Canonical Ltd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * This program is free software: you can redistribute it and/or modify
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * it under the terms of the GNU General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * This program is distributed in the hope that it will be useful,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * GNU General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * You should have received a copy of the GNU General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stddef.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <signal.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdio.h>
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include <stdlib.h>
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering#include <stdbool.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <string.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <fcntl.h>
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include <getopt.h>
96aad8d15a324d0e956a4e5653a11a67b209b41aLennart Poettering#include <sys/file.h>
23c80348e656a4e6fd9ba8f17523a65b6fa349a0Kay Sievers#include <sys/time.h>
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include <sys/prctl.h>
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering#include <sys/socket.h>
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering#include <sys/signalfd.h>
003dffde2c1b93afbc9aff24b277276f65424406Lennart Poettering#include <sys/epoll.h>
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering#include <sys/mount.h>
003dffde2c1b93afbc9aff24b277276f65424406Lennart Poettering#include <sys/wait.h>
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include <sys/stat.h>
003dffde2c1b93afbc9aff24b277276f65424406Lennart Poettering#include <sys/ioctl.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <sys/inotify.h>
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "sd-daemon.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "rtnl-util.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "cgroup-util.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "dev-setup.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "fileio.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "selinux-util.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "udev.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "udev-util.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "formats-util.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering#include "hashmap.h"
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic struct udev_rules *rules;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic struct udev_ctrl *udev_ctrl;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic struct udev_ctrl_connection *udev_ctrl_conn;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic struct udev_monitor *monitor;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int worker_watch[2] = { -1, -1 };
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int fd_signal = -1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int fd_ep = -1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int fd_inotify = -1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic bool stop_exec_queue;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic bool reload;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic bool arg_debug = false;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int arg_daemonize = false;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int arg_resolve_names = 1;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic unsigned arg_children_max;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic int arg_exec_delay;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic sigset_t sigmask_orig;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic UDEV_LIST(event_list);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart PoetteringHashmap *workers;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic char *udev_cgroup;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic struct udev_list properties_list;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic bool udev_exit;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringenum event_state {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering EVENT_UNDEF,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering EVENT_QUEUED,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering EVENT_RUNNING,
cb81cd8073392936882643af0129934bf67e96c4Lennart Poettering};
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstruct event {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct udev_list_node node;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct udev *udev;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct udev_device *dev;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct udev_device *dev_kernel;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct worker *worker;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering enum event_state state;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering unsigned long long int delaying_seqnum;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering unsigned long long int seqnum;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering const char *devpath;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering size_t devpath_len;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering const char *devpath_old;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering dev_t devnum;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering int ifindex;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering bool is_block;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering usec_t start_usec;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering bool warned;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering};
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return container_of(node, struct event, node);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state type);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringenum worker_state {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering WORKER_UNDEF,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering WORKER_RUNNING,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering WORKER_IDLE,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering WORKER_KILLED,
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering};
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstruct worker {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct udev_list_node node;
cb81cd8073392936882643af0129934bf67e96c4Lennart Poettering struct udev *udev;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering int refcount;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering pid_t pid;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct udev_monitor *monitor;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering enum worker_state state;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering struct event *event;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering};
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering/* passed from worker to main process */
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poetteringstruct worker_message {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering};
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void event_free(struct event *event) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (!event)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_list_node_remove(&event->node);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_device_unref(event->dev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_device_unref(event->dev_kernel);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (event->worker)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->worker->event = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering free(event);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void worker_free(struct worker *worker) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (!worker)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering hashmap_remove(workers, UINT_TO_PTR(worker->pid));
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_monitor_unref(worker->monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_unref(worker->udev);
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering event_free(worker->event);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering free(worker);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering}
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poetteringstatic void workers_free(void) {
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering struct worker *worker;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering Iterator i;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering HASHMAP_FOREACH(worker, workers, i)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering worker_free(worker);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering hashmap_free(workers);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering workers = NULL;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering}
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poetteringstatic int worker_new(struct worker **ret, struct udev *udev, struct udev_monitor *worker_monitor, pid_t pid) {
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering _cleanup_free_ struct worker *worker = NULL;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering int r;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering assert(ret);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering assert(udev);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering assert(worker_monitor);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering assert(pid > 1);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering worker = new0(struct worker, 1);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering if (!worker)
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering return -ENOMEM;
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering worker->refcount = 1;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering worker->udev = udev_ref(udev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* close monitor, but keep address around */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_monitor_disconnect(worker_monitor);
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering worker->monitor = udev_monitor_ref(worker_monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering worker->pid = pid;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = hashmap_ensure_allocated(&workers, NULL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return r;
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering r = hashmap_put(workers, UINT_TO_PTR(pid), worker);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return r;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering *ret = worker;
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering worker = NULL;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering return 0;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering}
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poetteringstatic void worker_attach_event(struct worker *worker, struct event *event) {
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering assert(worker);
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering assert(event);
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering assert(!event->worker);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering assert(!worker->event);
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering
4e724d9c5ab76c3f8327945317463ef706011082Lennart Poettering worker->state = WORKER_RUNNING;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering worker->event = event;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->state = EVENT_RUNNING;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering event->start_usec = now(CLOCK_MONOTONIC);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->warned = false;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek event->worker = worker;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void worker_spawn(struct event *event) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering struct udev *udev = event->udev;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL;
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering pid_t pid;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* listen for new events */
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (worker_monitor == NULL)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* allow the main daemon netlink address to send devices to the worker */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_monitor_allow_unicast_sender(worker_monitor, monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_monitor_enable_receiving(worker_monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering pid = fork();
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering switch (pid) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case 0: {
df2d202e6ed4001a21c6512c244acad5d4706c87Lennart Poettering struct udev_device *dev = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int fd_monitor;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct epoll_event ep_signal, ep_monitor;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sigset_t mask;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int r = 0;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* take initial device from queue */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering dev = event->dev;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->dev = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering workers_free();
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event_queue_cleanup(udev, EVENT_UNDEF);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering udev_monitor_unref(monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_ctrl_unref(udev_ctrl);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering close(fd_signal);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering close(fd_ep);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering close(worker_watch[READ_END]);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sigfillset(&mask);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (fd_signal < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = log_error_errno(errno, "error creating signalfd %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering fd_ep = epoll_create1(EPOLL_CLOEXEC);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (fd_ep < 0) {
9030ca462bd13cd6536299814e4a71d5c5e85be9Lennart Poettering r = log_error_errno(errno, "error creating epoll fd: %m");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
8aec412ff697bc14995746953912ca6fdf2c9ba8Lennart Poettering memzero(&ep_signal, sizeof(struct epoll_event));
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering ep_signal.events = EPOLLIN;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ep_signal.data.fd = fd_signal;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd_monitor = udev_monitor_get_fd(worker_monitor);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering memzero(&ep_monitor, sizeof(struct epoll_event));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ep_monitor.events = EPOLLIN;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering ep_monitor.data.fd = fd_monitor;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering r = log_error_errno(errno, "fail to add fds to epoll: %m");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* request TERM signal if parent exits */
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering prctl(PR_SET_PDEATHSIG, SIGTERM);
7f0d207d2c816e0a8cb2742b0a789911f7c99356Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering /* reset OOM score, we only protect the main daemon */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering write_string_file("/proc/self/oom_score_adj", "0");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering for (;;) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering struct udev_event *udev_event;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct worker_message msg;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int fd_lock = -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("seq %llu running", udev_device_get_seqnum(dev));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_event = udev_event_new(dev);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (udev_event == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = -ENOMEM;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* needed for SIGCHLD/SIGTERM in spawn() */
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_event->fd_signal = fd_signal;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (arg_exec_delay > 0)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_event->exec_delay = arg_exec_delay;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering /*
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering * Take a shared lock on the device node; this establishes
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering * a concept of device "ownership" to serialize device
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering * access. External processes holding an exclusive lock will
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering * cause udev to skip the event handling; in the case udev
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering * acquired the lock, the external process can block until
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering * udev has finished its event handling.
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering */
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (!streq_ptr(udev_device_get_action(dev), "remove") &&
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering streq_ptr("block", udev_device_get_subsystem(dev)) &&
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering !startswith(udev_device_get_sysname(dev), "dm-") &&
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering !startswith(udev_device_get_sysname(dev), "md")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev_device *d = dev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (streq_ptr("partition", udev_device_get_devtype(d)))
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering d = udev_device_get_parent(d);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (d) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fd_lock = safe_close(fd_lock);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering r = -EAGAIN;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto skip;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering }
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering }
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering /* needed for renaming netifs */
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering udev_event->rtnl = rtnl;
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* apply rules, create node, symlinks */
554604b3073467af75dc94fac9e2343148603289Lennart Poettering udev_event_execute_rules(udev_event,
5b12334d35eadf1f45cc3d631fd1a2e72ffaea0aLennart Poettering arg_event_timeout_usec, arg_event_timeout_warn_usec,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering &properties_list,
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering rules,
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering &sigmask_orig);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_event_execute_run(udev_event,
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering arg_event_timeout_usec, arg_event_timeout_warn_usec,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering &sigmask_orig);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_event->rtnl)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering /* in case rtnl was initialized */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering rtnl = sd_rtnl_ref(udev_event->rtnl);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* apply/restore inotify watch */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_event->inotify_watch) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_watch_begin(udev, dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_device_update_db(dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering safe_close(fd_lock);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* send processed event back to libudev listeners */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_monitor_send_device(worker_monitor, NULL, dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringskip:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_debug("seq %llu processed", udev_device_get_seqnum(dev));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering /* send udevd the result of the event execution */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering memzero(&msg, sizeof(struct worker_message));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(errno, "failed to send result of seq %llu to main daemon: %m",
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_device_get_seqnum(dev));
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_device_unref(dev);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering dev = NULL;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering if (udev_event->sigterm) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_event_unref(udev_event);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering goto out;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering }
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_event_unref(udev_event);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering /* wait for more device messages from main udevd, or term signal */
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering while (dev == NULL) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering struct epoll_event ev[4];
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering int fdcount;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering int i;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (fdcount < 0) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (errno == EINTR)
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering continue;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering r = log_error_errno(errno, "failed to poll: %m");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering goto out;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering for (i = 0; i < fdcount; i++) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering dev = udev_monitor_receive_device(worker_monitor);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering break;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering struct signalfd_siginfo fdsi;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering ssize_t size;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (size != sizeof(struct signalfd_siginfo))
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering continue;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering switch (fdsi.ssi_signo) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case SIGTERM:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a3e7f417d72ba3251fd6b3a228a2721a4b725a03Zbigniew Jędrzejewski-Szmek }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poetteringout:
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering udev_device_unref(dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering safe_close(fd_signal);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering safe_close(fd_ep);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering close(fd_inotify);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering close(worker_watch[WRITE_END]);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering udev_rules_unref(rules);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_builtin_exit(udev);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_unref(udev);
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering log_close();
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering case -1:
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering event->state = EVENT_QUEUED;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering log_error_errno(errno, "fork of child failed: %m");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering break;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering default:
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering struct worker *worker;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering int r;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering r = worker_new(&worker, udev, worker_monitor, pid);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (r < 0)
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering worker_attach_event(worker, event);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering log_debug("seq %llu forked new worker ["PID_FMT"]", udev_device_get_seqnum(event->dev), pid);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering break;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering}
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poetteringstatic void event_run(struct event *event) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering struct worker *worker;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering Iterator i;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering HASHMAP_FOREACH(worker, workers, i) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering ssize_t count;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (worker->state != WORKER_IDLE)
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering continue;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (count < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(errno, "worker ["PID_FMT"] did not accept message %zi (%m), kill it",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering worker->pid, count);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering kill(worker->pid, SIGKILL);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering worker->state = WORKER_KILLED;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering continue;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering }
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering worker_attach_event(worker, event);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return;
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering }
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (hashmap_size(workers) >= arg_children_max) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (arg_children_max > 1)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_debug("maximum number (%i) of children reached", hashmap_size(workers));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* start new worker and pass initial device */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering worker_spawn(event);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic int event_queue_insert(struct udev_device *dev) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering struct event *event;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event = new0(struct event, 1);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (event == NULL)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering return -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering event->udev = udev_device_get_udev(dev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->dev = dev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering event->dev_kernel = udev_device_shallow_clone(dev);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_device_copy_properties(event->dev_kernel, dev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->seqnum = udev_device_get_seqnum(dev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->devpath = udev_device_get_devpath(dev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->devpath_len = strlen(event->devpath);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->devpath_old = udev_device_get_devpath_old(dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering event->devnum = udev_device_get_devnum(dev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->is_block = streq("block", udev_device_get_subsystem(dev));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event->ifindex = udev_device_get_ifindex(dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev),
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_device_get_action(dev), udev_device_get_subsystem(dev));
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering event->state = EVENT_QUEUED;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_list_node_append(&event->node, &event_list);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return 0;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poetteringstatic void worker_kill(void) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering struct worker *worker;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering Iterator i;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering HASHMAP_FOREACH(worker, workers, i) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (worker->state == WORKER_KILLED)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering continue;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering worker->state = WORKER_KILLED;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering kill(worker->pid, SIGTERM);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering/* lookup event for identical, parent, child device */
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poetteringstatic bool is_devpath_busy(struct event *event) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev_list_node *loop;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering size_t common;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering /* check if queue contains events we depend on */
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering udev_list_node_foreach(loop, &event_list) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering struct event *loop_event = node_to_event(loop);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* we already found a later event, earlier can not block us, no need to check again */
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (loop_event->seqnum < event->delaying_seqnum)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering continue;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* event we checked earlier still exists, no need to check again */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (loop_event->seqnum == event->delaying_seqnum)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return true;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* found ourself, no later event can block us */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (loop_event->seqnum >= event->seqnum)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering break;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* check major/minor */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return true;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* check network device ifindex */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return true;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* check our old name */
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return true;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* compare devpath */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering common = MIN(loop_event->devpath_len, event->devpath_len);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* one devpath is contained in the other? */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (memcmp(loop_event->devpath, event->devpath, common) != 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering continue;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* identical device event found */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (loop_event->devpath_len == event->devpath_len) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* devices names might have changed/swapped in the meantime */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering continue;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering continue;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return true;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering /* parent device event found */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (event->devpath[common] == '/') {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return true;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* child device event found */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (loop_event->devpath[common] == '/') {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return true;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* no matching device */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering continue;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return false;
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic void event_queue_start(struct udev *udev) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering struct udev_list_node *loop;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering udev_list_node_foreach(loop, &event_list) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering struct event *event = node_to_event(loop);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (event->state != EVENT_QUEUED)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering continue;
9030ca462bd13cd6536299814e4a71d5c5e85be9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* do not start event if parent or child event is still running */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (is_devpath_busy(event))
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering continue;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering event_run(event);
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering }
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering}
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state match_type) {
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering struct udev_list_node *loop, *tmp;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering udev_list_node_foreach_safe(loop, tmp, &event_list) {
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering struct event *event = node_to_event(loop);
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering if (match_type != EVENT_UNDEF && match_type != event->state)
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering continue;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering event_free(event);
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering }
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering}
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering
40205d706e1210763ff4c98a317556375bd04bcdLennart Poetteringstatic int on_worker(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering for (;;) {
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering struct worker_message msg;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering struct iovec iovec = {
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering .iov_base = &msg,
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering .iov_len = sizeof(msg),
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering };
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering union {
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering struct cmsghdr cmsghdr;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering } control = {};
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering struct msghdr msghdr = {
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering .msg_iov = &iovec,
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering .msg_iovlen = 1,
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering .msg_control = &control,
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering .msg_controllen = sizeof(control),
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering };
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering struct cmsghdr *cmsg;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering ssize_t size;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering struct ucred *ucred = NULL;
5f8cc96a0301c1177b11dd2e89370ef0b2ef577bLennart Poettering struct worker *worker;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering size = recvmsg(fd, &msghdr, MSG_DONTWAIT);
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (size < 0) {
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (errno == EAGAIN || errno == EINTR)
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering return 1;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering return log_error_errno(errno, "failed to receive message: %m");
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering } else if (size != sizeof(struct worker_message)) {
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering log_warning_errno(EIO, "ignoring worker message with invalid size %zi bytes", size);
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering continue;
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering }
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (cmsg->cmsg_level == SOL_SOCKET &&
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering cmsg->cmsg_type == SCM_CREDENTIALS &&
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering ucred = (struct ucred*) CMSG_DATA(cmsg);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering }
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering
90adaa25e894a580930ef2c3e65ab8db8295515aLennart Poettering if (!ucred || ucred->pid <= 0) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering log_warning_errno(EIO, "ignoring worker message without valid PID");
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering continue;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering }
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering /* lookup worker who sent the signal */
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering worker = hashmap_get(workers, UINT_TO_PTR(ucred->pid));
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering if (!worker) {
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering log_debug("worker ["PID_FMT"] returned, but is no longer tracked", ucred->pid);
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering continue;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering }
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering if (worker->state != WORKER_KILLED)
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering worker->state = WORKER_IDLE;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering /* worker returned */
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering event_free(worker->event);
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering }
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering return 1;
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering}
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poetteringstatic int on_uevent(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering struct udev_monitor *m = userdata;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering struct udev_device *dev;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering int r;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering assert(m);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering dev = udev_monitor_receive_device(m);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering if (dev) {
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering udev_device_ensure_usec_initialized(dev, NULL);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering r = event_queue_insert(dev);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering if (r < 0)
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering udev_device_unref(dev);
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering }
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering return 1;
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering}
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poetteringstatic void event_queue_update(void) {
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering int r;
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering if (!udev_list_node_is_empty(&event_list)) {
086821244b5113f00a0ef993b78dc56aae2a8f6cLennart Poettering r = touch("/run/udev/queue");
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (r < 0)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_warning_errno(r, "could not touch /run/udev/queue: %m");
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering } else {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering r = unlink("/run/udev/queue");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (r < 0 && errno != ENOENT)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_warning("could not unlink /run/udev/queue: %m");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering}
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering/* receive the udevd message from userspace */
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poetteringstatic int on_ctrl_msg(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering struct udev_ctrl *uctrl = userdata;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering _cleanup_udev_ctrl_connection_unref_ struct udev_ctrl_connection *ctrl_conn = NULL;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering _cleanup_udev_ctrl_msg_unref_ struct udev_ctrl_msg *ctrl_msg = NULL;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering const char *str;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering int i;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering assert(uctrl);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering ctrl_conn = udev_ctrl_get_connection(uctrl);
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering if (!ctrl_conn)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return 1;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering ctrl_msg = udev_ctrl_receive_msg(ctrl_conn);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (!ctrl_msg)
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering return 1;
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering i = udev_ctrl_get_set_log_level(ctrl_msg);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (i >= 0) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", i);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering log_set_max_level(i);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering worker_kill();
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_debug("udevd message (STOP_EXEC_QUEUE) received");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering stop_exec_queue = true;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_debug("udevd message (START_EXEC_QUEUE) received");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering stop_exec_queue = false;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering if (udev_ctrl_get_reload(ctrl_msg) > 0) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering log_debug("udevd message (RELOAD) received");
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering reload = true;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering str = udev_ctrl_get_set_env(ctrl_msg);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (str != NULL) {
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering char *key;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering key = strdup(str);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (key != NULL) {
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering char *val;
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering val = strchr(key, '=');
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (val != NULL) {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering val[0] = '\0';
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering val = &val[1];
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering if (val[0] == '\0') {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_debug("udevd message (ENV) received, unset '%s'", key);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering udev_list_entry_add(&properties_list, key, NULL);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering } else {
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering log_debug("udevd message (ENV) received, set '%s=%s'", key, val);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering udev_list_entry_add(&properties_list, key, val);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering } else {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering log_error("wrong key format '%s'", key);
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering }
ebd93cb684806ac0f352139e69ac8f53eb49f5e4Lennart Poettering free(key);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering worker_kill();
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering i = udev_ctrl_get_set_children_max(ctrl_msg);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (i >= 0) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i", i);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering arg_children_max = i;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (udev_ctrl_get_ping(ctrl_msg) > 0) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering log_debug("udevd message (SYNC) received");
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering /* tell settle that we are busy or idle, this needs to be before the
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering * PING handling
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering */
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering event_queue_update();
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (udev_ctrl_get_exit(ctrl_msg) > 0) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering log_debug("udevd message (EXIT) received");
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering udev_exit = true;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering /* keep reference to block the client until we exit */
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering udev_ctrl_conn = udev_ctrl_connection_ref(ctrl_conn);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering return 1;
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering}
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poetteringstatic int synthesize_change(struct udev_device *dev) {
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering char filename[UTIL_PATH_SIZE];
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering int r;
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
efe0286285a7432f738fafae840fa4eda51c2986Lennart Poettering streq_ptr("disk", udev_device_get_devtype(dev)) &&
efe0286285a7432f738fafae840fa4eda51c2986Lennart Poettering !startswith(udev_device_get_sysname(dev), "dm-")) {
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering bool part_table_read = false;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering bool has_partitions = false;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering int fd;
26166c88e0b47b83972f32b5057ecbffe06bf904Lennart Poettering struct udev *udev = udev_device_get_udev(dev);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering struct udev_list_entry *item;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering /*
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering * Try to re-read the partition table. This only succeeds if
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering * none of the devices is busy. The kernel returns 0 if no
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering * partition table is found, and we will not get an event for
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering * the disk.
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering */
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (fd >= 0) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering r = flock(fd, LOCK_EX|LOCK_NB);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (r >= 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering r = ioctl(fd, BLKRRPART, 0);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering close(fd);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (r >= 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering part_table_read = true;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering }
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering /* search for partitions */
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering e = udev_enumerate_new(udev);
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering if (!e)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return -ENOMEM;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering r = udev_enumerate_add_match_parent(e, dev);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (r < 0)
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering return r;
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = udev_enumerate_add_match_subsystem(e, "block");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering r = udev_enumerate_scan_devices(e);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (r < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!d)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering continue;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!streq_ptr("partition", udev_device_get_devtype(d)))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering continue;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering has_partitions = true;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering break;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /*
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * We have partitions and re-read the table, the kernel already sent
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * out a "change" event for the disk, and "remove/add" for all
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * partitions.
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (part_table_read && has_partitions)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /*
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * We have partitions but re-reading the partition table did not
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * work, synthesize "change" for the disk and all partitions.
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering write_string_file(filename, "change");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!d)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering continue;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!streq_ptr("partition", udev_device_get_devtype(d)))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering continue;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_debug("device %s closed, synthesising partition '%s' 'change'",
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering udev_device_get_devnode(dev), udev_device_get_devnode(d));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering write_string_file(filename, "change");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering write_string_file(filename, "change");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct udev *udev = userdata;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering union inotify_event_buffer buffer;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct inotify_event *e;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering ssize_t l;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert(udev);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering l = read(fd, &buffer, sizeof(buffer));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (l < 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (errno == EAGAIN || errno == EINTR)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return log_error_errno(errno, "Failed to read inotify fd: %m");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering FOREACH_INOTIFY_EVENT(e, buffer, l) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering dev = udev_watch_lookup(udev, e->wd);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!dev)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering continue;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_debug("inotify event: %x for %s", e->mask, udev_device_get_devnode(dev));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (e->mask & IN_CLOSE_WRITE) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering synthesize_change(dev);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* settle might be waiting on us to determine the queue
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * state. If we just handled an inotify event, we might have
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * generated a "change" event, but we won't have queued up
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * the resultant uevent yet. Do that.
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering on_uevent(NULL, -1, 0, monitor);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering } else if (e->mask & IN_IGNORED)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering udev_watch_end(udev, dev);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int on_request_exit(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering udev_exit = true;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int on_request_reload(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering reload = true;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering for (;;) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pid_t pid;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int status;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering struct worker *worker;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering pid = waitpid(-1, &status, WNOHANG);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (pid <= 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering worker = hashmap_get(workers, UINT_TO_PTR(pid));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!worker) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_warning("worker ["PID_FMT"] is unknown, ignoring", pid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (WIFEXITED(status)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (WEXITSTATUS(status) == 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_debug("worker ["PID_FMT"] exited", pid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering else
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_warning("worker ["PID_FMT"] exited with return code %i", pid, WEXITSTATUS(status));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering } else if (WIFSIGNALED(status)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_warning("worker ["PID_FMT"] terminated by signal %i (%s)", pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering } else if (WIFSTOPPED(status)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_info("worker ["PID_FMT"] stopped", pid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering } else if (WIFCONTINUED(status)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_info("worker ["PID_FMT"] continued", pid);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering } else
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_warning("worker ["PID_FMT"] exit with status 0x%04x", pid, status);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (worker->event) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_error("worker ["PID_FMT"] failed while handling '%s'", pid, worker->event->devpath);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* delete state from disk */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering udev_device_delete_db(worker->event->dev);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering udev_device_tag_index(worker->event->dev, NULL, false);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering /* forward kernel event without amending it */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering udev_monitor_send_device(monitor, NULL, worker->event->dev_kernel);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering worker_free(worker);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int ctrl = -1, netlink = -1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int fd, n;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering n = sd_listen_fds(true);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (n <= 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (ctrl >= 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering ctrl = fd;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering continue;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1)) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (netlink >= 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering netlink = fd;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering continue;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering }
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (ctrl < 0 || netlink < 0)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return -1;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering log_debug("ctrl=%i netlink=%i", ctrl, netlink);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *rctrl = ctrl;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering *rnetlink = netlink;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering}
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering/*
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * read the kernel command line, in case we need to get into debug mode
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * udev.log-priority=<level> syslog priority
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * udev.children-max=<number of workers> events are fully serialized if set to 1
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * udev.exec-delay=<number of seconds> delay execution of every executed program
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering * udev.event-timeout=<number of seconds> seconds to wait before terminating an event
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering */
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poetteringstatic int parse_proc_cmdline_item(const char *key, const char *value) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering int r;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering assert(key);
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (!value)
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (startswith(key, "rd."))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering key += strlen("rd.");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (startswith(key, "udev."))
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering key += strlen("udev.");
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering else
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering return 0;
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering if (streq(key, "log-priority")) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int prio;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering prio = util_log_priority(value);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering log_set_max_level(prio);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering } else if (streq(key, "children-max")) {
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering r = safe_atou(value, &arg_children_max);
c2ce6a3d82b717c4c1e6245ad8c6ce1173f502d0Lennart Poettering if (r < 0)
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering log_warning("invalid udev.children-max ignored: %s", value);
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering } else if (streq(key, "exec-delay")) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering r = safe_atoi(value, &arg_exec_delay);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering log_warning("invalid udev.exec-delay ignored: %s", value);
8d07a7c47159ebc2bcc775e2c1997cb44bd1ac95Lennart Poettering } else if (streq(key, "event-timeout")) {
9b5ed6feda08290edce3bf916fa7362733dd30eaLennart Poettering r = safe_atou64(value, &arg_event_timeout_usec);
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering if (r < 0)
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering log_warning("invalid udev.event-timeout ignored: %s", value);
3a6fb33c54bc64398e0af1c9d7c74a6b614a849dLennart Poettering else {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering arg_event_timeout_usec *= USEC_PER_SEC;
40205d706e1210763ff4c98a317556375bd04bcdLennart Poettering arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering }
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering }
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering return 0;
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering}
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poetteringstatic void help(void) {
70244d1d25eb80b57e160ea004d0e6bf793d4cafLennart Poettering printf("%s [OPTIONS...]\n\n"
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering "Manages devices.\n\n"
d6ce17c7f02ed3facdb45f65f546e587c2f00950Lennart Poettering " -h --help Print this message\n"
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering " --version Print version of the program\n"
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering " --daemon Detach and run in the background\n"
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering " --debug Enable debug output\n"
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering " --children-max=INT Set maximum number of workers\n"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering " --exec-delay=SECONDS Seconds to wait before executing RUN=\n"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering " --event-timeout=SECONDS Seconds to wait before terminating an event\n"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering " --resolve-names=early|late|never\n"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering " When to resolve users and groups\n"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering , program_invocation_short_name);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering static const struct option options[] = {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering { "daemon", no_argument, NULL, 'd' },
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering { "debug", no_argument, NULL, 'D' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "children-max", required_argument, NULL, 'c' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "exec-delay", required_argument, NULL, 'e' },
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering { "event-timeout", required_argument, NULL, 't' },
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering { "resolve-names", required_argument, NULL, 'N' },
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering { "help", no_argument, NULL, 'h' },
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering { "version", no_argument, NULL, 'V' },
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering {}
65d73cf042ba7de11d254f5c4714f467db64b7c3Lennart Poettering };
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering int c;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert(argc >= 0);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert(argv);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering while ((c = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL)) >= 0) {
491ac9f2c4aeda8c40edde35112404b737e38b60Lennart Poettering int r;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering switch (c) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case 'd':
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering arg_daemonize = true;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering break;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering case 'c':
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering r = safe_atou(optarg, &arg_children_max);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering if (r < 0)
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering log_warning("Invalid --children-max ignored: %s", optarg);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case 'e':
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = safe_atoi(optarg, &arg_exec_delay);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_warning("Invalid --exec-delay ignored: %s", optarg);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering case 't':
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = safe_atou64(optarg, &arg_event_timeout_usec);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering log_warning("Invalid --event-timeout ignored: %s", optarg);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering else {
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering arg_event_timeout_usec *= USEC_PER_SEC;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering arg_event_timeout_warn_usec = (arg_event_timeout_usec / 3) ? : 1;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering break;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering case 'D':
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering arg_debug = true;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering case 'N':
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (streq(optarg, "early")) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering arg_resolve_names = 1;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering } else if (streq(optarg, "late")) {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering arg_resolve_names = 0;
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering } else if (streq(optarg, "never")) {
e5f5b5b9c92d59ef3cb5169982cb6f436c9206faLennart Poettering arg_resolve_names = -1;
e5f5b5b9c92d59ef3cb5169982cb6f436c9206faLennart Poettering } else {
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering log_error("resolve-names must be early, late or never");
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering return 0;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering }
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering break;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering case 'h':
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering help();
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering return 0;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering case 'V':
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering printf("%s\n", VERSION);
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering return 0;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering case '?':
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering return -EINVAL;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering default:
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering assert_not_reached("Unhandled option");
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering }
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering }
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering return 1;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering}
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poetteringint main(int argc, char *argv[]) {
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering struct udev *udev;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering sigset_t mask;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering int fd_ctrl = -1;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering int fd_netlink = -1;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering int fd_worker = -1;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering struct epoll_event ep_ctrl = { .events = EPOLLIN };
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering struct epoll_event ep_inotify = { .events = EPOLLIN };
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering struct epoll_event ep_signal = { .events = EPOLLIN };
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering struct epoll_event ep_netlink = { .events = EPOLLIN };
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering struct epoll_event ep_worker = { .events = EPOLLIN };
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering int r = 0, one = 1;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering udev = udev_new();
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering if (!udev) {
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering r = log_error_errno(errno, "could not allocate udev context: %m");
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering goto exit;
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering }
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_set_target(LOG_TARGET_AUTO);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_parse_environment();
554604b3073467af75dc94fac9e2343148603289Lennart Poettering log_open();
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = parse_argv(argc, argv);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r <= 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto exit;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering r = parse_proc_cmdline(parse_proc_cmdline_item);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_warning_errno(r, "failed to parse kernel command line, ignoring: %m");
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (arg_debug)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_set_max_level(LOG_DEBUG);
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering if (getuid() != 0) {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering r = log_error_errno(EPERM, "root privileges required");
554604b3073467af75dc94fac9e2343148603289Lennart Poettering goto exit;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering r = mac_selinux_init("/dev");
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering if (r < 0) {
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering log_error_errno(r, "could not initialize labelling: %m");
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering goto exit;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* set umask before creating any file/directory */
554604b3073467af75dc94fac9e2343148603289Lennart Poettering r = chdir("/");
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (r < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = log_error_errno(errno, "could not change dir to /: %m");
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering goto exit;
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering umask(022);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_list_init(udev, &properties_list, true);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = mkdir("/run/udev", 0755);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0 && errno != EEXIST) {
ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602Lennart Poettering r = log_error_errno(errno, "could not create /run/udev: %m");
65d73cf042ba7de11d254f5c4714f467db64b7c3Lennart Poettering goto exit;
554604b3073467af75dc94fac9e2343148603289Lennart Poettering }
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering dev_setup(NULL);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering /* before opening new files, make sure std{in,out,err} fds are in a sane state */
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering if (arg_daemonize) {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering int fd;
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering fd = open("/dev/null", O_RDWR);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering if (fd >= 0) {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering if (write(STDOUT_FILENO, 0, 0) < 0)
554604b3073467af75dc94fac9e2343148603289Lennart Poettering dup2(fd, STDOUT_FILENO);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering if (write(STDERR_FILENO, 0, 0) < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering dup2(fd, STDERR_FILENO);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (fd > STDERR_FILENO)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering close(fd);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("cannot open /dev/null");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* get control and netlink socket from systemd */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering if (!udev_ctrl) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = log_error_errno(EINVAL, "error taking over udev control socket");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto exit;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (!monitor) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = log_error_errno(EINVAL, "error taking over netlink socket");
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering goto exit;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* get our own cgroup, we regularly kill everything udev has left behind */
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_cgroup = NULL;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering } else {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* open control and netlink socket */
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering udev_ctrl = udev_ctrl_new(udev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (!udev_ctrl) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = log_error_errno(EINVAL, "error initializing udev control socket");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto exit;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fd_ctrl = udev_ctrl_get_fd(udev_ctrl);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering monitor = udev_monitor_new_from_netlink(udev, "kernel");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!monitor) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = log_error_errno(EINVAL, "error initializing netlink socket");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto exit;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd_netlink = udev_monitor_get_fd(monitor);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_monitor_enable_receiving(monitor) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = log_error_errno(EINVAL, "error binding netlink socket");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto exit;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering r = log_error_errno(EINVAL, "error binding udev control socket");
a931ad47a8623163a29d898224d8a8c1177ffdafLennart Poettering goto exit;
a931ad47a8623163a29d898224d8a8c1177ffdafLennart Poettering }
a931ad47a8623163a29d898224d8a8c1177ffdafLennart Poettering
a931ad47a8623163a29d898224d8a8c1177ffdafLennart Poettering log_info("starting version " VERSION);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering udev_builtin_init(udev);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering
554604b3073467af75dc94fac9e2343148603289Lennart Poettering rules = udev_rules_new(udev, arg_resolve_names);
554604b3073467af75dc94fac9e2343148603289Lennart Poettering if (!rules) {
554604b3073467af75dc94fac9e2343148603289Lennart Poettering r = log_error_errno(ENOMEM, "error reading rules");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto exit;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = udev_rules_apply_static_dev_perms(rules);
86b8d289717bad2800342efca0a5023aa8374e9cLennart Poettering if (r < 0)
86b8d289717bad2800342efca0a5023aa8374e9cLennart Poettering log_error_errno(r, "failed to apply permissions on static device nodes: %m");
86b8d289717bad2800342efca0a5023aa8374e9cLennart Poettering
86b8d289717bad2800342efca0a5023aa8374e9cLennart Poettering if (arg_daemonize) {
c49b30a23583ff39daaa26696bcab478d2fee0bbLennart Poettering pid_t pid;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering pid = fork();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering switch (pid) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case -1:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = log_error_errno(errno, "fork of daemon failed: %m");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto exit;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering default:
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto exit_daemonize;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering setsid();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering write_string_file("/proc/self/oom_score_adj", "-1000");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_notify(1, "READY=1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (arg_children_max == 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering cpu_set_t cpu_set;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering arg_children_max = 8;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering arg_children_max += CPU_COUNT(&cpu_set) * 2;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_debug("set children_max to %u", arg_children_max);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_list_node_init(&event_list);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd_inotify = udev_watch_init(udev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (fd_inotify < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = log_error_errno(ENOMEM, "error initializing inotify");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering goto exit;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_watch_restore(udev);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* block and listen to all signals on signalfd */
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering sigfillset(&mask);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering if (fd_signal < 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = log_error_errno(errno, "error creating signalfd");
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering goto exit;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering }
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* unnamed socket from workers to the main daemon */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = log_error_errno(errno, "error creating socketpair");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto exit;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd_worker = worker_watch[READ_END];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = setsockopt(fd_worker, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (r < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering return log_error_errno(errno, "could not enable SO_PASSCRED: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ep_ctrl.data.fd = fd_ctrl;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ep_inotify.data.fd = fd_inotify;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ep_signal.data.fd = fd_signal;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ep_netlink.data.fd = fd_netlink;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ep_worker.data.fd = fd_worker;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd_ep = epoll_create1(EPOLL_CLOEXEC);
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering if (fd_ep < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(errno, "error creating epoll fd: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto exit;
de58a50e24a0d55e3bbcc77f8f6170a7322acf52Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error_errno(errno, "fail to add fds to epoll: %m");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto exit;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (;;) {
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering static usec_t last_usec;
de58a50e24a0d55e3bbcc77f8f6170a7322acf52Lennart Poettering struct epoll_event ev[8];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int fdcount;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int timeout;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int i;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (udev_exit) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* close sources of new events and discard buffered events */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (fd_ctrl >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd_ctrl = -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (monitor != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_monitor_unref(monitor);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering monitor = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (fd_inotify >= 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering close(fd_inotify);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd_inotify = -1;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* discard queued events and kill workers */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event_queue_cleanup(udev, EVENT_QUEUED);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering worker_kill();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* exit after all has cleaned up */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_list_node_is_empty(&event_list) && hashmap_isempty(workers))
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering break;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* timeout at exit for workers to finish */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering timeout = 30 * MSEC_PER_SEC;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering } else if (udev_list_node_is_empty(&event_list) && hashmap_isempty(workers)) {
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering /* we are idle */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering timeout = -1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* cleanup possible left-over processes in our cgroup */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_cgroup)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* kill idle or hanging workers */
9b420b3cfb8b93daf50e4cdbc92b05f2209ef893Lennart Poettering timeout = 3 * MSEC_PER_SEC;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* tell settle that we are busy or idle */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering event_queue_update();
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek if (fdcount < 0)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering continue;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek if (fdcount == 0) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering struct worker *worker;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering Iterator j;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* timeout */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_exit) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_error("timeout, giving up waiting for workers to finish");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering break;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* kill idle workers */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (udev_list_node_is_empty(&event_list)) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_debug("cleanup idle workers");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering worker_kill();
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* check for hanging events */
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek HASHMAP_FOREACH(worker, workers, j) {
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek struct event *event = worker->event;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering usec_t ts;
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (worker->state != WORKER_RUNNING)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering continue;
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert(event);
bd16acf35e13a19cd2ded0a0c2ef774a98f73808Zbigniew Jędrzejewski-Szmek
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers ts = now(CLOCK_MONOTONIC);
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers if ((ts - event->start_usec) > arg_event_timeout_warn_usec) {
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers if ((ts - event->start_usec) > arg_event_timeout_usec) {
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers log_error("worker ["PID_FMT"] %s timeout; kill it", worker->pid, event->devpath);
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers kill(worker->pid, SIGKILL);
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers worker->state = WORKER_KILLED;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers log_error("seq %llu '%s' killed", udev_device_get_seqnum(event->dev), event->devpath);
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers } else if (!event->warned) {
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers log_warning("worker ["PID_FMT"] %s is taking a long time", worker->pid, event->devpath);
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers event->warned = true;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers }
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers }
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers }
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers }
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers is_worker = is_signal = is_inotify = is_netlink = is_ctrl = false;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers for (i = 0; i < fdcount; i++) {
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers if (ev[i].data.fd == fd_worker && ev[i].events & EPOLLIN)
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers is_worker = true;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers else if (ev[i].data.fd == fd_netlink && ev[i].events & EPOLLIN)
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers is_netlink = true;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN)
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers is_signal = true;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers else if (ev[i].data.fd == fd_inotify && ev[i].events & EPOLLIN)
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers is_inotify = true;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers else if (ev[i].data.fd == fd_ctrl && ev[i].events & EPOLLIN)
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers is_ctrl = true;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers }
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers /* check for changed config, every 3 seconds at most */
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * USEC_PER_SEC) {
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers if (udev_rules_check_timestamp(rules))
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers reload = true;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers if (udev_builtin_validate(udev))
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers reload = true;
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers last_usec = now(CLOCK_MONOTONIC);
ab49725fd8587ef2b90dd0a67b2c915bc772d089Kay Sievers }
/* reload requested, HUP signal received, rules changed, builtin changed */
if (reload) {
worker_kill();
rules = udev_rules_unref(rules);
udev_builtin_exit(udev);
reload = false;
}
/* event has finished */
if (is_worker)
on_worker(NULL, fd_worker, 0, NULL);
/* uevent from kernel */
if (is_netlink)
on_uevent(NULL, fd_netlink, 0, monitor);
/* start new events */
if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) {
udev_builtin_init(udev);
if (rules == NULL)
rules = udev_rules_new(udev, arg_resolve_names);
if (rules != NULL)
event_queue_start(udev);
}
if (is_signal) {
struct signalfd_siginfo fdsi;
ssize_t size;
size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
if (size == sizeof(struct signalfd_siginfo)) {
switch (fdsi.ssi_signo) {
case SIGINT:
case SIGTERM:
on_request_exit(NULL, &fdsi, NULL);
break;
case SIGHUP:
on_request_reload(NULL, &fdsi, NULL);
break;
case SIGCHLD:
on_sigchld(NULL, &fdsi, NULL);
break;
}
}
}
/* we are shutting down, the events below are not handled anymore */
if (udev_exit)
continue;
/* device node watch */
if (is_inotify)
on_inotify(NULL, fd_inotify, 0, udev);
/*
* This needs to be after the inotify handling, to make sure,
* that the ping is send back after the possibly generated
* "change" events by the inotify device node watch.
*/
if (is_ctrl)
on_ctrl_msg(NULL, fd_ctrl, 0, udev_ctrl);
}
exit:
udev_ctrl_cleanup(udev_ctrl);
unlink("/run/udev/queue");
exit_daemonize:
if (fd_ep >= 0)
close(fd_ep);
workers_free();
event_queue_cleanup(udev, EVENT_UNDEF);
udev_rules_unref(rules);
udev_builtin_exit(udev);
if (fd_signal >= 0)
close(fd_signal);
if (worker_watch[READ_END] >= 0)
close(worker_watch[READ_END]);
if (worker_watch[WRITE_END] >= 0)
close(worker_watch[WRITE_END]);
udev_monitor_unref(monitor);
udev_ctrl_connection_unref(udev_ctrl_conn);
udev_ctrl_unref(udev_ctrl);
udev_list_cleanup(&properties_list);
mac_selinux_finish();
udev_unref(udev);
log_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}