chiark / gitweb /
cb63a66bba6bf624cf76a2ea95c4e94ab5bbfbc9
[elogind.git] / udevsettle.c
1 /*
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.,
15  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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 <getopt.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31
32 #include "udev.h"
33 #include "udevd.h"
34
35 #define DEFAULT_TIMEOUT                 180
36 #define LOOP_PER_SECOND                 20
37
38 static void print_queue(const char *dir)
39 {
40         LIST_HEAD(files);
41         struct name_entry *item;
42
43         if (add_matching_files(&files, dir, NULL) < 0)
44                 return;
45
46         printf("\n\nAfter the udevadm settle timeout, the events queue contains:\n\n");
47
48         list_for_each_entry(item, &files, node) {
49                 char target[NAME_SIZE];
50                 size_t len;
51                 const char *filename = strrchr(item->name, '/');
52
53                 if (filename == NULL)
54                         continue;
55                 filename++;
56                 if (*filename == '\0')
57                         continue;
58
59                 len = readlink(item->name, target, sizeof(target));
60                 if (len < 0)
61                         continue;
62                 target[len] = '\0';
63
64                 printf("%s: %s\n", filename, target);
65         }
66
67         printf("\n\n");
68 }
69
70 int udevsettle(int argc, char *argv[], char *envp[])
71 {
72         char queuename[PATH_SIZE];
73         char filename[PATH_SIZE];
74         unsigned long long seq_kernel;
75         unsigned long long seq_udev;
76         char seqnum[32];
77         int fd;
78         ssize_t len;
79         int timeout = DEFAULT_TIMEOUT;
80         int loop;
81         static const struct option options[] = {
82                 { "timeout", 1, NULL, 't' },
83                 { "help", 0, NULL, 'h' },
84                 {}
85         };
86         int option;
87         int rc = 1;
88         int seconds;
89
90         logging_init("udevsettle");
91         udev_config_init();
92         dbg("version %s\n", UDEV_VERSION);
93         sysfs_init();
94
95         while (1) {
96                 option = getopt_long(argc, argv, "t:h", options, NULL);
97                 if (option == -1)
98                         break;
99
100                 switch (option) {
101                 case 't':
102                         seconds = atoi(optarg);
103                         if (seconds > 0)
104                                 timeout = seconds;
105                         else
106                                 fprintf(stderr, "invalid timeout value\n");
107                         dbg("timeout=%i\n", timeout);
108                         break;
109                 case 'h':
110                         printf("Usage: udevadm settle [--help] [--timeout=<seconds>]\n\n");
111                         goto exit;
112                 }
113         }
114
115         strlcpy(queuename, udev_root, sizeof(queuename));
116         strlcat(queuename, "/" EVENT_QUEUE_DIR, sizeof(queuename));
117
118         loop = timeout * LOOP_PER_SECOND;
119         while (loop--) {
120                 /* wait for events in queue to finish */
121                 while (loop--) {
122                         struct stat statbuf;
123
124                         if (stat(queuename, &statbuf) < 0) {
125                                 info("queue is empty\n");
126                                 break;
127                         }
128                         usleep(1000 * 1000 / LOOP_PER_SECOND);
129                 }
130                 if (loop <= 0) {
131                         info("timeout waiting for queue\n");
132                         print_queue(queuename);
133                         goto exit;
134                 }
135
136                 /* read current udev seqnum */
137                 strlcpy(filename, udev_root, sizeof(filename));
138                 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
139                 fd = open(filename, O_RDONLY);
140                 if (fd < 0)
141                         goto exit;
142                 len = read(fd, seqnum, sizeof(seqnum)-1);
143                 close(fd);
144                 if (len <= 0)
145                         goto exit;
146                 seqnum[len] = '\0';
147                 seq_udev = strtoull(seqnum, NULL, 10);
148                 info("udev seqnum = %llu\n", seq_udev);
149
150                 /* read current kernel seqnum */
151                 strlcpy(filename, sysfs_path, sizeof(filename));
152                 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
153                 fd = open(filename, O_RDONLY);
154                 if (fd < 0)
155                         goto exit;
156                 len = read(fd, seqnum, sizeof(seqnum)-1);
157                 close(fd);
158                 if (len <= 0)
159                         goto exit;
160                 seqnum[len] = '\0';
161                 seq_kernel = strtoull(seqnum, NULL, 10);
162                 info("kernel seqnum = %llu\n", seq_kernel);
163
164                 /* make sure all kernel events have arrived in the queue */
165                 if (seq_udev >= seq_kernel) {
166                         info("queue is empty and no pending events left\n");
167                         rc = 0;
168                         goto exit;
169                 }
170                 usleep(1000 * 1000 / LOOP_PER_SECOND);
171                 info("queue is empty, but events still pending\n");
172         }
173
174 exit:
175         sysfs_cleanup();
176         logging_close();
177         return rc;
178 }