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(struct udev *udev, const char *dir)
41 struct name_entry *item;
43 if (add_matching_files(udev, &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 udevadm_settle(struct udev *udev, int argc, char *argv[])
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 dbg(udev, "version %s\n", VERSION);
93 option = getopt_long(argc, argv, "t:h", options, NULL);
99 seconds = atoi(optarg);
103 fprintf(stderr, "invalid timeout value\n");
104 dbg(udev, "timeout=%i\n", timeout);
107 printf("Usage: udevadm settle [--help] [--timeout=<seconds>]\n\n");
112 strlcpy(queuename, udev_get_dev_path(udev), sizeof(queuename));
113 strlcat(queuename, "/.udev/queue", sizeof(queuename));
115 loop = timeout * LOOP_PER_SECOND;
117 /* wait for events in queue to finish */
121 if (stat(queuename, &statbuf) < 0) {
122 info(udev, "queue is empty\n");
125 usleep(1000 * 1000 / LOOP_PER_SECOND);
128 info(udev, "timeout waiting for queue\n");
129 print_queue(udev, queuename);
133 /* read current udev seqnum */
134 strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
135 strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
136 fd = open(filename, O_RDONLY);
139 len = read(fd, seqnum, sizeof(seqnum)-1);
144 seq_udev = strtoull(seqnum, NULL, 10);
145 info(udev, "udev seqnum = %llu\n", seq_udev);
147 /* read current kernel seqnum */
148 strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
149 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
150 fd = open(filename, O_RDONLY);
153 len = read(fd, seqnum, sizeof(seqnum)-1);
158 seq_kernel = strtoull(seqnum, NULL, 10);
159 info(udev, "kernel seqnum = %llu\n", seq_kernel);
161 /* make sure all kernel events have arrived in the queue */
162 if (seq_udev >= seq_kernel) {
163 info(udev, "queue is empty and no pending events left\n");
167 usleep(1000 * 1000 / LOOP_PER_SECOND);
168 info(udev, "queue is empty, but events still pending\n");