]>
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 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 static int wait_for_class_device_attributes(struct sysfs_class_device
*class_dev
)
54 static struct class_file
{
58 { .subsystem
= "net", .file
= "ifindex" },
59 { .subsystem
= "usb_host", .file
= NULL
},
60 { .subsystem
= "pcmcia_socket", .file
= NULL
},
63 struct class_file
*classfile
;
64 const char *file
= "dev";
67 /* look if we want to look for another file instead of "dev" */
68 for (classfile
= class_files
; classfile
->subsystem
!= NULL
; classfile
++) {
69 if (strcmp(class_dev
->classname
, classfile
->subsystem
) == 0) {
70 if (classfile
->file
== NULL
) {
71 dbg("class '%s' has no file to wait for", class_dev
->classname
);
74 file
= classfile
->file
;
78 dbg("looking at class '%s' for specific file '%s'", class_dev
->classname
, file
);
80 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
82 if (sysfs_get_classdev_attr(class_dev
, file
) != NULL
) {
83 dbg("class '%s' specific file '%s' found", class_dev
->classname
, file
);
88 dbg("error: getting bus '%s' specific file '%s'", class_dev
->classname
, file
);
92 static int class_device_expect_no_device_link(struct sysfs_class_device
*class_dev
)
96 static char *devices_without_link
[] = {
124 for (device
= devices_without_link
; *device
!= NULL
; device
++) {
125 int len
= strlen(*device
);
127 /* look if name matches */
128 if (strncmp(class_dev
->name
, *device
, len
) != 0)
132 if (strlen(class_dev
->name
) == len
)
135 /* instance numbers are matching too */
136 if (isdigit(class_dev
->name
[len
]))
143 static int wait_for_bus_device(struct sysfs_device
*device_dev
)
145 static struct bus_file
{
149 { .bus
= "scsi", .file
= "vendor" },
150 { .bus
= "usb", .file
= "idVendor" },
151 { .bus
= "usb", .file
= "iInterface" },
152 { .bus
= "usb-serial", .file
= "detach_state" },
153 { .bus
= "ide", .file
= "detach_state" },
154 { .bus
= "pci", .file
= "vendor" },
157 struct bus_file
*busfile
;
160 /* wait for the /bus-device link to the /device-device */
161 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
163 if (sysfs_get_device_bus(device_dev
) == 0)
166 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
169 dbg("error: getting /bus-device link");
172 dbg("/bus-device link found for bus '%s'", device_dev
->bus
);
174 /* wait for a bus specific file to show up */
175 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
177 for (busfile
= bus_files
; busfile
->bus
!= NULL
; busfile
++) {
178 if (strcmp(device_dev
->bus
, busfile
->bus
) == 0) {
179 dbg("looking at bus '%s' for specific file '%s'", device_dev
->bus
, busfile
->file
);
180 if (sysfs_get_device_attr(device_dev
, busfile
->file
) != NULL
) {
181 dbg("bus '%s' specific file '%s' found", device_dev
->bus
, busfile
->file
);
184 if (busfile
->bus
== NULL
) {
185 info("error: unknown bus, update the build-in list '%s'", device_dev
->bus
);
192 dbg("error: getting bus '%s' specific file '%s'", device_dev
->bus
, busfile
->file
);
196 int main(int argc
, char *argv
[], char *envp
[])
198 const char *devpath
= "";
200 const char *subsystem
;
201 char sysfs_path
[SYSFS_PATH_MAX
];
202 char filename
[SYSFS_PATH_MAX
];
203 struct sysfs_class_device
*class_dev
;
204 struct sysfs_class_device
*class_dev_parent
;
205 struct sysfs_device
*device_dev
= NULL
;
210 dbg("error: subsystem");
215 devpath
= getenv ("DEVPATH");
217 dbg("error: no DEVPATH");
221 action
= getenv ("ACTION");
223 dbg("error: no ACTION");
227 if (strcmp(action
, "add") != 0)
230 if (sysfs_get_mnt_path(sysfs_path
, SYSFS_PATH_MAX
) != 0) {
231 dbg("error: no sysfs path");
235 if ((strncmp(devpath
, "/block/", 7) == 0) || (strncmp(devpath
, "/class/", 7) == 0)) {
236 /* open the class device we are called for */
237 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s%s", sysfs_path
, devpath
);
238 filename
[SYSFS_PATH_MAX
-1] = '\0';
240 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
242 class_dev
= sysfs_open_class_device_path(filename
);
246 if (class_dev
== NULL
) {
247 dbg("error: getting class_device");
251 dbg("class_device opened '%s'", filename
);
253 wait_for_class_device_attributes(class_dev
);
255 if (class_device_expect_no_device_link(class_dev
)) {
256 dbg("no device symlink expected");
257 sysfs_close_class_device(class_dev
);
261 /* the symlink may be on the parent device */
262 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
263 if (class_dev_parent
)
264 dbg("looking at parent device for device link '%s'", class_dev_parent
->path
);
266 /* wait for the symlink to the /device-device */
267 dbg("waiting for symlink to /device-device");
268 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
270 if (class_dev_parent
)
271 device_dev
= sysfs_get_classdev_device(class_dev_parent
);
273 device_dev
= sysfs_get_classdev_device(class_dev
);
278 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
280 if (device_dev
== NULL
) {
281 dbg("error: getting /device-device");
282 sysfs_close_class_device(class_dev
);
286 dbg("device symlink found pointing to '%s'", device_dev
->path
);
288 /* wait for the bus value */
289 if (wait_for_bus_device(device_dev
) != 0)
291 sysfs_close_class_device(class_dev
);
296 } else if ((strncmp(devpath
, "/devices/", 9) == 0)) {
297 /* open the path we are called for */
298 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s%s", sysfs_path
, devpath
);
299 filename
[SYSFS_PATH_MAX
-1] = '\0';
301 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
303 device_dev
= sysfs_open_device_path(filename
);
307 if (device_dev
== NULL
) {
308 dbg("error: getting /device-device");
312 dbg("device_device opened '%s'", filename
);
314 /* wait for the bus value */
315 if (wait_for_bus_device(device_dev
) != 0)
318 sysfs_close_device(device_dev
);
324 dbg("unhandled sysfs path, no need to wait");
329 info("result: waiting for sysfs successful '%s'", devpath
);
331 info("result: waiting for sysfs failed '%s'", devpath
);