1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
4 * Copyright (C) 2009 Canonical Ltd.
5 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "parse-util.h"
32 #include "udevadm-util.h"
35 static void help(void) {
36 printf("%s settle [OPTIONS]\n\n"
37 "Wait for pending udev events.\n\n"
38 " -h --help Show this help\n"
39 " -V --version Show package version\n"
40 " -t --timeout=SECONDS Maximum time to wait for events\n"
41 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
42 , program_invocation_short_name
);
45 static int adm_settle(struct udev
*udev
, int argc
, char *argv
[]) {
46 static const struct option options
[] = {
47 { "timeout", required_argument
, NULL
, 't' },
48 { "exit-if-exists", required_argument
, NULL
, 'E' },
49 { "version", no_argument
, NULL
, 'V' },
50 { "help", no_argument
, NULL
, 'h' },
51 { "seq-start", required_argument
, NULL
, 's' }, /* removed */
52 { "seq-end", required_argument
, NULL
, 'e' }, /* removed */
53 { "quiet", no_argument
, NULL
, 'q' }, /* removed */
57 const char *exists
= NULL
;
58 unsigned int timeout
= 120;
59 struct pollfd pfd
[1] = { {.fd
= -1}, };
61 struct udev_queue
*queue
;
62 int rc
= EXIT_FAILURE
;
64 while ((c
= getopt_long(argc
, argv
, "t:E:Vhs:e:q", options
, NULL
)) >= 0) {
70 r
= safe_atou(optarg
, &timeout
);
72 log_error_errno(r
, "Invalid timeout value '%s': %m", optarg
);
93 log_info("Option -%c no longer supported.", c
);
100 assert_not_reached("Unknown argument");
105 fprintf(stderr
, "Extraneous argument: '%s'\n", argv
[optind
]);
109 deadline
= now(CLOCK_MONOTONIC
) + timeout
* USEC_PER_SEC
;
111 /* guarantee that the udev daemon isn't pre-processing */
113 struct udev_ctrl
*uctrl
;
115 uctrl
= udev_ctrl_new(udev
);
117 if (udev_ctrl_send_ping(uctrl
, MAX(5U, timeout
)) < 0) {
118 log_debug("no connection to daemon");
119 udev_ctrl_unref(uctrl
);
122 udev_ctrl_unref(uctrl
);
126 queue
= udev_queue_new(udev
);
128 log_error("unable to get udev queue");
132 pfd
[0].events
= POLLIN
;
133 pfd
[0].fd
= udev_queue_get_fd(queue
);
135 log_debug("queue is empty, nothing to watch");
141 if (exists
&& access(exists
, F_OK
) >= 0) {
146 /* exit if queue is empty */
147 if (udev_queue_get_queue_is_empty(queue
)) {
152 if (now(CLOCK_MONOTONIC
) >= deadline
)
155 /* wake up when queue is empty */
156 if (poll(pfd
, 1, MSEC_PER_SEC
) > 0 && pfd
[0].revents
& POLLIN
)
157 udev_queue_flush(queue
);
161 udev_queue_unref(queue
);
165 const struct udevadm_cmd udevadm_settle
= {
168 .help
= "Wait for pending udev events",