2 * Copyright (C) 2006-2008 Kay Sievers <kay@vrfy.org>
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.
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.
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/>.
29 #include <sys/types.h>
33 #define DEFAULT_TIMEOUT 180
34 #define LOOP_PER_SECOND 20
36 static void print_queue(struct udev *udev, const char *dir)
39 struct name_entry *item;
41 if (add_matching_files(udev, &files, dir, NULL) < 0)
44 printf("\n\nAfter the udevadm settle timeout, the events queue contains:\n\n");
46 list_for_each_entry(item, &files, node) {
47 char target[UTIL_NAME_SIZE];
49 const char *filename = strrchr(item->name, '/');
54 if (*filename == '\0')
57 len = readlink(item->name, target, sizeof(target));
62 printf("%s: %s\n", filename, target);
68 int udevadm_settle(struct udev *udev, int argc, char *argv[])
70 char queuename[UTIL_PATH_SIZE];
71 char filename[UTIL_PATH_SIZE];
72 unsigned long long seq_kernel;
73 unsigned long long seq_udev;
77 int timeout = DEFAULT_TIMEOUT;
79 static const struct option options[] = {
80 { "timeout", 1, NULL, 't' },
81 { "help", 0, NULL, 'h' },
88 dbg(udev, "version %s\n", VERSION);
91 option = getopt_long(argc, argv, "t:h", options, NULL);
97 seconds = atoi(optarg);
101 fprintf(stderr, "invalid timeout value\n");
102 dbg(udev, "timeout=%i\n", timeout);
105 printf("Usage: udevadm settle [--help] [--timeout=<seconds>]\n\n");
110 util_strlcpy(queuename, udev_get_dev_path(udev), sizeof(queuename));
111 util_strlcat(queuename, "/.udev/queue", sizeof(queuename));
113 loop = timeout * LOOP_PER_SECOND;
115 /* wait for events in queue to finish */
119 if (stat(queuename, &statbuf) < 0) {
120 info(udev, "queue is empty\n");
123 usleep(1000 * 1000 / LOOP_PER_SECOND);
126 info(udev, "timeout waiting for queue\n");
127 print_queue(udev, queuename);
131 /* read current udev seqnum */
132 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
133 util_strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
134 fd = open(filename, O_RDONLY);
137 len = read(fd, seqnum, sizeof(seqnum)-1);
142 seq_udev = strtoull(seqnum, NULL, 10);
143 info(udev, "udev seqnum = %llu\n", seq_udev);
145 /* read current kernel seqnum */
146 util_strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
147 util_strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
148 fd = open(filename, O_RDONLY);
151 len = read(fd, seqnum, sizeof(seqnum)-1);
156 seq_kernel = strtoull(seqnum, NULL, 10);
157 info(udev, "kernel seqnum = %llu\n", seq_kernel);
159 /* make sure all kernel events have arrived in the queue */
160 if (seq_udev >= seq_kernel) {
161 info(udev, "queue is empty and no pending events left\n");
165 usleep(1000 * 1000 / LOOP_PER_SECOND);
166 info(udev, "queue is empty, but events still pending\n");