udevd.c revision ab7854df736585e42ec208012b7e2e11b652998a
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * Copyright (C) 2009 Canonical Ltd.
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * This program is free software: you can redistribute it and/or modify
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * it under the terms of the GNU General Public License as published by
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * (at your option) any later version.
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * This program is distributed in the hope that it will be useful,
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * GNU General Public License for more details.
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * You should have received a copy of the GNU General Public License
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic bool arg_debug = false;
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic int arg_daemonize = false;
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
bf7fabd6000fd95495904ed55db62caf361b3d8fLennart Poetteringtypedef struct Manager {
bf7fabd6000fd95495904ed55db62caf361b3d8fLennart Poettering pid_t pid; /* the process that originally allocated the manager object */
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering struct udev_ctrl_connection *ctrl_conn_blocking;
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poettering unsigned long long int delaying_seqnum;
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poettering unsigned long long int seqnum;
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering return container_of(node, struct event, node);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic void event_queue_cleanup(Manager *manager, enum event_state type);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering/* passed from worker to main process */
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic void event_free(struct event *event) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering sd_event_source_unref(event->timeout_warning);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering if (udev_list_node_is_empty(&event->manager->events)) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering /* only clean up the queue from the process that created it */
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic void worker_free(struct worker *worker) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering hashmap_remove(worker->manager->workers, UINT_TO_PTR(worker->pid));
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic void manager_workers_free(Manager *manager) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering HASHMAP_FOREACH(worker, manager->workers, i)
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering manager->workers = hashmap_free(manager->workers);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic int worker_new(struct worker **ret, Manager *manager, struct udev_monitor *worker_monitor, pid_t pid) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering _cleanup_free_ struct worker *worker = NULL;
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering /* close monitor, but keep address around */
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering worker->monitor = udev_monitor_ref(worker_monitor);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering r = hashmap_ensure_allocated(&manager->workers, NULL);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering r = hashmap_put(manager->workers, UINT_TO_PTR(pid), worker);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering kill_and_sigcont(event->worker->pid, SIGKILL);
9176a57c101d51b4a7fb4141240b5ce03abac57dLennart Poettering log_error("seq %llu '%s' killed", udev_device_get_seqnum(event->dev), event->devpath);
9176a57c101d51b4a7fb4141240b5ce03abac57dLennart Poetteringstatic int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
f8dc7e343d903f053070d01f3273db819deee951Lennart Poettering log_warning("seq %llu '%s' is taking a long time", udev_device_get_seqnum(event->dev), event->devpath);
f8dc7e343d903f053070d01f3273db819deee951Lennart Poetteringstatic void worker_attach_event(struct worker *worker, struct event *event) {
if (!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:
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))
sd_notify(false,
sd_notify(false,
sd_notify(false,
} 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, r;
n = sd_listen_fds(true);
if (ctrl_fd >= 0)
return -EINVAL;
if (netlink_fd >= 0)
return -EINVAL;
return -EINVAL;
if (ctrl_fd < 0) {
if (!udev)
return -ENOMEM;
if (!ctrl)
if (fd < 0)
if (ctrl_fd < 0)
if (netlink_fd < 0) {
if (!udev) {
if (!udev)
return -ENOMEM;
if (!monitor)
if (fd < 0)
if (ctrl_fd < 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();
r = sd_event_add_io(manager->event, &manager->inotify_event, manager->fd_inotify, EPOLLIN, on_inotify, manager);
r = sd_event_add_io(manager->event, &manager->uevent_event, fd_uevent, EPOLLIN, on_uevent, manager);
log_open();
goto exit;
if (arg_debug)
if (getuid() != 0) {
goto exit;
if (arg_children_max == 0) {
goto exit;
goto exit;
goto exit;
goto exit;
if (arg_daemonize) {
switch (pid) {
goto exit;
log_close();
setsid();
goto exit;
(void) sd_notify(false,
goto exit;
exit:
sd_notify(false,
if (manager)
log_close();