chiark / gitweb /
path_id: implement in C using libudev
[elogind.git] / udev / udevadm-settle.c
1 /*
2  * Copyright (C) 2006-2008 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 <sys/stat.h>
32 #include <sys/types.h>
33
34 #include "udev.h"
35
36 #define DEFAULT_TIMEOUT                 180
37 #define LOOP_PER_SECOND                 20
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                 case SIGUSR1:
47                         ;
48         }
49 }
50
51 int udevadm_settle(struct udev *udev, int argc, char *argv[])
52 {
53         static const struct option options[] = {
54                 { "seq-start", required_argument, NULL, 's' },
55                 { "seq-end", required_argument, NULL, 'e' },
56                 { "timeout", required_argument, NULL, 't' },
57                 { "exit-if-exists", required_argument, NULL, 'E' },
58                 { "quiet", no_argument, NULL, 'q' },
59                 { "help", no_argument, NULL, 'h' },
60                 {}
61         };
62         unsigned long long start = 0;
63         unsigned long long end = 0;
64         int quiet = 0;
65         const char *exists = NULL;
66         int timeout = DEFAULT_TIMEOUT;
67         struct sigaction act;
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         sigaction(SIGUSR1, &act, NULL);
80
81         while (1) {
82                 int option;
83                 int seconds;
84
85                 option = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL);
86                 if (option == -1)
87                         break;
88
89                 switch (option) {
90                 case 's':
91                         start = strtoull(optarg, NULL, 0);
92                         break;
93                 case 'e':
94                         end = strtoull(optarg, NULL, 0);
95                         break;
96                 case 't':
97                         seconds = atoi(optarg);
98                         if (seconds >= 0)
99                                 timeout = seconds;
100                         else
101                                 fprintf(stderr, "invalid timeout value\n");
102                         dbg(udev, "timeout=%i\n", timeout);
103                         break;
104                 case 'q':
105                         quiet = 1;
106                         break;
107                 case 'E':
108                         exists = optarg;
109                         break;
110                 case 'h':
111                         printf("Usage: udevadm settle OPTIONS\n"
112                                "  --timeout=<seconds>     maximum time to wait for events\n"
113                                "  --seq-start=<seqnum>    first seqnum to wait for\n"
114                                "  --seq-end=<seqnum>      last seqnum to wait for\n"
115                                "  --exit-if-exists=<file> stop waiting if file exists\n"
116                                "  --quiet                 do not print list after timeout\n"
117                                "  --help\n\n");
118                         exit(0);
119                 }
120         }
121
122         if (timeout > 0)
123                 alarm(timeout);
124         else
125                 is_timeout = 1;
126
127         udev_queue = udev_queue_new(udev);
128         if (udev_queue == NULL)
129                 exit(2);
130
131         if (start > 0) {
132                 unsigned long long kernel_seq;
133
134                 kernel_seq = udev_queue_get_kernel_seqnum(udev_queue);
135
136                 /* unless specified, the last event is the current kernel seqnum */
137                 if (end == 0)
138                         end = udev_queue_get_kernel_seqnum(udev_queue);
139
140                 if (start > end) {
141                         err(udev, "seq-start larger than seq-end, ignoring\n");
142                         fprintf(stderr, "seq-start larger than seq-end, ignoring\n");
143                         start = 0;
144                         end = 0;
145                 }
146
147                 if (start > kernel_seq || end > kernel_seq) {
148                         err(udev, "seq-start or seq-end larger than current kernel value, ignoring\n");
149                         fprintf(stderr, "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                         fprintf(stderr, "seq-end needs seq-start parameter, ignoring\n");
158                         end = 0;
159                 }
160         }
161
162         /* guarantee that the udev daemon isn't pre-processing */
163         if (getuid() == 0) {
164                 struct udev_ctrl *uctrl;
165
166                 uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
167                 if (uctrl != NULL) {
168                         sigset_t mask, oldmask;
169
170                         sigemptyset(&mask);
171                         sigaddset(&mask, SIGUSR1);
172                         sigaddset(&mask, SIGALRM);
173                         sigprocmask(SIG_BLOCK, &mask, &oldmask);
174                         if (udev_ctrl_send_settle(uctrl) > 0)
175                                 sigsuspend(&oldmask);
176                         sigprocmask(SIG_SETMASK, &oldmask, NULL);
177                         udev_ctrl_unref(uctrl);
178                 }
179         }
180
181         while (1) {
182                 struct stat statbuf;
183
184                 if (exists != NULL && stat(exists, &statbuf) == 0) {
185                         rc = 0;
186                         break;
187                 }
188
189                 if (start > 0) {
190                         /* if asked for, wait for a specific sequence of events */
191                         if (udev_queue_get_seqnum_sequence_is_finished(udev_queue, start, end) == 1) {
192                                 rc = 0;
193                                 break;
194                         }
195                 } else {
196                         /* exit if queue is empty */
197                         if (udev_queue_get_queue_is_empty(udev_queue)) {
198                                 rc = 0;
199                                 break;
200                         }
201                 }
202
203                 if (is_timeout)
204                         break;
205
206                 usleep(1000 * 1000 / LOOP_PER_SECOND);
207         }
208
209         /* if we reached the timeout, print the list of remaining events */
210         if (is_timeout) {
211                 struct udev_list_entry *list_entry;
212
213                 if (!quiet && udev_queue_get_queued_list_entry(udev_queue) != NULL) {
214                         info(udev, "timeout waiting for udev queue\n");
215                         printf("\nudevadm settle - timeout of %i seconds reached, the event queue contains:\n", timeout);
216                         udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
217                                 printf("  %s (%s)\n",
218                                        udev_list_entry_get_name(list_entry),
219                                        udev_list_entry_get_value(list_entry));
220                 }
221         }
222
223         udev_queue_unref(udev_queue);
224         return rc;
225 }