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