chiark / gitweb /
[PATCH] minor grammer fixes for the udev_vs_devfs document
[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 "libsysfs.h"
24 #include "sysfs.h"
25
26 /**
27  * get_device_bus: retrieves the bus name the device is on, checks path to
28  *      bus' link to make sure it has correct device.
29  * @dev: device to get busname.
30  * returns 0 with success and -1 with error.
31  */
32 static int get_device_bus(struct sysfs_device *dev)
33 {
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;
37
38         if (dev == NULL) {
39                 errno = EINVAL;
40                 return -1;
41         }
42
43         memset(subsys, 0, SYSFS_NAME_LEN);
44         strcat(subsys, "/");
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");
52                         if (c == NULL) {
53                                 dprintf("Invalid path to device %s\n", path);
54                                 sysfs_close_list(buslist);
55                                 return -1;
56                         }
57                         *c = '\0';
58                         strcat(path, "/");
59                         strcat(path, SYSFS_BUS_NAME);
60                         strcat(path, "/");
61                         strcat(path, bus);
62                         strcat(path, "/");
63                         strcat(path, SYSFS_DEVICES_NAME);
64                         strcat(path, "/");
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);
72                                         return -1;
73                                 }
74                                 if (!(strncmp(target, dev->path, 
75                                                         SYSFS_PATH_MAX))) {
76                                         strcpy(dev->bus, bus);
77                                         sysfs_close_list(buslist);
78                                         return 0;
79                                 }
80                         }
81                 }
82                 sysfs_close_list(buslist);
83         }
84         return -1;
85 }
86
87 /**
88  * sysfs_close_device_tree: closes every device in the supplied tree, 
89  *      closing children only.
90  * @devroot: device root of tree.
91  */
92 static void sysfs_close_device_tree(struct sysfs_device *devroot)
93 {
94         if (devroot != NULL) {
95                 if (devroot->children != NULL) {
96                         struct sysfs_device *child = NULL;
97
98                         dlist_for_each_data(devroot->children, child,
99                                         struct sysfs_device) {
100                                 sysfs_close_device_tree(child);
101                         }
102                 }
103                 sysfs_close_device(devroot);
104         }
105 }
106
107 /**
108  * sysfs_close_dev_tree: routine for dlist integration
109  */
110 static void sysfs_close_dev_tree(void *dev)
111 {
112         sysfs_close_device_tree((struct sysfs_device *)dev);
113 }
114
115 /**
116  * sysfs_close_device: closes and cleans up a device
117  * @dev = device to clean up
118  */
119 void sysfs_close_device(struct sysfs_device *dev)
120 {
121         if (dev != NULL) {
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);
128                 free(dev);
129         }
130 }
131
132 /**
133  * alloc_device: allocates and initializes device structure
134  * returns struct sysfs_device
135  */
136 static struct sysfs_device *alloc_device(void)
137 {
138         return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
139 }
140
141 /**
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
145  */
146 static struct sysfs_directory *open_device_dir(const unsigned char *path)
147 {
148         struct sysfs_directory *rdir = NULL;
149
150         if (path == NULL) {
151                 errno = EINVAL;
152                 return NULL;
153         }
154
155         rdir = sysfs_open_directory(path);
156         if (rdir == NULL) {
157                 errno = EINVAL;
158                 dprintf ("Device %s not supported on this system\n", path);
159                 return NULL;
160         }
161         if ((sysfs_read_dir_subdirs(rdir)) != 0) {
162                 dprintf ("Error reading device at dir %s\n", path);
163                 sysfs_close_directory(rdir);
164                 return NULL;
165         }
166         
167         return rdir;
168 }
169
170 /**
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
174  */
175 struct sysfs_device *sysfs_open_device_path(const unsigned char *path)
176 {
177         struct sysfs_device *dev = NULL;
178
179         if (path == NULL) {
180                 errno = EINVAL;
181                 return NULL;
182         }
183         if ((sysfs_path_is_dir(path)) != 0) {
184                 dprintf("Incorrect path to device: %s\n", path);
185                 return NULL;
186         }
187         dev = alloc_device();   
188         if (dev == NULL) {
189                 dprintf("Error allocating device at %s\n", path);
190                 return NULL;
191         }
192         if ((sysfs_get_name_from_path(path, dev->bus_id, 
193                                         SYSFS_NAME_LEN)) != 0) {
194                 errno = EINVAL;
195                 dprintf("Error getting device bus_id\n");
196                 sysfs_close_device(dev);
197                 return NULL;
198         }
199         strcpy(dev->path, path);
200         /* 
201          * The "name" attribute no longer exists... return the device's
202          * sysfs representation instead, in the "dev->name" field, which
203          * implies that the dev->name and dev->bus_id contain same data.
204          */
205         strncpy(dev->name, dev->bus_id, SYSFS_NAME_LEN);
206         
207         if (get_device_bus(dev) != 0)
208                 strcpy(dev->bus, SYSFS_UNKNOWN);
209
210         return dev;
211 }
212
213 /**
214  * sysfs_open_device_tree: opens root device and all of its children,
215  *      creating a tree of devices. Only opens children.
216  * @path: sysfs path to devices
217  * returns struct sysfs_device and its children with success or NULL with
218  *      error.
219  */
220 static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
221 {
222         struct sysfs_device *rootdev = NULL, *new = NULL;
223         struct sysfs_directory *cur = NULL;
224
225         if (path == NULL) {
226                 errno = EINVAL;
227                 return NULL;
228         }
229         rootdev = sysfs_open_device_path(path);
230         if (rootdev == NULL) {
231                 dprintf("Error opening root device at %s\n", path);
232                 return NULL;
233         }
234         if (rootdev->directory == NULL) {
235                 rootdev->directory = open_device_dir(rootdev->path);
236                 if (rootdev->directory == NULL) 
237                         return NULL;
238         }
239         if (rootdev->directory->subdirs != NULL) {
240                 dlist_for_each_data(rootdev->directory->subdirs, cur,
241                                 struct sysfs_directory) {
242                         new = sysfs_open_device_tree(cur->path);
243                         if (new == NULL) {
244                                 dprintf("Error opening device tree at %s\n",
245                                         cur->path);
246                                 sysfs_close_device_tree(rootdev);
247                                 return NULL;
248                         }
249                         if (rootdev->children == NULL)
250                                 rootdev->children = dlist_new_with_delete
251                                         (sizeof(struct sysfs_device),
252                                         sysfs_close_dev_tree);
253                         dlist_unshift(rootdev->children, new);
254                 }
255         }
256
257         return rootdev;
258 }
259
260 /**
261  * sysfs_close_root_device: closes root and all devices
262  * @root: root device to close
263  */
264 void sysfs_close_root_device(struct sysfs_root_device *root)
265 {
266         if (root != NULL) {
267                 if (root->devices != NULL)
268                         dlist_destroy(root->devices);
269                 if (root->directory != NULL)
270                         sysfs_close_directory(root->directory);
271                 free(root);
272         }
273 }
274
275 /**
276  * sysfs_get_root_devices: opens up all the devices under this root device
277  * @root: root device to open devices for
278  * returns dlist of devices with success and NULL with error
279  */
280 struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root)
281 {
282         struct sysfs_device *dev = NULL;
283         struct sysfs_directory *cur = NULL;
284
285         if (root == NULL) {
286                 errno = EINVAL;
287                 return NULL;
288         }
289         if (root->directory == NULL) {
290                 root->directory = open_device_dir(root->path);
291                 if (root->directory == NULL)
292                         return NULL;
293         }
294                 
295         if (root->directory->subdirs == NULL)
296                 return 0;
297
298         dlist_for_each_data(root->directory->subdirs, cur,
299                         struct sysfs_directory) {
300                 dev = sysfs_open_device_tree(cur->path);
301                 if (dev == NULL) {
302                         dprintf ("Error opening device at %s\n", cur->path);
303                         continue;
304                 }
305                 if (root->devices == NULL)
306                         root->devices = dlist_new_with_delete
307                                 (sizeof(struct sysfs_device), 
308                                 sysfs_close_dev_tree);
309                 dlist_unshift(root->devices, dev);
310         }
311
312         return root->devices;
313 }
314
315 /**
316  * sysfs_open_root_device: opens sysfs devices root and all of its
317  *      devices.
318  * @name: name of /sys/devices/root to open
319  * returns struct sysfs_root_device if success and NULL with error
320  */
321 struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
322 {
323         struct sysfs_root_device *root = NULL;
324         unsigned char rootpath[SYSFS_PATH_MAX];
325
326         if (name == NULL) {
327                 errno = EINVAL;
328                 return NULL;
329         }
330
331         memset(rootpath, 0, SYSFS_PATH_MAX);
332         if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
333                 dprintf ("Sysfs not supported on this system\n");
334                 return NULL;
335         }
336
337         if (sysfs_trailing_slash(rootpath) == 0)
338                 strcat(rootpath, "/");
339         strcat(rootpath, SYSFS_DEVICES_NAME);
340         strcat(rootpath, "/");
341         strcat(rootpath, name);
342         if ((sysfs_path_is_dir(rootpath)) != 0) {
343                 errno = EINVAL;
344                 dprintf("Invalid root device: %s\n", name);
345                 return NULL;
346         }
347         root = (struct sysfs_root_device *)calloc
348                                         (1, sizeof(struct sysfs_root_device));
349         if (root == NULL) {
350                 dprintf("calloc failure\n");
351                 return NULL;
352         }
353         strcpy(root->name, name);
354         strcpy(root->path, rootpath);
355         return root;
356 }
357
358 /**
359  * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
360  *      the specific device
361  * @device: struct sysfs_device * for which attributes are to be returned
362  */
363 struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
364 {
365         if (device == NULL) 
366                 return NULL;
367
368         if (device->directory == NULL) {
369                 device->directory = sysfs_open_directory(device->path);
370                 if (device->directory == NULL) 
371                         return NULL;
372         }
373         if (device->directory->attributes == NULL) {
374                 if ((sysfs_read_dir_attributes(device->directory)) != 0)
375                         return NULL;
376         } else {
377                 if ((sysfs_path_is_dir(device->path)) != 0) {
378                         dprintf("Device at %s no longer exists", device->path);
379                         return NULL;
380                 }
381                 if ((sysfs_refresh_attributes
382                                 (device->directory->attributes)) != 0) {
383                         dprintf("Error refreshing device attributes\n");
384                         return NULL;
385                 }
386         }
387         return (device->directory->attributes);
388 }
389
390 /**
391  * sysfs_get_device_attr: searches dev's attributes by name
392  * @dev: device to look through
393  * @name: attribute name to get
394  * returns sysfs_attribute reference with success or NULL with error.
395  */
396 struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
397                                                 const unsigned char *name)
398 {
399         struct sysfs_attribute *cur = NULL;
400         struct dlist *attrlist = NULL;
401
402         if (dev == NULL || name == NULL) {
403                 errno = EINVAL;
404                 return NULL;
405         }
406
407         attrlist = sysfs_get_device_attributes(dev);
408         if (attrlist == NULL)
409                 return NULL;
410
411         cur = sysfs_get_directory_attribute(dev->directory, 
412                         (unsigned char *)name);
413
414         return cur;
415 }
416
417 /**
418  * get_device_absolute_path: looks up the bus the device is on, gets 
419  *              absolute path to the device
420  * @device: device for which path is needed
421  * @path: buffer to store absolute path
422  * @psize: size of "path"
423  * Returns 0 on success -1 on failure
424  */
425 static int get_device_absolute_path(const unsigned char *device,
426                 const unsigned char *bus, unsigned char *path, size_t psize)
427 {
428         unsigned char bus_path[SYSFS_PATH_MAX];
429
430         if (device == NULL || path == NULL) {
431                 errno = EINVAL;
432                 return -1;
433         }
434
435         memset(bus_path, 0, SYSFS_PATH_MAX);
436         if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
437                 dprintf ("Sysfs not supported on this system\n");
438                 return -1;
439         }
440         if (sysfs_trailing_slash(bus_path) == 0)
441                 strcat(bus_path, "/");
442         strcat(bus_path, SYSFS_BUS_NAME);
443         strcat(bus_path, "/");
444         strcat(bus_path, bus);
445         strcat(bus_path, "/");
446         strcat(bus_path, SYSFS_DEVICES_NAME);
447         strcat(bus_path, "/");
448         strcat(bus_path, device);
449         /*
450          * We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
451          * Now read this link to reach to the device.
452          */ 
453         if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) {
454                 dprintf("Error getting to device %s\n", device);
455                 return -1;
456         }
457         return 0;
458 }
459
460 /**
461  * sysfs_open_device: open a device by id (use the "bus" subsystem)
462  * @bus_id: bus_id of the device to open - has to be the "bus_id" in 
463  *              /sys/bus/xxx/devices
464  * @bus: bus the device belongs to
465  * returns struct sysfs_device if found, NULL otherwise
466  * NOTE: 
467  * 1. Use sysfs_close_device to close the device
468  * 2. Bus the device is on must be supplied
469  *      Use sysfs_find_device_bus to get the bus name
470  */
471 struct sysfs_device *sysfs_open_device(const unsigned char *bus_id, 
472                                                 const unsigned char *bus)
473 {
474         char sysfs_path[SYSFS_PATH_MAX];
475         struct sysfs_device *device = NULL;
476
477         if (bus_id == NULL || bus == NULL) {
478                 errno = EINVAL;
479                 return NULL;
480         }
481         memset(sysfs_path, 0, SYSFS_PATH_MAX);
482         if ((get_device_absolute_path(bus_id, bus, sysfs_path, 
483                                                 SYSFS_PATH_MAX)) != 0) {
484                 dprintf("Error getting to device %s\n", bus_id);
485                 return NULL;
486         }
487         
488         device = sysfs_open_device_path(sysfs_path);
489         if (device == NULL) {
490                 dprintf("Error opening device %s\n", bus_id);
491                 return NULL;
492         }
493
494         return device;
495 }
496
497 /**
498  * sysfs_get_device_parent: opens up given device's parent and returns a 
499  *      reference to its sysfs_device
500  * @dev: sysfs_device whose parent is requested
501  * Returns sysfs_device of the parent on success and NULL on failure
502  */
503 struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
504 {
505         unsigned char ppath[SYSFS_PATH_MAX], *tmp = NULL;
506
507         if (dev == NULL) {
508                 errno = EINVAL;
509                 return NULL;
510         }
511
512         if (dev->parent != NULL)
513                 return (dev->parent);
514
515         memset(ppath, 0, SYSFS_PATH_MAX);
516         strcpy(ppath, dev->path);
517         tmp = strrchr(ppath, '/');
518         if (tmp == NULL) {
519                 dprintf("Invalid path to device %s\n", ppath);
520                 return NULL;
521         }
522         if (*(tmp +1) == '\0') {
523                 *tmp = '\0';
524                 tmp = strrchr(tmp, '/');
525                 if (tmp == NULL) {
526                         dprintf("Invalid path to device %s\n", ppath);
527                         return NULL;
528                 }
529         }
530         *tmp = '\0';
531         
532         /*
533          * All "devices" have the "detach_state" attribute - validate here
534          */
535         strcat(ppath, "/detach_state");
536         if ((sysfs_path_is_file(ppath)) != 0) {
537                 dprintf("Device at %s does not have a parent\n", dev->path);
538                 return NULL;
539         }
540         tmp = strrchr(ppath, '/');
541         *tmp = '\0';
542         dev->parent = sysfs_open_device_path(ppath);
543         if (dev->parent == NULL) {
544                 dprintf("Error opening device %s's parent at %s\n", 
545                                         dev->bus_id, ppath);
546                 return NULL;
547         }
548         return (dev->parent);
549 }
550
551 /*
552  * sysfs_open_device_attr: open the given device's attribute
553  * @bus: Bus on which to look
554  * @dev_id: device for which attribute is required
555  * @attrname: name of the attribute to look for
556  * Returns struct sysfs_attribute on success and NULL on failure
557  * 
558  * NOTE:
559  *      A call to sysfs_close_attribute() is required to close
560  *      the attribute returned and free memory. 
561  */
562 struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus,
563                 const unsigned char *bus_id, const unsigned char *attrib)
564 {
565         struct sysfs_attribute *attribute = NULL;
566         unsigned char devpath[SYSFS_PATH_MAX];
567         
568         if (bus == NULL || bus_id == NULL || attrib == NULL) {
569                 errno = EINVAL;
570                 return NULL;
571         }
572
573         memset(devpath, 0, SYSFS_PATH_MAX);
574         if ((get_device_absolute_path(bus_id, bus, devpath, 
575                                         SYSFS_PATH_MAX)) != 0) {
576                 dprintf("Error getting to device %s\n", bus_id);
577                 return NULL;
578         }
579         strcat(devpath, "/");
580         strcat(devpath, attrib);
581         attribute = sysfs_open_attribute(devpath);
582         if (attribute == NULL) {
583                 dprintf("Error opening attribute %s for device %s\n",
584                                 attrib, bus_id);
585                 return NULL;
586         }
587         if ((sysfs_read_attribute(attribute)) != 0) {
588                 dprintf("Error reading attribute %s for device %s\n",
589                                 attrib, bus_id);
590                 sysfs_close_attribute(attribute);
591                 return NULL;
592         }
593         return attribute;
594 }
595