udevd.c revision 7500cd5e96cff52a726d03194e3b927771ddb1d4
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * Copyright (C) 2009 Canonical Ltd.
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * This program is free software: you can redistribute it and/or modify
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * it under the terms of the GNU General Public License as published by
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * the Free Software Foundation, either version 2 of the License, or
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * (at your option) any later version.
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * This program is distributed in the hope that it will be useful,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * but WITHOUT ANY WARRANTY; without even the implied warranty of
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * GNU General Public License for more details.
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * You should have received a copy of the GNU General Public License
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * along with this program. If not, see <http://www.gnu.org/licenses/>.
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic bool arg_debug = false;
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic int arg_daemonize = false;
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic unsigned arg_children_max;
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
2f8d336478536af789d654599f9523d02e0ca693Kay Sieverstypedef struct Manager {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers pid_t pid; /* the process that originally allocated the manager object */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers struct udev_ctrl_connection *ctrl_conn_blocking;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers unsigned long long int delaying_seqnum;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers unsigned long long int seqnum;
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic inline struct event *node_to_event(struct udev_list_node *node) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic void event_queue_cleanup(Manager *manager, enum event_state type);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers/* passed from worker to main process */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (udev_list_node_is_empty(&event->manager->events)) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* only clean up the queue from the process that created it */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers log_warning_errno(errno, "could not unlink /run/udev/queue: %m");
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers hashmap_remove(worker->manager->workers, UINT_TO_PTR(worker->pid));
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic void manager_workers_free(Manager *manager) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers manager->workers = hashmap_free(manager->workers);
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic int worker_new(struct worker **ret, Manager *manager, struct udev_monitor *worker_monitor, pid_t pid) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* close monitor, but keep address around */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers worker->monitor = udev_monitor_ref(worker_monitor);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers r = hashmap_ensure_allocated(&manager->workers, NULL);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers r = hashmap_put(manager->workers, UINT_TO_PTR(pid), worker);
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers log_error("seq %llu '%s' killed", udev_device_get_seqnum(event->dev), event->devpath);
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers log_warning("seq %llu '%s' is taking a long time", udev_device_get_seqnum(event->dev), event->devpath);
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic void worker_attach_event(struct worker *worker, struct event *event) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers r = sd_event_now(e, clock_boottime_or_monotonic(), &usec);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers (void) sd_event_add_time(e, &event->timeout_warning, clock_boottime_or_monotonic(),
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers usec + arg_event_timeout_warn_usec, USEC_PER_SEC, on_event_timeout_warning, event);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers (void) sd_event_add_time(e, &event->timeout, clock_boottime_or_monotonic(),
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers usec + arg_event_timeout_usec, USEC_PER_SEC, on_event_timeout, event);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers udev_ctrl_connection_unref(manager->ctrl_conn_blocking);
2f8d336478536af789d654599f9523d02e0ca693Kay SieversDEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return loop_write(fd, &message, sizeof(message), false);
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->ctrl_event, udev_ctrl_get_fd(manager->ctrl), EPOLLIN, on_ctrl_msg, manager);
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, udev_monitor_get_fd(manager->monitor), 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;
goto exit;
if (arg_daemonize) {
switch (pid) {
goto exit;
log_close();
setsid();
(void) sd_notify(false,
goto exit;
exit:
sd_notify(false,
if (manager)
log_close();