udevd.c revision e9fc29f4ecc9509ccc02eb8a014341e26c0d7831
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 <ctype.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <fcntl.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <time.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <getopt.h>
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include <dirent.h>
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering#include <sys/file.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/time.h>
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include <sys/prctl.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <sys/socket.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/un.h>
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include <sys/signalfd.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <sys/epoll.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <sys/mount.h>
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include <sys/poll.h>
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include <sys/wait.h>
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include <sys/stat.h>
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include <sys/ioctl.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <sys/inotify.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <sys/utsname.h>
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include "udev.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "udev-util.h"
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering#include "sd-daemon.h"
a761c1ca851a9397b5a207ef600e077d0f7f4534Lennart Poettering#include "cgroup-util.h"
019036a47fcd10fcf0286800d144c706f3773e2fLennart Poettering#include "dev-setup.h"
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering#include "fileio.h"
b2b796b8ab5565fbe60b544d2579e2bfca31bf6aLennart Poettering
b2b796b8ab5565fbe60b544d2579e2bfca31bf6aLennart Poetteringstatic bool debug;
b2b796b8ab5565fbe60b544d2579e2bfca31bf6aLennart Poettering
91adc4db33f69606aabd332813a5d7d5751c859fLennart Poetteringvoid udev_main_log(struct udev *udev, int priority,
91adc4db33f69606aabd332813a5d7d5751c859fLennart Poettering const char *file, int line, const char *fn,
91adc4db33f69606aabd332813a5d7d5751c859fLennart Poettering const char *format, va_list args)
3bbdc31df37a23b5134a115c01d15e7ff870b3ccLennart Poettering{
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering log_metav(priority, file, line, fn, format, args);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringstatic struct udev_rules *rules;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic struct udev_ctrl *udev_ctrl;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic struct udev_monitor *monitor;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic int worker_watch[2] = { -1, -1 };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int fd_signal = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int fd_ep = -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int fd_inotify = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool stop_exec_queue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool reload;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int children;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int children_max;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poetteringstatic int exec_delay;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringstatic sigset_t sigmask_orig;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic UDEV_LIST(event_list);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic UDEV_LIST(worker_list);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic char *udev_cgroup;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool udev_exit;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poetteringenum event_state {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering EVENT_UNDEF,
ef9fb66c0b292d3543c16bfce99ad677bef0f401Lennart Poettering EVENT_QUEUED,
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering EVENT_RUNNING,
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering};
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstruct event {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering struct udev_list_node node;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct udev *udev;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering struct udev_device *dev;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering enum event_state state;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering int exitcode;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering unsigned long long int delaying_seqnum;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering unsigned long long int seqnum;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering const char *devpath;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering size_t devpath_len;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering const char *devpath_old;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering dev_t devnum;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int ifindex;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering bool is_block;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering#ifdef HAVE_FIRMWARE
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering bool nodelay;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering#endif
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering};
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node)
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering{
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return container_of(node, struct event, node);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart 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};
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstruct worker {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering struct udev_list_node node;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering struct udev *udev;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering int refcount;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering pid_t pid;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering struct udev_monitor *monitor;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering enum worker_state state;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering struct event *event;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering usec_t event_start_usec;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering};
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering/* passed from worker to main process */
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poetteringstruct worker_message {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid_t pid;
51323288fc628a5cac50914df915545d685b793eLennart Poettering int exitcode;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering};
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline struct worker *node_to_worker(struct udev_list_node *node)
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering{
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering return container_of(node, struct worker, node);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering}
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void event_queue_delete(struct event *event)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_remove(&event->node);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_device_unref(event->dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering free(event);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic struct worker *worker_ref(struct worker *worker)
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->refcount++;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return worker;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering}
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic void worker_cleanup(struct worker *worker)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering udev_list_node_remove(&worker->node);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_monitor_unref(worker->monitor);
51323288fc628a5cac50914df915545d685b793eLennart Poettering children--;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering free(worker);
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void worker_unref(struct worker *worker)
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->refcount--;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (worker->refcount > 0)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_debug("worker [%u] cleaned up", worker->pid);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering worker_cleanup(worker);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering}
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void worker_list_cleanup(struct udev *udev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering struct udev_list_node *loop, *tmp;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach_safe(loop, tmp, &worker_list) {
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering struct worker *worker = node_to_worker(loop);
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker_cleanup(worker);
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poetteringstatic void worker_new(struct event *event)
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering{
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering struct udev *udev = event->udev;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct worker *worker;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_monitor *worker_monitor;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering pid_t pid;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* listen for new events */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if (worker_monitor == NULL)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering return;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* allow the main daemon netlink address to send devices to the worker */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering udev_monitor_allow_unicast_sender(worker_monitor, monitor);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_enable_receiving(worker_monitor);
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker = new0(struct worker, 1);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker == NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_unref(worker_monitor);
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering return;
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering }
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering /* worker + event reference */
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering worker->refcount = 2;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering worker->udev = udev;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering pid = fork();
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering switch (pid) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt case 0: {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_device *dev = NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int fd_monitor;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct epoll_event ep_signal, ep_monitor;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sigset_t mask;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int rc = EXIT_SUCCESS;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* take initial device from queue */
51323288fc628a5cac50914df915545d685b793eLennart Poettering dev = event->dev;
51323288fc628a5cac50914df915545d685b793eLennart Poettering event->dev = NULL;
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering free(worker);
51323288fc628a5cac50914df915545d685b793eLennart Poettering worker_list_cleanup(udev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event_queue_cleanup(udev, EVENT_UNDEF);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_monitor_unref(monitor);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_ctrl_unref(udev_ctrl);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering close(fd_signal);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering close(fd_ep);
51323288fc628a5cac50914df915545d685b793eLennart Poettering close(worker_watch[READ_END]);
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering sigfillset(&mask);
51323288fc628a5cac50914df915545d685b793eLennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering if (fd_signal < 0) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("error creating signalfd %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering rc = 2;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto out;
51323288fc628a5cac50914df915545d685b793eLennart Poettering }
51323288fc628a5cac50914df915545d685b793eLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd_ep = epoll_create1(EPOLL_CLOEXEC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fd_ep < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("error creating epoll fd: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering rc = 3;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto out;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering memzero(&ep_signal, sizeof(struct epoll_event));
51323288fc628a5cac50914df915545d685b793eLennart Poettering ep_signal.events = EPOLLIN;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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));
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering ep_monitor.events = EPOLLIN;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ep_monitor.data.fd = fd_monitor;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("fail to add fds to epoll: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering rc = 4;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto out;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* request TERM signal if parent exits */
51323288fc628a5cac50914df915545d685b793eLennart Poettering prctl(PR_SET_PDEATHSIG, SIGTERM);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* reset OOM score, we only protect the main daemon */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering write_string_file("/proc/self/oom_score_adj", "0");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering for (;;) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_event *udev_event;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct worker_message msg;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int fd_lock = -1;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering int err = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("seq %llu running", udev_device_get_seqnum(dev));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_event = udev_event_new(dev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (udev_event == NULL) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering rc = 5;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto out;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
966c66e34940001a40806142ecebaae61b478444Lennart Poettering
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering /* needed for SIGCHLD/SIGTERM in spawn() */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_event->fd_signal = fd_signal;
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (exec_delay > 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_event->exec_delay = exec_delay;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /*
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * Take a "read lock" on the device node; this establishes
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * a concept of device "ownership" to serialize device
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * access. External processes holding a "write lock" will
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * cause udev to skip the event handling; in the case udev
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * acquired the lock, the external process will block until
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * udev has finished its event handling.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering /*
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * <kabi_> since we make check - device seems unused - we try
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * ioctl to deactivate - and device is found to be opened
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * <kay> sure, you try to take a write lock
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * <kay> if you get it udev is out
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * <kay> if you can't get it, udev is busy
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * <kabi_> we cannot deactivate openned device (as it is in-use)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * <kay> maybe we should just exclude dm from that thing entirely
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering * <kabi_> IMHO this sounds like a good plan for this moment
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering !startswith("dm-", udev_device_get_sysname(dev))) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct udev_device *d = dev;
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen if (streq_ptr("partition", udev_device_get_devtype(d)))
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering d = udev_device_get_parent(d);
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen if (d) {
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen log_debug("Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen err = -EWOULDBLOCK;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd_lock = safe_close(fd_lock);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering goto skip;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* apply rules, create node, symlinks */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering udev_event_execute_rules(udev_event, rules, &sigmask_orig);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_event_execute_run(udev_event, &sigmask_orig);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* apply/restore inotify watch */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (udev_event->inotify_watch) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_watch_begin(udev, dev);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_device_update_db(dev);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering safe_close(fd_lock);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* send processed event back to libudev listeners */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_monitor_send_device(worker_monitor, NULL, dev);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringskip:
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* send udevd the result of the event execution */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memzero(&msg, sizeof(struct worker_message));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering msg.exitcode = err;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering msg.pid = getpid();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("seq %llu processed with %i", udev_device_get_seqnum(dev), err);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_device_unref(dev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dev = NULL;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if (udev_event->sigterm) {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering udev_event_unref(udev_event);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto out;
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering }
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_event_unref(udev_event);
51323288fc628a5cac50914df915545d685b793eLennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* wait for more device messages from main udevd, or term signal */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while (dev == NULL) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct epoll_event ev[4];
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int fdcount;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt int i;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fdcount < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (errno == EINTR)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("failed to poll: %m");
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering goto out;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering for (i = 0; i < fdcount; i++) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering switch (fdsi.ssi_signo) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering case SIGTERM:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto out;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringout:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_device_unref(dev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering safe_close(fd_signal);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering safe_close(fd_ep);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering close(fd_inotify);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering close(worker_watch[WRITE_END]);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_rules_unref(rules);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_builtin_exit(udev);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_monitor_unref(worker_monitor);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_unref(udev);
51323288fc628a5cac50914df915545d685b793eLennart Poettering log_close();
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering exit(rc);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case -1:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering udev_monitor_unref(worker_monitor);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->state = EVENT_QUEUED;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(worker);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("fork of child failed: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering default:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* close monitor, but keep address around */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_monitor_disconnect(worker_monitor);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->monitor = worker_monitor;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->pid = pid;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->state = WORKER_RUNNING;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->event_start_usec = now(CLOCK_MONOTONIC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->event = event;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering event->state = EVENT_RUNNING;
966c66e34940001a40806142ecebaae61b478444Lennart Poettering udev_list_node_append(&worker->node, &worker_list);
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering children++;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("seq %llu forked new worker [%u]", udev_device_get_seqnum(event->dev), pid);
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering break;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void event_run(struct event *event)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_list_node *loop;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach(loop, &worker_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct worker *worker = node_to_worker(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ssize_t count;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker->state != WORKER_IDLE)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (count < 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] did not accept message %zi (%m), kill it", worker->pid, count);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering kill(worker->pid, SIGKILL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->state = WORKER_KILLED;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_ref(worker);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->event = event;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->state = WORKER_RUNNING;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->event_start_usec = now(CLOCK_MONOTONIC);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->state = EVENT_RUNNING;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
4e2d538f33df8a425487aaa4facc23065a9bdaf7Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (children >= children_max) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (children_max > 1)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("maximum number (%i) of children reached", children);
4e2d538f33df8a425487aaa4facc23065a9bdaf7Lennart Poettering return;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* start new worker and pass initial device */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_new(event);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int event_queue_insert(struct udev_device *dev)
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering{
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering struct event *event;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event = new0(struct event, 1);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (event == NULL)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -1;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->udev = udev_device_get_udev(dev);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->dev = dev;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering event->seqnum = udev_device_get_seqnum(dev);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->devpath = udev_device_get_devpath(dev);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart 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);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->is_block = streq("block", udev_device_get_subsystem(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->ifindex = udev_device_get_ifindex(dev);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering#ifdef HAVE_FIRMWARE
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (streq(udev_device_get_subsystem(dev), "firmware"))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->nodelay = true;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering#endif
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev),
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_get_action(dev), udev_device_get_subsystem(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->state = EVENT_QUEUED;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering udev_list_node_append(&event->node, &event_list);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void worker_kill(struct udev *udev)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering{
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering struct udev_list_node *loop;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering udev_list_node_foreach(loop, &worker_list) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering struct worker *worker = node_to_worker(loop);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (worker->state == WORKER_KILLED)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering continue;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering worker->state = WORKER_KILLED;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering kill(worker->pid, SIGTERM);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering}
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering/* lookup event for identical, parent, child device */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringstatic bool is_devpath_busy(struct event *event)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering{
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering struct udev_list_node *loop;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering size_t common;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart 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)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* event we checked earlier still exists, no need to check again */
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (loop_event->seqnum == event->delaying_seqnum)
51323288fc628a5cac50914df915545d685b793eLennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* found ourself, no later event can block us */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (loop_event->seqnum >= event->seqnum)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* check network device ifindex */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* check our old name */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
51323288fc628a5cac50914df915545d685b793eLennart Poettering }
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* compare devpath */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering common = MIN(loop_event->devpath_len, event->devpath_len);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* one devpath is contained in the other? */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (memcmp(loop_event->devpath, event->devpath, common) != 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart 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))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->delaying_seqnum = loop_event->seqnum;
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering return true;
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering }
c463eb783e5ad999d400180c69b912c54fa07ee1Lennart Poettering
eee026a7ba16336b6493828a2a13ddc9908667ffLennart Poettering#ifdef HAVE_FIRMWARE
d0129ddb9fbb07bed7c8ea51b8031f824bf506fbLennart Poettering /* allow to bypass the dependency tracking */
eee026a7ba16336b6493828a2a13ddc9908667ffLennart Poettering if (event->nodelay)
c463eb783e5ad999d400180c69b912c54fa07ee1Lennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering#endif
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* parent device event found */
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (event->devpath[common] == '/') {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* child device event found */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (loop_event->devpath[common] == '/') {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* no matching device */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void event_queue_start(struct udev *udev)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering{
966c66e34940001a40806142ecebaae61b478444Lennart Poettering struct udev_list_node *loop;
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach(loop, &event_list) {
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering struct event *event = node_to_event(loop);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart 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{
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
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event_queue_delete(event);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void worker_returned(int fd_worker)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (;;) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering struct worker_message msg;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ssize_t size;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_list_node *loop;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering size = recv(fd_worker, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (size != sizeof(struct worker_message))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* lookup worker who sent the signal */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_list_node_foreach(loop, &worker_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct worker *worker = node_to_worker(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker->pid != msg.pid)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* worker returned */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker->event) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering worker->event->exitcode = msg.exitcode;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event_queue_delete(worker->event);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->event = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker->state != WORKER_KILLED)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->state = WORKER_IDLE;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_unref(worker);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering/* receive the udevd message from userspace */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev *udev = udev_ctrl_get_udev(uctrl);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_ctrl_connection *ctrl_conn;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_ctrl_msg *ctrl_msg = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering const char *str;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int i;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ctrl_conn = udev_ctrl_get_connection(uctrl);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (ctrl_conn == NULL)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto out;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ctrl_msg = udev_ctrl_receive_msg(ctrl_conn);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (ctrl_msg == NULL)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto out;
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 udev_set_log_priority(udev, i);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_kill(udev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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_add_property(udev, key, NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (ENV) received, set '%s=%s'", key, val);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_add_property(udev, 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(udev);
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 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) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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_connection_ref(ctrl_conn);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringout:
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_ctrl_msg_unref(ctrl_msg);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return udev_ctrl_connection_unref(ctrl_conn);
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("dm-", udev_device_get_sysname(dev))) {
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
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /*
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * Try to re-read the partition table. This only succeeds if
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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_EXCL|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (fd >= 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = ioctl(fd, BLKRRPART, 0);
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
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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
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 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
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (!streq_ptr("partition", udev_device_get_devtype(d)))
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering continue;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_debug("device %s closed, synthesising partition '%s' 'change'",
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_device_get_devnode(dev), udev_device_get_devnode(d));
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering write_string_file(filename, "change");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return 0;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering write_string_file(filename, "change");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return 0;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int handle_inotify(struct udev *udev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering int nbytes, pos;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering char *buf;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct inotify_event *ev;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int r;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = ioctl(fd_inotify, FIONREAD, &nbytes);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (r < 0 || nbytes <= 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return -errno;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering buf = malloc(nbytes);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!buf) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error getting buffer for inotify");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return -ENOMEM;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering nbytes = read(fd_inotify, buf, nbytes);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_device *dev;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering ev = (struct inotify_event *)(buf + pos);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering dev = udev_watch_lookup(udev, ev->wd);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!dev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("inotify event: %x for %s", ev->mask, udev_device_get_devnode(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (ev->mask & IN_CLOSE_WRITE)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering synthesize_change(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering else if (ev->mask & IN_IGNORED)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_watch_end(udev, dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_unref(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering free(buf);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return 0;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void handle_signal(struct udev *udev, int signo)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering switch (signo) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case SIGINT:
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case SIGTERM:
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_exit = true;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case SIGCHLD:
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (;;) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid_t pid;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int status;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_list_node *loop, *tmp;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid = waitpid(-1, &status, WNOHANG);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (pid <= 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach_safe(loop, tmp, &worker_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct worker *worker = node_to_worker(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker->pid != pid)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("worker [%u] exit", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (WIFEXITED(status)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (WEXITSTATUS(status) != 0)
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen log_error("worker [%u] exit with return code %i",
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid, WEXITSTATUS(status));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (WIFSIGNALED(status)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] terminated by signal %i (%s)",
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (WIFSTOPPED(status)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] stopped", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (WIFCONTINUED(status)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] continued", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] exit with status 0x%04x", pid, status);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker->event) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] failed while handling '%s'",
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid, worker->event->devpath);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->event->exitcode = -32;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event_queue_delete(worker->event);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* drop reference taken for state 'running' */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering worker_unref(worker);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_unref(worker);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case SIGHUP:
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering reload = true;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return -1;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (ctrl < 0 || netlink < 0)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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/*
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * read the kernel commandline, in case we need to get into debug mode
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * udev.log-priority=<level> syslog priority
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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 */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void kernel_cmdline_options(struct udev *udev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen _cleanup_free_ char *line = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering char *w, *state;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering size_t l;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering int r;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering r = proc_cmdline(&line);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (r < 0)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (r <= 0)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering FOREACH_WORD_QUOTED(w, l, line, state) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering char *s, *opt;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
9a1f0c283bac56eb26a2fa02d62e0e9ece82f25aLennart Poettering s = strndup(w, l);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (!s)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering break;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* accept the same options for the initrd, prefixed with "rd." */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (in_initrd() && startswith(s, "rd."))
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering opt = s + 3;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering else
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering opt = s;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (startswith(opt, "udev.log-priority=")) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering int prio;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering prio = util_log_priority(opt + 18);
9a1f0c283bac56eb26a2fa02d62e0e9ece82f25aLennart Poettering log_set_max_level(prio);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_set_log_priority(udev, prio);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering } else if (startswith(opt, "udev.children-max=")) {
9a1f0c283bac56eb26a2fa02d62e0e9ece82f25aLennart Poettering children_max = strtoul(opt + 18, NULL, 0);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering } else if (startswith(opt, "udev.exec-delay=")) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering exec_delay = strtoul(opt + 16, NULL, 0);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering free(s);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering}
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringint main(int argc, char *argv[])
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev *udev;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering sigset_t mask;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering int daemonize = false;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int resolve_names = 1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering static const struct option options[] = {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "daemon", no_argument, NULL, 'd' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "debug", no_argument, NULL, 'D' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "children-max", required_argument, NULL, 'c' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "exec-delay", required_argument, NULL, 'e' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "resolve-names", required_argument, NULL, 'N' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering { "help", no_argument, NULL, 'h' },
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering { "version", no_argument, NULL, 'V' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering {}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering };
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int fd_ctrl = -1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int fd_netlink = -1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int fd_worker = -1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct epoll_event ep_ctrl, ep_inotify, ep_signal, ep_netlink, ep_worker;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_ctrl_connection *ctrl_conn = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int rc = 1;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev = udev_new();
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev == NULL)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_set_target(LOG_TARGET_AUTO);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_parse_environment();
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_open();
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_set_log_fn(udev, udev_main_log);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_set_max_level(udev_get_log_priority(udev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("version %s", VERSION);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering label_init("/dev");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (;;) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int option;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering option = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (option == -1)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering switch (option) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering case 'd':
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering daemonize = true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'c':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering children_max = strtoul(optarg, NULL, 0);
7e8131e9c6c150732503899a092206578fdc13deLennart Poettering break;
7e8131e9c6c150732503899a092206578fdc13deLennart Poettering case 'e':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering exec_delay = strtoul(optarg, NULL, 0);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'D':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering debug = true;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_set_max_level(LOG_DEBUG);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_set_log_priority(udev, LOG_DEBUG);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'N':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (streq(optarg, "early")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering resolve_names = 1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (streq(optarg, "late")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering resolve_names = 0;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (streq(optarg, "never")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering resolve_names = -1;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering } else {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fprintf(stderr, "resolve-names must be early, late or never\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("resolve-names must be early, late or never");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'h':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering printf("Usage: udevd OPTIONS\n"
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering " --daemon\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --debug\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --children-max=<maximum number of workers>\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --exec-delay=<seconds to wait before executing RUN=>\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --resolve-names=early|late|never\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --version\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering " --help\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering "\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'V':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering printf("%s\n", VERSION);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering default:
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering kernel_cmdline_options(udev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (getuid() != 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fprintf(stderr, "root privileges required\n");
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_error("root privileges required");
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* set umask before creating any file/directory */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering chdir("/");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering umask(022);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering mkdir("/run/udev", 0755);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering dev_setup(NULL);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* before opening new files, make sure std{in,out,err} fds are in a sane state */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (daemonize) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering int fd;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd = open("/dev/null", O_RDWR);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (fd >= 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (write(STDOUT_FILENO, 0, 0) < 0)
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering dup2(fd, STDOUT_FILENO);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (write(STDERR_FILENO, 0, 0) < 0)
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering dup2(fd, STDERR_FILENO);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (fd > STDERR_FILENO)
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering close(fd);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering } else {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "cannot open /dev/null\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("cannot open /dev/null");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* get control and netlink socket from systemd */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (udev_ctrl == NULL) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error taking over udev control socket");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 1;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (monitor == NULL) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error taking over netlink socket");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 3;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* get our own cgroup, we regularly kill everything udev has left behind */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_cgroup = NULL;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering } else {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* open control and netlink socket */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_ctrl = udev_ctrl_new(udev);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (udev_ctrl == NULL) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error initializing udev control socket");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error initializing udev control socket");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 1;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd_ctrl = udev_ctrl_get_fd(udev_ctrl);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering monitor = udev_monitor_new_from_netlink(udev, "kernel");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (monitor == NULL) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error initializing netlink socket\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error initializing netlink socket");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 3;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd_netlink = udev_monitor_get_fd(monitor);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (udev_monitor_enable_receiving(monitor) < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error binding netlink socket\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error binding netlink socket");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 3;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error binding udev control socket\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error binding udev control socket");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 1;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (daemonize) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering pid_t pid;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering pid = fork();
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering switch (pid) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering case 0:
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering break;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering case -1:
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("fork of daemon failed: %m");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 4;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering default:
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = EXIT_SUCCESS;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit_daemonize;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering setsid();
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering write_string_file("/proc/self/oom_score_adj", "-1000");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering } else {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering sd_notify(1, "READY=1");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering print_kmsg("starting version " VERSION "\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (!debug) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering int fd;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fd = open("/dev/null", O_RDWR);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (fd >= 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering dup2(fd, STDIN_FILENO);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering dup2(fd, STDOUT_FILENO);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering dup2(fd, STDERR_FILENO);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering close(fd);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fd_inotify = udev_watch_init(udev);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (fd_inotify < 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fprintf(stderr, "error initializing inotify\n");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("error initializing inotify");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering rc = 4;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering goto exit;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering udev_watch_restore(udev);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* block and listen to all signals on signalfd */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering sigfillset(&mask);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (fd_signal < 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fprintf(stderr, "error creating signalfd\n");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("error creating signalfd");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering rc = 5;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering goto exit;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* unnamed socket from workers to the main daemon */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fprintf(stderr, "error creating socketpair\n");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("error creating socketpair");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering rc = 6;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering goto exit;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fd_worker = worker_watch[READ_END];
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering udev_builtin_init(udev);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering rules = udev_rules_new(udev, resolve_names);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (rules == NULL) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("error reading rules");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering goto exit;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering memzero(&ep_ctrl, sizeof(struct epoll_event));
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_ctrl.events = EPOLLIN;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_ctrl.data.fd = fd_ctrl;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering memzero(&ep_inotify, sizeof(struct epoll_event));
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_inotify.events = EPOLLIN;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_inotify.data.fd = fd_inotify;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering memzero(&ep_signal, sizeof(struct epoll_event));
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_signal.events = EPOLLIN;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_signal.data.fd = fd_signal;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering memzero(&ep_netlink, sizeof(struct epoll_event));
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_netlink.events = EPOLLIN;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_netlink.data.fd = fd_netlink;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering memzero(&ep_worker, sizeof(struct epoll_event));
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_worker.events = EPOLLIN;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering ep_worker.data.fd = fd_worker;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fd_ep = epoll_create1(EPOLL_CLOEXEC);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (fd_ep < 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("error creating epoll fd: %m");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering goto exit;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 ||
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 ||
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 ||
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("fail to add fds to epoll: %m");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering goto exit;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (children_max <= 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cpu_set_t cpu_set;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering children_max = 8;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering children_max += CPU_COUNT(&cpu_set) * 2;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering log_debug("set children_max to %u", children_max);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering rc = udev_rules_apply_static_dev_perms(rules);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (rc < 0)
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("failed to apply permissions on static device nodes - %s", strerror(-rc));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_init(&event_list);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_init(&worker_list);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (;;) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering static usec_t last_usec;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct epoll_event ev[8];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int fdcount;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int timeout;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int i;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (udev_exit) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* close sources of new events and discard buffered events */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fd_ctrl >= 0) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering fd_ctrl = -1;
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering }
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering if (monitor != NULL) {
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering udev_monitor_unref(monitor);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering monitor = NULL;
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering }
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering if (fd_inotify >= 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering close(fd_inotify);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering fd_inotify = -1;
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering }
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering /* discard queued events and kill workers */
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering event_queue_cleanup(udev, EVENT_QUEUED);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering worker_kill(udev);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering /* exit after all has cleaned up */
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering if (udev_list_node_is_empty(&event_list) && children == 0)
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering break;
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* timeout at exit for workers to finish */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering timeout = 30 * MSEC_PER_SEC;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (udev_list_node_is_empty(&event_list) && children == 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* we are idle */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering timeout = -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* cleanup possible left-over processes in our cgroup */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (udev_cgroup)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* kill idle or hanging workers */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering timeout = 3 * MSEC_PER_SEC;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt /* tell settle that we are busy or idle */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!udev_list_node_is_empty(&event_list)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int fd;
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt fd = open("/run/udev/queue", O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fd >= 0)
aa4a9deb7d3db95ffb1fd18791be66f58d06a69eLennart Poettering close(fd);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unlink("/run/udev/queue");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
4d1cf1e229c9bdbc44778c0863eaad918f5724e7Lennart Poettering
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (fdcount < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (fdcount == 0) {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt struct udev_list_node *loop;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* timeout */
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt if (udev_exit) {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt log_error("timeout, giving up waiting for workers to finish");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering }
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering /* kill idle workers */
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering if (udev_list_node_is_empty(&event_list)) {
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering log_debug("cleanup idle workers");
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering worker_kill(udev);
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering }
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering /* check for hanging events */
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt udev_list_node_foreach(loop, &worker_list) {
902bb5d8abb2a7d258741828d212ca549ab16950Lennart Poettering struct worker *worker = node_to_worker(loop);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (worker->state != WORKER_RUNNING)
continue;
if ((now(CLOCK_MONOTONIC) - worker->event_start_usec) > 30 * USEC_PER_SEC) {
log_error("worker [%u] %s timeout; kill it", worker->pid,
worker->event ? worker->event->devpath : "<idle>");
kill(worker->pid, SIGKILL);
worker->state = WORKER_KILLED;
/* drop reference taken for state 'running' */
worker_unref(worker);
if (worker->event) {
log_error("seq %llu '%s' killed", udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
worker->event->exitcode = -64;
event_queue_delete(worker->event);
worker->event = NULL;
}
}
}
}
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(udev);
rules = udev_rules_unref(rules);
udev_builtin_exit(udev);
reload = false;
}
/* event has finished */
if (is_worker)
worker_returned(fd_worker);
if (is_netlink) {
struct udev_device *dev;
dev = udev_monitor_receive_device(monitor);
if (dev != NULL) {
udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC));
if (event_queue_insert(dev) < 0)
udev_device_unref(dev);
}
}
/* 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, 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))
handle_signal(udev, fdsi.ssi_signo);
}
/* we are shutting down, the events below are not handled anymore */
if (udev_exit)
continue;
/* device node watch */
if (is_inotify)
handle_inotify(udev);
/*
* This needs to be after the inotify handling, to make sure,
* that the ping is send back after the possibly generated
* "change" events by the inotify device node watch.
*
* 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)
ctrl_conn = handle_ctrl_msg(udev_ctrl);
}
rc = EXIT_SUCCESS;
exit:
udev_ctrl_cleanup(udev_ctrl);
unlink("/run/udev/queue");
exit_daemonize:
if (fd_ep >= 0)
close(fd_ep);
worker_list_cleanup(udev);
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(ctrl_conn);
udev_ctrl_unref(udev_ctrl);
label_finish();
udev_unref(udev);
log_close();
return rc;
}