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