]>
Commit | Line | Data |
---|---|---|
7baada47 | 1 | /* |
fc206fbe | 2 | * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org> |
bb38678e SJR |
3 | * Copyright (C) 2009 Canonical Ltd. |
4 | * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com> | |
7baada47 | 5 | * |
55e9959b KS |
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. | |
7baada47 | 10 | * |
55e9959b KS |
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. | |
15 | * | |
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/>. | |
7baada47 KS |
18 | */ |
19 | ||
20 | #include <stdlib.h> | |
21 | #include <stddef.h> | |
22 | #include <string.h> | |
23 | #include <stdio.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
26 | #include <dirent.h> | |
27 | #include <fcntl.h> | |
28 | #include <syslog.h> | |
c2666405 | 29 | #include <getopt.h> |
c2c24d4d | 30 | #include <signal.h> |
959e8b5d | 31 | #include <time.h> |
a3eca08b KS |
32 | #include <sys/inotify.h> |
33 | #include <sys/poll.h> | |
7baada47 KS |
34 | #include <sys/stat.h> |
35 | #include <sys/types.h> | |
36 | ||
37 | #include "udev.h" | |
44433ebd | 38 | #include "udev-util.h" |
4e93793d | 39 | #include "util.h" |
7baada47 | 40 | |
7643ac9a ZJS |
41 | static void help(void) { |
42 | printf("Usage: udevadm settle OPTIONS\n" | |
43 | " -t,--timeout=<seconds> maximum time to wait for events\n" | |
44 | " -s,--seq-start=<seqnum> first seqnum to wait for\n" | |
45 | " -e,--seq-end=<seqnum> last seqnum to wait for\n" | |
46 | " -E,--exit-if-exists=<file> stop waiting if file exists\n" | |
47 | " -q,--quiet do not print list after timeout\n" | |
48 | " -h,--help\n\n"); | |
49 | } | |
50 | ||
1985c76e | 51 | static int adm_settle(struct udev *udev, int argc, char *argv[]) |
7baada47 | 52 | { |
912541b0 | 53 | static const struct option options[] = { |
7643ac9a ZJS |
54 | { "seq-start", required_argument, NULL, 's' }, |
55 | { "seq-end", required_argument, NULL, 'e' }, | |
56 | { "timeout", required_argument, NULL, 't' }, | |
912541b0 | 57 | { "exit-if-exists", required_argument, NULL, 'E' }, |
7643ac9a ZJS |
58 | { "quiet", no_argument, NULL, 'q' }, |
59 | { "help", no_argument, NULL, 'h' }, | |
912541b0 KS |
60 | {} |
61 | }; | |
40fe8b11 KS |
62 | usec_t start_usec = now(CLOCK_MONOTONIC); |
63 | usec_t start = 0; | |
64 | usec_t end = 0; | |
912541b0 KS |
65 | int quiet = 0; |
66 | const char *exists = NULL; | |
67 | unsigned int timeout = 120; | |
b49d9b50 | 68 | struct pollfd pfd[1] = { {.fd = -1}, }; |
44433ebd | 69 | _cleanup_udev_queue_unref_ struct udev_queue *udev_queue = NULL; |
7643ac9a | 70 | int rc = EXIT_FAILURE, c; |
912541b0 | 71 | |
7643ac9a ZJS |
72 | while ((c = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL)) >= 0) |
73 | switch (c) { | |
912541b0 KS |
74 | case 's': |
75 | start = strtoull(optarg, NULL, 0); | |
76 | break; | |
77 | case 'e': | |
78 | end = strtoull(optarg, NULL, 0); | |
79 | break; | |
4e93793d YZ |
80 | case 't': { |
81 | int r; | |
82 | ||
83 | r = safe_atou(optarg, &timeout); | |
84 | if (r < 0) { | |
85 | fprintf(stderr, "Invalid timeout value '%s': %s\n", | |
86 | optarg, strerror(-r)); | |
87 | exit(EXIT_FAILURE); | |
88 | }; | |
912541b0 | 89 | break; |
7643ac9a | 90 | } |
912541b0 KS |
91 | case 'E': |
92 | exists = optarg; | |
93 | break; | |
7643ac9a ZJS |
94 | case 'q': |
95 | quiet = 1; | |
96 | break; | |
912541b0 | 97 | case 'h': |
7643ac9a | 98 | help(); |
912541b0 | 99 | exit(EXIT_SUCCESS); |
7643ac9a | 100 | case '?': |
912541b0 | 101 | exit(EXIT_FAILURE); |
7643ac9a ZJS |
102 | default: |
103 | assert_not_reached("Unkown argument"); | |
912541b0 | 104 | } |
7643ac9a ZJS |
105 | |
106 | if (optind < argc) { | |
107 | fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]); | |
108 | exit(EXIT_FAILURE); | |
912541b0 KS |
109 | } |
110 | ||
111 | udev_queue = udev_queue_new(udev); | |
112 | if (udev_queue == NULL) | |
113 | exit(2); | |
114 | ||
115 | if (start > 0) { | |
116 | unsigned long long kernel_seq; | |
117 | ||
118 | kernel_seq = udev_queue_get_kernel_seqnum(udev_queue); | |
119 | ||
120 | /* unless specified, the last event is the current kernel seqnum */ | |
121 | if (end == 0) | |
122 | end = udev_queue_get_kernel_seqnum(udev_queue); | |
123 | ||
124 | if (start > end) { | |
9f6445e3 | 125 | log_error("seq-start larger than seq-end, ignoring"); |
912541b0 KS |
126 | start = 0; |
127 | end = 0; | |
128 | } | |
129 | ||
130 | if (start > kernel_seq || end > kernel_seq) { | |
9f6445e3 | 131 | log_error("seq-start or seq-end larger than current kernel value, ignoring"); |
912541b0 KS |
132 | start = 0; |
133 | end = 0; | |
134 | } | |
9f6445e3 | 135 | log_debug("start=%llu end=%llu current=%llu", (unsigned long long)start, (unsigned long long)end, kernel_seq); |
912541b0 KS |
136 | } else { |
137 | if (end > 0) { | |
9f6445e3 | 138 | log_error("seq-end needs seq-start parameter, ignoring"); |
912541b0 KS |
139 | end = 0; |
140 | } | |
141 | } | |
142 | ||
143 | /* guarantee that the udev daemon isn't pre-processing */ | |
144 | if (getuid() == 0) { | |
145 | struct udev_ctrl *uctrl; | |
146 | ||
147 | uctrl = udev_ctrl_new(udev); | |
148 | if (uctrl != NULL) { | |
149 | if (udev_ctrl_send_ping(uctrl, timeout) < 0) { | |
9f6445e3 | 150 | log_debug("no connection to daemon"); |
912541b0 KS |
151 | udev_ctrl_unref(uctrl); |
152 | rc = EXIT_SUCCESS; | |
153 | goto out; | |
154 | } | |
155 | udev_ctrl_unref(uctrl); | |
156 | } | |
157 | } | |
158 | ||
159 | pfd[0].events = POLLIN; | |
160 | pfd[0].fd = inotify_init1(IN_CLOEXEC); | |
161 | if (pfd[0].fd < 0) { | |
9f6445e3 | 162 | log_error("inotify_init failed: %m"); |
912541b0 | 163 | } else { |
6ada823a | 164 | if (inotify_add_watch(pfd[0].fd, "/run/udev" , IN_MOVED_TO) < 0) { |
9f6445e3 | 165 | log_error("watching /run/udev failed"); |
912541b0 KS |
166 | close(pfd[0].fd); |
167 | pfd[0].fd = -1; | |
168 | } | |
169 | } | |
170 | ||
171 | for (;;) { | |
172 | struct stat statbuf; | |
173 | ||
174 | if (exists != NULL && stat(exists, &statbuf) == 0) { | |
175 | rc = EXIT_SUCCESS; | |
176 | break; | |
177 | } | |
178 | ||
179 | if (start > 0) { | |
180 | /* if asked for, wait for a specific sequence of events */ | |
181 | if (udev_queue_get_seqnum_sequence_is_finished(udev_queue, start, end) == 1) { | |
182 | rc = EXIT_SUCCESS; | |
183 | break; | |
184 | } | |
185 | } else { | |
186 | /* exit if queue is empty */ | |
187 | if (udev_queue_get_queue_is_empty(udev_queue)) { | |
188 | rc = EXIT_SUCCESS; | |
189 | break; | |
190 | } | |
191 | } | |
192 | ||
193 | if (pfd[0].fd >= 0) { | |
194 | int delay; | |
195 | ||
196 | if (exists != NULL || start > 0) | |
197 | delay = 100; | |
198 | else | |
199 | delay = 1000; | |
200 | /* wake up after delay, or immediately after the queue is rebuilt */ | |
201 | if (poll(pfd, 1, delay) > 0 && pfd[0].revents & POLLIN) { | |
202 | char buf[sizeof(struct inotify_event) + PATH_MAX]; | |
203 | ||
204 | read(pfd[0].fd, buf, sizeof(buf)); | |
205 | } | |
206 | } else { | |
207 | sleep(1); | |
208 | } | |
209 | ||
210 | if (timeout > 0) { | |
40fe8b11 | 211 | usec_t age_usec; |
912541b0 | 212 | |
40fe8b11 | 213 | age_usec = now(CLOCK_MONOTONIC) - start_usec; |
912541b0 KS |
214 | if (age_usec / (1000 * 1000) >= timeout) { |
215 | struct udev_list_entry *list_entry; | |
216 | ||
217 | if (!quiet && udev_queue_get_queued_list_entry(udev_queue) != NULL) { | |
9f6445e3 | 218 | log_debug("timeout waiting for udev queue"); |
912541b0 KS |
219 | printf("\nudevadm settle - timeout of %i seconds reached, the event queue contains:\n", timeout); |
220 | udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue)) | |
221 | printf(" %s (%s)\n", | |
222 | udev_list_entry_get_name(list_entry), | |
223 | udev_list_entry_get_value(list_entry)); | |
224 | } | |
225 | ||
226 | break; | |
227 | } | |
228 | } | |
229 | } | |
ff2c503d | 230 | out: |
912541b0 KS |
231 | if (pfd[0].fd >= 0) |
232 | close(pfd[0].fd); | |
912541b0 | 233 | return rc; |
7baada47 | 234 | } |
1985c76e KS |
235 | |
236 | const struct udevadm_cmd udevadm_settle = { | |
912541b0 KS |
237 | .name = "settle", |
238 | .cmd = adm_settle, | |
239 | .help = "wait for the event queue to finish", | |
1985c76e | 240 | }; |