udevd.c revision 185abfc3d6b4e8f804a3f7216cd8b0459593af87
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * Copyright (C) 2009 Canonical Ltd.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * This program is free software: you can redistribute it and/or modify
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * it under the terms of the GNU General Public License as published by
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * the Free Software Foundation, either version 2 of the License, or
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * (at your option) any later version.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * This program is distributed in the hope that it will be useful,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * GNU General Public License for more details.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * You should have received a copy of the GNU General Public License
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool arg_debug = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int arg_daemonize = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringtypedef struct Manager {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering pid_t pid; /* the process that originally allocated the manager object */
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering struct udev_ctrl_connection *ctrl_conn_blocking;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering unsigned long long int delaying_seqnum;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering unsigned long long int seqnum;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node) {
34b9656f0b2890743eee6a746ef08d817abfd5e9Lennart Poettering return container_of(node, struct event, node);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void event_queue_cleanup(Manager *manager, enum event_state type);
34b9656f0b2890743eee6a746ef08d817abfd5e9Lennart Poettering/* passed from worker to main process */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void event_free(struct event *event) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (udev_list_node_is_empty(&event->manager->events)) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* only clean up the queue from the process that created it */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void worker_free(struct worker *worker) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering hashmap_remove(worker->manager->workers, UINT_TO_PTR(worker->pid));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void manager_workers_free(Manager *manager) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering HASHMAP_FOREACH(worker, manager->workers, i)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering manager->workers = hashmap_free(manager->workers);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int worker_new(struct worker **ret, Manager *manager, struct udev_monitor *worker_monitor, pid_t pid) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ struct worker *worker = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* close monitor, but keep address around */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering worker->monitor = udev_monitor_ref(worker_monitor);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = hashmap_ensure_allocated(&manager->workers, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = hashmap_put(manager->workers, UINT_TO_PTR(pid), worker);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering kill_and_sigcont(event->worker->pid, SIGKILL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("seq %llu '%s' killed", udev_device_get_seqnum(event->dev), event->devpath);
b914e211f3a40f507b3cdc572838ec7f3fd5e4cfLennart Poetteringstatic int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
b914e211f3a40f507b3cdc572838ec7f3fd5e4cfLennart Poettering log_warning("seq %llu '%s' is taking a long time", udev_device_get_seqnum(event->dev), event->devpath);
b914e211f3a40f507b3cdc572838ec7f3fd5e4cfLennart Poetteringstatic void worker_attach_event(struct worker *worker, struct event *event) {
b914e211f3a40f507b3cdc572838ec7f3fd5e4cfLennart Poettering event->start_usec = now(CLOCK_MONOTONIC);
b914e211f3a40f507b3cdc572838ec7f3fd5e4cfLennart Poetteringstatic void manager_free(Manager *manager) {
switch (pid) {
int fd_monitor;
if (fd_signal < 0) {
goto out;
if (fd_ep < 0) {
goto out;
goto out;
r = -ENOMEM;
goto out;
if (arg_exec_delay > 0)
d = udev_device_get_parent(d);
log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
r = -EAGAIN;
goto skip;
skip:
goto out;
int fdcount;
if (fdcount < 0) {
goto out;
for (i = 0; i < fdcount; i++) {
case SIGTERM:
goto out;
out:
log_close();
Iterator i;
if (count < 0) {
if (!event)
return -ENOMEM;
Iterator i;
if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
} control = {};
if (size < 0) {
if (!worker) {
if (dev) {
const char *str;
if (!ctrl_conn)
if (!ctrl_msg)
if (key) {
char *val;
arg_children_max = i;
bool part_table_read = false;
bool has_partitions = false;
int fd;
if (fd >= 0) {
part_table_read = true;
return -ENOMEM;
r = udev_enumerate_scan_devices(e);
has_partitions = true;
struct inotify_event *e;
ssize_t l;
if (!dev)
int status;
if (pid <= 0)
if (!worker) {
log_warning("worker ["PID_FMT"] terminated by signal %i (%s)", pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
int fd, n;
n = sd_listen_fds(true);
if (ctrl >= 0)
if (netlink >= 0)
* udev.children-max=<number of workers> events are fully serialized if set to 1
if (!value)
int prio;
static void help(void) {
arg_daemonize = true;
arg_debug = true;
arg_resolve_names = 0;
help();
return -EINVAL;
if (!manager)
return log_oom();
log_open();
goto exit;
if (arg_debug)
if (getuid() != 0) {
goto exit;
if (arg_children_max == 0) {
if (arg_daemonize) {
int fd;
if (fd < 0)
goto exit;
goto exit;
goto exit;
goto exit;
if (arg_daemonize) {
switch (pid) {
goto exit;
log_close();
setsid();
int fdcount;
int timeout;
if (fdcount < 0)
if (fdcount == 0) {
Iterator j;
for (i = 0; i < fdcount; i++) {
is_worker = true;
is_uevent = true;
is_signal = true;
is_inotify = true;
is_ctrl = true;
if (is_worker)
if (is_uevent)
if (is_signal) {
case SIGINT:
case SIGTERM:
case SIGHUP:
case SIGCHLD:
if (is_inotify)
if (is_ctrl)
exit:
if (manager)
log_close();