]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_sysfs.c
[PATCH] don't wait for sysfs if the kernel(2.6.10-rc2) tells us what not to expect
[thirdparty/systemd.git] / udev_sysfs.c
1 /*
2 * udev_sysfs.c - sysfs linux kernel specific knowledge
3 *
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
21
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30
31 #include "libsysfs/sysfs/libsysfs.h"
32 #include "udev_version.h"
33 #include "udev_sysfs.h"
34 #include "udev_lib.h"
35 #include "logging.h"
36
37 /* list of subsystem specific files, NULL if there is no file to wait for */
38 static struct subsystem_file {
39 char *subsystem;
40 char *file;
41 } subsystem_files[] = {
42 { .subsystem = "net", .file = "ifindex" },
43 { .subsystem = "scsi_host", .file = "unique_id" },
44 { .subsystem = "scsi_device", .file = NULL },
45 { .subsystem = "pcmcia_socket", .file = "card_type" },
46 { .subsystem = "usb_host", .file = NULL },
47 { .subsystem = "bluetooth", .file = "address" },
48 { .subsystem = "firmware", .file = "data" },
49 { .subsystem = "i2c-adapter", .file = NULL },
50 { .subsystem = "pci_bus", .file = NULL },
51 { .subsystem = "ieee1394", .file = NULL },
52 { .subsystem = "ieee1394_host", .file = NULL },
53 { .subsystem = "ieee1394_node", .file = NULL },
54 { NULL, NULL }
55 };
56
57 int subsystem_expect_no_dev(const char *subsystem)
58 {
59 struct subsystem_file *file;
60
61 for (file = subsystem_files; file->subsystem != NULL; file++)
62 if (strcmp(subsystem, file->subsystem) == 0)
63 return 1;
64
65 return 0;
66 }
67
68 /* get subsystem specific files, returns "dev" if no other found */
69 static char *get_subsystem_specific_file(const char *subsystem)
70 {
71 struct subsystem_file *file;
72
73 /* look if we want to look for another file instead of "dev" */
74 for (file = subsystem_files; file->subsystem != NULL; file++)
75 if (strcmp(subsystem, file->subsystem) == 0)
76 return file->file;
77
78 return "dev";
79 }
80
81 /* wait for class pecific file to show up */
82 static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev,
83 const char **error)
84 {
85 const char *file;
86 char filename[SYSFS_PATH_MAX];
87 int loop;
88
89 file = get_subsystem_specific_file(class_dev->classname);
90 if (file == NULL) {
91 dbg("class '%s' has no file to wait for", class_dev->classname);
92 return 0;
93 }
94
95 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", class_dev->path, file);
96 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, filename);
97
98 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
99 while (--loop) {
100 struct stat stats;
101
102 if (stat(class_dev->path, &stats) == -1) {
103 dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
104 return -ENODEV;
105 }
106
107 if (stat(filename, &stats) == 0) {
108 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
109 return 0;
110 }
111
112 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
113 }
114
115 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
116 if (error)
117 *error = "class specific file unavailable";
118 return -ENOENT;
119 }
120
121 /* check if we need to wait for a physical device */
122 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
123 {
124 /* list of devices without a "device" symlink to the physical device
125 * if device is set to NULL, no devices in that subsystem has a link */
126 static struct class_device {
127 char *subsystem;
128 char *device;
129 } class_device[] = {
130 { .subsystem = "block", .device = "double" },
131 { .subsystem = "block", .device = "nb" },
132 { .subsystem = "block", .device = "ram" },
133 { .subsystem = "block", .device = "loop" },
134 { .subsystem = "block", .device = "fd" },
135 { .subsystem = "block", .device = "md" },
136 { .subsystem = "block", .device = "dos_cd" },
137 { .subsystem = "block", .device = "rflash" },
138 { .subsystem = "block", .device = "rom" },
139 { .subsystem = "block", .device = "rrom" },
140 { .subsystem = "block", .device = "flash" },
141 { .subsystem = "block", .device = "msd" },
142 { .subsystem = "block", .device = "sbpcd" },
143 { .subsystem = "block", .device = "pcd" },
144 { .subsystem = "block", .device = "pf" },
145 { .subsystem = "block", .device = "scd" },
146 { .subsystem = "block", .device = "ubd" },
147 { .subsystem = "block", .device = "dm-" },
148 { .subsystem = "input", .device = "event" },
149 { .subsystem = "input", .device = "mice" },
150 { .subsystem = "input", .device = "mouse" },
151 { .subsystem = "input", .device = "ts" },
152 { .subsystem = "vc", .device = NULL },
153 { .subsystem = "tty", .device = NULL },
154 { .subsystem = "cpuid", .device = "cpu" },
155 { .subsystem = "graphics", .device = "fb" },
156 { .subsystem = "mem", .device = NULL },
157 { .subsystem = "misc", .device = NULL },
158 { .subsystem = "msr", .device = NULL },
159 { .subsystem = "netlink", .device = NULL },
160 { .subsystem = "net", .device = "sit" },
161 { .subsystem = "net", .device = "lo" },
162 { .subsystem = "net", .device = "tap" },
163 { .subsystem = "net", .device = "ipsec" },
164 { .subsystem = "net", .device = "dummy" },
165 { .subsystem = "net", .device = "irda" },
166 { .subsystem = "net", .device = "ppp" },
167 { .subsystem = "net", .device = "tun" },
168 { .subsystem = "net", .device = "pan" },
169 { .subsystem = "net", .device = "bnep" },
170 { .subsystem = "net", .device = "vmnet" },
171 { .subsystem = "ppp", .device = NULL },
172 { .subsystem = "sound", .device = NULL },
173 { .subsystem = "printer", .device = "lp" },
174 { .subsystem = "nvidia", .device = NULL },
175 { .subsystem = "video4linux", .device = "vbi" },
176 { .subsystem = "dvb", .device = NULL },
177 { .subsystem = "lirc", .device = NULL },
178 { .subsystem = "firmware", .device = NULL },
179 { .subsystem = "drm", .device = NULL },
180 { .subsystem = "pci_bus", .device = NULL },
181 { .subsystem = "ieee1394", .device = NULL },
182 { .subsystem = "ieee1394_host", .device = NULL },
183 { .subsystem = "ieee1394_node", .device = NULL },
184 { .subsystem = "raw", .device = NULL },
185 { .subsystem = "zaptel", .device = NULL },
186 { .subsystem = "tiglusb", .device = NULL },
187 { .subsystem = "ppdev", .device = NULL },
188 { .subsystem = "ticables", .device = NULL },
189 { .subsystem = "snsc", .device = NULL },
190 { .subsystem = "staliomem", .device = NULL },
191 { .subsystem = "tape", .device = NULL },
192 { .subsystem = "ip2", .device = NULL },
193 { .subsystem = "tpqic02", .device = NULL },
194 { .subsystem = "dsp56k", .device = NULL },
195 { .subsystem = "zft", .device = NULL },
196 { .subsystem = "adb", .device = NULL },
197 { .subsystem = "cosa", .device = NULL },
198 { .subsystem = "pg", .device = NULL },
199 { .subsystem = "pt", .device = NULL },
200 { .subsystem = "capi", .device = NULL },
201 { NULL, NULL }
202 };
203 struct class_device *classdevice;
204 int len;
205
206 /* the kernel may tell us what to wait for */
207 if (kernel_release_satisfactory(2,6,10) > 0)
208 if (getenv("PHYSDEVPATH") == NULL) {
209 dbg("the kernel says, that there is no physical device for '%s'", class_dev->path);
210 return 1;
211 }
212
213 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
214 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
215 /* see if no device in this class is expected to have a device-link */
216 if (classdevice->device == NULL)
217 return 1;
218
219 len = strlen(classdevice->device);
220
221 /* see if device name matches */
222 if (strncmp(class_dev->name, classdevice->device, len) != 0)
223 continue;
224
225 /* exact name match */
226 if (strlen(class_dev->name) == len)
227 return 1;
228
229 /* name match with instance number */
230 if (isdigit(class_dev->name[len]))
231 return 1;
232 }
233 }
234
235 return 0;
236 }
237
238 /* skip waiting for the bus of the devices device */
239 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
240 {
241 static char *devices_without_bus[] = {
242 "scsi_host",
243 "i2c-adapter",
244 "i2c-dev",
245 NULL
246 };
247 char **device;
248
249 for (device = devices_without_bus; *device != NULL; device++) {
250 int len = strlen(*device);
251
252 if (strncmp(class_dev->classname, *device, len) == 0)
253 return 1;
254 }
255
256 return 0;
257 }
258
259 /* wait for a devices device specific file to show up */
260 int wait_for_devices_device(struct sysfs_device *devices_dev,
261 const char **error)
262 {
263 static struct device_file {
264 char *bus;
265 char *file;
266 } device_files[] = {
267 { .bus = "scsi", .file = "vendor" },
268 { .bus = "usb", .file = "idVendor" },
269 { .bus = "usb", .file = "iInterface" },
270 { .bus = "usb", .file = "bNumEndpoints" },
271 { .bus = "usb-serial", .file = "detach_state" },
272 { .bus = "ide", .file = "detach_state" },
273 { .bus = "pci", .file = "vendor" },
274 { .bus = "platform", .file = "detach_state" },
275 { .bus = "i2c", .file = "detach_state" },
276 { .bus = "ieee1394", .file = "node_count" },
277 { .bus = "ieee1394", .file = "nodeid" },
278 { .bus = "ieee1394", .file = "address" },
279 { NULL, NULL }
280 };
281 struct device_file *devicefile;
282 int loop;
283
284 /* the kernel may tell us what to wait for */
285 if (kernel_release_satisfactory(2,6,10) > 0)
286 if (getenv("PHYSDEVBUS") == NULL) {
287 dbg("the kernel says, that there is no bus for '%s'", devices_dev->path);
288 return 0;
289 }
290
291 /* wait for the bus device link to the devices device */
292 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
293 while (--loop) {
294 if (sysfs_get_device_bus(devices_dev) == 0)
295 break;
296
297 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
298 }
299 if (loop == 0) {
300 dbg("error: getting bus device link");
301 if (error)
302 *error = "no bus device link";
303 return -1;
304 }
305 dbg("bus device link found for bus '%s'", devices_dev->bus);
306
307 /* wait for a bus device specific file to show up */
308 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
309 while (--loop) {
310 int found_bus_type = 0;
311
312 for (devicefile = device_files; devicefile->bus != NULL; devicefile++) {
313 if (strcmp(devices_dev->bus, devicefile->bus) == 0) {
314 char filename[SYSFS_PATH_MAX];
315 struct stat stats;
316
317 found_bus_type = 1;
318 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", devices_dev->path, devicefile->file);
319 dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename);
320
321 if (stat(filename, &stats) == 0) {
322 dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file);
323 return 0;
324 }
325 }
326 }
327 if (found_bus_type == 0) {
328 if (error)
329 *error = "unknown bus";
330 info("error: unknown bus, please report to "
331 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
332 return -1;
333 }
334 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
335 }
336
337 dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file);
338 if (error)
339 *error = "bus device specific file unavailable";
340 return -1;
341 }
342
343
344 struct sysfs_class_device *wait_class_device_open(const char *path)
345 {
346 struct sysfs_class_device *class_dev;
347 int loop;
348
349 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
350 while (--loop) {
351 class_dev = sysfs_open_class_device_path(path);
352 if (class_dev)
353 break;
354
355 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
356 }
357
358 return (class_dev);
359 }
360
361 int wait_for_class_device(struct sysfs_class_device *class_dev,
362 const char **error)
363 {
364 struct sysfs_class_device *class_dev_parent;
365 struct sysfs_device *devices_dev = NULL;
366 int loop;
367
368 if (wait_for_class_device_attributes(class_dev, error) != 0)
369 return -ENOENT;
370
371 /* skip devices without devices-link */
372 if (class_device_expect_no_device_link(class_dev)) {
373 dbg("no device symlink expected for '%s', ", class_dev->name);
374 return 0;
375 }
376
377 /* the symlink may be on the parent device */
378 class_dev_parent = sysfs_get_classdev_parent(class_dev);
379 if (class_dev_parent)
380 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
381
382 /* wait for the symlink to the devices device */
383 dbg("waiting for symlink to devices device");
384 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
385 while (--loop) {
386 if (class_dev_parent)
387 devices_dev = sysfs_get_classdev_device(class_dev_parent);
388 else
389 devices_dev = sysfs_get_classdev_device(class_dev);
390
391 if (devices_dev)
392 break;
393
394 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
395 }
396 if (!devices_dev) {
397 dbg(" error: no devices device symlink found");
398 if (error)
399 *error = "no device symlink";
400 return -ENODEV;
401 }
402 dbg("device symlink found pointing to '%s'", devices_dev->path);
403
404 /* wait for the devices device */
405 if (class_device_expect_no_bus(class_dev)) {
406 dbg("no bus device expected for '%s', ", class_dev->classname);
407 return 0;
408 }
409
410 return wait_for_devices_device(devices_dev, error);
411 }
412
413 struct sysfs_device *wait_devices_device_open(const char *path)
414 {
415 struct sysfs_device *devices_dev;
416 int loop;
417
418 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
419 while (--loop) {
420 devices_dev = sysfs_open_device_path(path);
421 if (devices_dev)
422 break;
423
424 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
425 }
426
427 return(devices_dev);
428 }