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