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