1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
5 * Copyright (C) 2009 Canonical Ltd.
6 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "parse-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 " --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 { "help", no_argument
, NULL
, 'h' },
50 { "seq-start", required_argument
, NULL
, 's' }, /* removed */
51 { "seq-end", required_argument
, NULL
, 'e' }, /* removed */
52 { "quiet", no_argument
, NULL
, 'q' }, /* removed */
56 const char *exists
= NULL
;
57 unsigned int timeout
= 120;
58 struct pollfd pfd
[1] = { {.fd
= -1}, };
60 struct udev_queue
*queue
;
61 int rc
= EXIT_FAILURE
;
63 while ((c
= getopt_long(argc
, argv
, "t:E:hs:e:q", options
, NULL
)) >= 0) {
69 r
= safe_atou(optarg
, &timeout
);
71 log_error_errno(r
, "Invalid timeout value '%s': %m", optarg
);
88 log_info("Option -%c no longer supported.", c
);
95 assert_not_reached("Unknown argument");
100 fprintf(stderr
, "Extraneous argument: '%s'\n", argv
[optind
]);
104 deadline
= now(CLOCK_MONOTONIC
) + timeout
* USEC_PER_SEC
;
106 /* guarantee that the udev daemon isn't pre-processing */
108 struct udev_ctrl
*uctrl
;
110 uctrl
= udev_ctrl_new(udev
);
112 if (udev_ctrl_send_ping(uctrl
, MAX(5U, timeout
)) < 0) {
113 log_debug("no connection to daemon");
114 udev_ctrl_unref(uctrl
);
117 udev_ctrl_unref(uctrl
);
121 queue
= udev_queue_new(udev
);
123 log_error("unable to get udev queue");
127 pfd
[0].events
= POLLIN
;
128 pfd
[0].fd
= udev_queue_get_fd(queue
);
130 log_debug("queue is empty, nothing to watch");
136 if (exists
&& access(exists
, F_OK
) >= 0) {
141 /* exit if queue is empty */
142 if (udev_queue_get_queue_is_empty(queue
)) {
147 if (now(CLOCK_MONOTONIC
) >= deadline
)
150 /* wake up when queue is empty */
151 if (poll(pfd
, 1, MSEC_PER_SEC
) > 0 && pfd
[0].revents
& POLLIN
)
152 udev_queue_flush(queue
);
156 udev_queue_unref(queue
);
160 const struct udevadm_cmd udevadm_settle
= {
163 .help
= "Wait for pending udev events",