udevd.c revision 1fa2f38f0f011010bf57522b42fcc168856a7003
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * Copyright (C) 2009 Canonical Ltd.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * This program is free software: you can redistribute it and/or modify
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * it under the terms of the GNU General Public License as published by
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * (at your option) any later version.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * This program is distributed in the hope that it will be useful,
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * GNU General Public License for more details.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * You should have received a copy of the GNU General Public License
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic bool arg_debug = false;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic int arg_daemonize = false;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic usec_t arg_event_timeout_warn_usec = 180 * USEC_PER_SEC / 3;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering unsigned long long int delaying_seqnum;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering unsigned long long int seqnum;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic inline struct event *node_to_event(struct udev_list_node *node) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return container_of(node, struct event, node);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic void event_queue_cleanup(struct udev *udev, enum event_state type);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering/* passed from worker to main process */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic inline struct worker *node_to_worker(struct udev_list_node *node) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return container_of(node, struct worker, node);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic void event_queue_delete(struct event *event) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic struct worker *worker_ref(struct worker *worker) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic void worker_cleanup(struct worker *worker) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic void worker_unref(struct worker *worker) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering log_debug("worker ["PID_FMT"] cleaned up", worker->pid);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic void worker_list_cleanup(struct udev *udev) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering udev_list_node_foreach_safe(loop, tmp, &worker_list) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering struct worker *worker = node_to_worker(loop);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic void worker_new(struct event *event) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* listen for new events */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* allow the main daemon netlink address to send devices to the worker */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering udev_monitor_allow_unicast_sender(worker_monitor, monitor);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering udev_monitor_enable_receiving(worker_monitor);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* worker + event reference */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering struct epoll_event ep_signal, ep_monitor;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* take initial device from queue */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering log_error_errno(errno, "error creating signalfd %m");
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering log_error_errno(errno, "error creating epoll fd: %m");
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering memzero(&ep_signal, sizeof(struct epoll_event));
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering fd_monitor = udev_monitor_get_fd(worker_monitor);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering memzero(&ep_monitor, sizeof(struct epoll_event));
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering log_error_errno(errno, "fail to add fds to epoll: %m");
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* request TERM signal if parent exits */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* reset OOM score, we only protect the main daemon */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering write_string_file("/proc/self/oom_score_adj", "0");
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering log_debug("seq %llu running", udev_device_get_seqnum(dev));
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering /* needed for SIGCHLD/SIGTERM in spawn() */
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * Take a shared lock on the device node; this establishes
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * a concept of device "ownership" to serialize device
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * access. External processes holding an exclusive lock will
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * cause udev to skip the event handling; in the case udev
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering * acquired the lock, the external process can block until
d = udev_device_get_parent(d);
log_debug_errno(errno, "Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
goto skip;
&sigmask_orig);
&sigmask_orig);
skip:
goto out;
int fdcount;
if (fdcount < 0) {
goto out;
for (i = 0; i < fdcount; i++) {
case SIGTERM:
goto out;
out:
log_close();
children++;
if (count < 0) {
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))
const char *str;
goto out;
goto out;
stop_exec_queue = true;
stop_exec_queue = false;
reload = true;
char *key;
char *val;
arg_children_max = i;
udev_exit = true;
out:
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)
switch (signo) {
case SIGINT:
case SIGTERM:
udev_exit = true;
case SIGCHLD:
int status;
if (pid <= 0)
case SIGHUP:
reload = true;
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
size_t l;
opt = s;
int prio;
free(s);
static void help(void) {
arg_daemonize = true;
arg_debug = true;
arg_resolve_names = 0;
help();
return -EINVAL;
goto exit;
log_open();
goto exit;
if (arg_debug)
if (getuid() != 0) {
goto exit;
goto exit;
goto exit;
goto exit;
if (arg_daemonize) {
int fd;
if (fd >= 0) {
goto exit;
goto exit;
goto exit;
goto exit;
goto exit;
goto exit;
goto exit;
if (rc < 0)
if (arg_daemonize) {
switch (pid) {
goto exit;
goto exit_daemonize;
setsid();
if (arg_children_max <= 0) {
if (fd_inotify < 0) {
goto exit;
if (fd_signal < 0) {
goto exit;
goto exit;
if (fd_ep < 0) {
goto exit;
goto exit;
int fdcount;
int timeout;
if (udev_exit) {
if (fd_ctrl >= 0) {
if (fd_inotify >= 0) {
if (udev_cgroup)
int fd;
if (fd >= 0)
if (fdcount < 0)
if (fdcount == 0) {
if (udev_exit) {
log_error("seq %llu '%s' killed", udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
for (i = 0; i < fdcount; i++) {
is_worker = true;
is_netlink = true;
is_signal = true;
is_inotify = true;
is_ctrl = true;
reload = true;
reload = true;
if (reload) {
reload = false;
if (is_worker)
if (is_netlink) {
if (is_signal) {
if (udev_exit)
if (is_inotify)
if (is_ctrl)
exit:
if (fd_ep >= 0)
if (fd_signal >= 0)
log_close();
return rc;