]>
git.ipfire.org Git - thirdparty/systemd.git/blob - libsysfs/sysfs_device.c
4 * Generic device utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003
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.
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.
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
27 * sysfs_get_device_bus: retrieves the bus name the device is on, checks path
28 * to bus' link to make sure it has correct device.
29 * @dev: device to get busname.
30 * returns 0 with success and -1 with error.
32 int sysfs_get_device_bus(struct sysfs_device
*dev
)
34 unsigned char subsys
[SYSFS_NAME_LEN
], path
[SYSFS_PATH_MAX
];
35 unsigned char target
[SYSFS_PATH_MAX
], *bus
= NULL
, *c
= NULL
;
36 struct dlist
*buslist
= NULL
;
43 memset(subsys
, 0, SYSFS_NAME_LEN
);
45 strcpy(subsys
, SYSFS_BUS_NAME
); /* subsys = /bus */
46 buslist
= sysfs_open_subsystem_list(subsys
);
47 if (buslist
!= NULL
) {
48 dlist_for_each_data(buslist
, bus
, char) {
49 memset(path
, 0, SYSFS_PATH_MAX
);
50 strcpy(path
, dev
->path
);
51 c
= strstr(path
, "/devices");
53 dprintf("Invalid path to device %s\n", path
);
54 sysfs_close_list(buslist
);
59 strcat(path
, SYSFS_BUS_NAME
);
63 strcat(path
, SYSFS_DEVICES_NAME
);
65 strcat(path
, dev
->bus_id
);
66 if ((sysfs_path_is_link(path
)) == 0) {
67 memset(target
, 0, SYSFS_PATH_MAX
);
68 if ((sysfs_get_link(path
, target
,
69 SYSFS_PATH_MAX
)) != 0) {
70 dprintf("Error getting link target\n");
71 sysfs_close_list(buslist
);
74 if (!(strncmp(target
, dev
->path
,
76 strcpy(dev
->bus
, bus
);
77 sysfs_close_list(buslist
);
82 sysfs_close_list(buslist
);
88 * sysfs_close_device_tree: closes every device in the supplied tree,
89 * closing children only.
90 * @devroot: device root of tree.
92 static void sysfs_close_device_tree(struct sysfs_device
*devroot
)
94 if (devroot
!= NULL
) {
95 if (devroot
->children
!= NULL
) {
96 struct sysfs_device
*child
= NULL
;
98 dlist_for_each_data(devroot
->children
, child
,
99 struct sysfs_device
) {
100 sysfs_close_device_tree(child
);
103 sysfs_close_device(devroot
);
108 * sysfs_close_dev_tree: routine for dlist integration
110 static void sysfs_close_dev_tree(void *dev
)
112 sysfs_close_device_tree((struct sysfs_device
*)dev
);
116 * sysfs_close_device: closes and cleans up a device
117 * @dev = device to clean up
119 void sysfs_close_device(struct sysfs_device
*dev
)
122 if (dev
->parent
!= NULL
)
123 sysfs_close_device(dev
->parent
);
124 if (dev
->directory
!= NULL
)
125 sysfs_close_directory(dev
->directory
);
126 if (dev
->children
!= NULL
&& dev
->children
->count
== 0)
127 dlist_destroy(dev
->children
);
133 * alloc_device: allocates and initializes device structure
134 * returns struct sysfs_device
136 static struct sysfs_device
*alloc_device(void)
138 return (struct sysfs_device
*)calloc(1, sizeof(struct sysfs_device
));
142 * open_device_dir: opens up sysfs_directory for specific root dev
143 * @name: name of root
144 * returns struct sysfs_directory with success and NULL with error
146 static struct sysfs_directory
*open_device_dir(const unsigned char *path
)
148 struct sysfs_directory
*rdir
= NULL
;
155 rdir
= sysfs_open_directory(path
);
158 dprintf ("Device %s not supported on this system\n", path
);
161 if ((sysfs_read_dir_subdirs(rdir
)) != 0) {
162 dprintf ("Error reading device at dir %s\n", path
);
163 sysfs_close_directory(rdir
);
171 * sysfs_open_device_path: opens and populates device structure
172 * @path: path to device, this is the /sys/devices/ path
173 * returns sysfs_device structure with success or NULL with error
175 struct sysfs_device
*sysfs_open_device_path(const unsigned char *path
)
177 struct sysfs_device
*dev
= NULL
;
183 if ((sysfs_path_is_dir(path
)) != 0) {
184 dprintf("Incorrect path to device: %s\n", path
);
187 dev
= alloc_device();
189 dprintf("Error allocating device at %s\n", path
);
192 if ((sysfs_get_name_from_path(path
, dev
->bus_id
,
193 SYSFS_NAME_LEN
)) != 0) {
195 dprintf("Error getting device bus_id\n");
196 sysfs_close_device(dev
);
199 strcpy(dev
->path
, path
);
200 if ((sysfs_remove_trailing_slash(dev
->path
)) != 0) {
201 dprintf("Invalid path to device %s\n", dev
->path
);
202 sysfs_close_device(dev
);
206 * The "name" attribute no longer exists... return the device's
207 * sysfs representation instead, in the "dev->name" field, which
208 * implies that the dev->name and dev->bus_id contain same data.
210 strncpy(dev
->name
, dev
->bus_id
, SYSFS_NAME_LEN
);
212 if (sysfs_get_device_bus(dev
) != 0)
213 dprintf("Could not get device bus\n");
219 * sysfs_open_device_tree: opens root device and all of its children,
220 * creating a tree of devices. Only opens children.
221 * @path: sysfs path to devices
222 * returns struct sysfs_device and its children with success or NULL with
225 static struct sysfs_device
*sysfs_open_device_tree(const unsigned char *path
)
227 struct sysfs_device
*rootdev
= NULL
, *new = NULL
;
228 struct sysfs_directory
*cur
= NULL
;
234 rootdev
= sysfs_open_device_path(path
);
235 if (rootdev
== NULL
) {
236 dprintf("Error opening root device at %s\n", path
);
239 if (rootdev
->directory
== NULL
) {
240 rootdev
->directory
= open_device_dir(rootdev
->path
);
241 if (rootdev
->directory
== NULL
)
244 if (rootdev
->directory
->subdirs
!= NULL
) {
245 dlist_for_each_data(rootdev
->directory
->subdirs
, cur
,
246 struct sysfs_directory
) {
247 new = sysfs_open_device_tree(cur
->path
);
249 dprintf("Error opening device tree at %s\n",
251 sysfs_close_device_tree(rootdev
);
254 if (rootdev
->children
== NULL
)
255 rootdev
->children
= dlist_new_with_delete
256 (sizeof(struct sysfs_device
),
257 sysfs_close_dev_tree
);
258 dlist_unshift(rootdev
->children
, new);
266 * sysfs_close_root_device: closes root and all devices
267 * @root: root device to close
269 void sysfs_close_root_device(struct sysfs_root_device
*root
)
272 if (root
->devices
!= NULL
)
273 dlist_destroy(root
->devices
);
274 if (root
->directory
!= NULL
)
275 sysfs_close_directory(root
->directory
);
281 * sysfs_get_root_devices: opens up all the devices under this root device
282 * @root: root device to open devices for
283 * returns dlist of devices with success and NULL with error
285 struct dlist
*sysfs_get_root_devices(struct sysfs_root_device
*root
)
287 struct sysfs_device
*dev
= NULL
;
288 struct sysfs_directory
*cur
= NULL
;
294 if (root
->directory
== NULL
) {
295 root
->directory
= open_device_dir(root
->path
);
296 if (root
->directory
== NULL
)
300 if (root
->directory
->subdirs
== NULL
)
303 dlist_for_each_data(root
->directory
->subdirs
, cur
,
304 struct sysfs_directory
) {
305 dev
= sysfs_open_device_tree(cur
->path
);
307 dprintf ("Error opening device at %s\n", cur
->path
);
310 if (root
->devices
== NULL
)
311 root
->devices
= dlist_new_with_delete
312 (sizeof(struct sysfs_device
),
313 sysfs_close_dev_tree
);
314 dlist_unshift(root
->devices
, dev
);
317 return root
->devices
;
321 * sysfs_open_root_device: opens sysfs devices root and all of its
323 * @name: name of /sys/devices/root to open
324 * returns struct sysfs_root_device if success and NULL with error
326 struct sysfs_root_device
*sysfs_open_root_device(const unsigned char *name
)
328 struct sysfs_root_device
*root
= NULL
;
329 unsigned char rootpath
[SYSFS_PATH_MAX
];
336 memset(rootpath
, 0, SYSFS_PATH_MAX
);
337 if (sysfs_get_mnt_path(rootpath
, SYSFS_PATH_MAX
) != 0) {
338 dprintf ("Sysfs not supported on this system\n");
342 strcat(rootpath
, "/");
343 strcat(rootpath
, SYSFS_DEVICES_NAME
);
344 strcat(rootpath
, "/");
345 strcat(rootpath
, name
);
346 if ((sysfs_path_is_dir(rootpath
)) != 0) {
348 dprintf("Invalid root device: %s\n", name
);
351 root
= (struct sysfs_root_device
*)calloc
352 (1, sizeof(struct sysfs_root_device
));
354 dprintf("calloc failure\n");
357 strcpy(root
->name
, name
);
358 strcpy(root
->path
, rootpath
);
359 if ((sysfs_remove_trailing_slash(root
->path
)) != 0) {
360 dprintf("Invalid path to root device %s\n", root
->path
);
361 sysfs_close_root_device(root
);
368 * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
369 * the specific device
370 * @device: struct sysfs_device * for which attributes are to be returned
372 struct dlist
*sysfs_get_device_attributes(struct sysfs_device
*device
)
377 if (device
->directory
== NULL
) {
378 device
->directory
= sysfs_open_directory(device
->path
);
379 if (device
->directory
== NULL
)
382 if (device
->directory
->attributes
== NULL
) {
383 if ((sysfs_read_dir_attributes(device
->directory
)) != 0)
386 return (device
->directory
->attributes
);
390 * sysfs_refresh_device_attributes: refreshes the device's list of attributes
391 * @device: sysfs_device whose attributes to refresh
393 * NOTE: Upon return, prior references to sysfs_attributes for this device
396 * Returns list of attributes on success and NULL on failure
398 struct dlist
*sysfs_refresh_device_attributes(struct sysfs_device
*device
)
400 if (device
== NULL
) {
405 if (device
->directory
== NULL
)
406 return (sysfs_get_device_attributes(device
));
408 if ((sysfs_refresh_dir_attributes(device
->directory
)) != 0) {
409 dprintf("Error refreshing device attributes\n");
413 return (device
->directory
->attributes
);
417 * sysfs_get_device_attr: searches dev's attributes by name
418 * @dev: device to look through
419 * @name: attribute name to get
420 * returns sysfs_attribute reference with success or NULL with error.
422 struct sysfs_attribute
*sysfs_get_device_attr(struct sysfs_device
*dev
,
423 const unsigned char *name
)
425 struct dlist
*attrlist
= NULL
;
427 if (dev
== NULL
|| name
== NULL
) {
432 attrlist
= sysfs_get_device_attributes(dev
);
433 if (attrlist
== NULL
)
436 return sysfs_get_directory_attribute(dev
->directory
,
437 (unsigned char *)name
);
441 * get_device_absolute_path: looks up the bus the device is on, gets
442 * absolute path to the device
443 * @device: device for which path is needed
444 * @path: buffer to store absolute path
445 * @psize: size of "path"
446 * Returns 0 on success -1 on failure
448 static int get_device_absolute_path(const unsigned char *device
,
449 const unsigned char *bus
, unsigned char *path
, size_t psize
)
451 unsigned char bus_path
[SYSFS_PATH_MAX
];
453 if (device
== NULL
|| path
== NULL
) {
458 memset(bus_path
, 0, SYSFS_PATH_MAX
);
459 if (sysfs_get_mnt_path(bus_path
, SYSFS_PATH_MAX
) != 0) {
460 dprintf ("Sysfs not supported on this system\n");
463 strcat(bus_path
, "/");
464 strcat(bus_path
, SYSFS_BUS_NAME
);
465 strcat(bus_path
, "/");
466 strcat(bus_path
, bus
);
467 strcat(bus_path
, "/");
468 strcat(bus_path
, SYSFS_DEVICES_NAME
);
469 strcat(bus_path
, "/");
470 strcat(bus_path
, device
);
472 * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
473 * Now read this link to reach to the device.
475 if ((sysfs_get_link(bus_path
, path
, SYSFS_PATH_MAX
)) != 0) {
476 dprintf("Error getting to device %s\n", device
);
483 * sysfs_open_device: open a device by id (use the "bus" subsystem)
484 * @bus_id: bus_id of the device to open - has to be the "bus_id" in
485 * /sys/bus/xxx/devices
486 * @bus: bus the device belongs to
487 * returns struct sysfs_device if found, NULL otherwise
489 * 1. Use sysfs_close_device to close the device
490 * 2. Bus the device is on must be supplied
491 * Use sysfs_find_device_bus to get the bus name
493 struct sysfs_device
*sysfs_open_device(const unsigned char *bus_id
,
494 const unsigned char *bus
)
496 char sysfs_path
[SYSFS_PATH_MAX
];
497 struct sysfs_device
*device
= NULL
;
499 if (bus_id
== NULL
|| bus
== NULL
) {
503 memset(sysfs_path
, 0, SYSFS_PATH_MAX
);
504 if ((get_device_absolute_path(bus_id
, bus
, sysfs_path
,
505 SYSFS_PATH_MAX
)) != 0) {
506 dprintf("Error getting to device %s\n", bus_id
);
510 device
= sysfs_open_device_path(sysfs_path
);
511 if (device
== NULL
) {
512 dprintf("Error opening device %s\n", bus_id
);
520 * sysfs_get_device_parent: opens up given device's parent and returns a
521 * reference to its sysfs_device
522 * @dev: sysfs_device whose parent is requested
523 * Returns sysfs_device of the parent on success and NULL on failure
525 struct sysfs_device
*sysfs_get_device_parent(struct sysfs_device
*dev
)
527 unsigned char ppath
[SYSFS_PATH_MAX
], *tmp
= NULL
;
534 if (dev
->parent
!= NULL
)
535 return (dev
->parent
);
537 memset(ppath
, 0, SYSFS_PATH_MAX
);
538 strcpy(ppath
, dev
->path
);
539 tmp
= strrchr(ppath
, '/');
541 dprintf("Invalid path to device %s\n", ppath
);
544 if (*(tmp
+1) == '\0') {
546 tmp
= strrchr(tmp
, '/');
548 dprintf("Invalid path to device %s\n", ppath
);
555 * All "devices" have the "detach_state" attribute - validate here
557 strcat(ppath
, "/detach_state");
558 if ((sysfs_path_is_file(ppath
)) != 0) {
559 dprintf("Device at %s does not have a parent\n", dev
->path
);
562 tmp
= strrchr(ppath
, '/');
564 dev
->parent
= sysfs_open_device_path(ppath
);
565 if (dev
->parent
== NULL
) {
566 dprintf("Error opening device %s's parent at %s\n",
570 return (dev
->parent
);
574 * sysfs_open_device_attr: open the given device's attribute
575 * @bus: Bus on which to look
576 * @dev_id: device for which attribute is required
577 * @attrname: name of the attribute to look for
578 * Returns struct sysfs_attribute on success and NULL on failure
581 * A call to sysfs_close_attribute() is required to close
582 * the attribute returned and free memory.
584 struct sysfs_attribute
*sysfs_open_device_attr(const unsigned char *bus
,
585 const unsigned char *bus_id
, const unsigned char *attrib
)
587 struct sysfs_attribute
*attribute
= NULL
;
588 unsigned char devpath
[SYSFS_PATH_MAX
];
590 if (bus
== NULL
|| bus_id
== NULL
|| attrib
== NULL
) {
595 memset(devpath
, 0, SYSFS_PATH_MAX
);
596 if ((get_device_absolute_path(bus_id
, bus
, devpath
,
597 SYSFS_PATH_MAX
)) != 0) {
598 dprintf("Error getting to device %s\n", bus_id
);
601 strcat(devpath
, "/");
602 strcat(devpath
, attrib
);
603 attribute
= sysfs_open_attribute(devpath
);
604 if (attribute
== NULL
) {
605 dprintf("Error opening attribute %s for device %s\n",
609 if ((sysfs_read_attribute(attribute
)) != 0) {
610 dprintf("Error reading attribute %s for device %s\n",
612 sysfs_close_attribute(attribute
);