chiark / gitweb /
8976c9a16bde9e69df1ced477c3b0c0b0bb0bdc7
[elogind.git] / udev / udevadm-settle.c
1 /*
2  * Copyright (C) 2006-2008 Kay Sievers <kay@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <syslog.h>
27 #include <getopt.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30
31 #include "udev.h"
32
33 #define DEFAULT_TIMEOUT                 180
34 #define LOOP_PER_SECOND                 20
35
36 int udevadm_settle(struct udev *udev, int argc, char *argv[])
37 {
38         static const struct option options[] = {
39                 { "seq-start", required_argument, NULL, 's' },
40                 { "seq-end", required_argument, NULL, 'e' },
41                 { "timeout", required_argument, NULL, 't' },
42                 { "quiet", no_argument, NULL, 'q' },
43                 { "help", no_argument, NULL, 'h' },
44                 {}
45         };
46         unsigned long long start = 0;
47         unsigned long long end = 0;
48         int timeout = DEFAULT_TIMEOUT;
49         int quiet = 0;
50         struct udev_queue *udev_queue = NULL;
51         int loop;
52         int rc = 0;
53
54         dbg(udev, "version %s\n", VERSION);
55
56         while (1) {
57                 int option;
58                 int seconds;
59
60                 option = getopt_long(argc, argv, "s:e:t:qh", options, NULL);
61                 if (option == -1)
62                         break;
63
64                 switch (option) {
65                 case 's':
66                         start = strtoull(optarg, NULL, 0);
67                         break;
68                 case 'e':
69                         end = strtoull(optarg, NULL, 0);
70                         break;
71                 case 't':
72                         seconds = atoi(optarg);
73                         if (seconds >= 0)
74                                 timeout = seconds;
75                         else
76                                 fprintf(stderr, "invalid timeout value\n");
77                         dbg(udev, "timeout=%i\n", timeout);
78                         break;
79                 case 'q':
80                         quiet = 1;
81                         break;
82                 case 'h':
83                         printf("Usage: udevadm settle [--help] [--timeout=<seconds>] [--quiet]\n\n");
84                         goto exit;
85                 }
86         }
87
88         udev_queue = udev_queue_new(udev);
89         if (udev_queue == NULL)
90                 goto exit;
91         loop = timeout * LOOP_PER_SECOND;
92
93         if (start > 0) {
94                 unsigned long long kernel_seq;
95
96                 kernel_seq = udev_queue_get_kernel_seqnum(udev_queue);
97
98                 /* unless specified, the last event is the current kernel seqnum */
99                 if (end == 0)
100                         end = udev_queue_get_kernel_seqnum(udev_queue);
101
102                 if (start > end) {
103                         err(udev, "seq-start larger than seq-end, ignoring\n");
104                         fprintf(stderr, "seq-start larger than seq-end, ignoring\n");
105                         start = 0;
106                         end = 0;
107                 }
108
109                 if (start > kernel_seq || end > kernel_seq) {
110                         err(udev, "seq-start or seq-end larger than current kernel value, ignoring\n");
111                         fprintf(stderr, "seq-start or seq-end larger than current kernel value, ignoring\n");
112                         start = 0;
113                         end = 0;
114                 }
115                 info(udev, "start=%llu end=%llu current=%llu\n", start, end, kernel_seq);
116         } else {
117                 if (end > 0) {
118                         err(udev, "seq-end needs seq-start parameter, ignoring\n");
119                         fprintf(stderr, "seq-end needs seq-start parameter, ignoring\n");
120                         end = 0;
121                 }
122         }
123
124         while (loop--) {
125                 /* exit if queue is empty */
126                 if (udev_queue_get_queue_is_empty(udev_queue))
127                         break;
128
129                 /* if asked for, wait for a specific sequence of events */
130                 if (start > 0) {
131                         unsigned long long seq;
132                         int finished;
133
134                         finished = 0;
135                         for (seq = start; seq <= end; seq++) {
136                                 finished  = udev_queue_get_seqnum_is_finished(udev_queue, seq);
137                                 if (!finished)
138                                         break;
139                         }
140                         if (finished)
141                                 break;
142                 }
143                 usleep(1000 * 1000 / LOOP_PER_SECOND);
144         }
145
146         /* if we reached the timeout, print the list of remaining events */
147         if (loop <= 0) {
148                 struct udev_list_entry *list_entry;
149
150                 if (!quiet) {
151                         info(udev, "timeout waiting for udev queue\n");
152                         printf("\nudevadm settle - timeout of %i seconds reached, the event queue contains:\n", timeout);
153                         udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
154                                 printf("  %s (%s)\n",
155                                        udev_list_entry_get_name(list_entry),
156                                        udev_list_entry_get_value(list_entry));
157                 }
158                 rc = 1;
159         }
160 exit:
161         udev_queue_unref(udev_queue);
162         return rc;
163 }