chiark / gitweb /
[PATCH] finally solve the bad sysfs-timing for all of us
[elogind.git] / libsysfs / sysfs_device.c
1 /*
2  * sysfs_device.c
3  *
4  * Generic device utility functions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2003
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23 #include "sysfs/libsysfs.h"
24 #include "sysfs.h"
25
26 /**
27  * get_dev_driver: fills in the dev->driver_name field 
28  *
29  * Returns 0 on SUCCESS and 1 on error
30  */
31 static int get_dev_driver(struct sysfs_device *dev)
32 {
33         struct dlist *drvlist = NULL;
34         char path[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
35         char *drv = NULL, *c = NULL;
36         
37         if (dev == NULL) {
38                 errno = EINVAL;
39                 return 1;
40         }
41         if (dev->bus[0] == '\0')
42                 return 1;
43         memset(path, 0, SYSFS_PATH_MAX);
44         memset(devpath, 0, SYSFS_PATH_MAX);
45         safestrcpy(path, SYSFS_BUS_NAME);
46         safestrcat(path, "/");
47         safestrcat(path, dev->bus);
48         safestrcat(path, "/");
49         safestrcat(path, SYSFS_DRIVERS_NAME);
50
51         safestrcpy(devpath, dev->path);
52         c = strstr(devpath, SYSFS_DEVICES_NAME);
53         if (c == NULL)
54                 return 1;
55         *c = '\0';
56         safestrcatmax(c, path, (sizeof(devpath) - strlen(devpath)));
57
58         drvlist = sysfs_open_subsystem_list(path);
59         if (drvlist != NULL) {
60                 dlist_for_each_data(drvlist, drv, char) {
61                         safestrcpy(path, devpath);
62                         safestrcat(path, "/");
63                         safestrcat(path, drv);
64                         safestrcat(path, "/");
65                         safestrcat(path, dev->bus_id);
66                         if (sysfs_path_is_link(path) == 0) {
67                                 safestrcpy(dev->driver_name, drv);
68                                 sysfs_close_list(drvlist);
69                                 return 0;
70                         }
71                 }
72                 sysfs_close_list(drvlist);
73         }
74         return 1;
75 }
76         
77 /**
78  * sysfs_get_device_bus: retrieves the bus name the device is on, checks path 
79  *      to bus' link to make sure it has correct device.
80  * @dev: device to get busname.
81  * returns 0 with success and -1 with error.
82  */
83 int sysfs_get_device_bus(struct sysfs_device *dev)
84 {
85         char subsys[SYSFS_NAME_LEN], path[SYSFS_PATH_MAX];
86         char target[SYSFS_PATH_MAX], *bus = NULL, *c = NULL;
87         struct dlist *buslist = NULL;
88
89         if (dev == NULL) {
90                 errno = EINVAL;
91                 return -1;
92         }
93
94         memset(subsys, 0, SYSFS_NAME_LEN);
95         safestrcpy(subsys, SYSFS_BUS_NAME);  /* subsys = bus */
96         buslist = sysfs_open_subsystem_list(subsys);
97         if (buslist != NULL) {
98                 dlist_for_each_data(buslist, bus, char) {
99                         memset(path, 0, SYSFS_PATH_MAX);
100                         safestrcpy(path, dev->path);
101                         c = strstr(path, "/devices");
102                         if (c == NULL) {
103                                 dprintf("Invalid path to device %s\n", path);
104                                 sysfs_close_list(buslist);
105                                 return -1;
106                         }
107                         *c = '\0';
108                         safestrcat(path, "/");
109                         safestrcat(path, SYSFS_BUS_NAME);
110                         safestrcat(path, "/");
111                         safestrcat(path, bus);
112                         safestrcat(path, "/");
113                         safestrcat(path, SYSFS_DEVICES_NAME);
114                         safestrcat(path, "/");
115                         safestrcat(path, dev->bus_id);
116                         if ((sysfs_path_is_link(path)) == 0) {
117                                 memset(target, 0, SYSFS_PATH_MAX);
118                                 if ((sysfs_get_link(path, target, 
119                                                 SYSFS_PATH_MAX)) != 0) {
120                                         dprintf("Error getting link target\n");
121                                         sysfs_close_list(buslist);
122                                         return -1;
123                                 }
124                                 if (!(strncmp(target, dev->path, 
125                                                         SYSFS_PATH_MAX))) {
126                                         safestrcpy(dev->bus, bus);
127                                         sysfs_close_list(buslist);
128                                         return 0;
129                                 }
130                         }
131                 }
132                 sysfs_close_list(buslist);
133         }
134         return -1;
135 }
136
137 /**
138  * sysfs_close_device_tree: closes every device in the supplied tree, 
139  *      closing children only.
140  * @devroot: device root of tree.
141  */
142 void sysfs_close_device_tree(struct sysfs_device *devroot)
143 {
144         if (devroot != NULL) {
145                 if (devroot->children != NULL) {
146                         struct sysfs_device *child = NULL;
147
148                         dlist_for_each_data(devroot->children, child,
149                                         struct sysfs_device) {
150                                 sysfs_close_device_tree(child);
151                         }
152                 }
153                 sysfs_close_device(devroot);
154         }
155 }
156
157 /**
158  * sysfs_close_dev_tree: routine for dlist integration
159  */
160 static void sysfs_close_dev_tree(void *dev)
161 {
162         sysfs_close_device_tree((struct sysfs_device *)dev);
163 }
164
165 /**
166  * sysfs_close_device: closes and cleans up a device
167  * @dev = device to clean up
168  */
169 void sysfs_close_device(struct sysfs_device *dev)
170 {
171         if (dev != NULL) {
172                 if (dev->parent != NULL)
173                         sysfs_close_device(dev->parent);
174                 if (dev->directory != NULL)
175                         sysfs_close_directory(dev->directory);
176                 if (dev->children != NULL && dev->children->count == 0)
177                         dlist_destroy(dev->children);
178                 free(dev);
179         }
180 }
181
182 /**
183  * alloc_device: allocates and initializes device structure
184  * returns struct sysfs_device
185  */
186 static struct sysfs_device *alloc_device(void)
187 {
188         return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
189 }
190
191 /**
192  * open_device_dir: opens up sysfs_directory for specific root dev
193  * @name: name of root
194  * returns struct sysfs_directory with success and NULL with error
195  */
196 static struct sysfs_directory *open_device_dir(const char *path)
197 {
198         struct sysfs_directory *rdir = NULL;
199
200         if (path == NULL) {
201                 errno = EINVAL;
202                 return NULL;
203         }
204
205         rdir = sysfs_open_directory(path);
206         if (rdir == NULL) {
207                 errno = EINVAL;
208                 dprintf ("Device %s not supported on this system\n", path);
209                 return NULL;
210         }
211         if ((sysfs_read_dir_subdirs(rdir)) != 0) {
212                 dprintf ("Error reading device at dir %s\n", path);
213                 sysfs_close_directory(rdir);
214                 return NULL;
215         }
216         
217         return rdir;
218 }
219
220 /**
221  * sysfs_open_device_path: opens and populates device structure
222  * @path: path to device, this is the /sys/devices/ path
223  * returns sysfs_device structure with success or NULL with error
224  */
225 struct sysfs_device *sysfs_open_device_path(const char *path)
226 {
227         struct sysfs_device *dev = NULL;
228
229         if (path == NULL) {
230                 errno = EINVAL;
231                 return NULL;
232         }
233         if ((sysfs_path_is_dir(path)) != 0) {
234                 dprintf("Incorrect path to device: %s\n", path);
235                 return NULL;
236         }
237         dev = alloc_device();   
238         if (dev == NULL) {
239                 dprintf("Error allocating device at %s\n", path);
240                 return NULL;
241         }
242         if ((sysfs_get_name_from_path(path, dev->bus_id, 
243                                         SYSFS_NAME_LEN)) != 0) {
244                 errno = EINVAL;
245                 dprintf("Error getting device bus_id\n");
246                 sysfs_close_device(dev);
247                 return NULL;
248         }
249         safestrcpy(dev->path, path);
250         if ((sysfs_remove_trailing_slash(dev->path)) != 0) {
251                 dprintf("Invalid path to device %s\n", dev->path);
252                 sysfs_close_device(dev);
253                 return NULL;
254         }
255         /* 
256          * The "name" attribute no longer exists... return the device's
257          * sysfs representation instead, in the "dev->name" field, which
258          * implies that the dev->name and dev->bus_id contain same data.
259          */
260         safestrcpy(dev->name, dev->bus_id);
261         
262         if (sysfs_get_device_bus(dev) != 0)
263                 dprintf("Could not get device bus\n");
264         
265         if (get_dev_driver(dev) != 0) {
266                 dprintf("Could not get device %s's driver\n", dev->bus_id);
267                 safestrcpy(dev->driver_name, SYSFS_UNKNOWN);
268         }
269
270         return dev;
271 }
272
273 /**
274  * sysfs_open_device_tree: opens root device and all of its children,
275  *      creating a tree of devices. Only opens children.
276  * @path: sysfs path to devices
277  * returns struct sysfs_device and its children with success or NULL with
278  *      error.
279  */
280 struct sysfs_device *sysfs_open_device_tree(const char *path)
281 {
282         struct sysfs_device *rootdev = NULL, *new = NULL;
283         struct sysfs_directory *cur = NULL;
284
285         if (path == NULL) {
286                 errno = EINVAL;
287                 return NULL;
288         }
289         rootdev = sysfs_open_device_path(path);
290         if (rootdev == NULL) {
291                 dprintf("Error opening root device at %s\n", path);
292                 return NULL;
293         }
294         if (rootdev->directory == NULL) {
295                 rootdev->directory = open_device_dir(rootdev->path);
296                 if (rootdev->directory == NULL) 
297                         return NULL;
298         }
299         if (rootdev->directory->subdirs != NULL) {
300                 dlist_for_each_data(rootdev->directory->subdirs, cur,
301                                 struct sysfs_directory) {
302                         new = sysfs_open_device_tree(cur->path);
303                         if (new == NULL) {
304                                 dprintf("Error opening device tree at %s\n",
305                                         cur->path);
306                                 sysfs_close_device_tree(rootdev);
307                                 return NULL;
308                         }
309                         if (rootdev->children == NULL)
310                                 rootdev->children = dlist_new_with_delete
311                                         (sizeof(struct sysfs_device),
312                                         sysfs_close_dev_tree);
313                         dlist_unshift_sorted(rootdev->children, 
314                                                         new, sort_list);
315                 }
316         }
317
318         return rootdev;
319 }
320
321 /**
322  * sysfs_close_root_device: closes root and all devices
323  * @root: root device to close
324  */
325 void sysfs_close_root_device(struct sysfs_root_device *root)
326 {
327         if (root != NULL) {
328                 if (root->devices != NULL)
329                         dlist_destroy(root->devices);
330                 if (root->directory != NULL)
331                         sysfs_close_directory(root->directory);
332                 free(root);
333         }
334 }
335
336 /**
337  * sysfs_get_root_devices: opens up all the devices under this root device
338  * @root: root device to open devices for
339  * returns dlist of devices with success and NULL with error
340  */
341 struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root)
342 {
343         struct sysfs_device *dev = NULL;
344         struct sysfs_directory *cur = NULL;
345
346         if (root == NULL) {
347                 errno = EINVAL;
348                 return NULL;
349         }
350         if (root->directory == NULL) {
351                 root->directory = open_device_dir(root->path);
352                 if (root->directory == NULL)
353                         return NULL;
354         }
355                 
356         if (root->directory->subdirs == NULL)
357                 return 0;
358
359         dlist_for_each_data(root->directory->subdirs, cur,
360                         struct sysfs_directory) {
361                 dev = sysfs_open_device_tree(cur->path);
362                 if (dev == NULL) {
363                         dprintf ("Error opening device at %s\n", cur->path);
364                         continue;
365                 }
366                 if (root->devices == NULL)
367                         root->devices = dlist_new_with_delete
368                                 (sizeof(struct sysfs_device), 
369                                 sysfs_close_dev_tree);
370                 dlist_unshift_sorted(root->devices, dev, sort_list);
371         }
372
373         return root->devices;
374 }
375
376 /**
377  * sysfs_open_root_device: opens sysfs devices root and all of its
378  *      devices.
379  * @name: name of /sys/devices/root to open
380  * returns struct sysfs_root_device if success and NULL with error
381  */
382 struct sysfs_root_device *sysfs_open_root_device(const char *name)
383 {
384         struct sysfs_root_device *root = NULL;
385         char rootpath[SYSFS_PATH_MAX];
386
387         if (name == NULL) {
388                 errno = EINVAL;
389                 return NULL;
390         }
391
392         memset(rootpath, 0, SYSFS_PATH_MAX);
393         if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
394                 dprintf ("Sysfs not supported on this system\n");
395                 return NULL;
396         }
397
398         safestrcat(rootpath, "/");
399         safestrcat(rootpath, SYSFS_DEVICES_NAME);
400         safestrcat(rootpath, "/");
401         safestrcat(rootpath, name);
402         if ((sysfs_path_is_dir(rootpath)) != 0) {
403                 errno = EINVAL;
404                 dprintf("Invalid root device: %s\n", name);
405                 return NULL;
406         }
407         root = (struct sysfs_root_device *)calloc
408                                         (1, sizeof(struct sysfs_root_device));
409         if (root == NULL) {
410                 dprintf("calloc failure\n");
411                 return NULL;
412         }
413         safestrcpy(root->name, name);
414         safestrcpy(root->path, rootpath);
415         if ((sysfs_remove_trailing_slash(root->path)) != 0) {
416                 dprintf("Invalid path to root device %s\n", root->path);
417                 sysfs_close_root_device(root);
418                 return NULL;
419         }
420         return root;
421 }
422
423 /**
424  * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
425  *      the specific device
426  * @device: struct sysfs_device * for which attributes are to be returned
427  */
428 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
429 {
430         if (device == NULL) {
431                 errno = EINVAL;
432                 return NULL;
433         }
434
435         if (device->directory == NULL) {
436                 device->directory = sysfs_open_directory(device->path);
437                 if (device->directory == NULL) 
438                         return NULL;
439         }
440         if (device->directory->attributes == NULL) {
441                 if ((sysfs_read_dir_attributes(device->directory)) != 0)
442                         return NULL;
443         }
444         return (device->directory->attributes);
445 }
446
447 /**
448  * sysfs_refresh_device_attributes: refreshes the device's list of attributes
449  * @device: sysfs_device whose attributes to refresh
450  *  
451  * NOTE: Upon return, prior references to sysfs_attributes for this device
452  *              _may_ not be valid
453  *
454  * Returns list of attributes on success and NULL on failure
455  */
456 struct dlist *sysfs_refresh_device_attributes(struct sysfs_device *device)
457 {
458         if (device == NULL) {
459                 errno = EINVAL;
460                 return NULL;
461         }
462
463         if (device->directory == NULL)
464                 return (sysfs_get_device_attributes(device));
465
466         if ((sysfs_refresh_dir_attributes(device->directory)) != 0) {
467                 dprintf("Error refreshing device attributes\n");
468                 return NULL;
469         }
470
471         return (device->directory->attributes);
472 }
473
474 /**
475  * sysfs_get_device_attr: searches dev's attributes by name
476  * @dev: device to look through
477  * @name: attribute name to get
478  * returns sysfs_attribute reference with success or NULL with error.
479  */
480 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
481                                                 const char *name)
482 {
483         struct dlist *attrlist = NULL;
484
485         if (dev == NULL || name == NULL) {
486                 errno = EINVAL;
487                 return NULL;
488         }
489         
490         attrlist = sysfs_get_device_attributes(dev);
491         if (attrlist == NULL)
492                 return NULL;
493
494         return sysfs_get_directory_attribute(dev->directory, (char *)name);
495 }
496
497 /**
498  * get_device_absolute_path: looks up the bus the device is on, gets 
499  *              absolute path to the device
500  * @device: device for which path is needed
501  * @path: buffer to store absolute path
502  * @psize: size of "path"
503  * Returns 0 on success -1 on failure
504  */
505 static int get_device_absolute_path(const char *device, const char *bus, 
506                                 char *path, size_t psize)
507 {
508         char bus_path[SYSFS_PATH_MAX];
509
510         if (device == NULL || path == NULL) {
511                 errno = EINVAL;
512                 return -1;
513         }
514
515         memset(bus_path, 0, SYSFS_PATH_MAX);
516         if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
517                 dprintf ("Sysfs not supported on this system\n");
518                 return -1;
519         }
520         safestrcat(bus_path, "/");
521         safestrcat(bus_path, SYSFS_BUS_NAME);
522         safestrcat(bus_path, "/");
523         safestrcat(bus_path, bus);
524         safestrcat(bus_path, "/");
525         safestrcat(bus_path, SYSFS_DEVICES_NAME);
526         safestrcat(bus_path, "/");
527         safestrcat(bus_path, device);
528         /*
529          * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
530          * Now read this link to reach to the device.
531          */ 
532         if ((sysfs_get_link(bus_path, path, psize)) != 0) {
533                 dprintf("Error getting to device %s\n", device);
534                 return -1;
535         }
536         return 0;
537 }
538
539 /**
540  * sysfs_open_device: open a device by id (use the "bus" subsystem)
541  * @bus: bus the device belongs to
542  * @bus_id: bus_id of the device to open - has to be the "bus_id" in 
543  *              /sys/bus/xxx/devices
544  * returns struct sysfs_device if found, NULL otherwise
545  * NOTE: 
546  * 1. Use sysfs_close_device to close the device
547  * 2. Bus the device is on must be supplied
548  *      Use sysfs_find_device_bus to get the bus name
549  */
550 struct sysfs_device *sysfs_open_device(const char *bus, const char *bus_id)
551 {
552         char sysfs_path[SYSFS_PATH_MAX];
553         struct sysfs_device *device = NULL;
554
555         if (bus_id == NULL || bus == NULL) {
556                 errno = EINVAL;
557                 return NULL;
558         }
559         memset(sysfs_path, 0, SYSFS_PATH_MAX);
560         if ((get_device_absolute_path(bus_id, bus, sysfs_path, 
561                                                 SYSFS_PATH_MAX)) != 0) {
562                 dprintf("Error getting to device %s\n", bus_id);
563                 return NULL;
564         }
565         
566         device = sysfs_open_device_path(sysfs_path);
567         if (device == NULL) {
568                 dprintf("Error opening device %s\n", bus_id);
569                 return NULL;
570         }
571
572         return device;
573 }
574
575 /**
576  * sysfs_get_device_parent: opens up given device's parent and returns a 
577  *      reference to its sysfs_device
578  * @dev: sysfs_device whose parent is requested
579  * Returns sysfs_device of the parent on success and NULL on failure
580  */
581 struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
582 {
583         char ppath[SYSFS_PATH_MAX], *tmp = NULL;
584
585         if (dev == NULL) {
586                 errno = EINVAL;
587                 return NULL;
588         }
589
590         if (dev->parent != NULL)
591                 return (dev->parent);
592
593         memset(ppath, 0, SYSFS_PATH_MAX);
594         safestrcpy(ppath, dev->path);
595         tmp = strrchr(ppath, '/');
596         if (tmp == NULL) {
597                 dprintf("Invalid path to device %s\n", ppath);
598                 return NULL;
599         }
600         if (*(tmp + 1) == '\0') {
601                 *tmp = '\0';
602                 tmp = strrchr(tmp, '/');
603                 if (tmp == NULL) {
604                         dprintf("Invalid path to device %s\n", ppath);
605                         return NULL;
606                 }
607         }
608         *tmp = '\0';
609         
610         /*
611          * All "devices" have the "detach_state" attribute - validate here
612          */
613         safestrcat(ppath, "/detach_state");
614         if ((sysfs_path_is_file(ppath)) != 0) {
615                 dprintf("Device at %s does not have a parent\n", dev->path);
616                 return NULL;
617         }
618         tmp = strrchr(ppath, '/');
619         *tmp = '\0';
620         dev->parent = sysfs_open_device_path(ppath);
621         if (dev->parent == NULL) {
622                 dprintf("Error opening device %s's parent at %s\n", 
623                                         dev->bus_id, ppath);
624                 return NULL;
625         }
626         return (dev->parent);
627 }
628
629 /*
630  * sysfs_open_device_attr: open the given device's attribute
631  * @bus: Bus on which to look
632  * @dev_id: device for which attribute is required
633  * @attrname: name of the attribute to look for
634  * Returns struct sysfs_attribute on success and NULL on failure
635  * 
636  * NOTE:
637  *      A call to sysfs_close_attribute() is required to close
638  *      the attribute returned and free memory. 
639  */
640 struct sysfs_attribute *sysfs_open_device_attr(const char *bus,
641                 const char *bus_id, const char *attrib)
642 {
643         struct sysfs_attribute *attribute = NULL;
644         char devpath[SYSFS_PATH_MAX];
645         
646         if (bus == NULL || bus_id == NULL || attrib == NULL) {
647                 errno = EINVAL;
648                 return NULL;
649         }
650
651         memset(devpath, 0, SYSFS_PATH_MAX);
652         if ((get_device_absolute_path(bus_id, bus, devpath, 
653                                         SYSFS_PATH_MAX)) != 0) {
654                 dprintf("Error getting to device %s\n", bus_id);
655                 return NULL;
656         }
657         safestrcat(devpath, "/");
658         safestrcat(devpath, attrib);
659         attribute = sysfs_open_attribute(devpath);
660         if (attribute == NULL) {
661                 dprintf("Error opening attribute %s for device %s\n",
662                                 attrib, bus_id);
663                 return NULL;
664         }
665         if ((sysfs_read_attribute(attribute)) != 0) {
666                 dprintf("Error reading attribute %s for device %s\n",
667                                 attrib, bus_id);
668                 sysfs_close_attribute(attribute);
669                 return NULL;
670         }
671         return attribute;
672 }
673