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 int udevsettle(int argc, char *argv[], char *envp[])
40 char queuename[PATH_SIZE];
41 char filename[PATH_SIZE];
42 unsigned long long seq_kernel;
43 unsigned long long seq_udev;
47 int timeout = DEFAULT_TIMEOUT;
49 static const struct option options[] = {
50 { "timeout", 1, NULL, 't' },
51 { "help", 0, NULL, 'h' },
58 logging_init("udevsettle");
60 dbg("version %s\n", UDEV_VERSION);
64 option = getopt_long(argc, argv, "t:h", options, NULL);
70 seconds = atoi(optarg);
74 fprintf(stderr, "invalid timeout value\n");
75 dbg("timeout=%i\n", timeout);
78 printf("Usage: udevadm settle [--help] [--timeout=<seconds>]\n\n");
83 strlcpy(queuename, udev_root, sizeof(queuename));
84 strlcat(queuename, "/" EVENT_QUEUE_DIR, sizeof(queuename));
86 loop = timeout * LOOP_PER_SECOND;
88 /* wait for events in queue to finish */
92 if (stat(queuename, &statbuf) < 0) {
93 info("queue is empty\n");
96 usleep(1000 * 1000 / LOOP_PER_SECOND);
99 info("timeout waiting for queue\n");
103 /* read current udev seqnum */
104 strlcpy(filename, udev_root, sizeof(filename));
105 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
106 fd = open(filename, O_RDONLY);
109 len = read(fd, seqnum, sizeof(seqnum)-1);
114 seq_udev = strtoull(seqnum, NULL, 10);
115 info("udev seqnum = %llu\n", seq_udev);
117 /* read current kernel seqnum */
118 strlcpy(filename, sysfs_path, sizeof(filename));
119 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
120 fd = open(filename, O_RDONLY);
123 len = read(fd, seqnum, sizeof(seqnum)-1);
128 seq_kernel = strtoull(seqnum, NULL, 10);
129 info("kernel seqnum = %llu\n", seq_kernel);
131 /* make sure all kernel events have arrived in the queue */
132 if (seq_udev >= seq_kernel) {
133 info("queue is empty and no pending events left\n");
137 usleep(1000 * 1000 / LOOP_PER_SECOND);
138 info("queue is empty, but events still pending\n");