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