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