2 * Copyright (C) 2006 Kay Sievers <kay@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <sys/types.h>
35 #define DEFAULT_TIMEOUT 180
36 #define LOOP_PER_SECOND 20
38 static void print_queue(const char *dir)
41 struct name_entry *item;
43 if (add_matching_files(&files, dir, NULL) < 0)
46 printf("\n\nAfter the udevadm settle timeout, the events queue contains:\n\n");
48 list_for_each_entry(item, &files, node) {
49 char target[NAME_SIZE];
51 const char *filename = strrchr(item->name, '/');
56 if (*filename == '\0')
59 len = readlink(item->name, target, sizeof(target));
64 printf("%s: %s\n", filename, target);
70 int udevsettle(int argc, char *argv[], char *envp[])
72 char queuename[PATH_SIZE];
73 char filename[PATH_SIZE];
74 unsigned long long seq_kernel;
75 unsigned long long seq_udev;
79 int timeout = DEFAULT_TIMEOUT;
81 static const struct option options[] = {
82 { "timeout", 1, NULL, 't' },
83 { "help", 0, NULL, 'h' },
90 logging_init("udevsettle");
92 dbg("version %s\n", VERSION);
96 option = getopt_long(argc, argv, "t:h", options, NULL);
102 seconds = atoi(optarg);
106 fprintf(stderr, "invalid timeout value\n");
107 dbg("timeout=%i\n", timeout);
110 printf("Usage: udevadm settle [--help] [--timeout=<seconds>]\n\n");
115 strlcpy(queuename, udev_root, sizeof(queuename));
116 strlcat(queuename, "/.udev/queue", sizeof(queuename));
118 loop = timeout * LOOP_PER_SECOND;
120 /* wait for events in queue to finish */
124 if (stat(queuename, &statbuf) < 0) {
125 info("queue is empty\n");
128 usleep(1000 * 1000 / LOOP_PER_SECOND);
131 info("timeout waiting for queue\n");
132 print_queue(queuename);
136 /* read current udev seqnum */
137 strlcpy(filename, udev_root, sizeof(filename));
138 strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
139 fd = open(filename, O_RDONLY);
142 len = read(fd, seqnum, sizeof(seqnum)-1);
147 seq_udev = strtoull(seqnum, NULL, 10);
148 info("udev seqnum = %llu\n", seq_udev);
150 /* read current kernel seqnum */
151 strlcpy(filename, sysfs_path, sizeof(filename));
152 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
153 fd = open(filename, O_RDONLY);
156 len = read(fd, seqnum, sizeof(seqnum)-1);
161 seq_kernel = strtoull(seqnum, NULL, 10);
162 info("kernel seqnum = %llu\n", seq_kernel);
164 /* make sure all kernel events have arrived in the queue */
165 if (seq_udev >= seq_kernel) {
166 info("queue is empty and no pending events left\n");
170 usleep(1000 * 1000 / LOOP_PER_SECOND);
171 info("queue is empty, but events still pending\n");