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