chiark / gitweb /
[PATCH] udev_volume_id: fix FAT label reading
[elogind.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_utils.h"
35 #include "logging.h"
36
37 /* list of subsystem specific files, NULL if there is no file to wait for */
38 static const struct subsystem_file {
39         const char *subsystem;
40         const char *file;
41 } subsystem_files[] = {
42         { .subsystem = "class",         .file = NULL },
43         { .subsystem = "net",           .file = "ifindex" },
44         { .subsystem = "scsi_host",     .file = "unique_id" },
45         { .subsystem = "scsi_device",   .file = NULL },
46         { .subsystem = "pcmcia_socket", .file = "card_type" },
47         { .subsystem = "usb_host",      .file = NULL },
48         { .subsystem = "bluetooth",     .file = "address" },
49         { .subsystem = "firmware",      .file = "data" },
50         { .subsystem = "i2c-adapter",   .file = NULL },
51         { .subsystem = "pci_bus",       .file = NULL },
52         { .subsystem = "ieee1394",      .file = NULL },
53         { .subsystem = "ieee1394_host", .file = NULL },
54         { .subsystem = "ieee1394_node", .file = NULL },
55         { .subsystem = "fc_transport",  .file = "port_id" },
56         { .subsystem = "fc_host",       .file = "port_id" },
57         { .subsystem = "spi_transport", .file = "width" },
58         { .subsystem = "spi_host",      .file = "width" },
59         { NULL, NULL }
60 };
61
62 int subsystem_expect_no_dev(const char *subsystem)
63 {
64         const struct subsystem_file *file;
65
66         for (file = subsystem_files; file->subsystem != NULL; file++)
67                 if (strcmp(subsystem, file->subsystem) == 0)
68                         return 1;
69
70         return 0;
71 }
72
73 /* get subsystem specific files, returns "dev" if no other found */
74 static const char *get_subsystem_specific_file(const char *subsystem)
75 {
76         const struct subsystem_file *file;
77
78         /* look if we want to look for another file instead of "dev" */
79         for (file = subsystem_files; file->subsystem != NULL; file++)
80                 if (strcmp(subsystem, file->subsystem) == 0)
81                         return file->file;
82
83         return "dev";
84 }
85
86 /* wait for class pecific file to show up */
87 static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev,
88                                             const char **error)
89 {
90         const char *file;
91         char filename[SYSFS_PATH_MAX];
92         int loop;
93
94         file = get_subsystem_specific_file(class_dev->classname);
95         if (file == NULL) {
96                 dbg("class '%s' has no file to wait for", class_dev->classname);
97                 return 0;
98         }
99
100         snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", class_dev->path, file);
101         dbg("looking at class '%s' for specific file '%s'", class_dev->classname, filename);
102
103         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
104         while (--loop) {
105                 struct stat stats;
106
107                 if (stat(class_dev->path, &stats) == -1) {
108                         dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
109                         return -ENODEV;
110                 }
111
112                 if (stat(filename, &stats) == 0) {
113                         dbg("class '%s' specific file '%s' found", class_dev->classname, file);
114                         return 0;
115                 }
116
117                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
118         }
119
120         dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
121         if (error)
122                 *error = "class specific file unavailable";
123         return -ENOENT;
124 }
125
126 /* check if we need to wait for a physical device */
127 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
128 {
129         /* list of devices without a "device" symlink to the physical device
130          * if device is set to NULL, no devices in that subsystem has a link */
131         static const struct class_device {
132                 const char *subsystem;
133                 const char *device;
134         } class_device[] = {
135                 { .subsystem = "block",         .device = "double" },
136                 { .subsystem = "block",         .device = "nb" },
137                 { .subsystem = "block",         .device = "ram" },
138                 { .subsystem = "block",         .device = "loop" },
139                 { .subsystem = "block",         .device = "fd" },
140                 { .subsystem = "block",         .device = "md" },
141                 { .subsystem = "block",         .device = "dos_cd" },
142                 { .subsystem = "block",         .device = "rflash" },
143                 { .subsystem = "block",         .device = "rom" },
144                 { .subsystem = "block",         .device = "rrom" },
145                 { .subsystem = "block",         .device = "flash" },
146                 { .subsystem = "block",         .device = "msd" },
147                 { .subsystem = "block",         .device = "sbpcd" },
148                 { .subsystem = "block",         .device = "pcd" },
149                 { .subsystem = "block",         .device = "pf" },
150                 { .subsystem = "block",         .device = "scd" },
151                 { .subsystem = "block",         .device = "ubd" },
152                 { .subsystem = "block",         .device = "dm-" },
153                 { .subsystem = "block",         .device = "bcrypt" },
154                 { .subsystem = "input",         .device = "event" },
155                 { .subsystem = "input",         .device = "mice" },
156                 { .subsystem = "input",         .device = "mouse" },
157                 { .subsystem = "input",         .device = "ts" },
158                 { .subsystem = "vc",            .device = NULL },
159                 { .subsystem = "tty",           .device = NULL },
160                 { .subsystem = "cpuid",         .device = "cpu" },
161                 { .subsystem = "graphics",      .device = "fb" },
162                 { .subsystem = "mem",           .device = NULL },
163                 { .subsystem = "misc",          .device = NULL },
164                 { .subsystem = "msr",           .device = NULL },
165                 { .subsystem = "netlink",       .device = NULL },
166                 { .subsystem = "net",           .device = "sit" },
167                 { .subsystem = "net",           .device = "lo" },
168                 { .subsystem = "net",           .device = "tap" },
169                 { .subsystem = "net",           .device = "ipsec" },
170                 { .subsystem = "net",           .device = "dummy" },
171                 { .subsystem = "net",           .device = "irda" },
172                 { .subsystem = "net",           .device = "ppp" },
173                 { .subsystem = "net",           .device = "tun" },
174                 { .subsystem = "net",           .device = "pan" },
175                 { .subsystem = "net",           .device = "bnep" },
176                 { .subsystem = "net",           .device = "vmnet" },
177                 { .subsystem = "net",           .device = "ippp" },
178                 { .subsystem = "net",           .device = "nlv" },
179                 { .subsystem = "net",           .device = "atml" },
180                 { .subsystem = "ppp",           .device = NULL },
181                 { .subsystem = "sound",         .device = NULL },
182                 { .subsystem = "printer",       .device = "lp" },
183                 { .subsystem = "ppdev",         .device = NULL },
184                 { .subsystem = "nvidia",        .device = NULL },
185                 { .subsystem = "video4linux",   .device = "vbi" },
186                 { .subsystem = "dvb",           .device = NULL },
187                 { .subsystem = "lirc",          .device = NULL },
188                 { .subsystem = "firmware",      .device = NULL },
189                 { .subsystem = "drm",           .device = NULL },
190                 { .subsystem = "pci_bus",       .device = NULL },
191                 { .subsystem = "ieee1394",      .device = NULL },
192                 { .subsystem = "ieee1394_host", .device = NULL },
193                 { .subsystem = "ieee1394_node", .device = NULL },
194                 { .subsystem = "raw",           .device = NULL },
195                 { .subsystem = "zaptel",        .device = NULL },
196                 { .subsystem = "tiglusb",       .device = NULL },
197                 { .subsystem = "ppdev",         .device = NULL },
198                 { .subsystem = "ticables",      .device = NULL },
199                 { .subsystem = "snsc",          .device = NULL },
200                 { .subsystem = "staliomem",     .device = NULL },
201                 { .subsystem = "tape",          .device = NULL },
202                 { .subsystem = "ip2",           .device = NULL },
203                 { .subsystem = "tpqic02",       .device = NULL },
204                 { .subsystem = "dsp56k",        .device = NULL },
205                 { .subsystem = "zft",           .device = NULL },
206                 { .subsystem = "adb",           .device = NULL },
207                 { .subsystem = "cosa",          .device = NULL },
208                 { .subsystem = "pg",            .device = NULL },
209                 { .subsystem = "pt",            .device = NULL },
210                 { .subsystem = "capi",          .device = NULL },
211                 { NULL, NULL }
212         };
213         const struct class_device *classdevice;
214         int len;
215
216         /* the kernel may tell us what to wait for */
217         if (kernel_release_satisfactory(2,6,10) > 0)
218                 if (getenv("PHYSDEVPATH") == NULL) {
219                         dbg("the kernel says, that there is no physical device for '%s'", class_dev->path);
220                         return 1;
221                 }
222
223         for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
224                 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
225                         /* see if no device in this class is expected to have a device-link */
226                         if (classdevice->device == NULL)
227                                 return 1;
228
229                         len = strlen(classdevice->device);
230
231                         /* see if device name matches */
232                         if (strncmp(class_dev->name, classdevice->device, len) != 0)
233                                 continue;
234
235                         /* exact name match */
236                         if (strlen(class_dev->name) == len)
237                                 return 1;
238
239                         /* name match with instance number */
240                         if (isdigit(class_dev->name[len]))
241                                 return 1;
242                 }
243         }
244
245         return 0;
246 }
247
248 /* skip waiting for the bus of the devices device */
249 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
250 {
251         static const char *devices_without_bus[] = {
252                 "scsi_host",
253                 "i2c-adapter",
254                 "i2c-dev",
255                 NULL
256         };
257         const char **device;
258
259         for (device = devices_without_bus; *device != NULL; device++) {
260                 int len = strlen(*device);
261
262                 if (strncmp(class_dev->classname, *device, len) == 0)
263                         return 1;
264         }
265
266         return 0;
267 }
268
269 /* wait for a devices device specific file to show up */
270 int wait_for_devices_device(struct sysfs_device *devices_dev,
271                         const char **error)
272 {
273         static const struct device_file {
274                 const char *bus;
275                 const char *file;
276         } device_files[] = {
277                 { .bus = "scsi",        .file = "vendor" },
278                 { .bus = "usb",         .file = "idVendor" },
279                 { .bus = "usb",         .file = "iInterface" },
280                 { .bus = "usb",         .file = "bNumEndpoints" },
281                 { .bus = "usb-serial",  .file = "detach_state" },
282                 { .bus = "ide",         .file = "detach_state" },
283                 { .bus = "pci",         .file = "vendor" },
284                 { .bus = "platform",    .file = "detach_state" },
285                 { .bus = "pcmcia",      .file = "detach_state" },
286                 { .bus = "i2c",         .file = "detach_state" },
287                 { .bus = "ieee1394",    .file = "node_count" },
288                 { .bus = "ieee1394",    .file = "nodeid" },
289                 { .bus = "ieee1394",    .file = "address" },
290                 { .bus = "bttv-sub",    .file = NULL },
291                 { .bus = "pnp",         .file = "detach_state" },
292                 { .bus = "eisa",        .file = "detach_state" },
293                 { .bus = "pseudo",      .file = "detach_state" },
294                 { .bus = "mmc",         .file = "detach_state" },
295                 { .bus = "macio",       .file = "detach_state" },
296                 { .bus = "of_platform", .file = "detach_state" },
297                 { .bus = "vio",         .file = "detach_state" },
298                 { .bus = "ecard",       .file = "detach_state" },
299                 { .bus = "sa1111-rab",  .file = "detach_state" },
300                 { .bus = "amba",        .file = "detach_state" },
301                 { .bus = "locomo-bus",  .file = "detach_state" },
302                 { .bus = "logicmodule", .file = "detach_state" },
303                 { .bus = "parisc",      .file = "detach_state" },
304                 { .bus = "ocp",         .file = "detach_state" },
305                 { .bus = "dio",         .file = "detach_state" },
306                 { .bus = "MCA",         .file = "detach_state" },
307                 { .bus = "wl",          .file = "detach_state" },
308                 { .bus = "ccwgroup",    .file = "detach_state" },
309                 { .bus = "css",         .file = "detach_state" },
310                 { .bus = "ccw",         .file = "detach_state" },
311                 { .bus = "iucv",        .file = "detach_state" },
312                 { NULL, NULL }
313         };
314         const struct device_file *devicefile;
315         int loop;
316
317         /* the kernel may tell us what to wait for */
318         if (kernel_release_satisfactory(2,6,10) > 0)
319                 if (getenv("PHYSDEVBUS") == NULL) {
320                         dbg("the kernel says, that there is no bus for '%s'", devices_dev->path);
321                         return 0;
322                 }
323
324         /* wait for the bus device link to the devices device */
325         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
326         while (--loop) {
327                 if (sysfs_get_device_bus(devices_dev) == 0)
328                         break;
329
330                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
331         }
332         if (loop == 0) {
333                 dbg("error: getting bus device link");
334                 if (error)
335                         *error = "no bus device link";
336                 return -1;
337         }
338         dbg("bus device link found for bus '%s'", devices_dev->bus);
339
340         /* wait for a bus device specific file to show up */
341         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
342         while (--loop) {
343                 int found_bus_type = 0;
344
345                 for (devicefile = device_files; devicefile->bus != NULL; devicefile++) {
346                         if (strcmp(devices_dev->bus, devicefile->bus) == 0) {
347                                 char filename[SYSFS_PATH_MAX];
348                                 struct stat stats;
349
350                                 if (devicefile->file == NULL) {
351                                         dbg("bus '%s' has no file to wait for", devices_dev->bus);
352                                         return 0;
353                                 }
354
355                                 found_bus_type = 1;
356                                 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", devices_dev->path, devicefile->file);
357                                 dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename);
358
359                                 if (stat(filename, &stats) == 0) {
360                                         dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file);
361                                         return 0;
362                                 }
363                         }
364                 }
365                 if (found_bus_type == 0) {
366                         if (error)
367                                 *error = "unknown bus";
368                         info("error: unknown bus, please report to "
369                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
370                         return -1;
371                 }
372                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
373         }
374
375         dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file);
376         if (error)
377                 *error = "bus device specific file unavailable";
378         return -1;
379 }
380
381
382 struct sysfs_class_device *wait_class_device_open(const char *path)
383 {
384         struct sysfs_class_device *class_dev = NULL;
385         int loop;
386
387         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
388         while (--loop) {
389                 class_dev = sysfs_open_class_device_path(path);
390                 if (class_dev)
391                         break;
392
393                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
394         }
395
396         return class_dev;
397 }
398
399 int wait_for_class_device(struct sysfs_class_device *class_dev,
400                           const char **error)
401 {
402         struct sysfs_class_device *class_dev_parent;
403         struct sysfs_device *devices_dev = NULL;
404         int loop;
405
406         if (wait_for_class_device_attributes(class_dev, error) != 0)
407                 return -ENOENT;
408
409         /* skip devices without devices-link */
410         if (class_device_expect_no_device_link(class_dev)) {
411                 dbg("no device symlink expected for '%s', ", class_dev->name);
412                 return 0;
413         }
414
415         /* the symlink may be on the parent device */
416         class_dev_parent = sysfs_get_classdev_parent(class_dev);
417         if (class_dev_parent)
418                 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
419
420         /* wait for the symlink to the devices device */
421         dbg("waiting for symlink to devices device");
422         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
423         while (--loop) {
424                 if (class_dev_parent)
425                         devices_dev = sysfs_get_classdev_device(class_dev_parent);
426                 else
427                         devices_dev = sysfs_get_classdev_device(class_dev);
428
429                 if (devices_dev)
430                         break;
431
432                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
433         }
434         if (!devices_dev) {
435                 dbg(" error: no devices device symlink found");
436                 if (error)
437                         *error = "no device symlink";
438                 return -ENODEV;
439         }
440         dbg("device symlink found pointing to '%s'", devices_dev->path);
441
442         /* wait for the devices device */
443         if (class_device_expect_no_bus(class_dev)) {
444                 dbg("no bus device expected for '%s', ", class_dev->classname);
445                 return 0;
446         }
447
448         return wait_for_devices_device(devices_dev, error);
449 }
450
451 struct sysfs_device *wait_devices_device_open(const char *path)
452 {
453         struct sysfs_device *devices_dev = NULL;
454         int loop;
455
456         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
457         while (--loop) {
458                 devices_dev = sysfs_open_device_path(path);
459                 if (devices_dev)
460                         break;
461
462                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
463         }
464
465         return devices_dev;
466 }