]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_sysfs.c
[PATCH] add ippp and bcrypt to the exception lists of wait_for_sysfs
[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 = "block", .device = "bcrypt" },
149 { .subsystem = "input", .device = "event" },
150 { .subsystem = "input", .device = "mice" },
151 { .subsystem = "input", .device = "mouse" },
152 { .subsystem = "input", .device = "ts" },
153 { .subsystem = "vc", .device = NULL },
154 { .subsystem = "tty", .device = NULL },
155 { .subsystem = "cpuid", .device = "cpu" },
156 { .subsystem = "graphics", .device = "fb" },
157 { .subsystem = "mem", .device = NULL },
158 { .subsystem = "misc", .device = NULL },
159 { .subsystem = "msr", .device = NULL },
160 { .subsystem = "netlink", .device = NULL },
161 { .subsystem = "net", .device = "sit" },
162 { .subsystem = "net", .device = "lo" },
163 { .subsystem = "net", .device = "tap" },
164 { .subsystem = "net", .device = "ipsec" },
165 { .subsystem = "net", .device = "dummy" },
166 { .subsystem = "net", .device = "irda" },
167 { .subsystem = "net", .device = "ppp" },
168 { .subsystem = "net", .device = "tun" },
169 { .subsystem = "net", .device = "pan" },
170 { .subsystem = "net", .device = "bnep" },
171 { .subsystem = "net", .device = "vmnet" },
172 { .subsystem = "net", .device = "ippp" },
173 { .subsystem = "ppp", .device = NULL },
174 { .subsystem = "sound", .device = NULL },
175 { .subsystem = "printer", .device = "lp" },
176 { .subsystem = "nvidia", .device = NULL },
177 { .subsystem = "video4linux", .device = "vbi" },
178 { .subsystem = "dvb", .device = NULL },
179 { .subsystem = "lirc", .device = NULL },
180 { .subsystem = "firmware", .device = NULL },
181 { .subsystem = "drm", .device = NULL },
182 { .subsystem = "pci_bus", .device = NULL },
183 { .subsystem = "ieee1394", .device = NULL },
184 { .subsystem = "ieee1394_host", .device = NULL },
185 { .subsystem = "ieee1394_node", .device = NULL },
186 { .subsystem = "raw", .device = NULL },
187 { .subsystem = "zaptel", .device = NULL },
188 { .subsystem = "tiglusb", .device = NULL },
189 { .subsystem = "ppdev", .device = NULL },
190 { .subsystem = "ticables", .device = NULL },
191 { .subsystem = "snsc", .device = NULL },
192 { .subsystem = "staliomem", .device = NULL },
193 { .subsystem = "tape", .device = NULL },
194 { .subsystem = "ip2", .device = NULL },
195 { .subsystem = "tpqic02", .device = NULL },
196 { .subsystem = "dsp56k", .device = NULL },
197 { .subsystem = "zft", .device = NULL },
198 { .subsystem = "adb", .device = NULL },
199 { .subsystem = "cosa", .device = NULL },
200 { .subsystem = "pg", .device = NULL },
201 { .subsystem = "pt", .device = NULL },
202 { .subsystem = "capi", .device = NULL },
203 { NULL, NULL }
204 };
205 struct class_device *classdevice;
206 int len;
207
208 /* the kernel may tell us what to wait for */
209 if (kernel_release_satisfactory(2,6,10) > 0)
210 if (getenv("PHYSDEVPATH") == NULL) {
211 dbg("the kernel says, that there is no physical device for '%s'", class_dev->path);
212 return 1;
213 }
214
215 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
216 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
217 /* see if no device in this class is expected to have a device-link */
218 if (classdevice->device == NULL)
219 return 1;
220
221 len = strlen(classdevice->device);
222
223 /* see if device name matches */
224 if (strncmp(class_dev->name, classdevice->device, len) != 0)
225 continue;
226
227 /* exact name match */
228 if (strlen(class_dev->name) == len)
229 return 1;
230
231 /* name match with instance number */
232 if (isdigit(class_dev->name[len]))
233 return 1;
234 }
235 }
236
237 return 0;
238 }
239
240 /* skip waiting for the bus of the devices device */
241 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
242 {
243 static char *devices_without_bus[] = {
244 "scsi_host",
245 "i2c-adapter",
246 "i2c-dev",
247 NULL
248 };
249 char **device;
250
251 for (device = devices_without_bus; *device != NULL; device++) {
252 int len = strlen(*device);
253
254 if (strncmp(class_dev->classname, *device, len) == 0)
255 return 1;
256 }
257
258 return 0;
259 }
260
261 /* wait for a devices device specific file to show up */
262 int wait_for_devices_device(struct sysfs_device *devices_dev,
263 const char **error)
264 {
265 static struct device_file {
266 char *bus;
267 char *file;
268 } device_files[] = {
269 { .bus = "scsi", .file = "vendor" },
270 { .bus = "usb", .file = "idVendor" },
271 { .bus = "usb", .file = "iInterface" },
272 { .bus = "usb", .file = "bNumEndpoints" },
273 { .bus = "usb-serial", .file = "detach_state" },
274 { .bus = "ide", .file = "detach_state" },
275 { .bus = "pci", .file = "vendor" },
276 { .bus = "platform", .file = "detach_state" },
277 { .bus = "i2c", .file = "detach_state" },
278 { .bus = "ieee1394", .file = "node_count" },
279 { .bus = "ieee1394", .file = "nodeid" },
280 { .bus = "ieee1394", .file = "address" },
281 { NULL, NULL }
282 };
283 struct device_file *devicefile;
284 int loop;
285
286 /* the kernel may tell us what to wait for */
287 if (kernel_release_satisfactory(2,6,10) > 0)
288 if (getenv("PHYSDEVBUS") == NULL) {
289 dbg("the kernel says, that there is no bus for '%s'", devices_dev->path);
290 return 0;
291 }
292
293 /* wait for the bus device link to the devices device */
294 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
295 while (--loop) {
296 if (sysfs_get_device_bus(devices_dev) == 0)
297 break;
298
299 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
300 }
301 if (loop == 0) {
302 dbg("error: getting bus device link");
303 if (error)
304 *error = "no bus device link";
305 return -1;
306 }
307 dbg("bus device link found for bus '%s'", devices_dev->bus);
308
309 /* wait for a bus device specific file to show up */
310 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
311 while (--loop) {
312 int found_bus_type = 0;
313
314 for (devicefile = device_files; devicefile->bus != NULL; devicefile++) {
315 if (strcmp(devices_dev->bus, devicefile->bus) == 0) {
316 char filename[SYSFS_PATH_MAX];
317 struct stat stats;
318
319 found_bus_type = 1;
320 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", devices_dev->path, devicefile->file);
321 dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename);
322
323 if (stat(filename, &stats) == 0) {
324 dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file);
325 return 0;
326 }
327 }
328 }
329 if (found_bus_type == 0) {
330 if (error)
331 *error = "unknown bus";
332 info("error: unknown bus, please report to "
333 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
334 return -1;
335 }
336 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
337 }
338
339 dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file);
340 if (error)
341 *error = "bus device specific file unavailable";
342 return -1;
343 }
344
345
346 struct sysfs_class_device *wait_class_device_open(const char *path)
347 {
348 struct sysfs_class_device *class_dev;
349 int loop;
350
351 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
352 while (--loop) {
353 class_dev = sysfs_open_class_device_path(path);
354 if (class_dev)
355 break;
356
357 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
358 }
359
360 return (class_dev);
361 }
362
363 int wait_for_class_device(struct sysfs_class_device *class_dev,
364 const char **error)
365 {
366 struct sysfs_class_device *class_dev_parent;
367 struct sysfs_device *devices_dev = NULL;
368 int loop;
369
370 if (wait_for_class_device_attributes(class_dev, error) != 0)
371 return -ENOENT;
372
373 /* skip devices without devices-link */
374 if (class_device_expect_no_device_link(class_dev)) {
375 dbg("no device symlink expected for '%s', ", class_dev->name);
376 return 0;
377 }
378
379 /* the symlink may be on the parent device */
380 class_dev_parent = sysfs_get_classdev_parent(class_dev);
381 if (class_dev_parent)
382 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
383
384 /* wait for the symlink to the devices device */
385 dbg("waiting for symlink to devices device");
386 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
387 while (--loop) {
388 if (class_dev_parent)
389 devices_dev = sysfs_get_classdev_device(class_dev_parent);
390 else
391 devices_dev = sysfs_get_classdev_device(class_dev);
392
393 if (devices_dev)
394 break;
395
396 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
397 }
398 if (!devices_dev) {
399 dbg(" error: no devices device symlink found");
400 if (error)
401 *error = "no device symlink";
402 return -ENODEV;
403 }
404 dbg("device symlink found pointing to '%s'", devices_dev->path);
405
406 /* wait for the devices device */
407 if (class_device_expect_no_bus(class_dev)) {
408 dbg("no bus device expected for '%s', ", class_dev->classname);
409 return 0;
410 }
411
412 return wait_for_devices_device(devices_dev, error);
413 }
414
415 struct sysfs_device *wait_devices_device_open(const char *path)
416 {
417 struct sysfs_device *devices_dev;
418 int loop;
419
420 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
421 while (--loop) {
422 devices_dev = sysfs_open_device_path(path);
423 if (devices_dev)
424 break;
425
426 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
427 }
428
429 return(devices_dev);
430 }