]>
git.ipfire.org Git - thirdparty/systemd.git/blob - wait_for_sysfs.c
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.
8 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
35 #include "libsysfs/sysfs/libsysfs.h"
38 unsigned char logname
[LOGNAME_SIZE
];
39 void log_message(int level
, const char *format
, ...)
43 va_start(args
, format
);
44 vsyslog(level
, format
, args
);
49 #define WAIT_MAX_SECONDS 5
50 #define WAIT_LOOP_PER_SECOND 20
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
)
55 static struct class_file
{
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
},
69 struct class_file
*classfile
;
70 const char *file
= "dev";
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
);
80 file
= classfile
->file
;
84 dbg("looking at class '%s' for specific file '%s'", class_dev
->classname
, file
);
86 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
88 if (sysfs_get_classdev_attr(class_dev
, file
) != NULL
) {
89 dbg("class '%s' specific file '%s' found", class_dev
->classname
, file
);
93 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
96 dbg("error: getting class '%s' specific file '%s'", class_dev
->classname
, file
);
100 /* skip waiting for physical device */
101 static int class_device_expect_no_device_link(struct sysfs_class_device
*class_dev
)
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
{
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
},
149 struct class_device
*classdevice
;
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
)
159 len
= strlen(classdevice
->device
);
161 /* see if device name matches */
162 if (strncmp(class_dev
->name
, classdevice
->device
, len
) != 0)
166 if (strlen(class_dev
->name
) == len
)
169 /* instance numbers are matching too */
170 if (isdigit(class_dev
->name
[len
]))
178 /* skip waiting for the bus */
179 static int class_device_expect_no_bus(struct sysfs_class_device
*class_dev
)
181 static char *devices_without_bus
[] = {
188 for (device
= devices_without_bus
; *device
!= NULL
; device
++) {
189 int len
= strlen(*device
);
191 if (strncmp(class_dev
->classname
, *device
, len
) == 0)
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
)
201 static struct bus_file
{
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" },
216 struct bus_file
*busfile
;
219 /* wait for the /bus-device link to the /device-device */
220 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
222 if (sysfs_get_device_bus(device_dev
) == 0)
225 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
228 dbg("error: getting /bus-device link");
231 dbg("/bus-device link found for bus '%s'", device_dev
->bus
);
233 /* wait for a bus specific file to show up */
234 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
238 for (busfile
= bus_files
; busfile
->bus
!= NULL
; busfile
++) {
239 if (strcmp(device_dev
->bus
, busfile
->bus
) == 0) {
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
);
249 info("error: unknown bus, please report to "
250 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev
->bus
);
253 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
256 dbg("error: getting bus '%s' specific file '%s'", device_dev
->bus
, busfile
->file
);
260 int main(int argc
, char *argv
[], char *envp
[])
262 const char *devpath
= "";
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
;
273 init_logging("wait_for_sysfs");
276 dbg("error: subsystem");
281 devpath
= getenv ("DEVPATH");
283 dbg("error: no DEVPATH");
287 action
= getenv ("ACTION");
289 dbg("error: no ACTION");
293 /* we only wait on an add event */
294 if (strcmp(action
, "add") != 0)
297 if (sysfs_get_mnt_path(sysfs_path
, SYSFS_PATH_MAX
) != 0) {
298 dbg("error: no sysfs path");
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';
307 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
309 class_dev
= sysfs_open_class_device_path(filename
);
313 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
315 if (class_dev
== NULL
) {
316 dbg("error: getting class_device");
320 dbg("class_device opened '%s'", filename
);
322 if (wait_for_class_device_attributes(class_dev
) != 0) {
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
);
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
);
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
;
342 if (class_dev_parent
)
343 device_dev
= sysfs_get_classdev_device(class_dev_parent
);
345 device_dev
= sysfs_get_classdev_device(class_dev
);
350 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
352 if (device_dev
== NULL
) {
353 dbg("error: getting /device-device");
357 dbg("device symlink found pointing to '%s'", device_dev
->path
);
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
);
363 if (wait_for_bus_device(device_dev
) != 0)
368 sysfs_close_class_device(class_dev
);
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';
375 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
377 device_dev
= sysfs_open_device_path(filename
);
381 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
383 if (device_dev
== NULL
) {
384 dbg("error: getting /device-device");
388 dbg("device_device opened '%s'", filename
);
390 /* wait for the bus value */
391 if (wait_for_bus_device(device_dev
) != 0)
394 sysfs_close_device(device_dev
);
397 dbg("unhandled sysfs path, no need to wait");
402 dbg("result: waiting for sysfs successful '%s'", devpath
);
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>",