]> git.ipfire.org Git - thirdparty/systemd.git/blob - wait_for_sysfs.c
[PATCH] update wait_for_sysfs with a bunch more devices thanks to user reports.
[thirdparty/systemd.git] / wait_for_sysfs.c
1 /*
2 * wait_for_sysfs.c - small program to delay the execution
3 * of /etc/hotplug.d/ programs, until sysfs
4 * is fully populated by the kernel. Depending on
5 * the type of device, we wait for all expected
6 * directories and then just exit.
7 *
8 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25 #include <stdio.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33
34 #include "logging.h"
35 #include "libsysfs/sysfs/libsysfs.h"
36
37 #ifdef LOG
38 unsigned char logname[LOGNAME_SIZE];
39 void log_message(int level, const char *format, ...)
40 {
41 va_list args;
42
43 va_start(args, format);
44 vsyslog(level, format, args);
45 va_end(args);
46 }
47 #endif
48
49 #define WAIT_MAX_SECONDS 5
50 #define WAIT_LOOP_PER_SECOND 20
51
52 /* wait for specific file to show up, normally the "dev"-file */
53 static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev)
54 {
55 static struct class_file {
56 char *subsystem;
57 char *file;
58 } class_files[] = {
59 { .subsystem = "net", .file = "ifindex" },
60 { .subsystem = "scsi_host", .file = "unique_id" },
61 { .subsystem = "scsi_device", .file = NULL },
62 { .subsystem = "pcmcia_socket", .file = "card_type" },
63 { .subsystem = "usb_host", .file = NULL },
64 { .subsystem = "bluetooth", .file = "address" },
65 { .subsystem = "i2c-adapter", .file = NULL },
66 { .subsystem = "pci_bus", .file = NULL },
67 { NULL, NULL }
68 };
69 struct class_file *classfile;
70 const char *file = "dev";
71 int loop;
72
73 /* look if we want to look for another file instead of "dev" */
74 for (classfile = class_files; classfile->subsystem != NULL; classfile++) {
75 if (strcmp(class_dev->classname, classfile->subsystem) == 0) {
76 if (classfile->file == NULL) {
77 dbg("class '%s' has no file to wait for", class_dev->classname);
78 return 0;
79 }
80 file = classfile->file;
81 break;
82 }
83 }
84 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, file);
85
86 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
87 while (--loop) {
88 if (sysfs_get_classdev_attr(class_dev, file) != NULL) {
89 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
90 return 0;
91 }
92
93 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
94 }
95
96 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
97 return -1;
98 }
99
100 /* skip waiting for physical device */
101 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
102 {
103 /* List of devices without a "device" symlink
104 * set .device to NULL to accept all devices in that subsystem */
105 static struct class_device {
106 char *subsystem;
107 char *device;
108 } class_device[] = {
109 { .subsystem = "block", .device = "double" },
110 { .subsystem = "block", .device = "nb" },
111 { .subsystem = "block", .device = "ram" },
112 { .subsystem = "block", .device = "loop" },
113 { .subsystem = "block", .device = "fd" },
114 { .subsystem = "block", .device = "md" },
115 { .subsystem = "block", .device = "dos_cd" },
116 { .subsystem = "block", .device = "rflash" },
117 { .subsystem = "block", .device = "rom" },
118 { .subsystem = "block", .device = "rrom" },
119 { .subsystem = "block", .device = "flash" },
120 { .subsystem = "block", .device = "msd" },
121 { .subsystem = "block", .device = "sbpcd" },
122 { .subsystem = "block", .device = "pcd" },
123 { .subsystem = "block", .device = "pf" },
124 { .subsystem = "block", .device = "scd" },
125 { .subsystem = "block", .device = "ubd" },
126 { .subsystem = "input", .device = "event" },
127 { .subsystem = "input", .device = "mice" },
128 { .subsystem = "input", .device = "mouse" },
129 { .subsystem = "input", .device = "ts" },
130 { .subsystem = "vc", .device = NULL },
131 { .subsystem = "tty", .device = NULL },
132 { .subsystem = "cpuid", .device = "cpu" },
133 { .subsystem = "graphics", .device = "fb" },
134 { .subsystem = "mem", .device = NULL },
135 { .subsystem = "misc", .device = NULL },
136 { .subsystem = "msr", .device = NULL },
137 { .subsystem = "netlink", .device = NULL },
138 { .subsystem = "net", .device = NULL },
139 { .subsystem = "sound", .device = NULL },
140 { .subsystem = "printer", .device = "lp" },
141 { .subsystem = "nvidia", .device = NULL },
142 { .subsystem = "video4linux", .device = NULL },
143 { .subsystem = "lirc", .device = NULL },
144 { .subsystem = "firmware", .device = NULL },
145 { .subsystem = "drm", .device = NULL },
146 { .subsystem = "pci_bus", .device = NULL },
147 { NULL, NULL }
148 };
149 struct class_device *classdevice;
150 int len;
151
152 /* look if we want to look for another file instead of "dev" */
153 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
154 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
155 /* if device is NULL, all devices in this class are ok */
156 if (classdevice->device == NULL)
157 return 1;
158
159 len = strlen(classdevice->device);
160
161 /* see if device name matches */
162 if (strncmp(class_dev->name, classdevice->device, len) != 0)
163 continue;
164
165 /* exact match */
166 if (strlen(class_dev->name) == len)
167 return 1;
168
169 /* instance numbers are matching too */
170 if (isdigit(class_dev->name[len]))
171 return 1;
172 }
173 }
174
175 return 0;
176 }
177
178 /* skip waiting for the bus */
179 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
180 {
181 static char *devices_without_bus[] = {
182 "scsi_host",
183 "i2c-adapter",
184 NULL
185 };
186 char **device;
187
188 for (device = devices_without_bus; *device != NULL; device++) {
189 int len = strlen(*device);
190
191 if (strncmp(class_dev->classname, *device, len) == 0)
192 return 1;
193 }
194
195 return 0;
196 }
197
198 /* wait for the bus and for a bus specific file to show up */
199 static int wait_for_bus_device(struct sysfs_device *device_dev)
200 {
201 static struct bus_file {
202 char *bus;
203 char *file;
204 } bus_files[] = {
205 { .bus = "scsi", .file = "vendor" },
206 { .bus = "usb", .file = "idVendor" },
207 { .bus = "usb", .file = "iInterface" },
208 { .bus = "usb", .file = "bNumEndpoints" },
209 { .bus = "usb-serial", .file = "detach_state" },
210 { .bus = "ide", .file = "detach_state" },
211 { .bus = "pci", .file = "vendor" },
212 { .bus = "platform", .file = "detach_state" },
213 { .bus = "i2c", .file = "detach_state" },
214 { NULL }
215 };
216 struct bus_file *busfile;
217 int loop;
218
219 /* wait for the /bus-device link to the /device-device */
220 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
221 while (--loop) {
222 if (sysfs_get_device_bus(device_dev) == 0)
223 break;
224
225 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
226 }
227 if (loop == 0) {
228 dbg("error: getting /bus-device link");
229 return -1;
230 }
231 dbg("/bus-device link found for bus '%s'", device_dev->bus);
232
233 /* wait for a bus specific file to show up */
234 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
235 while (--loop) {
236 int found = 0;
237
238 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
239 if (strcmp(device_dev->bus, busfile->bus) == 0) {
240 found = 1;
241 dbg("looking at bus '%s' for specific file '%s'", device_dev->bus, busfile->file);
242 if (sysfs_get_device_attr(device_dev, busfile->file) != NULL) {
243 dbg("bus '%s' specific file '%s' found", device_dev->bus, busfile->file);
244 return 0;
245 }
246 }
247 }
248 if (found == 0) {
249 info("error: unknown bus, please report to "
250 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
251 return -1;
252 }
253 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
254 }
255
256 dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
257 return -1;
258 }
259
260 int main(int argc, char *argv[], char *envp[])
261 {
262 const char *devpath = "";
263 const char *action;
264 const char *subsystem;
265 char sysfs_path[SYSFS_PATH_MAX];
266 char filename[SYSFS_PATH_MAX];
267 struct sysfs_class_device *class_dev;
268 struct sysfs_class_device *class_dev_parent;
269 struct sysfs_device *device_dev = NULL;
270 int loop;
271 int rc = 0;
272
273 init_logging("wait_for_sysfs");
274
275 if (argc != 2) {
276 dbg("error: subsystem");
277 return 1;
278 }
279 subsystem = argv[1];
280
281 devpath = getenv ("DEVPATH");
282 if (!devpath) {
283 dbg("error: no DEVPATH");
284 return 1;
285 }
286
287 action = getenv ("ACTION");
288 if (!action) {
289 dbg("error: no ACTION");
290 return 1;
291 }
292
293 /* we only wait on an add event */
294 if (strcmp(action, "add") != 0)
295 return 0;
296
297 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
298 dbg("error: no sysfs path");
299 return 2;
300 }
301
302 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
303 /* open the class device we are called for */
304 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
305 filename[SYSFS_PATH_MAX-1] = '\0';
306
307 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
308 while (--loop) {
309 class_dev = sysfs_open_class_device_path(filename);
310 if (class_dev)
311 break;
312
313 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
314 }
315 if (class_dev == NULL) {
316 dbg("error: getting class_device");
317 rc = 3;
318 goto exit;
319 }
320 dbg("class_device opened '%s'", filename);
321
322 if (wait_for_class_device_attributes(class_dev) != 0) {
323 rc = 4;
324 goto exit_class;
325 }
326
327 /* skip devices without /device-link */
328 if (class_device_expect_no_device_link(class_dev)) {
329 dbg("no device symlink expected for '%s', ", class_dev->name);
330 goto exit_class;
331 }
332
333 /* the symlink may be on the parent device */
334 class_dev_parent = sysfs_get_classdev_parent(class_dev);
335 if (class_dev_parent)
336 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
337
338 /* wait for the symlink to the /device-device */
339 dbg("waiting for symlink to /device-device");
340 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
341 while (--loop) {
342 if (class_dev_parent)
343 device_dev = sysfs_get_classdev_device(class_dev_parent);
344 else
345 device_dev = sysfs_get_classdev_device(class_dev);
346
347 if (device_dev)
348 break;
349
350 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
351 }
352 if (device_dev == NULL) {
353 dbg("error: getting /device-device");
354 rc = 5;
355 goto exit_class;
356 }
357 dbg("device symlink found pointing to '%s'", device_dev->path);
358
359 /* wait for the bus value */
360 if (class_device_expect_no_bus(class_dev)) {
361 dbg("no bus device expected for '%s', ", class_dev->classname);
362 } else {
363 if (wait_for_bus_device(device_dev) != 0)
364 rc = 6;
365 }
366
367 exit_class:
368 sysfs_close_class_device(class_dev);
369
370 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
371 /* open the path we are called for */
372 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
373 filename[SYSFS_PATH_MAX-1] = '\0';
374
375 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
376 while (--loop) {
377 device_dev = sysfs_open_device_path(filename);
378 if (device_dev)
379 break;
380
381 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
382 }
383 if (device_dev == NULL) {
384 dbg("error: getting /device-device");
385 rc = 7;
386 goto exit;
387 }
388 dbg("device_device opened '%s'", filename);
389
390 /* wait for the bus value */
391 if (wait_for_bus_device(device_dev) != 0)
392 rc = 8;
393
394 sysfs_close_device(device_dev);
395
396 } else {
397 dbg("unhandled sysfs path, no need to wait");
398 }
399
400 exit:
401 if (rc == 0)
402 dbg("result: waiting for sysfs successful '%s'", devpath);
403 else
404 info("error: wait_for_sysfs needs an update to handle the device '%s' "
405 "properly, please report to <linux-hotplug-devel@lists.sourceforge.net>",
406 devpath);
407
408 return rc;
409 }