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>
34 #define DEFAULT_TIMEOUT 180
35 #define LOOP_PER_SECOND 20
37 static void print_queue(struct udev *udev, const char *dir)
40 struct name_entry *item;
42 if (add_matching_files(udev, &files, dir, NULL) < 0)
45 printf("\n\nAfter the udevadm settle timeout, the events queue contains:\n\n");
47 list_for_each_entry(item, &files, node) {
48 char target[NAME_SIZE];
50 const char *filename = strrchr(item->name, '/');
55 if (*filename == '\0')
58 len = readlink(item->name, target, sizeof(target));
63 printf("%s: %s\n", filename, target);
69 int udevadm_settle(struct udev *udev, int argc, char *argv[])
71 char queuename[PATH_SIZE];
72 char filename[PATH_SIZE];
73 unsigned long long seq_kernel;
74 unsigned long long seq_udev;
78 int timeout = DEFAULT_TIMEOUT;
80 static const struct option options[] = {
81 { "timeout", 1, NULL, 't' },
82 { "help", 0, NULL, 'h' },
89 dbg(udev, "version %s\n", VERSION);
92 option = getopt_long(argc, argv, "t:h", options, NULL);
98 seconds = atoi(optarg);
102 fprintf(stderr, "invalid timeout value\n");
103 dbg(udev, "timeout=%i\n", timeout);
106 printf("Usage: udevadm settle [--help] [--timeout=<seconds>]\n\n");
111 strlcpy(queuename, udev_get_dev_path(udev), sizeof(queuename));
112 strlcat(queuename, "/.udev/queue", sizeof(queuename));
114 loop = timeout * LOOP_PER_SECOND;
116 /* wait for events in queue to finish */
120 if (stat(queuename, &statbuf) < 0) {
121 info(udev, "queue is empty\n");
124 usleep(1000 * 1000 / LOOP_PER_SECOND);
127 info(udev, "timeout waiting for queue\n");
128 print_queue(udev, queuename);
132 /* read current udev seqnum */
133 strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
134 strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
135 fd = open(filename, O_RDONLY);
138 len = read(fd, seqnum, sizeof(seqnum)-1);
143 seq_udev = strtoull(seqnum, NULL, 10);
144 info(udev, "udev seqnum = %llu\n", seq_udev);
146 /* read current kernel seqnum */
147 strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
148 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
149 fd = open(filename, O_RDONLY);
152 len = read(fd, seqnum, sizeof(seqnum)-1);
157 seq_kernel = strtoull(seqnum, NULL, 10);
158 info(udev, "kernel seqnum = %llu\n", seq_kernel);
160 /* make sure all kernel events have arrived in the queue */
161 if (seq_udev >= seq_kernel) {
162 info(udev, "queue is empty and no pending events left\n");
166 usleep(1000 * 1000 / LOOP_PER_SECOND);
167 info(udev, "queue is empty, but events still pending\n");