]>
Commit | Line | Data |
---|---|---|
7baada47 | 1 | /* |
7baada47 KS |
2 | * Copyright (C) 2006 Kay Sievers <kay@vrfy.org> |
3 | * | |
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. | |
7 | * | |
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. | |
12 | * | |
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., | |
27b77df4 | 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
7baada47 KS |
16 | * |
17 | */ | |
18 | ||
19 | #include <stdlib.h> | |
20 | #include <stddef.h> | |
21 | #include <string.h> | |
22 | #include <stdio.h> | |
23 | #include <unistd.h> | |
24 | #include <errno.h> | |
25 | #include <dirent.h> | |
26 | #include <fcntl.h> | |
27 | #include <syslog.h> | |
28 | #include <sys/stat.h> | |
29 | #include <sys/types.h> | |
30 | ||
31 | #include "udev.h" | |
32 | #include "udevd.h" | |
33 | ||
fc8ec932 | 34 | #define DEFAULT_TIMEOUT 180 |
7baada47 KS |
35 | #define LOOP_PER_SECOND 20 |
36 | ||
7baada47 KS |
37 | |
38 | #ifdef USE_LOG | |
39 | void log_message(int priority, const char *format, ...) | |
40 | { | |
41 | va_list args; | |
42 | ||
43 | if (priority > udev_log_priority) | |
44 | return; | |
45 | ||
46 | va_start(args, format); | |
47 | vsyslog(priority, format, args); | |
48 | va_end(args); | |
49 | } | |
50 | #endif | |
51 | ||
52 | int main(int argc, char *argv[], char *envp[]) | |
53 | { | |
54 | char queuename[PATH_SIZE]; | |
55 | char filename[PATH_SIZE]; | |
56 | unsigned long long seq_kernel; | |
57 | unsigned long long seq_udev; | |
58 | char seqnum[32]; | |
59 | int fd; | |
60 | ssize_t len; | |
fc8ec932 | 61 | int timeout = DEFAULT_TIMEOUT; |
7baada47 KS |
62 | int loop; |
63 | int i; | |
64 | int rc = 1; | |
65 | ||
66 | logging_init("udevsettle"); | |
67 | udev_config_init(); | |
68 | dbg("version %s", UDEV_VERSION); | |
e3396a2d | 69 | sysfs_init(); |
7baada47 KS |
70 | |
71 | for (i = 1 ; i < argc; i++) { | |
72 | char *arg = argv[i]; | |
73 | ||
74 | if (strncmp(arg, "--timeout=", 10) == 0) { | |
75 | char *str = &arg[10]; | |
e3396a2d | 76 | int seconds; |
7baada47 | 77 | |
e3396a2d KS |
78 | seconds = atoi(str); |
79 | if (seconds > 0) | |
80 | timeout = seconds; | |
81 | else | |
82 | fprintf(stderr, "invalid timeout value\n"); | |
7baada47 | 83 | dbg("timeout=%i", timeout); |
e3396a2d KS |
84 | } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) { |
85 | printf("Usage: udevsettle [--help] [--timeout=<seconds>]\n"); | |
7baada47 | 86 | goto exit; |
e3396a2d KS |
87 | } else { |
88 | fprintf(stderr, "unrecognized option '%s'\n", arg); | |
89 | err("unrecognized option '%s'\n", arg); | |
7baada47 KS |
90 | } |
91 | } | |
92 | ||
7baada47 KS |
93 | strlcpy(queuename, udev_root, sizeof(queuename)); |
94 | strlcat(queuename, "/" EVENT_QUEUE_DIR, sizeof(queuename)); | |
95 | ||
96 | loop = timeout * LOOP_PER_SECOND; | |
97 | while (loop--) { | |
98 | /* wait for events in queue to finish */ | |
99 | while (loop--) { | |
100 | struct stat statbuf; | |
101 | ||
102 | if (stat(queuename, &statbuf) < 0) { | |
103 | info("queue is empty"); | |
104 | break; | |
105 | } | |
106 | usleep(1000 * 1000 / LOOP_PER_SECOND); | |
107 | } | |
108 | if (loop <= 0) { | |
109 | info("timeout waiting for queue"); | |
110 | goto exit; | |
111 | } | |
112 | ||
4e2b2908 PB |
113 | /* read current udev seqnum */ |
114 | strlcpy(filename, udev_root, sizeof(filename)); | |
115 | strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename)); | |
7baada47 KS |
116 | fd = open(filename, O_RDONLY); |
117 | if (fd < 0) | |
118 | goto exit; | |
119 | len = read(fd, seqnum, sizeof(seqnum)-1); | |
120 | close(fd); | |
121 | if (len <= 0) | |
122 | goto exit; | |
123 | seqnum[len] = '\0'; | |
4e2b2908 PB |
124 | seq_udev = strtoull(seqnum, NULL, 10); |
125 | info("udev seqnum = %llu", seq_udev); | |
7baada47 | 126 | |
4e2b2908 PB |
127 | /* read current kernel seqnum */ |
128 | strlcpy(filename, sysfs_path, sizeof(filename)); | |
129 | strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename)); | |
7baada47 KS |
130 | fd = open(filename, O_RDONLY); |
131 | if (fd < 0) | |
132 | goto exit; | |
133 | len = read(fd, seqnum, sizeof(seqnum)-1); | |
134 | close(fd); | |
135 | if (len <= 0) | |
136 | goto exit; | |
137 | seqnum[len] = '\0'; | |
4e2b2908 PB |
138 | seq_kernel = strtoull(seqnum, NULL, 10); |
139 | info("kernel seqnum = %llu", seq_kernel); | |
7baada47 KS |
140 | |
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"); | |
144 | rc = 0; | |
145 | goto exit; | |
146 | } | |
147 | usleep(1000 * 1000 / LOOP_PER_SECOND); | |
148 | info("queue is empty, but events still pending"); | |
149 | } | |
150 | ||
151 | exit: | |
152 | sysfs_cleanup(); | |
153 | logging_close(); | |
154 | return rc; | |
155 | } |