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