chiark / gitweb /
vol_id: fix language in manpage
[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 int volatile is_timeout;
40
41 static void asmlinkage 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                 { "quiet", no_argument, NULL, 'q' },
58                 { "help", no_argument, NULL, 'h' },
59                 {}
60         };
61         unsigned long long start = 0;
62         unsigned long long end = 0;
63         int quiet = 0;
64         int timeout = 0;
65         struct sigaction act;
66         struct udev_queue *udev_queue = NULL;
67         int rc = 0;
68
69         dbg(udev, "version %s\n", VERSION);
70
71         /* set signal handlers */
72         memset(&act, 0x00, sizeof(act));
73         act.sa_handler = (void (*)(int)) sig_handler;
74         sigemptyset (&act.sa_mask);
75         act.sa_flags = 0;
76         sigaction(SIGALRM, &act, NULL);
77         sigaction(SIGUSR1, &act, NULL);
78
79         while (1) {
80                 int option;
81                 int seconds;
82
83                 option = getopt_long(argc, argv, "s:e:t:qh", options, NULL);
84                 if (option == -1)
85                         break;
86
87                 switch (option) {
88                 case 's':
89                         start = strtoull(optarg, NULL, 0);
90                         break;
91                 case 'e':
92                         end = strtoull(optarg, NULL, 0);
93                         break;
94                 case 't':
95                         seconds = atoi(optarg);
96                         if (seconds >= 0)
97                                 timeout = seconds;
98                         else
99                                 fprintf(stderr, "invalid timeout value\n");
100                         dbg(udev, "timeout=%i\n", timeout);
101                         break;
102                 case 'q':
103                         quiet = 1;
104                         break;
105                 case 'h':
106                         printf("Usage: udevadm settle OPTIONS\n"
107                                "  --timeout=<seconds>   maximum time to wait for events\n"
108                                "  --seq-start=<seqnum>  first seqnum to wait for\n"
109                                "  --seq-end=<seqnum>    last seqnum to wait for\n"
110                                "  --quiet               do not print list after timeout\n"
111                                "  --help\n\n");
112                         goto exit;
113                 }
114         }
115
116         if (timeout > 0)
117                 alarm(timeout);
118         else
119                 alarm(DEFAULT_TIMEOUT);
120
121         udev_queue = udev_queue_new(udev);
122         if (udev_queue == NULL)
123                 goto exit;
124
125         if (start > 0) {
126                 unsigned long long kernel_seq;
127
128                 kernel_seq = udev_queue_get_kernel_seqnum(udev_queue);
129
130                 /* unless specified, the last event is the current kernel seqnum */
131                 if (end == 0)
132                         end = udev_queue_get_kernel_seqnum(udev_queue);
133
134                 if (start > end) {
135                         err(udev, "seq-start larger than seq-end, ignoring\n");
136                         fprintf(stderr, "seq-start larger than seq-end, ignoring\n");
137                         start = 0;
138                         end = 0;
139                 }
140
141                 if (start > kernel_seq || end > kernel_seq) {
142                         err(udev, "seq-start or seq-end larger than current kernel value, ignoring\n");
143                         fprintf(stderr, "seq-start or seq-end larger than current kernel value, ignoring\n");
144                         start = 0;
145                         end = 0;
146                 }
147                 info(udev, "start=%llu end=%llu current=%llu\n", start, end, kernel_seq);
148         } else {
149                 if (end > 0) {
150                         err(udev, "seq-end needs seq-start parameter, ignoring\n");
151                         fprintf(stderr, "seq-end needs seq-start parameter, ignoring\n");
152                         end = 0;
153                 }
154         }
155
156         /* guarantee that the udev daemon isn't pre-processing */
157         if (getuid() == 0) {
158                 struct udev_ctrl *uctrl;
159
160                 uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
161                 if (uctrl != NULL) {
162                         sigset_t mask, oldmask;
163
164                         sigemptyset(&mask);
165                         sigaddset(&mask, SIGUSR1);
166                         sigaddset(&mask, SIGALRM);
167                         sigprocmask(SIG_BLOCK, &mask, &oldmask);
168                         if (udev_ctrl_send_settle(uctrl) > 0)
169                                 sigsuspend(&oldmask);
170                         udev_ctrl_unref(uctrl);
171                 }
172         }
173
174         while (!is_timeout) {
175                 /* exit if queue is empty */
176                 if (udev_queue_get_queue_is_empty(udev_queue))
177                         break;
178
179                 /* if asked for, wait for a specific sequence of events */
180                 if (start > 0) {
181                         unsigned long long seq;
182                         int finished;
183
184                         finished = 0;
185                         for (seq = start; seq <= end; seq++) {
186                                 finished  = udev_queue_get_seqnum_is_finished(udev_queue, seq);
187                                 if (!finished)
188                                         break;
189                         }
190                         if (finished)
191                                 break;
192                 }
193                 usleep(1000 * 1000 / LOOP_PER_SECOND);
194         }
195
196         /* if we reached the timeout, print the list of remaining events */
197         if (is_timeout) {
198                 struct udev_list_entry *list_entry;
199
200                 if (!quiet) {
201                         info(udev, "timeout waiting for udev queue\n");
202                         printf("\nudevadm settle - timeout of %i seconds reached, the event queue contains:\n", timeout);
203                         udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
204                                 printf("  %s (%s)\n",
205                                        udev_list_entry_get_name(list_entry),
206                                        udev_list_entry_get_value(list_entry));
207                 }
208                 rc = 1;
209         }
210 exit:
211         udev_queue_unref(udev_queue);
212         return rc;
213 }