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