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.
29 #include <sys/types.h>
34 #define DEFAULT_TIMEOUT 180
35 #define LOOP_PER_SECOND 20
39 void log_message(int priority, const char *format, ...)
43 if (priority > udev_log_priority)
46 va_start(args, format);
47 vsyslog(priority, format, args);
52 int main(int argc, char *argv[], char *envp[])
54 char queuename[PATH_SIZE];
55 char filename[PATH_SIZE];
56 unsigned long long seq_kernel;
57 unsigned long long seq_udev;
61 int timeout = DEFAULT_TIMEOUT;
66 logging_init("udevsettle");
68 dbg("version %s", UDEV_VERSION);
71 for (i = 1 ; i < argc; i++) {
74 if (strncmp(arg, "--timeout=", 10) == 0) {
82 fprintf(stderr, "invalid timeout value\n");
83 dbg("timeout=%i", timeout);
84 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
85 printf("Usage: udevsettle [--help] [--timeout=<seconds>]\n");
88 fprintf(stderr, "unrecognized option '%s'\n", arg);
89 err("unrecognized option '%s'\n", arg);
93 strlcpy(queuename, udev_root, sizeof(queuename));
94 strlcat(queuename, "/" EVENT_QUEUE_DIR, sizeof(queuename));
96 loop = timeout * LOOP_PER_SECOND;
98 /* wait for events in queue to finish */
102 if (stat(queuename, &statbuf) < 0) {
103 info("queue is empty");
106 usleep(1000 * 1000 / LOOP_PER_SECOND);
109 info("timeout waiting for queue");
113 /* read current kernel seqnum */
114 strlcpy(filename, sysfs_path, sizeof(filename));
115 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
116 fd = open(filename, O_RDONLY);
119 len = read(fd, seqnum, sizeof(seqnum)-1);
124 seq_kernel = strtoull(seqnum, NULL, 10);
125 info("kernel seqnum = %llu", seq_kernel);
127 /* read current udev seqnum */
128 strlcpy(filename, udev_root, sizeof(filename));
129 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
130 fd = open(filename, O_RDONLY);
133 len = read(fd, seqnum, sizeof(seqnum)-1);
138 seq_udev = strtoull(seqnum, NULL, 10);
139 info("udev seqnum = %llu", seq_udev);
141 /* make sure all kernel events have arrived in the queue */
142 if (seq_udev >= seq_kernel) {
143 info("queue is empty and no pending events left");
147 usleep(1000 * 1000 / LOOP_PER_SECOND);
148 info("queue is empty, but events still pending");