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