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