chiark / gitweb /
man: fix typos
[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                 { "timeout", required_argument, NULL, 't' },
40                 { "help", no_argument, NULL, 'h' },
41                 {}
42         };
43         int timeout = DEFAULT_TIMEOUT;
44         struct udev_queue *udev_queue = NULL;
45         int loop;
46         int rc = 0;
47
48         dbg(udev, "version %s\n", VERSION);
49
50         while (1) {
51                 int option;
52                 int seconds;
53
54                 option = getopt_long(argc, argv, "t:h", options, NULL);
55                 if (option == -1)
56                         break;
57
58                 switch (option) {
59                 case 't':
60                         seconds = atoi(optarg);
61                         if (seconds > 0)
62                                 timeout = seconds;
63                         else
64                                 fprintf(stderr, "invalid timeout value\n");
65                         dbg(udev, "timeout=%i\n", timeout);
66                         break;
67                 case 'h':
68                         printf("Usage: udevadm settle [--help] [--timeout=<seconds>]\n\n");
69                         goto exit;
70                 }
71         }
72
73         udev_queue = udev_queue_new(udev);
74         if (udev_queue == NULL)
75                 goto exit;
76         loop = timeout * LOOP_PER_SECOND;
77         while (loop--) {
78                 if (udev_queue_get_queue_is_empty(udev_queue))
79                         break;
80                 usleep(1000 * 1000 / LOOP_PER_SECOND);
81         }
82         if (loop <= 0) {
83                 struct udev_list_entry *list_entry;
84
85                 info(udev, "timeout waiting for udev queue\n");
86                 printf("\ndevadm settle timeout of %i seconds reached, the event queue contains:\n", timeout);
87                 udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
88                         printf("  '%s' [%s]\n",
89                                udev_list_entry_get_name(list_entry),
90                                udev_list_entry_get_value(list_entry));
91                 rc = 1;
92         }
93 exit:
94         udev_queue_unref(udev_queue);
95         return rc;
96 }