chiark / gitweb /
c519f91cd583198eb7b92278212f7d401ce7ec8b
[elogind.git] / udev / udevadm-settle.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(struct udev *udev, const char *dir)
39 {
40         LIST_HEAD(files);
41         struct name_entry *item;
42
43         if (add_matching_files(udev, &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 udevadm_settle(struct udev *udev, int argc, char *argv[])
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         dbg(udev, "version %s\n", VERSION);
91
92         while (1) {
93                 option = getopt_long(argc, argv, "t:h", options, NULL);
94                 if (option == -1)
95                         break;
96
97                 switch (option) {
98                 case 't':
99                         seconds = atoi(optarg);
100                         if (seconds > 0)
101                                 timeout = seconds;
102                         else
103                                 fprintf(stderr, "invalid timeout value\n");
104                         dbg(udev, "timeout=%i\n", timeout);
105                         break;
106                 case 'h':
107                         printf("Usage: udevadm settle [--help] [--timeout=<seconds>]\n\n");
108                         goto exit;
109                 }
110         }
111
112         strlcpy(queuename, udev_get_dev_path(udev), sizeof(queuename));
113         strlcat(queuename, "/.udev/queue", sizeof(queuename));
114
115         loop = timeout * LOOP_PER_SECOND;
116         while (loop--) {
117                 /* wait for events in queue to finish */
118                 while (loop--) {
119                         struct stat statbuf;
120
121                         if (stat(queuename, &statbuf) < 0) {
122                                 info(udev, "queue is empty\n");
123                                 break;
124                         }
125                         usleep(1000 * 1000 / LOOP_PER_SECOND);
126                 }
127                 if (loop <= 0) {
128                         info(udev, "timeout waiting for queue\n");
129                         print_queue(udev, queuename);
130                         goto exit;
131                 }
132
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);
137                 if (fd < 0)
138                         goto exit;
139                 len = read(fd, seqnum, sizeof(seqnum)-1);
140                 close(fd);
141                 if (len <= 0)
142                         goto exit;
143                 seqnum[len] = '\0';
144                 seq_udev = strtoull(seqnum, NULL, 10);
145                 info(udev, "udev seqnum = %llu\n", seq_udev);
146
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);
151                 if (fd < 0)
152                         goto exit;
153                 len = read(fd, seqnum, sizeof(seqnum)-1);
154                 close(fd);
155                 if (len <= 0)
156                         goto exit;
157                 seqnum[len] = '\0';
158                 seq_kernel = strtoull(seqnum, NULL, 10);
159                 info(udev, "kernel seqnum = %llu\n", seq_kernel);
160
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");
164                         rc = 0;
165                         goto exit;
166                 }
167                 usleep(1000 * 1000 / LOOP_PER_SECOND);
168                 info(udev, "queue is empty, but events still pending\n");
169         }
170
171 exit:
172         return rc;
173 }