2 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2009 Canonical Ltd.
4 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/inotify.h>
35 #include <sys/types.h>
38 #include "udev-util.h"
41 static void help(void) {
42 printf("Usage: udevadm settle OPTIONS\n"
43 " -t,--timeout=<seconds> maximum time to wait for events\n"
44 " -E,--exit-if-exists=<file> stop waiting if file exists\n"
48 static int adm_settle(struct udev *udev, int argc, char *argv[])
50 static const struct option options[] = {
51 { "seq-start", required_argument, NULL, '\0' }, /* removed */
52 { "seq-end", required_argument, NULL, '\0' }, /* removed */
53 { "timeout", required_argument, NULL, 't' },
54 { "exit-if-exists", required_argument, NULL, 'E' },
55 { "quiet", no_argument, NULL, 'q' }, /* removed */
56 { "help", no_argument, NULL, 'h' },
59 const char *exists = NULL;
60 unsigned int timeout = 120;
61 struct pollfd pfd[1] = { {.fd = -1}, };
62 int rc = EXIT_FAILURE, c;
64 while ((c = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL)) >= 0) {
69 r = safe_atou(optarg, &timeout);
71 fprintf(stderr, "Invalid timeout value '%s': %s\n",
72 optarg, strerror(-r));
86 assert_not_reached("Unknown argument");
91 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
95 /* guarantee that the udev daemon isn't pre-processing */
97 struct udev_ctrl *uctrl;
99 uctrl = udev_ctrl_new(udev);
101 if (udev_ctrl_send_ping(uctrl, timeout) < 0) {
102 log_debug("no connection to daemon");
103 udev_ctrl_unref(uctrl);
107 udev_ctrl_unref(uctrl);
111 pfd[0].events = POLLIN;
112 pfd[0].fd = inotify_init1(IN_CLOEXEC);
114 log_error("inotify_init failed: %m");
118 if (inotify_add_watch(pfd[0].fd, "/run/udev/queue" , IN_DELETE) < 0) {
119 /* If it does not exist, we don't have to wait */
123 log_debug("watching /run/udev/queue failed");
128 if (exists && access(exists, F_OK) >= 0) {
133 /* exit if queue is empty */
134 if (access("/run/udev/queue", F_OK) < 0) {
139 /* wake up when "queue" file is deleted */
140 if (poll(pfd, 1, 100) > 0 && pfd[0].revents & POLLIN) {
141 char buf[sizeof(struct inotify_event) + PATH_MAX];
143 read(pfd[0].fd, buf, sizeof(buf));
153 const struct udevadm_cmd udevadm_settle = {
156 .help = "wait for pending udev events",