udevd.c revision 689a97f52383110bf0da049e8f6294993f4020dd
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/time.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/prctl.h>
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include <sys/socket.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <sys/un.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/signalfd.h>
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include <sys/epoll.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <sys/poll.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include <sys/wait.h>
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include <sys/stat.h>
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include <sys/ioctl.h>
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include <sys/inotify.h>
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include <sys/utsname.h>
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#include "udev.h"
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include "sd-daemon.h"
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering#include "cgroup-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "dev-setup.h"
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering#include "fileio.h"
a761c1ca851a9397b5a207ef600e077d0f7f4534Lennart Poettering
019036a47fcd10fcf0286800d144c706f3773e2fLennart Poetteringstatic bool debug;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
b2b796b8ab5565fbe60b544d2579e2bfca31bf6aLennart Poetteringvoid udev_main_log(struct udev *udev, int priority,
b2b796b8ab5565fbe60b544d2579e2bfca31bf6aLennart Poettering const char *file, int line, const char *fn,
b2b796b8ab5565fbe60b544d2579e2bfca31bf6aLennart Poettering const char *format, va_list args)
91adc4db33f69606aabd332813a5d7d5751c859fLennart Poettering{
91adc4db33f69606aabd332813a5d7d5751c859fLennart Poettering log_metav(priority, file, line, fn, format, args);
91adc4db33f69606aabd332813a5d7d5751c859fLennart Poettering}
3bbdc31df37a23b5134a115c01d15e7ff870b3ccLennart Poettering
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poetteringstatic struct udev_rules *rules;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic struct udev_queue_export *udev_queue_export;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic struct udev_ctrl *udev_ctrl;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringstatic struct udev_monitor *monitor;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int worker_watch[2] = { -1, -1 };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int fd_signal = -1;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic int fd_ep = -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int fd_inotify = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool stop_exec_queue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool reload;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int children;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int children_max;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int exec_delay;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic sigset_t sigmask_orig;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poetteringstatic UDEV_LIST(event_list);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringstatic UDEV_LIST(worker_list);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringchar *udev_cgroup;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool udev_exit;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringenum event_state {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering EVENT_UNDEF,
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering EVENT_QUEUED,
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering EVENT_RUNNING,
ef9fb66c0b292d3543c16bfce99ad677bef0f401Lennart Poettering};
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstruct event {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering struct udev_list_node node;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering struct udev *udev;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering struct udev_device *dev;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering enum event_state state;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int exitcode;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering unsigned long long int delaying_seqnum;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering unsigned long long int seqnum;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering const char *devpath;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering size_t devpath_len;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering const char *devpath_old;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering dev_t devnum;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int ifindex;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering bool is_block;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering#ifdef HAVE_FIRMWARE
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering bool nodelay;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering#endif
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering};
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node)
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering{
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return container_of(node, struct event, node);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering}
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state type);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringenum worker_state {
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering WORKER_UNDEF,
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering WORKER_RUNNING,
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering WORKER_IDLE,
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering WORKER_KILLED,
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering};
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstruct worker {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart 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};
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering/* passed from worker to main process */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstruct worker_message {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid_t pid;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering int exitcode;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering};
51323288fc628a5cac50914df915545d685b793eLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline struct worker *node_to_worker(struct udev_list_node *node)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering{
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return container_of(node, struct worker, node);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering}
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic void event_queue_delete(struct event *event, bool export)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering{
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_remove(&event->node);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (export) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_queue_export_device_finished(udev_queue_export, event->dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_unref(event->dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering free(event);
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic struct worker *worker_ref(struct worker *worker)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering{
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering worker->refcount++;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering return worker;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poetteringstatic void worker_cleanup(struct worker *worker)
51323288fc628a5cac50914df915545d685b793eLennart Poettering{
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering udev_list_node_remove(&worker->node);
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering udev_monitor_unref(worker->monitor);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering children--;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering free(worker);
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringstatic void worker_unref(struct worker *worker)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering{
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering worker->refcount--;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (worker->refcount > 0)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering log_debug("worker [%u] cleaned up\n", worker->pid);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering worker_cleanup(worker);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void worker_list_cleanup(struct udev *udev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering struct udev_list_node *loop, *tmp;
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_foreach_safe(loop, tmp, &worker_list) {
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering struct worker *worker = node_to_worker(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering worker_cleanup(worker);
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering }
3339cb71d44c5198f9546f113674f06dc7b01a6fLennart Poettering}
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void worker_new(struct event *event)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering struct udev *udev = event->udev;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct worker *worker;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct udev_monitor *worker_monitor;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering pid_t pid;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* listen for new events */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if (worker_monitor == NULL)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return;
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering /* allow the main daemon netlink address to send devices to the worker */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_allow_unicast_sender(worker_monitor, monitor);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_enable_receiving(worker_monitor);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering worker = calloc(1, sizeof(struct worker));
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering if (worker == NULL) {
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering udev_monitor_unref(worker_monitor);
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering return;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* worker + event reference */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->refcount = 2;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering worker->udev = udev;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid = fork();
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering switch (pid) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case 0: {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct udev_device *dev = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int fd_monitor;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct epoll_event ep_signal, ep_monitor;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering sigset_t mask;
51323288fc628a5cac50914df915545d685b793eLennart Poettering int rc = EXIT_SUCCESS;
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* take initial device from queue */
51323288fc628a5cac50914df915545d685b793eLennart Poettering dev = event->dev;
51323288fc628a5cac50914df915545d685b793eLennart Poettering event->dev = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering free(worker);
51323288fc628a5cac50914df915545d685b793eLennart Poettering worker_list_cleanup(udev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event_queue_cleanup(udev, EVENT_UNDEF);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_queue_export_unref(udev_queue_export);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_monitor_unref(monitor);
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_ctrl_unref(udev_ctrl);
51323288fc628a5cac50914df915545d685b793eLennart Poettering close(fd_signal);
51323288fc628a5cac50914df915545d685b793eLennart Poettering close(fd_ep);
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering close(worker_watch[READ_END]);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering sigfillset(&mask);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (fd_signal < 0) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering log_error("error creating signalfd %m\n");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering rc = 2;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto out;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd_ep = epoll_create1(EPOLL_CLOEXEC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fd_ep < 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error creating epoll fd: %m\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering rc = 3;
51323288fc628a5cac50914df915545d685b793eLennart Poettering goto out;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memset(&ep_signal, 0, sizeof(struct epoll_event));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ep_signal.events = EPOLLIN;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering ep_signal.data.fd = fd_signal;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd_monitor = udev_monitor_get_fd(worker_monitor);
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering memset(&ep_monitor, 0, sizeof(struct epoll_event));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ep_monitor.events = EPOLLIN;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ep_monitor.data.fd = fd_monitor;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering log_error("fail to add fds to epoll: %m\n");
51323288fc628a5cac50914df915545d685b793eLennart Poettering rc = 4;
51323288fc628a5cac50914df915545d685b793eLennart Poettering goto out;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* request TERM signal if parent exits */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering prctl(PR_SET_PDEATHSIG, SIGTERM);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* reset OOM score, we only protect the main daemon */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering write_string_file("/proc/self/oom_score_adj", "0");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering for (;;) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct udev_event *udev_event;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct worker_message msg;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int err;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("seq %llu running\n", udev_device_get_seqnum(dev));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_event = udev_event_new(dev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (udev_event == NULL) {
966c66e34940001a40806142ecebaae61b478444Lennart Poettering rc = 5;
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering goto out;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* needed for SIGCHLD/SIGTERM in spawn() */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_event->fd_signal = fd_signal;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (exec_delay > 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_event->exec_delay = exec_delay;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* apply rules, create node, symlinks */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering err = udev_event_execute_rules(udev_event, rules, &sigmask_orig);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (err == 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_event_execute_run(udev_event, &sigmask_orig);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering /* apply/restore inotify watch */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (err == 0 && udev_event->inotify_watch) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_watch_begin(udev, dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_update_db(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* send processed event back to libudev listeners */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_monitor_send_device(worker_monitor, NULL, dev);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* send udevd the result of the event execution */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering memset(&msg, 0, sizeof(struct worker_message));
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if (err != 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering msg.exitcode = err;
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen msg.pid = getpid();
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen log_debug("seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen udev_device_unref(dev);
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen dev = NULL;
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersen if (udev_event->sigterm) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_event_unref(udev_event);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering goto out;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_event_unref(udev_event);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* wait for more device messages from main udevd, or term signal */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering while (dev == NULL) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct epoll_event ev[4];
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering int fdcount;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int i;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (fdcount < 0) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (errno == EINTR)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering continue;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("failed to poll: %m\n");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto out;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering for (i = 0; i < fdcount; i++) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering dev = udev_monitor_receive_device(worker_monitor);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct signalfd_siginfo fdsi;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering ssize_t size;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (size != sizeof(struct signalfd_siginfo))
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering switch (fdsi.ssi_signo) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case SIGTERM:
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering goto out;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering }
51323288fc628a5cac50914df915545d685b793eLennart Poettering }
51323288fc628a5cac50914df915545d685b793eLennart Poetteringout:
51323288fc628a5cac50914df915545d685b793eLennart Poettering udev_device_unref(dev);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if (fd_signal >= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering close(fd_signal);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fd_ep >= 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering close(fd_ep);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt close(fd_inotify);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering close(worker_watch[WRITE_END]);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering udev_rules_unref(rules);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_builtin_exit(udev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_monitor_unref(worker_monitor);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_unref(udev);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_close();
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering exit(rc);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case -1:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering udev_monitor_unref(worker_monitor);
51323288fc628a5cac50914df915545d685b793eLennart Poettering event->state = EVENT_QUEUED;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(worker);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("fork of child failed: %m\n");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->state = WORKER_RUNNING;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->event_start_usec = now(CLOCK_MONOTONIC);
51323288fc628a5cac50914df915545d685b793eLennart Poettering worker->event = event;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering event->state = EVENT_RUNNING;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_append(&worker->node, &worker_list);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering children++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void event_run(struct event *event)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering struct udev_list_node *loop;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev_list_node_foreach(loop, &worker_list) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering struct worker *worker = node_to_worker(loop);
51323288fc628a5cac50914df915545d685b793eLennart Poettering ssize_t count;
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (worker->state != WORKER_IDLE)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (count < 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering kill(worker->pid, SIGKILL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->state = WORKER_KILLED;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering continue;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker_ref(worker);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->event = event;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->state = WORKER_RUNNING;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker->event_start_usec = now(CLOCK_MONOTONIC);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering event->state = EVENT_RUNNING;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
966c66e34940001a40806142ecebaae61b478444Lennart Poettering if (children >= children_max) {
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering if (children_max > 1)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("maximum number (%i) of children reached\n", children);
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering return;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* start new worker and pass initial device */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering worker_new(event);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int event_queue_insert(struct udev_device *dev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct event *event;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event = calloc(1, sizeof(struct event));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (event == NULL)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return -1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->udev = udev_device_get_udev(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->dev = dev;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->seqnum = udev_device_get_seqnum(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devpath = udev_device_get_devpath(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devpath_len = strlen(event->devpath);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devpath_old = udev_device_get_devpath_old(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->devnum = udev_device_get_devnum(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->is_block = streq("block", udev_device_get_subsystem(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->ifindex = udev_device_get_ifindex(dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering#ifdef HAVE_FIRMWARE
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (streq(udev_device_get_subsystem(dev), "firmware"))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event->nodelay = true;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering#endif
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
4e2d538f33df8a425487aaa4facc23065a9bdaf7Lennart Poettering udev_queue_export_device_queued(udev_queue_export, dev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_device_get_action(dev), udev_device_get_subsystem(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
4e2d538f33df8a425487aaa4facc23065a9bdaf7Lennart Poettering event->state = EVENT_QUEUED;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_append(&event->node, &event_list);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return 0;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void worker_kill(struct udev *udev)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering{
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering struct udev_list_node *loop;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_list_node_foreach(loop, &worker_list) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering struct worker *worker = node_to_worker(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (worker->state == WORKER_KILLED)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering worker->state = WORKER_KILLED;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering kill(worker->pid, SIGTERM);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering/* lookup event for identical, parent, child device */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic bool is_devpath_busy(struct event *event)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_list_node *loop;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering size_t common;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* check if queue contains events we depend on */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach(loop, &event_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct event *loop_event = node_to_event(loop);
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* we already found a later event, earlier can not block us, no need to check again */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (loop_event->seqnum < event->delaying_seqnum)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* event we checked earlier still exists, no need to check again */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (loop_event->seqnum == event->delaying_seqnum)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* found ourself, no later event can block us */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (loop_event->seqnum >= event->seqnum)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* check major/minor */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return true;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* check network device ifindex */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* check our old name */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* compare devpath */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* identical device event found */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (loop_event->devpath_len == event->devpath_len) {
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering /* devices names might have changed/swapped in the meantime */
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
51323288fc628a5cac50914df915545d685b793eLennart Poettering continue;
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering#ifdef HAVE_FIRMWARE
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* allow to bypass the dependency tracking */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (event->nodelay)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering#endif
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
190700621f95160d364f8ec1d3e360246c41ce75Lennart Poettering /* parent device event found */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (event->devpath[common] == '/') {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* child device event found */
51323288fc628a5cac50914df915545d685b793eLennart Poettering if (loop_event->devpath[common] == '/') {
51323288fc628a5cac50914df915545d685b793eLennart Poettering event->delaying_seqnum = loop_event->seqnum;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* no matching device */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
51323288fc628a5cac50914df915545d685b793eLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void event_queue_start(struct udev *udev)
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_list_node *loop;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering udev_list_node_foreach(loop, &event_list) {
7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cdLennart Poettering struct event *event = node_to_event(loop);
c463eb783e5ad999d400180c69b912c54fa07ee1Lennart Poettering
eee026a7ba16336b6493828a2a13ddc9908667ffLennart Poettering if (event->state != EVENT_QUEUED)
d0129ddb9fbb07bed7c8ea51b8031f824bf506fbLennart Poettering continue;
eee026a7ba16336b6493828a2a13ddc9908667ffLennart Poettering
c463eb783e5ad999d400180c69b912c54fa07ee1Lennart Poettering /* do not start event if parent or child event is still running */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (is_devpath_busy(event))
51323288fc628a5cac50914df915545d685b793eLennart Poettering continue;
51323288fc628a5cac50914df915545d685b793eLennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering event_run(event);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state match_type)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering{
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering struct udev_list_node *loop, *tmp;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering udev_list_node_foreach_safe(loop, tmp, &event_list) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering struct event *event = node_to_event(loop);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (match_type != EVENT_UNDEF && match_type != event->state)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering event_queue_delete(event, false);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic void worker_returned(int fd_worker)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering{
966c66e34940001a40806142ecebaae61b478444Lennart Poettering for (;;) {
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering struct worker_message msg;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ssize_t size;
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering struct udev_list_node *loop;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart 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;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* lookup worker who sent the signal */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_list_node_foreach(loop, &worker_list) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct worker *worker = node_to_worker(loop);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker->pid != msg.pid)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* worker returned */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (worker->event) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->event->exitcode = msg.exitcode;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event_queue_delete(worker->event, true);
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{
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (ctrl_conn == NULL)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering goto out;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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_PRIORITY) received, log_priority=%i\n", i);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_set_max_level(i);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_set_log_priority(udev, i);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_kill(udev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (STOP_EXEC_QUEUE) received\n");
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\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering stop_exec_queue = false;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_reload(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (RELOAD) received\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering reload = true;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering str = udev_ctrl_get_set_env(ctrl_msg);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (str != NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering char *key;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering key = strdup(str);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (key != NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering char *val;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering val = strchr(key, '=');
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (val != NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering val[0] = '\0';
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering val = &val[1];
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (val[0] == '\0') {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (ENV) received, unset '%s'\n", key);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_add_property(udev, key, NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (ENV) received, set '%s=%s'\n", key, val);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering udev_add_property(udev, key, val);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("wrong key format '%s'\n", key);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering free(key);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_kill(udev);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart 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\n", 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\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl_get_exit(ctrl_msg) > 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("udevd message (EXIT) received\n");
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 Poettering/* read inotify messages */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int handle_inotify(struct udev *udev)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int nbytes, pos;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering char *buf;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct inotify_event *ev;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if ((ioctl(fd_inotify, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return 0;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering buf = malloc(nbytes);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (buf == NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error getting buffer for inotify\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return -1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering nbytes = read(fd_inotify, buf, nbytes);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev_device *dev;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ev = (struct inotify_event *)(buf + pos);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering dev = udev_watch_lookup(udev, ev->wd);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (dev != NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (ev->mask & IN_CLOSE_WRITE) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering char filename[UTIL_PATH_SIZE];
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int fd;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd = open(filename, O_WRONLY);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (fd >= 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (write(fd, "change", 6) < 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("error writing uevent: %m\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering close(fd);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering 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 }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering free(buf);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return 0;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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\n", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (WIFEXITED(status)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (WEXITSTATUS(status) != 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] exit with return code %i\n", pid, WEXITSTATUS(status));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (WIFSIGNALED(status)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] terminated by signal %i (%s)\n",
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (WIFSTOPPED(status)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] stopped\n", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (WIFCONTINUED(status)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] continued\n", pid);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("worker [%u] exit with status 0x%04x\n", 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'\n",
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering pid, worker->event->devpath);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker->event->exitcode = -32;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering event_queue_delete(worker->event, true);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* drop reference taken for state 'running' */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_unref(worker);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering worker_unref(worker);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering int fd, n;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering n = sd_listen_fds(true);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (n <= 0)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return -1;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1)) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (ctrl >= 0)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return -1;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering ctrl = fd;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering continue;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1)) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (netlink >= 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return -1;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering netlink = fd;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering continue;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering return -1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (ctrl < 0 || netlink < 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return -1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("ctrl=%i netlink=%i\n", ctrl, netlink);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering *rctrl = ctrl;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering *rnetlink = netlink;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return 0;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering/*
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * read the kernel commandline, in case we need to get into debug mode
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * udev.log-priority=<level> syslog priority
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * udev.children-max=<number of workers> events are fully serialized if set to 1
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * udev.exec-delay=<number of seconds> delay execution of every executed program
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void kernel_cmdline_options(struct udev *udev)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering{
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering char *line, *w, *state;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering size_t l;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (read_one_line_file("/proc/cmdline", &line) < 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering FOREACH_WORD_QUOTED(w, l, line, state) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering char *s, *opt;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering s = strndup(w, l);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!s)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* accept the same options for the initrd, prefixed with "rd." */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (in_initrd() && startswith(s, "rd."))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering opt = s + 3;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering else
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering opt = s;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (startswith(opt, "udev.log-priority=")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int prio;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering prio = util_log_priority(opt + 18);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_set_max_level(prio);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_set_log_priority(udev, prio);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (startswith(opt, "udev.children-max=")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering children_max = strtoul(opt + 18, NULL, 0);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else if (startswith(opt, "udev.exec-delay=")) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering exec_delay = strtoul(opt + 16, NULL, 0);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering free(s);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering free(line);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering}
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringint main(int argc, char *argv[])
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering{
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering struct udev *udev;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering sigset_t mask;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int daemonize = false;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int resolve_names = 1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering static const struct option options[] = {
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen { "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' },
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev = udev_new();
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev == NULL)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_set_target(LOG_TARGET_AUTO);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_parse_environment();
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_open();
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_set_log_fn(udev, udev_main_log);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_set_max_level(udev_get_log_priority(udev));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_debug("version %s\n", VERSION);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering label_init("/dev");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (;;) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'd':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering daemonize = true;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'c':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering children_max = strtoul(optarg, NULL, 0);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'h':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering printf("Usage: udevd OPTIONS\n"
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart 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"
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering "\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case 'V':
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering printf("%s\n", VERSION);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
7588460aaf6bd33f6c9bd5645916cfd8a862e9c4Tom Gundersen default:
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering kernel_cmdline_options(udev);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (getuid() != 0) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fprintf(stderr, "root privileges required\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("root privileges required\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* set umask before creating any file/directory */
9a1f0c283bac56eb26a2fa02d62e0e9ece82f25aLennart Poettering chdir("/");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering umask(022);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering mkdir("/run/udev", 0755);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering dev_setup(NULL);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* before opening new files, make sure std{in,out,err} fds are in a sane state */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (daemonize) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering int fd;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fd = open("/dev/null", O_RDWR);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (fd >= 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (write(STDOUT_FILENO, 0, 0) < 0)
9a1f0c283bac56eb26a2fa02d62e0e9ece82f25aLennart Poettering dup2(fd, STDOUT_FILENO);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (write(STDERR_FILENO, 0, 0) < 0)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering dup2(fd, STDERR_FILENO);
9a1f0c283bac56eb26a2fa02d62e0e9ece82f25aLennart Poettering if (fd > STDERR_FILENO)
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering close(fd);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering } else {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fprintf(stderr, "cannot open /dev/null\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("cannot open /dev/null\n");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* get control and netlink socket from systemd */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl == NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error taking over udev control socket");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering rc = 1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (monitor == NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error taking over netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering rc = 3;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* get our own cgroup, we regularly kill everything udev has left behind */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_cgroup = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* open control and netlink socket */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_ctrl = udev_ctrl_new(udev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_ctrl == NULL) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering fprintf(stderr, "error initializing udev control socket");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("error initializing udev control socket");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering rc = 1;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd_ctrl = udev_ctrl_get_fd(udev_ctrl);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering monitor = udev_monitor_new_from_netlink(udev, "kernel");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (monitor == NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fprintf(stderr, "error initializing netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error initializing netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering rc = 3;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fd_netlink = udev_monitor_get_fd(monitor);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_monitor_enable_receiving(monitor) < 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering fprintf(stderr, "error binding netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error binding netlink socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering rc = 3;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fprintf(stderr, "error binding udev control socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error binding udev control socket\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering rc = 1;
7e8131e9c6c150732503899a092206578fdc13deLennart Poettering goto exit;
7e8131e9c6c150732503899a092206578fdc13deLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* create queue file before signalling 'ready', to make sure we block 'settle' */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering udev_queue_export = udev_queue_export_new(udev);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (udev_queue_export == NULL) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_error("error creating queue file\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (daemonize) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid_t pid;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering pid = fork();
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering switch (pid) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering case 0:
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering break;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering case -1:
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_error("fork of daemon failed: %m\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering rc = 4;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering default:
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering rc = EXIT_SUCCESS;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering goto exit_daemonize;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering setsid();
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering write_string_file("/proc/self/oom_score_adj", "-1000");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } else {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering sd_notify(1, "READY=1");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering }
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering print_kmsg("starting version " VERSION "\n");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!debug) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int fd;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fd = open("/dev/null", O_RDWR);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (fd >= 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering dup2(fd, STDIN_FILENO);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering dup2(fd, STDOUT_FILENO);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering dup2(fd, STDERR_FILENO);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering close(fd);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd_inotify = udev_watch_init(udev);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (fd_inotify < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error initializing inotify\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error initializing inotify\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 4;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_watch_restore(udev);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* block and listen to all signals on signalfd */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering sigfillset(&mask);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (fd_signal < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error creating signalfd\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error creating signalfd\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 5;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* unnamed socket from workers to the main daemon */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fprintf(stderr, "error creating socketpair\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error creating socketpair\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = 6;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd_worker = worker_watch[READ_END];
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_builtin_init(udev);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rules = udev_rules_new(udev, resolve_names);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (rules == NULL) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error reading rules\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_ctrl, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_ctrl.events = EPOLLIN;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_ctrl.data.fd = fd_ctrl;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_inotify, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_inotify.events = EPOLLIN;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_inotify.data.fd = fd_inotify;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_signal, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_signal.events = EPOLLIN;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_signal.data.fd = fd_signal;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_netlink, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_netlink.events = EPOLLIN;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_netlink.data.fd = fd_netlink;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering memset(&ep_worker, 0, sizeof(struct epoll_event));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_worker.events = EPOLLIN;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering ep_worker.data.fd = fd_worker;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd_ep = epoll_create1(EPOLL_CLOEXEC);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (fd_ep < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("error creating epoll fd: %m\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 ||
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 ||
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 ||
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("fail to add fds to epoll: %m\n");
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering goto exit;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (children_max <= 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering cpu_set_t cpu_set;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering children_max = 8;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering children_max += CPU_COUNT(&cpu_set) * 2;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_debug("set children_max to %u\n", children_max);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering rc = udev_rules_apply_static_dev_perms(rules);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (rc < 0)
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering log_error("failed to apply permissions on static device nodes - %s\n", strerror(-rc));
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_list_node_init(&event_list);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_list_node_init(&worker_list);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering for (;;) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering static usec_t last_usec;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering struct epoll_event ev[8];
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering int fdcount;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering int timeout;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering int i;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (udev_exit) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering /* close sources of new events and discard buffered events */
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (fd_ctrl >= 0) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering fd_ctrl = -1;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (monitor != NULL) {
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering udev_monitor_unref(monitor);
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering monitor = NULL;
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering }
7f220d94a938a99c77400fa0ca30485e269bae7cLennart Poettering if (fd_inotify >= 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering close(fd_inotify);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fd_inotify = -1;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* discard queued events and kill workers */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering event_queue_cleanup(udev, EVENT_QUEUED);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker_kill(udev);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* exit after all has cleaned up */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (udev_list_node_is_empty(&event_list) && udev_list_node_is_empty(&worker_list))
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering break;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* timeout at exit for workers to finish */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering timeout = 30 * 1000;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering } else if (udev_list_node_is_empty(&event_list) && !children) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* we are idle */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering timeout = -1;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* cleanup possible left-over processes in our cgroup */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (udev_cgroup)
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering } else {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* kill idle or hanging workers */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering timeout = 3 * 1000;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (fdcount < 0)
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering continue;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (fdcount == 0) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering struct udev_list_node *loop;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* timeout */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (udev_exit) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("timeout, giving up waiting for workers to finish\n");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering break;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* kill idle workers */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (udev_list_node_is_empty(&event_list)) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_debug("cleanup idle workers\n");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker_kill(udev);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* check for hanging events */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering udev_list_node_foreach(loop, &worker_list) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering struct worker *worker = node_to_worker(loop);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (worker->state != WORKER_RUNNING)
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering continue;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if ((now(CLOCK_MONOTONIC) - worker->event_start_usec) > 30 * 1000 * 1000) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("worker [%u] %s timeout; kill it\n", worker->pid,
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker->event ? worker->event->devpath : "<idle>");
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering kill(worker->pid, SIGKILL);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker->state = WORKER_KILLED;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* drop reference taken for state 'running' */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker_unref(worker);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (worker->event) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering log_error("seq %llu '%s' killed\n",
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker->event->exitcode = -64;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering event_queue_delete(worker->event, true);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker->event = NULL;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering }
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering }
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering }
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering }
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering is_worker = is_signal = is_inotify = is_netlink = is_ctrl = false;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering for (i = 0; i < fdcount; i++) {
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering if (ev[i].data.fd == fd_worker && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering is_worker = true;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering else if (ev[i].data.fd == fd_netlink && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering is_netlink = true;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering is_signal = true;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering else if (ev[i].data.fd == fd_inotify && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering is_inotify = true;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering else if (ev[i].data.fd == fd_ctrl && ev[i].events & EPOLLIN)
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering is_ctrl = true;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering }
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering /* check for changed config, every 3 seconds at most */
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * 1000 * 1000) {
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering if (udev_rules_check_timestamp(rules))
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering reload = true;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering if (udev_builtin_validate(udev))
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering reload = true;
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering last_usec = now(CLOCK_MONOTONIC);
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering }
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering
593f665cd5ffddbaa3ddbe9d963af923ed1cc5dcLennart Poettering /* reload requested, HUP signal received, rules changed, builtin changed */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (reload) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker_kill(udev);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering rules = udev_rules_unref(rules);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering udev_builtin_exit(udev);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering reload = false;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering }
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering /* event has finished */
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (is_worker)
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering worker_returned(fd_worker);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (is_netlink) {
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering struct udev_device *dev;
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering dev = udev_monitor_receive_device(monitor);
a150ff5e4e2481eb28d6ed6e0d3e176623e25f5aLennart Poettering if (dev != NULL) {
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC));
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (event_queue_insert(dev) < 0)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_device_unref(dev);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering }
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering }
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering /* start new events */
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) {
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_builtin_init(udev);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (rules == NULL)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering rules = udev_rules_new(udev, resolve_names);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (rules != NULL)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering event_queue_start(udev);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering }
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (is_signal) {
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering struct signalfd_siginfo fdsi;
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering ssize_t size;
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (size == sizeof(struct signalfd_siginfo))
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering handle_signal(udev, fdsi.ssi_signo);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering }
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering /* we are shutting down, the events below are not handled anymore */
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (udev_exit)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering continue;
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering /* device node watch */
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (is_inotify)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering handle_inotify(udev);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering /*
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * This needs to be after the inotify handling, to make sure,
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * that the ping is send back after the possibly generated
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * "change" events by the inotify device node watch.
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering *
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * A single time we may receive a client connection which we need to
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * keep open to block the client. It will be closed right before we
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering * exit.
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering */
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (is_ctrl)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering ctrl_conn = handle_ctrl_msg(udev_ctrl);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering }
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering rc = EXIT_SUCCESS;
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poetteringexit:
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_queue_export_cleanup(udev_queue_export);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_ctrl_cleanup(udev_ctrl);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poetteringexit_daemonize:
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (fd_ep >= 0)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering close(fd_ep);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering worker_list_cleanup(udev);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering event_queue_cleanup(udev, EVENT_UNDEF);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_rules_unref(rules);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_builtin_exit(udev);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (fd_signal >= 0)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering close(fd_signal);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (worker_watch[READ_END] >= 0)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering close(worker_watch[READ_END]);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering if (worker_watch[WRITE_END] >= 0)
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering close(worker_watch[WRITE_END]);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_monitor_unref(monitor);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_queue_export_unref(udev_queue_export);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_ctrl_connection_unref(ctrl_conn);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_ctrl_unref(udev_ctrl);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering label_finish();
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering udev_unref(udev);
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering log_close();
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering return rc;
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering}
97e5d693c04e50605d438af1c8a965fd2963bbe9Lennart Poettering