From fe3fe3b29ffbc7d0ce7dca6a371da31d8b3ff7f8 Mon Sep 17 00:00:00 2001 From: "dsteklof@us.ibm.com" Date: Tue, 21 Oct 2003 01:19:14 -0700 Subject: [PATCH] [PATCH] new version of libsysfs patch Here's the patch applying the latest libsysfs. - adds the latest libsysfs code to udev * new code includes dlist implementation, a generic linked list implementation. Needed our own because LGPL * rearranged structures * provided more functions for accessing directory and attributes - gets rid of ->directory->path references in namedev.c - replaces sysfs_get_value_from_attributes with sysfs_get_classdev_attr --- libsysfs/Makefile | 9 +- libsysfs/dlist.c | 343 ++++++++++++++++++++++++ libsysfs/dlist.h | 195 ++++++++++++++ libsysfs/libsysfs.h | 198 ++++++++++---- libsysfs/sysfs.h | 2 +- libsysfs/sysfs_bus.c | 412 ++++++++++++++++++++++------- libsysfs/sysfs_class.c | 402 +++++++++++++++++++++++----- libsysfs/sysfs_device.c | 487 ++++++++++++++++++++++++++++------ libsysfs/sysfs_dir.c | 566 +++++++++++++++++++++++++++------------- libsysfs/sysfs_driver.c | 304 ++++++++++++++++++++- libsysfs/sysfs_utils.c | 163 ++++++++++-- namedev.c | 56 ++-- 12 files changed, 2599 insertions(+), 538 deletions(-) create mode 100644 libsysfs/dlist.c create mode 100644 libsysfs/dlist.h diff --git a/libsysfs/Makefile b/libsysfs/Makefile index 79cc53395..b82acbdbf 100644 --- a/libsysfs/Makefile +++ b/libsysfs/Makefile @@ -1,15 +1,15 @@ # Makefile for libsysfs.a # Copyright (c) International Business Machines Corp., 2003 -H_INCLUDE=../include +H_INCLUDE=. LIB_INCLUDE=. OBJS=sysfs_bus.o sysfs_class.o sysfs_device.o sysfs_dir.o sysfs_driver.o \ - sysfs_utils.o + sysfs_utils.o dlist.o # Install directory # Options -CFLAGS=-O2 -Wall -ansi -g +CFLAGS=-O2 -Wall -g # sysfs library LIBSYSFS=libsysfs.a @@ -38,5 +38,8 @@ sysfs_driver.o: sysfs_driver.c sysfs_utils.o: sysfs_utils.c $(CC) -I$(H_INCLUDE) -I$(LIB_INCLUDE) $(CFLAGS) -c sysfs_utils.c +dlist.o: dlist.c + $(CC) -I$(H_INCLUDE) -I$(LIB_INCLUDE) $(CFLAGS) -c dlist.c + clean: $(RM) *.o *~ core $(LIBSYSFS) diff --git a/libsysfs/dlist.c b/libsysfs/dlist.c new file mode 100644 index 000000000..6dfcf726b --- /dev/null +++ b/libsysfs/dlist.c @@ -0,0 +1,343 @@ +/* + * dlist.c + * + * Copyright (C) 2003 Eric J Bohm + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021110307 USA + * + */ + + +/* Double linked list implementation. + + * You allocate the data and give dlist the pointer. + * If your data is complex set the dlist->del_func to a an appropriate + * delete function. Otherwise dlist will just use free. + +*/ +#include "dlist.h" + +/* + * Return pointer to node at marker. + * else null if no nodes. + */ + +inline void *dlist_mark(Dlist *list) +{ + if(list->marker!=NULL) + return(list->marker->data); + else + return(NULL); +} + +/* + * Set marker to start. + */ + +inline void dlist_start(Dlist *list) +{ + list->marker=list->head; +} + +/* + * Set marker to end. + */ + +inline void dlist_end(Dlist *list) +{ + list->marker=list->head; +} + +/* internal use function + * quickie inline to consolidate the marker movement logic + * in one place + * + * when direction true it moves marker after + * when direction false it moves marker before. + * return pointer to data at new marker + * if nowhere to move the marker in desired direction return null + */ +inline void *_dlist_mark_move(Dlist *list,int direction) +{ + if(direction) + { + if( list->marker->next!=NULL) + list->marker=list->marker->next; + else + return(NULL); + } + else + { + if( list->marker->prev!=NULL) + list->marker=list->marker->prev; + else + return(NULL); + } + if(list->marker!=list->head) + return(list->marker->data); + else + return(NULL); +} + +/* + * Create new linked list to store nodes of datasize. + * return null if list cannot be created. + */ +Dlist *dlist_new(size_t datasize) +{ + Dlist *list=NULL; + if((list=malloc(sizeof(Dlist)))) + { + list->marker=NULL; + list->count=0L; + list->data_size=datasize; + list->del_func=free; + list->head=&(list->headnode); + list->head->prev=NULL; + list->head->next=NULL; + list->head->data=NULL; + } + return(list); +} + +/* + * Create new linked list to store nodes of datasize set list + * data node delete function to the passed in del_func + * return null if list cannot be created. + */ +Dlist *dlist_new_with_delete(size_t datasize,void (*del_func)(void*)) +{ + Dlist *list=NULL; + list=dlist_new(datasize); + if(list!=NULL) + list->del_func=del_func; + return(list); +} + + +/* + * remove marker node from list + * call data_delete function on data if registered. + * otherwise call free. + * when direction true it moves marker after + * when direction false it moves marker before. + * free marker node + * return nothing. + */ +void dlist_delete(Dlist *list,int direction) +{ + if((list->marker != list->head)&&(list->marker!=NULL)) + { + DL_node *corpse; + corpse=list->marker; + _dlist_mark_move(list,direction); + if(list->head->next==corpse) + list->head->next=corpse->next; + if(list->head->prev==corpse) + list->head->prev=corpse->prev; + if(corpse->prev!=NULL) //should be impossible + corpse->prev->next=corpse->next; + if(corpse->next!=NULL) //should be impossible + corpse->next->prev=corpse->prev; + list->del_func(corpse->data); + list->count--; + free(corpse); + } +} + +/* + * Insert node containing data at marker. + * If direction true it inserts after. + * If direction false it inserts before. + * move marker to inserted node + * return pointer to inserted node + */ +void *dlist_insert(Dlist *list,void *data,int direction) +{ + DL_node *new_node=NULL; + if(list==NULL || data==NULL) + return(NULL); + if(list->marker==NULL) //in case the marker ends up unset + list->marker=list->head; + if((new_node=malloc(sizeof(DL_node)))) + { + new_node->data=data; + new_node->prev=NULL; + new_node->next=NULL; + list->count++; + if(list->head->next==NULL) //no l + { + list->head->next=list->head->prev=new_node; + new_node->prev=list->head; + new_node->next=list->head; + } + else if(direction) + { + new_node->next=list->marker->next; + new_node->prev=list->marker; + list->marker->next->prev=new_node; + list->marker->next=new_node; + } + else + { + new_node->prev=list->marker->prev; + new_node->next=list->marker; + list->marker->prev->next=new_node; + list->marker->prev=new_node; + } + list->marker=new_node; + } + else + { + return(NULL); + } + return(list->marker->data); +} + +/* + * Remove DL_node from list without deallocating data. + * if marker == killme . + * when direction true it moves marker after + * when direction false it moves marker before. + * to previous if there is no next. + */ +void *_dlist_remove(Dlist *list,DL_node *killme,int direction) +{ + if(killme!=NULL) + { + void *killer_data=killme->data; + // take care of head and marker pointers. + if(list->marker==killme) + _dlist_mark_move(list,direction); + if(killme ==list->head->next) + list->head->next=killme->next; + if(killme==list->head->prev) + list->head->prev=killme->prev; + // remove from list + if(killme->prev !=NULL) + killme->prev->next=killme->next; + if(killme->next !=NULL) + killme->next->prev=killme->prev; + list->count--; + free(killme); + return(killer_data); + } + else + return (NULL); +} + + +/* + * Insert node containing data after end. + */ +void dlist_push(Dlist *list,void *data) +{ + list->marker=list->head->prev; + dlist_insert(list,data,1); +} + +/* + * Insert node containing data at start. + */ + +void dlist_unshift(Dlist *list,void *data) + +{ + list->marker=list->head->next; + dlist_insert(list,data,0); +} + + +/* + * Remove end node from list. + * Return pointer to data in removed node. + * Null if no nodes. + */ + +void *dlist_pop(Dlist *list) +{ + return(_dlist_remove(list,list->head->prev,0)); +} + +/* + * Remove start node from list. + * Return pointer to data in removed node. + * Null if no nodes. + */ + +void *dlist_shift(Dlist *list) +{ + return(_dlist_remove(list,list->head->next,1)); +} + + +/* + * destroy the list freeing all memory + */ + + +void dlist_destroy(Dlist *list) +{ + if(list !=NULL) + { + dlist_start(list); + dlist_next(list); + while (dlist_mark(list)) { + dlist_delete(list,1); + } + free(list); + } +} + +/** + * Return void pointer to list_data element matching comp function criteria + * else null + * Does not move the marker. + */ + +void *dlist_find_custom(struct dlist *list, void *target, int (*comp)(void *, void *)) +{ + /* test the comp function on each node */ + struct dl_node *nodepointer; + dlist_for_each_nomark(list,nodepointer) + if(comp(target,nodepointer->data)) + return(nodepointer->data); + return(NULL); +} + +/** + * Apply the node_operation function to each data node in the list + */ +void dlist_transform(struct dlist *list, void (*node_operation)(void *)) +{ + struct dl_node *nodepointer; + dlist_for_each_nomark(list,nodepointer) + node_operation(nodepointer->data); +} + +/** + * insert new into list in sorted order + * sorter function in form int sorter(new,ith) + * must return 1 for when new should go before ith + * else 0 + * return pointer to inserted node + * NOTE: assumes list is already sorted + */ +void *dlist_insert_sorted(struct dlist *list, void *new, int (*sorter)(void *, void *)) +{ + for(dlist_start(list),dlist_next(list); \ + list->marker!=list->head && !sorter(new,list->marker->data);dlist_next(list)); + return(dlist_insert_before(list,new)); +} diff --git a/libsysfs/dlist.h b/libsysfs/dlist.h new file mode 100644 index 000000000..5da79f9ba --- /dev/null +++ b/libsysfs/dlist.h @@ -0,0 +1,195 @@ +/* + * dlist.h + * + * Copyright (C) 2003 Eric J Bohm + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#ifndef _DLIST_H_ +#define _DLIST_H_ + +/* Double linked list header. + +* navigate your list with DLIST_PREV and DLIST_NEXT. These are macros +* so function call overhead is minimized. + +* Supports perl style push, pop, shift, unshift list semantics. + +* You allocate the data and give dlist the pointer. If your data is +* complex set the dlist->del_func to a an appropriate delete using +* dlist_new_with_delete. Your delete function must match +(void * )(del(void *) +*Otherwise dlist will just use free. + +* NOTE: The small amount of pain involved in doing that allows us to +* avoid copy in copy out semantics. + +* Dlist uses an internal mark pointer to keep track of where you are +* in the list. + +* insert and delete take a directional parameter. Where direction +* corresponds to the direction in which you want the list to go. +* true direction corresponded to progressing forward in the last +* false to regressing in the list. +* so a dlist_insert(yourlist,item,1) will insert it after the mark +* so a dlist_insert(yourlist,item,0) will insert it before the mark +* any insert will move the mark to the new node regardless of the direction. + +* Just use the dlist_(insert|delete)_(before|after) macros if you do not want +* to think about it. + +*/ +#include +typedef struct dl_node { + struct dl_node *prev; + struct dl_node *next; + void *data; +} DL_node; + +typedef struct dlist { + DL_node *marker; + unsigned long count; + size_t data_size; + void (*del_func)(void *); + DL_node headnode; + DL_node *head; +} Dlist; + +Dlist *dlist_new(size_t datasize); +Dlist *dlist_new_with_delete(size_t datasize,void (*del_func)(void*)); +void *_dlist_mark_move(Dlist *list,int direction); +void *dlist_mark(Dlist *); +void dlist_start(Dlist *); +void dlist_end(Dlist *); + +void *dlist_insert(Dlist *,void *,int) ; + +void *dlist_insert_sorted(struct dlist *list, void *new, int (*sorter)(void *, void *)); + +void dlist_delete(Dlist *,int); + +void dlist_push(Dlist *,void *); + +void dlist_unshift(Dlist *,void *); + +void *dlist_pop(Dlist *); + +void *dlist_shift(Dlist *); + +void dlist_destroy(Dlist *); + +void *dlist_find_custom(struct dlist *list, void *target, int (*comp)(void *, void *)); +void dlist_transform(struct dlist *list, void (*node_operation)(void *)); + + +/* + * _dlist_remove is for internal use only + * _dlist_mark_move is for internal use only + */ +void *_dlist_remove(struct dlist *,struct dl_node *,int ); + +#define dlist_prev(A) _dlist_mark_move((A),0) +#define dlist_next(A) _dlist_mark_move((A),1) + +#define dlist_insert_before(A,B) dlist_insert((A),(B),0) +#define dlist_insert_after(A,B) dlist_insert((A),(B),1) + +#define dlist_delete_before(A) dlist_delete((A),0) +#define dlist_delete_after(A) dlist_delete((A),1) + +/** + * provide for loop header which iterates the mark from start to end + * list: the dlist pointer, use dlist_mark(list) to get iterator + */ +#define dlist_for_each(list) \ + for(dlist_start(list),dlist_next(list); \ + (list)->marker!=(list)->head;dlist_next(list)) + +/** + * provide for loop header which iterates the mark from end to start + * list: the dlist pointer, use dlist_mark(list) to get iterator + */ +#define dlist_for_each_rev(list) \ + for(dlist_end(list),dlist_prev(list); \ + (list)->marker!=(list)->head;dlist_prev(list)) + +/** + * provide for loop header which iterates through the list without moving mark + * list: the dlist_pointer + * iterator: dl_node pointer to iterate + */ +#define dlist_for_each_nomark(list,iterator) \ + for((iterator)=(list)->head->next; (iterator)!=(list)->head; \ + (iterator)=(iterator)->next) + +/** + * provide for loop header which iterates through the list without moving mark + * in reverse + * list: the dlist_pointer + * iterator: dl_node pointer to iterate + */ +#define dlist_for_each_nomark_rev(list,iterator) \ + for((iterator)=(list)->head->prev; (iterator)!=(list)->head; \ + (iterator)=(iterator)->prev) +/** + * provide for loop header which iterates through the list providing a + * data iterator + * list: the dlist pointer + * data_iterator: the pointer of type datatype to iterate + * datatype: actual type of the contents in the dl_node->data + */ + +#define dlist_for_each_data(list,data_iterator,datatype) \ + for(dlist_start(list), (data_iterator)=(datatype *) dlist_next(list); \ + (list)->marker!=(list)->head;(data_iterator)=(datatype *) dlist_next(list)) + +/** + * provide for loop header which iterates through the list providing a + * data iterator in reverse + * list: the dlist pointer + * data_iterator: the pointer of type datatype to iterate + * datatype: actual type of the contents in the dl_node->data + */ +#define dlist_for_each_data_rev(list,data_iterator,datatype) \ + for(dlist_end(list), (data_iterator)=(datatype *) dlist_prev(list); \ + (list)->marker!=(list)->head;(data_iterator)=(datatype *) dlist_prev(list)) + +/** + * provide for loop header which iterates through the list providing a + * data iterator without moving the mark + * list: the dlist pointer + * iterator: the dl_node pointer to iterate + * data_iterator: the pointer of type datatype to iterate + * datatype: actual type of the contents in the dl_node->data + */ + +#define dlist_for_each_data_nomark(list,iterator,data_iterator,datatype) \ + for((iterator)=(list)->head->next, (data_iterator)=(datatype *) (iterator)->data; \ + (iterator)!=(list)->head;(iterator)=(iterator)->next,(data_iterator)=(datatype *) (iterator)) + +/** + * provide for loop header which iterates through the list providing a + * data iterator in reverse without moving the mark + * list: the dlist pointer + * iterator: the dl_node pointer to iterate + * data_iterator: the pointer of type datatype to iterate + * datatype: actual type of the contents in the dl_node->data + */ +#define dlist_for_each_data_nomark_rev(list,iterator, data_iterator,datatype) \ + for((iterator)=(list)->head->prev, (data_iterator)=(datatype *) (iterator)->data; \ + (iterator)!=(list)->head;(iterator)=(iterator)->prev,(data_iterator)=(datatype *) (iterator)) + +#endif /* _DLIST_H_ */ diff --git a/libsysfs/libsysfs.h b/libsysfs/libsysfs.h index 01143954e..ccb9898cb 100644 --- a/libsysfs/libsysfs.h +++ b/libsysfs/libsysfs.h @@ -3,7 +3,7 @@ * * Header Definitions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -24,6 +24,7 @@ #define _LIBSYSFS_H_ #include +#include "dlist.h" /* * Generic #defines go here.. @@ -32,11 +33,17 @@ #define SYSFS_PROC_MNTS "/proc/mounts" #define SYSFS_BUS_DIR "/bus" #define SYSFS_CLASS_DIR "/class" +#define SYSFS_BLOCK_DIR "/block" #define SYSFS_DEVICES_DIR "/devices" #define SYSFS_DEVICES_NAME "devices" #define SYSFS_DRIVERS_DIR "/drivers" #define SYSFS_DRIVERS_NAME "drivers" #define SYSFS_NAME_ATTRIBUTE "name" +#define SYSFS_UNKNOWN "unknown" + +/* Some "block" subsystem specific #defines */ +#define SYSFS_QUEUE_NAME "queue" +#define SYSFS_IOSCHED_NAME "iosched" #define SYSFS_PATH_MAX 255 #define SYSFS_NAME_LEN 50 @@ -46,65 +53,84 @@ #define SYSFS_METHOD_STORE 0x02 /* attr can be changed by user */ struct sysfs_attribute { - struct sysfs_attribute *next; - char path[SYSFS_PATH_MAX]; - char *value; + unsigned char *value; unsigned short len; /* value length */ unsigned short method; /* show and store */ + unsigned char name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; }; -struct sysfs_dlink { - struct sysfs_dlink *next; - char name[SYSFS_NAME_LEN]; - struct sysfs_directory *target; +struct sysfs_link { + unsigned char name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; + unsigned char target[SYSFS_PATH_MAX]; }; struct sysfs_directory { - struct sysfs_directory *next; - char path[SYSFS_PATH_MAX]; - struct sysfs_directory *subdirs; - struct sysfs_dlink *links; - struct sysfs_attribute *attributes; + struct dlist *subdirs; + struct dlist *links; + struct dlist *attributes; + unsigned char name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; }; struct sysfs_driver { - struct sysfs_driver *next; - char name[SYSFS_NAME_LEN]; - struct sysfs_directory *directory; - struct sysfs_device *device; + struct dlist *devices; + unsigned char name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; + + /* for internal use only */ + struct sysfs_directory *directory; }; struct sysfs_device { - struct sysfs_device *next; - char name[SYSFS_NAME_LEN]; - char bus_id[SYSFS_NAME_LEN]; - struct sysfs_driver *driver; + struct sysfs_device *parent; + struct dlist *children; + unsigned char name[SYSFS_NAME_LEN]; + unsigned char bus_id[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; + unsigned char driver_name[SYSFS_NAME_LEN]; + + /* for internal use only */ + struct sysfs_directory *directory; +}; + +struct sysfs_root_device { + struct dlist *devices; + unsigned char name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; + + /* for internal use only */ struct sysfs_directory *directory; - struct sysfs_device *parent; - struct sysfs_device *children; }; struct sysfs_bus { - struct sysfs_bus *next; - char name[SYSFS_NAME_LEN]; - struct sysfs_directory *directory; - struct sysfs_driver *drivers; - struct sysfs_device *devices; + struct dlist *drivers; + struct dlist *devices; + unsigned char name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; + + /* internal use only */ + struct sysfs_directory *directory; }; struct sysfs_class_device { - struct sysfs_class_device *next; - char name[SYSFS_NAME_LEN]; - struct sysfs_directory *directory; struct sysfs_device *sysdevice; /* NULL if virtual */ struct sysfs_driver *driver; /* NULL if not implemented */ + unsigned char name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; + + /* for internal use only */ + struct sysfs_directory *directory; }; struct sysfs_class { - struct sysfs_class *next; - char name[SYSFS_NAME_LEN]; - struct sysfs_directory *directory; - struct sysfs_class_device *devices; + struct dlist *devices; + unsigned char name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; + + /* for internal use only */ + struct sysfs_directory *directory; }; #ifdef __cplusplus @@ -114,46 +140,108 @@ extern "C" { /* * Function Prototypes */ -extern int sysfs_get_mnt_path(char *mnt_path, size_t len); -extern int sysfs_get_name_from_path(const char *path, char *name, size_t len); -extern int sysfs_get_link(const char *path, char *target, size_t len); +extern int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len); +extern int sysfs_get_name_from_path(const unsigned char *path, + unsigned char *name, size_t len); +extern int sysfs_get_link(const unsigned char *path, unsigned char *target, + size_t len); +extern struct dlist *sysfs_open_subsystem_list(unsigned char *name); +extern struct dlist *sysfs_open_bus_devices_list(unsigned char *name); +extern void sysfs_close_list(struct dlist *list); /* sysfs directory and file access */ extern void sysfs_close_attribute(struct sysfs_attribute *sysattr); -extern struct sysfs_attribute *sysfs_open_attribute(const char *path); +extern struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path); extern int sysfs_read_attribute(struct sysfs_attribute *sysattr); -extern int sysfs_read_attribute_value(const char *attrpath, char *value, - size_t vsize); -extern char *sysfs_get_value_from_attributes(struct sysfs_attribute *attr, - const char * name); +extern int sysfs_read_attribute_value(const unsigned char *attrpath, + unsigned char *value, size_t vsize); +extern int sysfs_write_attribute(struct sysfs_attribute *sysattr, + const unsigned char *new_value, size_t len); +extern unsigned char *sysfs_get_value_from_attributes(struct dlist *attr, + const unsigned char * name); extern void sysfs_close_directory(struct sysfs_directory *sysdir); -extern struct sysfs_directory *sysfs_open_directory(const char *path); +extern struct sysfs_directory *sysfs_open_directory(const unsigned char *path); extern int sysfs_read_directory(struct sysfs_directory *sysdir); -extern void sysfs_close_dlink(struct sysfs_dlink *dlink); -extern struct sysfs_dlink *sysfs_open_dlink(const char *linkpath); -extern int sysfs_read_dlinks(struct sysfs_dlink *dlink); +extern int sysfs_read_all_subdirs(struct sysfs_directory *sysdir); +extern struct sysfs_directory *sysfs_get_subdirectory + (struct sysfs_directory *dir, unsigned char *subname); +extern void sysfs_close_link(struct sysfs_link *ln); +extern struct sysfs_link *sysfs_open_link(const unsigned char *lnpath); +extern struct sysfs_link *sysfs_get_directory_link(struct sysfs_directory *dir, + unsigned char *linkname); +extern struct sysfs_link *sysfs_get_subdirectory_link + (struct sysfs_directory *dir, unsigned char *linkname); +extern struct sysfs_attribute *sysfs_get_directory_attribute + (struct sysfs_directory *dir, unsigned char *attrname); /* sysfs driver access */ extern void sysfs_close_driver(struct sysfs_driver *driver); -extern struct sysfs_driver *sysfs_open_driver(const char *path); +extern struct sysfs_driver *sysfs_open_driver(const unsigned char *path); +extern struct sysfs_attribute *sysfs_get_driver_attr + (struct sysfs_driver *drv, const unsigned char *name); +extern struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver); +extern struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver); +extern void sysfs_close_driver_by_name(struct sysfs_driver *driver); +extern struct sysfs_driver *sysfs_open_driver_by_name + (const unsigned char *drv_name, const unsigned char *bus, size_t bsize); +extern int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib, + unsigned char *value, size_t len); +extern int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib, + unsigned char *value, size_t len); /* generic sysfs device access */ +extern void sysfs_close_root_device(struct sysfs_root_device *root); +extern struct sysfs_root_device *sysfs_open_root_device + (const unsigned char *name); extern void sysfs_close_device(struct sysfs_device *dev); -extern void sysfs_close_device_tree(struct sysfs_device *dev); -extern struct sysfs_device *sysfs_open_device(const char *path); -extern struct sysfs_device *sysfs_open_device_tree(const char *path); +extern struct sysfs_device *sysfs_open_device(const unsigned char *path); extern struct sysfs_attribute *sysfs_get_device_attr - (struct sysfs_device *dev, const char *name); + (struct sysfs_device *dev, const unsigned char *name); +extern struct dlist *sysfs_get_device_attributes(struct sysfs_device *device); +extern struct sysfs_device *sysfs_open_device_by_id + (const unsigned char *bus_id, const unsigned char *bus, size_t bsize); +extern int sysfs_write_device_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value, size_t len); +extern int sysfs_read_device_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value, size_t len); /* generic sysfs bus access */ extern void sysfs_close_bus(struct sysfs_bus *bus); -extern struct sysfs_bus *sysfs_open_bus(const char *name); +extern struct sysfs_bus *sysfs_open_bus(const unsigned char *name); +extern struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, + unsigned char *id); +extern struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus, + unsigned char *drvname); +extern struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus); +extern struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus, + unsigned char *attrname); +extern struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, + unsigned char *dev_id); +extern int sysfs_find_device_bus(const unsigned char *dev_id, + unsigned char *busname, size_t bsize); +extern int sysfs_find_driver_bus(const unsigned char *driver, + unsigned char *busname, size_t bsize); /* generic sysfs class access */ extern void sysfs_close_class_device(struct sysfs_class_device *dev); -extern struct sysfs_class_device *sysfs_open_class_device(const char *path); +extern struct sysfs_class_device *sysfs_open_class_device + (const unsigned char *path); extern void sysfs_close_class(struct sysfs_class *cls); -extern struct sysfs_class *sysfs_open_class(const char *name); +extern struct sysfs_class *sysfs_open_class(const unsigned char *name); +extern struct sysfs_class_device *sysfs_get_class_device + (struct sysfs_class *class, unsigned char *name); +extern struct sysfs_class_device *sysfs_open_class_device_by_name + (const unsigned char *class, unsigned char *name); +extern struct dlist *sysfs_get_classdev_attributes + (struct sysfs_class_device *cdev); +extern int sysfs_find_device_class(const unsigned char *bus_id, + unsigned char *classname, size_t bsize); +extern struct sysfs_attribute *sysfs_get_classdev_attr + (struct sysfs_class_device *clsdev, const unsigned char *name); +extern int sysfs_write_classdev_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value, size_t len); +extern int sysfs_read_classdev_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value, size_t len); #ifdef __cplusplus } diff --git a/libsysfs/sysfs.h b/libsysfs/sysfs.h index eb2a00295..00599954f 100644 --- a/libsysfs/sysfs.h +++ b/libsysfs/sysfs.h @@ -3,7 +3,7 @@ * * Internal Header Definitions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/libsysfs/sysfs_bus.c b/libsysfs/sysfs_bus.c index b2e2b2dd7..19fc275d8 100644 --- a/libsysfs/sysfs_bus.c +++ b/libsysfs/sysfs_bus.c @@ -3,7 +3,7 @@ * * Generic bus utility functions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,28 +23,62 @@ #include "libsysfs.h" #include "sysfs.h" +static void sysfs_close_dev(void *dev) +{ + sysfs_close_device((struct sysfs_device *)dev); +} + +static void sysfs_close_drv(void *drv) +{ + sysfs_close_driver((struct sysfs_driver *)drv); +} + +/* + * compares devices' bus ids. + * @a: device id looking for + * @b: sysfs_device comparing being compared + * returns 1 if a==b->bus_id or 0 not equal + */ +static int bus_device_id_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((unsigned char *)a), ((struct sysfs_device *)b)->bus_id) + == 0) + return 1; + return 0; +} + +/* + * compares drivers' names. + * @a: driver name looking for + * @b: sysfs_driver comparing being compared + * returns 1 if a==b->name or 0 not equal + */ +static int bus_driver_name_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((unsigned char *)a), ((struct sysfs_driver *)b)->name) == 0) + return 1; + return 0; +} + /** * sysfs_close_bus: close single bus * @bus: bus structure */ void sysfs_close_bus(struct sysfs_bus *bus) { - struct sysfs_device *curdev = NULL, *nextdev = NULL; - struct sysfs_driver *curdrv = NULL, *nextdrv = NULL; - if (bus != NULL) { if (bus->directory != NULL) sysfs_close_directory(bus->directory); - for (curdev = bus->devices; curdev != NULL; - curdev = nextdev) { - nextdev = curdev->next; - sysfs_close_device(curdev); - } - for (curdrv = bus->drivers; curdrv != NULL; - curdrv = nextdrv) { - nextdrv = curdrv->next; - sysfs_close_driver(curdrv); - } + if (bus->devices) + dlist_destroy(bus->devices); + if (bus->drivers) + dlist_destroy(bus->drivers); free(bus); } } @@ -62,10 +96,10 @@ static struct sysfs_bus *alloc_bus(void) * open_bus_dir: opens up sysfs bus directory * returns sysfs_directory struct with success and NULL with error */ -static struct sysfs_directory *open_bus_dir(const char *name) +static struct sysfs_directory *open_bus_dir(const unsigned char *name) { - struct sysfs_directory *busdir = NULL, *cur = NULL, *next = NULL; - char buspath[SYSFS_PATH_MAX]; + struct sysfs_directory *busdir = NULL; + unsigned char buspath[SYSFS_PATH_MAX]; if (name == NULL) { errno = EINVAL; @@ -74,7 +108,7 @@ static struct sysfs_directory *open_bus_dir(const char *name) memset(buspath, 0, SYSFS_PATH_MAX); if ((sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX)) != 0) { - dprintf(stderr, "Sysfs not supported on this system\n"); + dprintf("Sysfs not supported on this system\n"); return NULL; } @@ -84,53 +118,22 @@ static struct sysfs_directory *open_bus_dir(const char *name) busdir = sysfs_open_directory(buspath); if (busdir == NULL) { errno = EINVAL; - dprintf(stderr,"Bus %s not supported on this system\n", + dprintf("Bus %s not supported on this system\n", name); return NULL; } if ((sysfs_read_directory(busdir)) != 0) { - dprintf(stderr, "Error reading %s bus dir %s\n", name, + dprintf("Error reading %s bus dir %s\n", name, buspath); sysfs_close_directory(busdir); return NULL; } /* read in devices and drivers subdirs */ - for (cur = busdir->subdirs; cur != NULL; cur = next) { - next = cur->next; - if ((sysfs_read_directory(cur)) != 0) - continue; - } + sysfs_read_all_subdirs(busdir); return busdir; } -/** - * add_dev_to_bus: adds a bus device to bus device list - * @bus: bus to add the device - * @dev: device to add - */ -static void add_dev_to_bus(struct sysfs_bus *bus, struct sysfs_device *dev) -{ - if (bus != NULL && dev != NULL) { - dev->next = bus->devices; - bus->devices = dev; - } -} - -/** - * add_driver_to_bus: adds a bus driver to bus driver list - * @bus: bus to add driver to - * @driver: driver to add - */ -static void add_driver_to_bus(struct sysfs_bus *bus, - struct sysfs_driver *driver) -{ - if (bus != NULL && driver != NULL) { - driver->next = bus->drivers; - bus->drivers = driver; - } -} - /** * get_all_bus_devices: gets all devices for bus * @bus: bus to get devices for @@ -140,29 +143,33 @@ static int get_all_bus_devices(struct sysfs_bus *bus) { struct sysfs_device *bdev = NULL; struct sysfs_directory *cur = NULL; - struct sysfs_dlink *curl = NULL, *nextl = NULL; - char dirname[SYSFS_NAME_LEN]; + struct sysfs_link *curl = NULL; if (bus == NULL || bus->directory == NULL) { errno = EINVAL; return -1; } - for (cur = bus->directory->subdirs; cur != NULL; cur = cur->next) { - memset(dirname, 0, SYSFS_NAME_LEN); - if ((sysfs_get_name_from_path(cur->path, dirname, - SYSFS_NAME_LEN)) != 0) + if (bus->directory->subdirs == NULL) + return 0; + + dlist_for_each_data(bus->directory->subdirs, cur, + struct sysfs_directory) { + if (strcmp(cur->name, SYSFS_DEVICES_NAME) != 0) continue; - if (strcmp(dirname, SYSFS_DEVICES_NAME) != 0) + if (cur->links == NULL) continue; - for (curl = cur->links; curl != NULL; curl = nextl) { - nextl = curl->next; - bdev = sysfs_open_device(curl->target->path); + dlist_for_each_data(cur->links, curl, struct sysfs_link) { + bdev = sysfs_open_device(curl->target); if (bdev == NULL) { - dprintf(stderr, "Error opening device at %s\n", - curl->target->path); + dprintf("Error opening device at %s\n", + curl->target); continue; } - add_dev_to_bus(bus, bdev); + if (bus->devices == NULL) + bus->devices = dlist_new_with_delete + (sizeof(struct sysfs_device), + sysfs_close_dev); + dlist_unshift(bus->devices, bdev); } } @@ -177,31 +184,35 @@ static int get_all_bus_devices(struct sysfs_bus *bus) static int get_all_bus_drivers(struct sysfs_bus *bus) { struct sysfs_driver *driver = NULL; - struct sysfs_directory *cur = NULL, *next = NULL; - struct sysfs_directory *cursub = NULL, *nextsub = NULL; - char dirname[SYSFS_NAME_LEN]; + struct sysfs_directory *cur = NULL; + struct sysfs_directory *cursub = NULL; if (bus == NULL || bus->directory == NULL) { errno = EINVAL; return -1; } - for (cur = bus->directory->subdirs; cur != NULL; cur = next) { - next = cur->next; - memset(dirname, 0, SYSFS_NAME_LEN); - if ((sysfs_get_name_from_path(cur->path, dirname, - SYSFS_NAME_LEN)) != 0) + if (bus->directory->subdirs == NULL) + return 0; + + dlist_for_each_data(bus->directory->subdirs, cur, + struct sysfs_directory) { + if (strcmp(cur->name, SYSFS_DRIVERS_NAME) != 0) continue; - if (strcmp(dirname, SYSFS_DRIVERS_NAME) != 0) + if (cur->subdirs == NULL) continue; - for (cursub = cur->subdirs; cursub != NULL; cursub = nextsub) { - nextsub = cursub->next; + dlist_for_each_data(cur->subdirs, cursub, + struct sysfs_directory) { driver = sysfs_open_driver(cursub->path); if (driver == NULL) { - dprintf(stderr, "Error opening driver at %s\n", + dprintf("Error opening driver at %s\n", cursub->path); continue; } - add_driver_to_bus(bus, driver); + if (bus->drivers == NULL) + bus->drivers = dlist_new_with_delete + (sizeof(struct sysfs_driver), + sysfs_close_drv); + dlist_unshift(bus->drivers, driver); } } @@ -214,20 +225,22 @@ static int get_all_bus_drivers(struct sysfs_bus *bus) * @busid: busid of device to match * returns 1 if found and 0 if not found */ -static int match_bus_device_to_driver(struct sysfs_driver *driver, char *busid) +static int match_bus_device_to_driver(struct sysfs_driver *driver, + unsigned char *busid) { - struct sysfs_dlink *cur = NULL, *next = NULL; + struct sysfs_link *cur = NULL; int found = 0; if (driver == NULL || driver->directory == NULL || busid == NULL) { errno = EINVAL; return found; } - for (cur = driver->directory->links; cur != NULL && found == 0; - cur = next) { - next = cur->next; - if ((strcmp(cur->name, busid)) == 0) - found++; + if (driver->directory->links != NULL) { + dlist_for_each_data(driver->directory->links, cur, + struct sysfs_link) { + if ((strcmp(cur->name, busid)) == 0) + found++; + } } return found; } @@ -238,19 +251,22 @@ static int match_bus_device_to_driver(struct sysfs_driver *driver, char *busid) */ static void link_bus_devices_to_drivers(struct sysfs_bus *bus) { - struct sysfs_device *dev = NULL, *nextdev = NULL; - struct sysfs_driver *drv = NULL, *nextdrv = NULL; + struct sysfs_device *dev = NULL; + struct sysfs_driver *drv = NULL; if (bus != NULL && bus->devices != NULL && bus->drivers != NULL) { - for (dev = bus->devices; dev != NULL; dev = nextdev) { - nextdev = dev->next; - - for (drv = bus->drivers; drv != NULL; drv = nextdrv) { - nextdrv = drv->next; + dlist_for_each_data(bus->devices, dev, struct sysfs_device) { + dlist_for_each_data(bus->drivers, drv, + struct sysfs_driver) { if ((match_bus_device_to_driver(drv, - dev->bus_id)) != 0) { - dev->driver = drv; - drv->device = dev; + dev->bus_id)) != 0) { + strncpy(dev->driver_name, drv->name, + SYSFS_NAME_LEN); + if (drv->devices == NULL) + drv->devices = dlist_new + (sizeof(struct + sysfs_device)); + dlist_unshift(drv->devices, dev); } } } @@ -261,7 +277,7 @@ static void link_bus_devices_to_drivers(struct sysfs_bus *bus) * sysfs_open_bus: opens specific bus and all its devices on system * returns sysfs_bus structure with success or NULL with error. */ -struct sysfs_bus *sysfs_open_bus(const char *name) +struct sysfs_bus *sysfs_open_bus(const unsigned char *name) { struct sysfs_bus *bus = NULL; struct sysfs_directory *busdir = NULL; @@ -273,25 +289,26 @@ struct sysfs_bus *sysfs_open_bus(const char *name) bus = alloc_bus(); if (bus == NULL) { - perror("malloc"); + dprintf("calloc failed\n"); return NULL; } strcpy(bus->name, name); busdir = open_bus_dir(name); if (busdir == NULL) { - dprintf(stderr,"Invalid bus, %s not supported on this system\n", + dprintf("Invalid bus, %s not supported on this system\n", name); sysfs_close_bus(bus); return NULL; } + strcpy(bus->path, busdir->path); bus->directory = busdir; if ((get_all_bus_devices(bus)) != 0) { - dprintf(stderr, "Error reading %s bus devices\n", name); + dprintf("Error reading %s bus devices\n", name); sysfs_close_bus(bus); return NULL; } if ((get_all_bus_drivers(bus)) != 0) { - dprintf(stderr, "Error reading %s bus drivers\n", name); + dprintf("Error reading %s bus drivers\n", name); sysfs_close_bus(bus); return NULL; } @@ -299,3 +316,198 @@ struct sysfs_bus *sysfs_open_bus(const char *name) return bus; } + +/** + * sysfs_get_bus_device: Get specific device on bus using device's id + * @bus: bus to find device on + * @id: bus_id for device + * returns struct sysfs_device reference or NULL if not found. + */ +struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, + unsigned char *id) +{ + if (bus == NULL || id == NULL) { + errno = EINVAL; + return NULL; + } + + return (struct sysfs_device *)dlist_find_custom(bus->devices, id, + bus_device_id_equal); +} + +/** + * sysfs_get_bus_driver: Get specific driver on bus using driver name + * @bus: bus to find driver on + * @drvname: name of driver + * returns struct sysfs_driver reference or NULL if not found. + */ +struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus, + unsigned char *drvname) +{ + if (bus == NULL || drvname == NULL) { + errno = EINVAL; + return NULL; + } + + return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname, + bus_driver_name_equal); +} + +/** + * sysfs_get_bus_attributes: returns bus' dlist of attributes + * @bus: bus to get attributes for. + * returns dlist of attributes or NULL if there aren't any. + */ +struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus) +{ + if (bus == NULL || bus->directory == NULL) + return NULL; + return bus->directory->attributes; +} + +/** + * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had + * attributes. + * @bus: bus to retrieve attribute from + * @attrname: attribute name to retrieve + * returns reference to sysfs_attribute if found or NULL if not found + */ +struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus, + unsigned char *attrname) +{ + if (bus == NULL || bus->directory == NULL || attrname == NULL) { + errno = EINVAL; + return NULL; + } + return sysfs_get_directory_attribute(bus->directory, attrname); +} + +/** + * sysfs_open_bus_device: locates a device on a bus and returns it. Device + * must be closed using sysfs_close_device. + * @busname: Name of bus to search + * @dev_id: Id of device on bus. + * returns sysfs_device if found or NULL if not. + */ +struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, + unsigned char *dev_id) +{ + struct sysfs_device *rdev = NULL; + char path[SYSFS_PATH_MAX]; + + if (busname == NULL || dev_id == NULL) { + errno = EINVAL; + return NULL; + } + + memset(path, 0, SYSFS_PATH_MAX); + if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) { + dprintf("Error getting sysfs mount point\n"); + return NULL; + } + + strcat(path, SYSFS_BUS_DIR); + strcat(path, "/"); + strcat(path, busname); + strcat(path, SYSFS_DEVICES_DIR); + strcat(path, "/"); + strcat(path, dev_id); + + rdev = sysfs_open_device(path); + if (rdev == NULL) { + dprintf("Error getting device %s on bus %s\n", + dev_id, busname); + return NULL; + } + + return rdev; +} + +/** + * sysfs_find_device_bus: locates the bus a device is on. + * @dev_id: device id. + * @busname: buffer to copy name to + * @bsize: buffer size + * returns 0 with success or -1 with error + */ +int sysfs_find_device_bus(const unsigned char *dev_id, unsigned char *busname, + size_t bsize) +{ + unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL; + struct dlist *buslist = NULL, *device_list = NULL; + + if (dev_id == NULL || busname == NULL) { + errno = EINVAL; + return -1; + } + + strcpy(subsys, SYSFS_BUS_DIR); /* subsys = /bus */ + buslist = sysfs_open_subsystem_list(subsys); + if (buslist != NULL) { + dlist_for_each_data(buslist, bus, char) { + device_list = sysfs_open_bus_devices_list(bus); + if (device_list != NULL) { + dlist_for_each_data(device_list, + curdev, char) { + if (strcmp(dev_id, curdev) == 0) { + strncpy(busname, + bus, bsize); + sysfs_close_list(device_list); + sysfs_close_list(buslist); + return 0; + } + } + sysfs_close_list(device_list); + } + } + sysfs_close_list(buslist); + } + return -1; +} + +/** + * sysfs_find_driver_bus: locates the bus the driver is on. + * @driver: name of the driver to locate + * @busname: buffer to copy name to + * @bsize: buffer size + * returns 0 with success, -1 with error + */ +int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname, + size_t bsize) +{ + unsigned char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL; + struct dlist *buslist = NULL, *drivers = NULL; + + if (driver == NULL || busname == NULL) { + errno = EINVAL; + return -1; + } + + memset(subsys, 0, SYSFS_PATH_MAX); + strcpy(subsys, SYSFS_BUS_DIR); + buslist = sysfs_open_subsystem_list(subsys); + if (buslist != NULL) { + dlist_for_each_data(buslist, bus, char) { + memset(subsys, 0, SYSFS_PATH_MAX); + strcpy(subsys, SYSFS_BUS_DIR); + strcat(subsys, "/"); + strcat(subsys, bus); + strcat(subsys, SYSFS_DRIVERS_DIR); + drivers = sysfs_open_subsystem_list(subsys); + if (drivers != NULL) { + dlist_for_each_data(drivers, curdrv, char) { + if (strcmp(driver, curdrv) == 0) { + strncpy(busname, bus, bsize); + sysfs_close_list(drivers); + sysfs_close_list(buslist); + return 0; + } + } + sysfs_close_list(drivers); + } + } + sysfs_close_list(buslist); + } + return -1; +} + diff --git a/libsysfs/sysfs_class.c b/libsysfs/sysfs_class.c index 9d7d8b222..cb6ca9d00 100644 --- a/libsysfs/sysfs_class.c +++ b/libsysfs/sysfs_class.c @@ -3,7 +3,7 @@ * * Generic class utility functions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,6 +23,28 @@ #include "libsysfs.h" #include "sysfs.h" +void sysfs_close_cls_dev(void *dev) +{ + sysfs_close_class_device((struct sysfs_class_device *)dev); +} + +/** + * class_name_equal: compares class_devices' name + * @a: class_name looking for + * @b: sysfs_class_device being compared + */ +static int class_name_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((unsigned char *)a), ((struct sysfs_class_device *)b)->name) + == 0) + return 1; + + return 0; +} + /** * sysfs_close_class_device: closes a single class device. * @dev: class device to close. @@ -46,15 +68,11 @@ void sysfs_close_class_device(struct sysfs_class_device *dev) */ void sysfs_close_class(struct sysfs_class *cls) { - struct sysfs_class_device *cur = NULL, *next = NULL; - if (cls != NULL) { if (cls->directory != NULL) sysfs_close_directory(cls->directory); - for (cur = cls->devices; cur != NULL; cur = next) { - next = cur->next; - sysfs_close_class_device(cur); - } + if (cls->devices != NULL) + dlist_destroy(cls->devices); free(cls); } } @@ -82,10 +100,10 @@ static struct sysfs_class *alloc_class(void) * open_class_dir: opens up sysfs class directory * returns sysfs_directory struct with success and NULL with error */ -static struct sysfs_directory *open_class_dir(const char *name) +static struct sysfs_directory *open_class_dir(const unsigned char *name) { struct sysfs_directory *classdir = NULL; - char classpath[SYSFS_PATH_MAX]; + unsigned char classpath[SYSFS_PATH_MAX]; if (name == NULL) { errno = EINVAL; @@ -94,7 +112,7 @@ static struct sysfs_directory *open_class_dir(const char *name) memset(classpath, 0, SYSFS_PATH_MAX); if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) { - dprintf(stderr, "Sysfs not supported on this system\n"); + dprintf("Sysfs not supported on this system\n"); return NULL; } @@ -104,13 +122,11 @@ static struct sysfs_directory *open_class_dir(const char *name) classdir = sysfs_open_directory(classpath); if (classdir == NULL) { errno = EINVAL; - dprintf(stderr,"Class %s not supported on this system\n", - name); + dprintf("Class %s not supported on this system\n", name); return NULL; } if ((sysfs_read_directory(classdir)) != 0) { - dprintf(stderr, "Error reading %s class dir %s\n", name, - classpath); + dprintf("Error reading %s class dir %s\n", name, classpath); sysfs_close_directory(classdir); return NULL; } @@ -123,14 +139,13 @@ static struct sysfs_directory *open_class_dir(const char *name) * @path: path to class device. * returns struct sysfs_class_device with success and NULL with error. */ -struct sysfs_class_device *sysfs_open_class_device(const char *path) +struct sysfs_class_device *sysfs_open_class_device(const unsigned char *path) { struct sysfs_class_device *cdev = NULL; - struct sysfs_directory *dir = NULL, *cur = NULL; - struct sysfs_dlink *curl = NULL; + struct sysfs_directory *dir = NULL; + struct sysfs_link *curl = NULL; struct sysfs_device *sdev = NULL; struct sysfs_driver *drv = NULL; - char temp[SYSFS_NAME_LEN]; if (path == NULL) { errno = EINVAL; @@ -138,74 +153,69 @@ struct sysfs_class_device *sysfs_open_class_device(const char *path) } cdev = alloc_class_device(); if (cdev == NULL) { - perror("malloc"); + dprintf("calloc failed\n"); return NULL; } - memset(temp, 0, SYSFS_NAME_LEN); - if ((sysfs_get_name_from_path(path, temp, SYSFS_NAME_LEN)) != 0) { + if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) { errno = EINVAL; - dprintf(stderr, "Invalid class device path %s\n", path); + dprintf("Invalid class device path %s\n", path); sysfs_close_class_device(cdev); return NULL; } - strcpy(cdev->name, temp); dir = sysfs_open_directory(path); if (dir == NULL) { - dprintf(stderr, "Error opening class device at %s\n", path); + dprintf("Error opening class device at %s\n", path); sysfs_close_class_device(cdev); return NULL; } if ((sysfs_read_directory(dir)) != 0) { - dprintf(stderr, "Error reading class device at %s\n", path); + dprintf("Error reading class device at %s\n", path); sysfs_close_directory(dir); sysfs_close_class_device(cdev); return NULL; } + sysfs_read_all_subdirs(dir); cdev->directory = dir; + strcpy(cdev->path, dir->path); - cur = cdev->directory->subdirs; - while(cur != NULL) { - sysfs_read_directory(cur); - cur = cur->next; - } /* get driver and device, if implemented */ - curl = cdev->directory->links; - while (curl != NULL) { - if (strncmp(curl->name, SYSFS_DEVICES_NAME, 6) == 0) { - sdev = sysfs_open_device(curl->target->path); - if (sdev != NULL) { - cdev->sysdevice = sdev; - if (cdev->driver != NULL) - sdev->driver = cdev->driver; - } - } else if (strncmp(curl->name, SYSFS_DRIVERS_NAME, 6) == 0) { - drv = sysfs_open_driver(curl->target->path); - if (drv != NULL) { - cdev->driver = drv; - if (cdev->sysdevice != NULL) - drv->device = cdev->sysdevice; + if (cdev->directory->links != NULL) { + dlist_for_each_data(cdev->directory->links, curl, + struct sysfs_link) { + if (strncmp(curl->name, SYSFS_DEVICES_NAME, 6) == 0) { + sdev = sysfs_open_device(curl->target); + if (sdev != NULL) { + cdev->sysdevice = sdev; + if (cdev->driver != NULL) + strncpy(sdev->driver_name, + cdev->driver->name, + SYSFS_NAME_LEN); + } + } else if (strncmp(curl->name, + SYSFS_DRIVERS_NAME, 6) == 0) { + drv = sysfs_open_driver(curl->target); + if (drv != NULL) { + cdev->driver = drv; + if (cdev->sysdevice != NULL) { + strncpy(cdev->sysdevice->name, + drv->name, + SYSFS_NAME_LEN); + if (drv->devices == NULL) + drv->devices = + dlist_new + (sizeof(struct + sysfs_device)); + dlist_unshift(drv->devices, + cdev->sysdevice); + } + } } } - curl = curl->next; } return cdev; } -/** - * add_dev_to_class: adds a class device to class list - * @class: class to add the device - * @dev: device to add - */ -static void add_dev_to_class(struct sysfs_class *cls, - struct sysfs_class_device *dev) -{ - if (cls != NULL && dev != NULL) { - dev->next = cls->devices; - cls->devices = dev; - } -} - /** * get_all_class_devices: gets all devices for class * @class: class to get devices for @@ -214,23 +224,27 @@ static void add_dev_to_class(struct sysfs_class *cls, static int get_all_class_devices(struct sysfs_class *cls) { struct sysfs_class_device *dev = NULL; - struct sysfs_directory *cur = NULL, *next = NULL; + struct sysfs_directory *cur = NULL; if (cls == NULL || cls->directory == NULL) { errno = EINVAL; return -1; } - for (cur = cls->directory->subdirs; cur != NULL; cur = next) { - next = cur->next; + if (cls->directory->subdirs == NULL) + return 0; + dlist_for_each_data(cls->directory->subdirs, cur, + struct sysfs_directory) { dev = sysfs_open_class_device(cur->path); if (dev == NULL) { - dprintf(stderr, "Error opening device at %s\n", - cur->path); + dprintf("Error opening device at %s\n", cur->path); continue; } - add_dev_to_class(cls, dev); + if (cls->devices == NULL) + cls->devices = dlist_new_with_delete + (sizeof(struct sysfs_class_device), + sysfs_close_cls_dev); + dlist_unshift(cls->devices, dev); } - return 0; } @@ -238,7 +252,7 @@ static int get_all_class_devices(struct sysfs_class *cls) * sysfs_open_class: opens specific class and all its devices on system * returns sysfs_class structure with success or NULL with error. */ -struct sysfs_class *sysfs_open_class(const char *name) +struct sysfs_class *sysfs_open_class(const unsigned char *name) { struct sysfs_class *cls = NULL; struct sysfs_directory *classdir = NULL; @@ -250,24 +264,268 @@ struct sysfs_class *sysfs_open_class(const char *name) cls = alloc_class(); if (cls == NULL) { - perror("malloc"); + dprintf("calloc failed\n"); return NULL; } strcpy(cls->name, name); classdir = open_class_dir(name); if (classdir == NULL) { - dprintf(stderr, - "Invalid class, %s not supported on this system\n", + dprintf("Invalid class, %s not supported on this system\n", name); sysfs_close_class(cls); return NULL; } cls->directory = classdir; + strcpy(cls->path, classdir->path); if ((get_all_class_devices(cls)) != 0) { - dprintf(stderr, "Error reading %s class devices\n", name); + dprintf("Error reading %s class devices\n", name); sysfs_close_class(cls); return NULL; } return cls; } + +/** + * sysfs_get_class_device: Get specific class device using the device's id + * @class: class to find device on + * @name: class name of the device + */ +struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class, + unsigned char *name) +{ + if (class == NULL || name == NULL) { + errno = EINVAL; + return NULL; + } + + return (struct sysfs_class_device *)dlist_find_custom(class->devices, + name, class_name_equal); +} + +/** + * sysfs_open_class_device_by_name: Locates a specific class_device and returns it. + * Class_device must be closed using sysfs_close_class_device + * @classname: Class to search + * @name: name of the class_device + */ +struct sysfs_class_device *sysfs_open_class_device_by_name + (const unsigned char *classname, unsigned char *name) +{ + struct sysfs_class *class = NULL; + struct sysfs_class_device *cdev = NULL, *rcdev = NULL; + + if (classname == NULL || name == NULL) { + errno = EINVAL; + return NULL; + } + + class = sysfs_open_class(classname); + if (class == NULL) { + dprintf("Error opening class %s\n", classname); + return NULL; + } + + cdev = sysfs_get_class_device(class, name); + if (cdev == NULL) { + dprintf("Error getting class device %s from class %s\n", + name, classname); + sysfs_close_class(class); + return NULL; + } + + rcdev = sysfs_open_class_device(cdev->directory->path); + if (rcdev == NULL) { + dprintf("Error getting class device %s from class %s\n", + name, classname); + sysfs_close_class(class); + return NULL; + } + sysfs_close_class(class); + + return rcdev; +} + +/** + * sysfs_get_classdev_attributes: returns a dlist of attributes for + * the requested class_device + * @cdev: sysfs_class_dev for which attributes are needed + * returns a dlist of attributes if exists, NULL otherwise + */ +struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev) +{ + if (cdev == NULL || cdev->directory == NULL) + return NULL; + + return (cdev->directory->attributes); +} + +/** + * sysfs_find_device_class: locates the device the device is on + * @bus_id: device to look for + * @classname: buffer to copy class name to + * @bsize: size of buffer + * returns 0 with success and -1 with error + */ +int sysfs_find_device_class(const unsigned char *bus_id, + unsigned char *classname, size_t bsize) +{ + unsigned char class[SYSFS_NAME_LEN], clspath[SYSFS_NAME_LEN]; + unsigned char *cls = NULL, *clsdev = NULL; + struct dlist *clslist = NULL, *clsdev_list = NULL; + + if (bus_id == NULL || classname == NULL) { + errno = EINVAL; + return -1; + } + + strcpy(class, SYSFS_CLASS_DIR); + clslist = sysfs_open_subsystem_list(class); + if (clslist != NULL) { + dlist_for_each_data(clslist, cls, char) { + memset(clspath, 0, SYSFS_NAME_LEN); + strcpy(clspath, SYSFS_CLASS_DIR); + strcat(clspath, "/"); + strcat(clspath, cls); + clsdev_list = sysfs_open_subsystem_list(clspath); + if (clsdev_list != NULL) { + dlist_for_each_data(clsdev_list, + clsdev, char) { + if (strcmp(bus_id, clsdev) == 0) { + strncpy(classname, + cls, bsize); + sysfs_close_list(clsdev_list); + sysfs_close_list(clslist); + return 0; + } + } + sysfs_close_list(clsdev_list); + } + } + sysfs_close_list(clslist); + } + return -1; +} + +/** + * sysfs_get_classdev_attr: searches class device's attributes by name + * @clsdev: class device to look through + * @name: attribute name to get + * returns sysfs_attribute reference with success or NULL with error + */ +struct sysfs_attribute *sysfs_get_classdev_attr + (struct sysfs_class_device *clsdev, const unsigned char *name) +{ + struct sysfs_attribute *cur = NULL; + + if (clsdev == NULL || clsdev->directory == NULL || + clsdev->directory->attributes == NULL || name == NULL) { + errno = EINVAL; + return NULL; + } + + cur = sysfs_get_directory_attribute(clsdev->directory, + (unsigned char *)name); + if (cur != NULL) + return cur; + + return NULL; +} + +/** + * sysfs_write_classdev_attr: modify writable attribute value for the given + * class device + * @dev: class device name for which the attribute has to be changed + * @attrib: attribute to change + * @value: value to change to + * @len: size of buffer at "value" + * Returns 0 on success and -1 on error + */ +int sysfs_write_classdev_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value, size_t len) +{ + struct sysfs_class_device *clsdev = NULL; + struct sysfs_attribute *attribute = NULL; + unsigned char class_name[SYSFS_NAME_LEN]; + + if (dev == NULL || attrib == NULL || value == NULL) { + errno = EINVAL; + return -1; + } + + memset(class_name, 0, SYSFS_NAME_LEN); + if ((sysfs_find_device_class(dev, + class_name, SYSFS_NAME_LEN)) < 0) { + dprintf("Class device %s not found\n", dev); + return -1; + } + clsdev = sysfs_open_class_device_by_name(class_name, dev); + if (clsdev == NULL) { + dprintf("Error opening %s in class %s\n", dev, class_name); + return -1; + } + attribute = sysfs_get_directory_attribute(clsdev->directory, attrib); + if (attribute == NULL) { + dprintf("Attribute %s not defined for device %s on class %s\n", + attrib, dev, class_name); + sysfs_close_class_device(clsdev); + return -1; + } + if ((sysfs_write_attribute(attribute, value, len)) < 0) { + dprintf("Error setting %s to %s\n", attrib, value); + sysfs_close_class_device(clsdev); + return -1; + } + sysfs_close_class_device(clsdev); + return 0; +} + +/** + * sysfs_read_classdev_attr: read an attribute for a given class device + * @dev: class device name for which the attribute has to be read + * @attrib: attribute to read + * @value: buffer to return value to user + * @len: size of buffer at "value" + * Returns 0 on success and -1 on error + */ +int sysfs_read_classdev_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value, size_t len) +{ + struct sysfs_class_device *clsdev = NULL; + struct sysfs_attribute *attribute = NULL; + unsigned char class_name[SYSFS_NAME_LEN]; + + if (dev == NULL || attrib == NULL || value == NULL) { + errno = EINVAL; + return -1; + } + + memset(class_name, 0, SYSFS_NAME_LEN); + if ((sysfs_find_device_class(dev, + class_name, SYSFS_NAME_LEN)) < 0) { + dprintf("Class device %s not found\n", dev); + return -1; + } + clsdev = sysfs_open_class_device_by_name(class_name, dev); + if (clsdev == NULL) { + dprintf("Error opening %s in class %s\n", dev, class_name); + return -1; + } + attribute = sysfs_get_directory_attribute(clsdev->directory, attrib); + if (attribute == NULL) { + dprintf("Attribute %s not defined for device %s on class %s\n", + attrib, dev, class_name); + sysfs_close_class_device(clsdev); + return -1; + } + if (attribute->len > len) { + dprintf("Value length %d is greater that suppled buffer %d\n", + attribute->len, len); + sysfs_close_class_device(clsdev); + return -1; + } + strncpy(value, attribute->value, attribute->len); + value[(attribute->len)+1] = 0; + sysfs_close_class_device(clsdev); + return 0; +} diff --git a/libsysfs/sysfs_device.c b/libsysfs/sysfs_device.c index 185b5cf37..89704dcd8 100644 --- a/libsysfs/sysfs_device.c +++ b/libsysfs/sysfs_device.c @@ -3,7 +3,7 @@ * * Generic device utility functions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,6 +23,42 @@ #include "libsysfs.h" #include "sysfs.h" +/** + * sysfs_close_device_tree: closes every device in the supplied tree, + * closing children only. + * @devroot: device root of tree. + */ +static void sysfs_close_device_tree(struct sysfs_device *devroot) +{ + if (devroot != NULL) { + if (devroot->children != NULL) { + struct sysfs_device *child = NULL; + + dlist_for_each_data(devroot->children, child, + struct sysfs_device) { + sysfs_close_device_tree(child); + } + } + sysfs_close_device(devroot); + } +} + +/** + * sysfs_del_device: routine for dlist integration + */ +static void sysfs_del_device(void *dev) +{ + sysfs_close_device((struct sysfs_device *)dev); +} + +/** + * sysfs_close_dev_tree: routine for dlist integration + */ +static void sysfs_close_dev_tree(void *dev) +{ + sysfs_close_device_tree((struct sysfs_device *)dev); +} + /** * sysfs_close_device: closes and cleans up a device * @dev = device to clean up @@ -30,11 +66,10 @@ void sysfs_close_device(struct sysfs_device *dev) { if (dev != NULL) { - dev->next = NULL; - dev->driver = NULL; if (dev->directory != NULL) sysfs_close_directory(dev->directory); - dev->children = NULL; + if (dev->children != NULL && dev->children->count == 0) + dlist_destroy(dev->children); free(dev); } } @@ -55,24 +90,20 @@ static struct sysfs_device *alloc_device(void) * returns sysfs_attribute reference with success or NULL with error. */ struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev, - const char *name) + const unsigned char *name) { struct sysfs_attribute *cur = NULL; - char attrname[SYSFS_NAME_LEN]; - if (dev == NULL || dev->directory == NULL || name == NULL) { + if (dev == NULL || dev->directory == NULL + || dev->directory->attributes == NULL || name == NULL) { errno = EINVAL; return NULL; } - for (cur = dev->directory->attributes; cur != NULL; cur = cur->next) { - if ((sysfs_get_name_from_path(cur->path, attrname, - SYSFS_NAME_LEN)) != 0) - continue; - if (strcmp(name, attrname) != 0) - continue; - + + cur = sysfs_get_directory_attribute(dev->directory, + (unsigned char *)name); + if (cur != NULL) return cur; - } return NULL; } @@ -82,11 +113,10 @@ struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev, * @path: path to device, this is the /sys/devices/ path * returns sysfs_device structure with success or NULL with error */ -struct sysfs_device *sysfs_open_device(const char *path) +struct sysfs_device *sysfs_open_device(const unsigned char *path) { struct sysfs_device *dev = NULL; struct sysfs_directory *sdir = NULL; - char *p = NULL; if (path == NULL) { errno = EINVAL; @@ -94,106 +124,415 @@ struct sysfs_device *sysfs_open_device(const char *path) } dev = alloc_device(); if (dev == NULL) { - dprintf(stderr, "Error allocating device at %s\n", path); + dprintf("Error allocating device at %s\n", path); return NULL; } sdir = sysfs_open_directory(path); if (sdir == NULL) { - dprintf(stderr, "Invalid device at %s\n", path); + dprintf("Invalid device at %s\n", path); errno = EINVAL; sysfs_close_device(dev); return NULL; } if ((sysfs_read_directory(sdir)) != 0) { - dprintf(stderr, "Error reading device directory at %s\n", path); + dprintf("Error reading device directory at %s\n", path); sysfs_close_directory(sdir); sysfs_close_device(dev); return NULL; } dev->directory = sdir; - sysfs_get_name_from_path(sdir->path, dev->bus_id, SYSFS_NAME_LEN); - /* get device name */ - p = sysfs_get_value_from_attributes(sdir->attributes, - SYSFS_NAME_ATTRIBUTE); - if (p != NULL) { - strncpy(dev->name, p, SYSFS_NAME_LEN); - p = dev->name + strlen(dev->name) - 1; - if ((strlen(dev->name) > 0) && *p == '\n') - *p = '\0'; - } + strcpy(dev->bus_id, sdir->name); + strcpy(dev->path, sdir->path); + + /* + * The "name" attribute no longer exists... return the device's + * sysfs representation instead, in the "dev->name" field, which + * implies that the dev->name and dev->bus_id contain same data. + */ + strncpy(dev->name, sdir->name, SYSFS_NAME_LEN); return dev; } /** - * sysfs_close_device_tree: closes every device in the supplied tree, - * closing children only. - * @devroot: device root of tree. + * sysfs_open_device_tree: opens root device and all of its children, + * creating a tree of devices. Only opens children. + * @path: sysfs path to devices + * returns struct sysfs_device and its children with success or NULL with + * error. */ -void sysfs_close_device_tree(struct sysfs_device *devroot) +static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path) { - if (devroot != NULL) { - if (devroot->children != NULL) { - struct sysfs_device *child = NULL, *next = NULL; - - for (child = devroot->children; child != NULL; - child = next) { - next = child->next; - sysfs_close_device_tree(child); + struct sysfs_device *rootdev = NULL, *new = NULL; + struct sysfs_directory *cur = NULL; + + if (path == NULL) { + errno = EINVAL; + return NULL; + } + rootdev = sysfs_open_device(path); + if (rootdev == NULL) { + dprintf("Error opening root device at %s\n", path); + return NULL; + } + if (rootdev->directory->subdirs != NULL) { + dlist_for_each_data(rootdev->directory->subdirs, cur, + struct sysfs_directory) { + new = sysfs_open_device_tree(cur->path); + if (new == NULL) { + dprintf("Error opening device tree at %s\n", + cur->path); + sysfs_close_device_tree(rootdev); + return NULL; } + if (rootdev->children == NULL) + rootdev->children = dlist_new_with_delete + (sizeof(struct sysfs_device), + sysfs_del_device); + dlist_unshift(rootdev->children, new); } - sysfs_close_device(devroot); } + + return rootdev; } /** - * add_device_child_to_parent: adds child device to parent - * @parent: parent device. - * @child: child device to add. + * sysfs_close_root_device: closes root and all devices + * @root: root device to close */ -static void add_device_child_to_parent(struct sysfs_device *parent, - struct sysfs_device *child) +void sysfs_close_root_device(struct sysfs_root_device *root) { - if (parent != NULL && child != NULL) { - child->next = parent->children; - parent->children = child; - child->parent = parent; + if (root != NULL) { + if (root->devices != NULL) + dlist_destroy(root->devices); + if (root->directory != NULL) + sysfs_close_directory(root->directory); + free(root); } } /** - * sysfs_open_device_tree: opens root device and all of its children, - * creating a tree of devices. Only opens children. - * @path: sysfs path to devices - * returns struct sysfs_device and its children with success or NULL with - * error. + * open_root_device_dir: opens up sysfs_directory for specific root dev + * @name: name of root + * returns struct sysfs_directory with success and NULL with error */ -struct sysfs_device *sysfs_open_device_tree(const char *path) +static struct sysfs_directory *open_root_device_dir(const unsigned char *name) { - struct sysfs_device *rootdev = NULL, *new = NULL; + struct sysfs_directory *rdir = NULL; + unsigned char rootpath[SYSFS_PATH_MAX]; + + if (name == NULL) { + errno = EINVAL; + return NULL; + } + + memset(rootpath, 0, SYSFS_PATH_MAX); + if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) { + dprintf ("Sysfs not supported on this system\n"); + return NULL; + } + + strcat(rootpath, SYSFS_DEVICES_DIR); + strcat(rootpath, "/"); + strcat(rootpath, name); + rdir = sysfs_open_directory(rootpath); + if (rdir == NULL) { + errno = EINVAL; + dprintf ("Root device %s not supported on this system\n", + name); + return NULL; + } + if (sysfs_read_directory(rdir) != 0) { + dprintf ("Error reading %s root device at dir %s\n", name, + rootpath); + sysfs_close_directory(rdir); + return NULL; + } + + return rdir; +} + +/** + * get_all_root_devices: opens up all the devices under this root device + * @root: root device to open devices for + * returns 0 with success and -1 with error + */ +static int get_all_root_devices(struct sysfs_root_device *root) +{ + struct sysfs_device *dev = NULL; struct sysfs_directory *cur = NULL; - if (path == NULL) { + if (root == NULL || root->directory == NULL) { + errno = EINVAL; + return -1; + } + if (root->directory->subdirs == NULL) + return 0; + + dlist_for_each_data(root->directory->subdirs, cur, + struct sysfs_directory) { + dev = sysfs_open_device_tree(cur->path); + if (dev == NULL) { + dprintf ("Error opening device at %s\n", cur->path); + continue; + } + if (root->devices == NULL) + root->devices = dlist_new_with_delete + (sizeof(struct sysfs_device), + sysfs_close_dev_tree); + dlist_unshift(root->devices, dev); + } + + return 0; +} + +/** + * sysfs_open_root_device: opens sysfs devices root and all of its + * devices. + * @name: name of /sys/devices/root to open + * returns struct sysfs_root_device if success and NULL with error + */ +struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name) +{ + struct sysfs_root_device *root = NULL; + struct sysfs_directory *rootdir = NULL; + + if (name == NULL) { errno = EINVAL; return NULL; } - rootdev = sysfs_open_device(path); - if (rootdev == NULL) { - dprintf(stderr, "Error opening root device at %s\n", path); + + root = (struct sysfs_root_device *)calloc + (1, sizeof(struct sysfs_root_device)); + if (root == NULL) { + dprintf("calloc failure\n"); + return NULL; + } + rootdir = open_root_device_dir(name); + if (rootdir == NULL) { + dprintf ("Invalid root device, %s not supported\n", name); + sysfs_close_root_device(root); + return NULL; + } + strcpy(root->path, rootdir->path); + root->directory = rootdir; + if (get_all_root_devices(root) != 0) { + dprintf ("Error retrieving devices for root %s\n", name); + sysfs_close_root_device(root); + return NULL; + } + + return root; +} + +/** + * sysfs_get_device_attributes: returns a dlist of attributes corresponding to + * the specific device + * @device: struct sysfs_device * for which attributes are to be returned + */ +struct dlist *sysfs_get_device_attributes(struct sysfs_device *device) +{ + if (device == NULL || device->directory == NULL) + return NULL; + + return (device->directory->attributes); +} + +/** + * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem) + * @bus_id: bus_id of the device to open - has to be the "bus_id" in + * /sys/bus/xxx/devices + * @bus: bus the device belongs to + * @bsize: size of the bus buffer + * returns struct sysfs_device if found, NULL otherwise + * NOTE: + * 1. Use sysfs_close_device to close the device + * 2. Bus the device is on must be supplied + * Use sysfs_find_device_bus to get the bus name + */ +struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id, + const unsigned char *bus, size_t bsize) +{ + char sysfs_path[SYSFS_PATH_MAX], device_path[SYSFS_PATH_MAX]; + struct sysfs_device *device = NULL; + + if (bus_id == NULL || bus == NULL) { + errno = EINVAL; + return NULL; + } + memset(sysfs_path, 0, SYSFS_PATH_MAX); + if ((sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX)) != 0) { + dprintf("Error getting sysfs mount path\n"); + return NULL; + } + strcat(sysfs_path, SYSFS_BUS_DIR); + strcat(sysfs_path, "/"); + strncat(sysfs_path, bus, bsize); + strcat(sysfs_path, SYSFS_DEVICES_DIR); + strcat(sysfs_path, "/"); + strcat(sysfs_path, bus_id); + + /* devices under /sys/bus/xxx/devices are links to devices subsystem */ + if ((sysfs_get_link(sysfs_path, device_path, SYSFS_PATH_MAX)) < 0) { + dprintf("Error getting device path\n"); + return NULL; + } + + device = sysfs_open_device(device_path); + if (device == NULL) { + dprintf("Error opening device %s\n", bus_id); return NULL; } - cur = rootdev->directory->subdirs; - while (cur != NULL) { - new = sysfs_open_device_tree(cur->path); - if (new == NULL) { - dprintf(stderr, "Error opening device tree at %s\n", - cur->path); - sysfs_close_device_tree(rootdev); - return NULL; + + return device; +} + +/** + * get_device_absolute_path: looks up the bus the device is on, gets + * absolute path to the device + * @device: device for which path is needed + * @path: buffer to store absolute path + * @psize: size of "path" + * Returns 0 on success -1 on failure + */ +static int get_device_absolute_path(const unsigned char *device, + unsigned char *path, size_t psize) +{ + unsigned char bus_name[SYSFS_NAME_LEN], bus_path[SYSFS_PATH_MAX]; + + if (device == NULL || path == NULL) { + errno = EINVAL; + return -1; + } + + memset(bus_name, 0, SYSFS_NAME_LEN); + memset(bus_path, 0, SYSFS_NAME_LEN); + if ((sysfs_find_device_bus(device, bus_name, SYSFS_NAME_LEN)) != 0) { + dprintf("Device %s not found\n", device); + return -1; + } + if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) { + dprintf ("Sysfs not supported on this system\n"); + return -1; + } + strcat(bus_path, SYSFS_BUS_DIR); + strcat(bus_path, "/"); + strcat(bus_path, bus_name); + strcat(bus_path, SYSFS_DEVICES_DIR); + strcat(bus_path, "/"); + strcat(bus_path, device); + /* + * We now are at /sys/bus/"bus_name"/devices/"device" which is a link. + * Now read this link to reach to the device. + */ + if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) { + dprintf("Error getting to device %s\n", device); + return -1; + } + return 0; +} + +/** + * sysfs_write_device_attr: modify a "writable" attribute for the given device + * @dev: device bus_id for which attribute has to be changed + * @attrib: attribute to change + * @value: value to change to + * @len: "value" length to write + * Returns 0 on success -1 on error + */ +int sysfs_write_device_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value, size_t len) +{ + struct sysfs_attribute *attribute = NULL; + unsigned char devpath[SYSFS_PATH_MAX]; + + if (dev == NULL || attrib == NULL || value == NULL) { + errno = EINVAL; + return -1; + } + + memset(devpath, 0, SYSFS_PATH_MAX); + if ((get_device_absolute_path(dev, devpath, SYSFS_PATH_MAX)) != 0) { + dprintf("Error finding absolute path to device %s\n", dev); + return -1; + } + strcat(devpath, "/"); + strcat(devpath, attrib); + attribute = sysfs_open_attribute(devpath); + if (attribute == NULL) { + dprintf("Attribute %s could not be retrieved for device %s\n", + attrib, dev); + return -1; + } + if (attribute->method & SYSFS_METHOD_SHOW) { + if ((sysfs_read_attribute(attribute)) != 0) { + dprintf("Error reading attribute %s for device %s\n", + attrib, dev); + sysfs_close_attribute(attribute); + return -1; } - add_device_child_to_parent(rootdev, new); - cur = cur->next; } + if ((sysfs_write_attribute(attribute, value, len)) < 0) { + dprintf("Error setting %s to %s\n", attrib, value); + sysfs_close_attribute(attribute); + return -1; + } + sysfs_close_attribute(attribute); + return 0; +} - return rootdev; +/** + * sysfs_read_device_attr: read an attribute of the given device + * @dev: device bus_id for which attribute has to be changed + * @attrib: attribute to read + * @value: buffer to return value in + * @len: size of buffer available + * Returns 0 on success -1 on error + */ +int sysfs_read_device_attr(unsigned char *dev, unsigned char *attrib, + unsigned char *value, size_t len) +{ + struct sysfs_attribute *attribute = NULL; + unsigned char devpath[SYSFS_PATH_MAX]; + + if (dev == NULL || attrib == NULL || value == NULL) { + errno = EINVAL; + return -1; + } + + memset(devpath, 0, SYSFS_PATH_MAX); + if ((get_device_absolute_path(dev, devpath, SYSFS_PATH_MAX)) != 0) { + dprintf("Error finding absolute path to device %s\n", dev); + return -1; + } + strcat(devpath, "/"); + strcat(devpath, attrib); + attribute = sysfs_open_attribute(devpath); + if (attribute == NULL) { + dprintf("Error opening attribute %s for device %s\n", + attrib, dev); + return -1; + } + if (!(attribute->method & SYSFS_METHOD_SHOW)) { + dprintf("Show method not supported for attribute %s\n", + attrib); + sysfs_close_attribute(attribute); + return -1; + } + if ((sysfs_read_attribute(attribute)) != 0) { + dprintf("Error reading attribute %s for device %s\n", + attrib, dev); + sysfs_close_attribute(attribute); + return -1; + } + if (attribute->len > len) { + dprintf("Value length %d is larger than supplied buffer %d\n", + attribute->len, len); + sysfs_close_attribute(attribute); + return -1; + } + strncpy(value, attribute->value, attribute->len); + value[(attribute->len)+1] = 0; + sysfs_close_attribute(attribute); + return 0; } diff --git a/libsysfs/sysfs_dir.c b/libsysfs/sysfs_dir.c index a83c81f49..ff2edf461 100644 --- a/libsysfs/sysfs_dir.c +++ b/libsysfs/sysfs_dir.c @@ -3,7 +3,7 @@ * * Directory utility functions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,6 +23,81 @@ #include "libsysfs.h" #include "sysfs.h" +/** + * sysfs_del_attribute: routine for dlist integration + */ +static void sysfs_del_attribute(void *attr) +{ + sysfs_close_attribute((struct sysfs_attribute *)attr); +} + +/** + * sysfs_del_link: routine for dlist integration + */ +static void sysfs_del_link(void *ln) +{ + sysfs_close_link((struct sysfs_link *)ln); +} + +/** + * sysfs_del_dir: routine for dlist integration + */ +static void sysfs_del_directory(void *dir) +{ + sysfs_close_directory((struct sysfs_directory *)dir); +} + +/** + * dir_attribute_name_equal: compares dir attributes by name + * @a: attribute name for comparison + * @b: sysfs_attribute to be compared. + * returns 1 if a==b->name or 0 if not equal + */ +static int dir_attribute_name_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((unsigned char *)a), ((struct sysfs_attribute *)b)->name) + == 0) + return 1; + return 0; +} + +/** + * dir_link_name_equal: compares dir links by name + * @a: link name for comparison + * @b: sysfs_link to be compared. + * returns 1 if a==b->name or 0 if not equal + */ +static int dir_link_name_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((unsigned char *)a), ((struct sysfs_link *)b)->name) + == 0) + return 1; + return 0; +} + +/** + * dir_subdir_name_equal: compares subdirs by name + * @a: name of subdirectory to compare + * @b: sysfs_directory subdirectory to be compared + * returns 1 if a==b->name or 0 if not equal + */ +static int dir_subdir_name_equal(void *a, void *b) +{ + if (a == NULL || b == NULL) + return 0; + + if (strcmp(((unsigned char *)a), ((struct sysfs_directory *)b)->name) + == 0) + return 1; + return 0; +} + /** * sysfs_close_attribute: closes and cleans up attribute * @sysattr: attribute to close. @@ -51,7 +126,7 @@ static struct sysfs_attribute *alloc_attribute(void) * @path: path to attribute. * returns sysfs_attribute struct with success and NULL with error. */ -struct sysfs_attribute *sysfs_open_attribute(const char *path) +struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path) { struct sysfs_attribute *sysattr = NULL; struct stat fileinfo; @@ -62,13 +137,22 @@ struct sysfs_attribute *sysfs_open_attribute(const char *path) } sysattr = alloc_attribute(); if (sysattr == NULL) { - dprintf(stderr, "Error allocating attribute at %s\n", path); + dprintf("Error allocating attribute at %s\n", path); + return NULL; + } + if (sysfs_get_name_from_path(path, sysattr->name, SYSFS_NAME_LEN) + != 0) { + dprintf("Error retrieving attribute name from path: %s\n", + path); + sysfs_close_attribute(sysattr); return NULL; } strncpy(sysattr->path, path, sizeof(sysattr->path)); if ((stat(sysattr->path, &fileinfo)) != 0) { - perror("stat"); + dprintf("Stat failed: No such attribute?\n"); sysattr->method = 0; + free(sysattr); + sysattr = NULL; } else { if (fileinfo.st_mode & S_IRUSR) sysattr->method |= SYSFS_METHOD_SHOW; @@ -79,6 +163,87 @@ struct sysfs_attribute *sysfs_open_attribute(const char *path) return sysattr; } +/** + * sysfs_write_attribute: write value to the attribute + * @sysattr: attribute to write + * @new_value: value to write + * @len: length of "new_value" + * returns 0 with success and -1 with error. + */ +int sysfs_write_attribute(struct sysfs_attribute *sysattr, + const unsigned char *new_value, size_t len) +{ + int fd; + int length; + + if (sysattr == NULL || new_value == NULL || len == 0) { + errno = EINVAL; + return -1; + } + + if (!(sysattr->method & SYSFS_METHOD_STORE)) { + dprintf ("Store method not supported for attribute %s\n", + sysattr->path); + return -1; + } + if (sysattr->method & SYSFS_METHOD_SHOW) { + if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) { + dprintf("Attribute %s already has the requested value %s\n", + sysattr->name, new_value); + return 0; + } + } + /* + * open O_WRONLY since some attributes have no "read" but only + * "write" permission + */ + if ((fd = open(sysattr->path, O_WRONLY)) < 0) { + dprintf("Error reading attribute %s\n", sysattr->path); + return -1; + } + + length = write(fd, new_value, len); + if (length < 0) { + dprintf("Error writing to the attribute %s - invalid value?\n", + sysattr->name); + close(fd); + return -1; + } else if (length != len) { + dprintf("Could not write %d bytes to attribute %s\n", + len, sysattr->name); + /* + * since we could not write user supplied number of bytes, + * restore the old value if one available + */ + if (sysattr->method & SYSFS_METHOD_SHOW) { + length = write(fd, sysattr->value, sysattr->len); + close(fd); + return -1; + } + } + + /* + * Validate length that has been copied. Alloc appropriate area + * in sysfs_attribute. Verify first if the attribute supports reading + * (show method). If it does not, do not bother + */ + if (sysattr->method & SYSFS_METHOD_SHOW) { + if (length != sysattr->len) { + sysattr->value = (char *)realloc(sysattr->value, + length); + sysattr->len = length; + strncpy(sysattr->value, new_value, length); + } else { + /*"length" of the new value is same as old one */ + strncpy(sysattr->value, new_value, length); + } + } + + close(fd); + return 0; +} + + /** * sysfs_read_attribute: reads value from attribute * @sysattr: attribute to read @@ -86,8 +251,8 @@ struct sysfs_attribute *sysfs_open_attribute(const char *path) */ int sysfs_read_attribute(struct sysfs_attribute *sysattr) { - char *fbuf = NULL; - char *vbuf = NULL; + unsigned char *fbuf = NULL; + unsigned char *vbuf = NULL; size_t length = 0; int pgsize = 0; int fd; @@ -97,34 +262,33 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) return -1; } if (!(sysattr->method & SYSFS_METHOD_SHOW)) { - dprintf (stderr, "Show method not supported for attribute %s\n", + dprintf("Show method not supported for attribute %s\n", sysattr->path); return -1; } pgsize = getpagesize(); - fbuf = (char *)calloc(1, pgsize+1); + fbuf = (unsigned char *)calloc(1, pgsize+1); if (fbuf == NULL) { - perror("calloc"); + dprintf("calloc failed\n"); return -1; } if ((fd = open(sysattr->path, O_RDONLY)) < 0) { - dprintf (stderr, "Error reading attribute %s\n", sysattr->path); + dprintf("Error reading attribute %s\n", sysattr->path); free(fbuf); return -1; } length = read(fd, fbuf, pgsize); if (length < 0) { - dprintf (stderr, "Error reading from attribute %s\n", - sysattr->path); + dprintf("Error reading from attribute %s\n", sysattr->path); close(fd); free(fbuf); return -1; } sysattr->len = length; close(fd); - vbuf = (char *)realloc(fbuf, length+1); + vbuf = (unsigned char *)realloc(fbuf, length+1); if (vbuf == NULL) { - perror("realloc"); + dprintf("realloc failed\n"); free(fbuf); return -1; } @@ -142,7 +306,8 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) * @vsize: size of value buffer * returns 0 with success and -1 with error. */ -int sysfs_read_attribute_value(const char *attrpath, char *value, size_t vsize) +int sysfs_read_attribute_value(const unsigned char *attrpath, + unsigned char *value, size_t vsize) { struct sysfs_attribute *attr = NULL; size_t length = 0; @@ -154,19 +319,18 @@ int sysfs_read_attribute_value(const char *attrpath, char *value, size_t vsize) attr = sysfs_open_attribute(attrpath); if (attr == NULL) { - dprintf(stderr, "Invalid attribute path %s\n", attrpath); + dprintf("Invalid attribute path %s\n", attrpath); errno = EINVAL; return -1; } if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) { - dprintf(stderr, "Error reading from attribute %s\n", attrpath); + dprintf("Error reading from attribute %s\n", attrpath); sysfs_close_attribute(attr); return -1; } length = strlen(attr->value); if (length > vsize) - dprintf(stderr, - "Value length %d is larger than supplied buffer %d\n", + dprintf("Value length %d is larger than supplied buffer %d\n", length, vsize); strncpy(value, attr->value, vsize); sysfs_close_attribute(attr); @@ -179,87 +343,32 @@ int sysfs_read_attribute_value(const char *attrpath, char *value, size_t vsize) * attribute name, return its value * @attr: attribute to search * @name: name to look for - * returns char * value - could be NULL + * returns unsigned char * value - could be NULL */ -char *sysfs_get_value_from_attributes(struct sysfs_attribute *attr, - const char *name) +unsigned char *sysfs_get_value_from_attributes(struct dlist *attr, + const unsigned char *name) { struct sysfs_attribute *cur = NULL; - char tmpname[SYSFS_NAME_LEN]; if (attr == NULL || name == NULL) { errno = EINVAL; return NULL; - } - cur = attr; - while (cur != NULL) { - memset(tmpname, 0, SYSFS_NAME_LEN); - if ((sysfs_get_name_from_path(cur->path, tmpname, - SYSFS_NAME_LEN)) != 0) { - cur = cur->next; - continue; - } - if (strcmp(tmpname, name) == 0) + } + dlist_for_each_data(attr, cur, struct sysfs_attribute) { + if (strcmp(cur->name, name) == 0) return cur->value; - cur = cur->next; } return NULL; } /** - * add_subdir_to_dir: adds subdirectory to directory's subdirs - * @sysdir: directory to add subdir to - * @subdir: subdirectory to add. + * sysfs_close_link: closes and cleans up link. + * @ln: link to close. */ -static void add_subdir_to_dir(struct sysfs_directory *sysdir, - struct sysfs_directory *subdir) +void sysfs_close_link(struct sysfs_link *ln) { - if (sysdir != NULL && subdir != NULL) { - subdir->next = sysdir->subdirs; - sysdir->subdirs = subdir; - } -} - -/** - * add_attr_to_dir: adds attribute to directory's attributes - * @sysdir: directory to add attribute to - * @sysattr: attribute to add. - */ -static void add_attr_to_dir(struct sysfs_directory *sysdir, - struct sysfs_attribute *sysattr) -{ - if (sysdir != NULL && sysattr != NULL) { - sysattr->next = sysdir->attributes; - sysdir->attributes = sysattr; - } -} - -/** - * sysfs_close_dlink: closes and cleans up directory link. - * @dlink: directory link to close. - */ -void sysfs_close_dlink(struct sysfs_dlink *dlink) -{ - if (dlink != NULL) { - dlink->next = NULL; - if (dlink->target != NULL) - sysfs_close_directory(dlink->target); - free(dlink); - } -} - -/** - * add_dlink_to_dir: adds directory link to directory's links list. - * @sysdir: directory to add it to. - * @dlink: link to add. - */ -static void add_dlink_to_dir(struct sysfs_directory *sysdir, - struct sysfs_dlink *dlink) -{ - if (sysdir != NULL && dlink != NULL) { - dlink->next = sysdir->links; - sysdir->links = dlink; - } + if (ln != NULL) + free(ln); } /** @@ -268,35 +377,13 @@ static void add_dlink_to_dir(struct sysfs_directory *sysdir, */ void sysfs_close_directory(struct sysfs_directory *sysdir) { - struct sysfs_directory *sdir = NULL, *dnext = NULL; - struct sysfs_dlink *dlink = NULL, *nextl = NULL; - struct sysfs_attribute *attr = NULL, *anext = NULL; - if (sysdir != NULL) { - if (sysdir->subdirs != NULL) { - for (sdir = sysdir->subdirs; sdir != NULL; - sdir = dnext) { - dnext = sdir->next; - sysfs_close_directory(sdir); - } - } - if (sysdir->links != NULL) { - for (dlink = sysdir->links; dlink != NULL; - dlink = nextl) { - nextl = dlink->next; - sysfs_close_dlink(dlink); - } - } - if (sysdir->attributes != NULL) { - for (attr = sysdir->attributes; attr != NULL; - attr = anext) { - anext = attr->next; - /* sysfs_close_attribute(attr); */ - if (attr->value != NULL) - free(attr->value); - free(attr); - } - } + if (sysdir->subdirs != NULL) + dlist_destroy(sysdir->subdirs); + if (sysdir->links != NULL) + dlist_destroy(sysdir->links); + if (sysdir->attributes != NULL) + dlist_destroy(sysdir->attributes); free(sysdir); } } @@ -312,12 +399,35 @@ static struct sysfs_directory *alloc_directory(void) } /** - * alloc_dlink: allocates and initializes directory link structure - * returns struct sysfs_dlink with success or NULL with error. + * alloc_link: allocates and initializes link structure + * returns struct sysfs_link with success or NULL with error. + */ +static struct sysfs_link *alloc_link(void) +{ + return (struct sysfs_link *)calloc(1, sizeof(struct sysfs_link)); +} + +/** + * sysfs_read_all_subdirs: calls sysfs_read_directory for all subdirs + * @sysdir: directory whose subdirs need reading. + * returns 0 with success and -1 with error. */ -static struct sysfs_dlink *alloc_dlink(void) +int sysfs_read_all_subdirs(struct sysfs_directory *sysdir) { - return (struct sysfs_dlink *)calloc(1, sizeof(struct sysfs_dlink)); + struct sysfs_directory *cursub = NULL; + + if (sysdir == NULL) { + errno = EINVAL; + return -1; + } + if (sysdir->subdirs == NULL) + return 0; + dlist_for_each_data(sysdir->subdirs, cursub, struct sysfs_directory) { + if (sysfs_read_directory(cursub) != 0) + dprintf ("Error reading subdirectory %s\n", + cursub->name); + } + return 0; } /** @@ -326,7 +436,7 @@ static struct sysfs_dlink *alloc_dlink(void) * @path: path of directory to open. * returns: struct sysfs_directory * with success and NULL on error. */ -struct sysfs_directory *sysfs_open_directory(const char *path) +struct sysfs_directory *sysfs_open_directory(const unsigned char *path) { struct sysfs_directory *sdir = NULL; @@ -336,7 +446,12 @@ struct sysfs_directory *sysfs_open_directory(const char *path) } sdir = alloc_directory(); if (sdir == NULL) { - dprintf(stderr, "Error allocating directory %s\n", path); + dprintf("Error allocating directory %s\n", path); + return NULL; + } + if (sysfs_get_name_from_path(path, sdir->name, SYSFS_NAME_LEN) != 0) { + dprintf("Error getting directory name from path: %s\n", path); + sysfs_close_directory(sdir); return NULL; } strncpy(sdir->path, path, sizeof(sdir->path)); @@ -345,46 +460,33 @@ struct sysfs_directory *sysfs_open_directory(const char *path) } /** - * sysfs_open_dlink: opens a sysfs directory link, creates struct, and returns + * sysfs_open_link: opens a sysfs link, creates struct, and returns * @path: path of link to open. - * returns: struct sysfs_dlink * with success and NULL on error. + * returns: struct sysfs_link * with success and NULL on error. */ -struct sysfs_dlink *sysfs_open_dlink(const char *linkpath) +struct sysfs_link *sysfs_open_link(const unsigned char *linkpath) { - struct sysfs_dlink *dlink = NULL; - struct sysfs_directory *tdir = NULL; - char name[SYSFS_NAME_LEN]; - char target[SYSFS_PATH_MAX]; + struct sysfs_link *ln = NULL; - if (linkpath == NULL) { + if (linkpath == NULL || strlen(linkpath) > SYSFS_PATH_MAX) { errno = EINVAL; return NULL; } - memset(name, 0, SYSFS_NAME_LEN); - memset(target, 0, SYSFS_PATH_MAX); - if ((sysfs_get_name_from_path(linkpath, name, SYSFS_NAME_LEN)) != 0 - || (sysfs_get_link(linkpath, target, SYSFS_PATH_MAX)) != 0) { - errno = EINVAL; - dprintf(stderr, "Invalid link path %s\n", linkpath); + ln = alloc_link(); + if (ln == NULL) { + dprintf("Error allocating link %s\n", linkpath); return NULL; } - dlink = alloc_dlink(); - if (dlink == NULL) { - dprintf(stderr, - "Error allocating directory link %s\n", linkpath); + strcpy(ln->path, linkpath); + if ((sysfs_get_name_from_path(linkpath, ln->name, SYSFS_NAME_LEN)) != 0 + || (sysfs_get_link(linkpath, ln->target, SYSFS_PATH_MAX)) != 0) { + errno = EINVAL; + dprintf("Invalid link path %s\n", linkpath); return NULL; } - strcpy(dlink->name, name); - tdir = sysfs_open_directory(target); - if (tdir == NULL) { - dprintf(stderr, "Invalid directory link target %s\n", target); - sysfs_close_dlink(dlink); - return NULL; - } - dlink->target = tdir; - return dlink; + return ln; } /** @@ -399,8 +501,8 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) struct stat astats; struct sysfs_attribute *attr = NULL; struct sysfs_directory *subdir = NULL; - struct sysfs_dlink *dlink = NULL; - char file_path[SYSFS_PATH_MAX]; + struct sysfs_link *ln = NULL; + unsigned char file_path[SYSFS_PATH_MAX]; int retval = 0; if (sysdir == NULL) { @@ -409,7 +511,7 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) } dir = opendir(sysdir->path); if (dir == NULL) { - perror("opendir"); + dprintf("Error opening directory %s\n", sysdir->path); return -1; } while(((dirent = readdir(dir)) != NULL) && retval == 0) { @@ -422,45 +524,57 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) strncat(file_path, "/", sizeof(file_path)); strncat(file_path, dirent->d_name, sizeof(file_path)); if ((lstat(file_path, &astats)) != 0) { - perror("stat"); + dprintf("stat failed\n"); continue; } if (S_ISREG(astats.st_mode)) { attr = sysfs_open_attribute(file_path); if (attr == NULL) { - dprintf (stderr, "Error opening attribute %s\n", + dprintf("Error opening attribute %s\n", file_path); retval = -1; break; } if (attr->method & SYSFS_METHOD_SHOW) { if ((sysfs_read_attribute(attr)) != 0) { - dprintf (stderr, - "Error reading attribute %s\n", + dprintf("Error reading attribute %s\n", file_path); sysfs_close_attribute(attr); continue; } } - add_attr_to_dir(sysdir, attr); + + if (sysdir->attributes == NULL) { + sysdir->attributes = dlist_new_with_delete + (sizeof(struct sysfs_attribute), + sysfs_del_attribute); + } + dlist_unshift(sysdir->attributes, attr); } else if (S_ISDIR(astats.st_mode)) { subdir = sysfs_open_directory(file_path); if (subdir == NULL) { - dprintf (stderr, "Error opening directory %s\n", + dprintf("Error opening directory %s\n", file_path); retval = -1; break; } - add_subdir_to_dir(sysdir, subdir); + if (sysdir->subdirs == NULL) + sysdir->subdirs = dlist_new_with_delete + (sizeof(struct sysfs_directory), + sysfs_del_directory); + dlist_unshift(sysdir->subdirs, subdir); } else if (S_ISLNK(astats.st_mode)) { - dlink = sysfs_open_dlink(file_path); - if (dlink == NULL) { - dprintf(stderr, "Error opening link %s\n", - file_path); + ln = sysfs_open_link(file_path); + if (ln == NULL) { + dprintf("Error opening link %s\n", file_path); retval = -1; break; } - add_dlink_to_dir(sysdir, dlink); + if (sysdir->links == NULL) + sysdir->links = dlist_new_with_delete + (sizeof(struct sysfs_link), + sysfs_del_link); + dlist_unshift(sysdir->links, ln); } } closedir(dir); @@ -468,29 +582,123 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) } /** - * sysfs_read_dlinks: reads a directory link's target directory. Can - * supply a linked list of links. - * @dlink: directory link to read. - * returns 0 with success or -1 with error. + * sysfs_get_directory_attribute: retrieves attribute attrname + * @dir: directory to retrieve attribute from + * @attrname: name of attribute to look for + * returns sysfs_attribute if found and NULL if not found */ -int sysfs_read_dlinks(struct sysfs_dlink *dlink) +struct sysfs_attribute *sysfs_get_directory_attribute + (struct sysfs_directory *dir, unsigned char *attrname) { - struct sysfs_dlink *cur = NULL; + struct sysfs_directory *sdir = NULL; + struct sysfs_attribute *attr = NULL; + + if (dir == NULL || attrname == NULL) { + errno = EINVAL; + return NULL; + } + + attr = (struct sysfs_attribute *)dlist_find_custom(dir->attributes, + attrname, dir_attribute_name_equal); + if (attr != NULL) + return attr; + + if (dir->subdirs != NULL) { + dlist_for_each_data(dir->subdirs, sdir, + struct sysfs_directory) { + if (sdir->attributes == NULL) + continue; + attr = sysfs_get_directory_attribute(sdir, attrname); + if (attr != NULL) + return attr; + } + } + return NULL; +} - if (dlink == NULL || dlink->target == NULL) { +/** + * sysfs_get_directory_link: retrieves link from one directory list + * @dir: directory to retrieve link from + * @linkname: name of link to look for + * returns reference to sysfs_link if found and NULL if not found + */ +struct sysfs_link *sysfs_get_directory_link + (struct sysfs_directory *dir, unsigned char *linkname) +{ + if (dir == NULL || linkname == NULL) { errno = EINVAL; - return -1; + return NULL; } - cur = dlink; - while (cur != NULL) { - if ((sysfs_read_directory(cur->target)) != 0) { - dprintf(stderr, - "Error reading directory link target %s\n", - dlink->name); - return -1; + return (struct sysfs_link *)dlist_find_custom(dir->links, + linkname, dir_link_name_equal); +} + +/** + * sysfs_get_subdirectory: retrieves subdirectory by name. + * @dir: directory to search for subdirectory. + * @subname: subdirectory name to get. + * returns reference to subdirectory or NULL if not found + */ +struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir, + unsigned char *subname) +{ + struct sysfs_directory *sub = NULL, *cursub = NULL; + + if (dir == NULL || dir->subdirs == NULL || subname == NULL) { + errno = EINVAL; + return NULL; + } + sub = (struct sysfs_directory *)dlist_find_custom(dir->subdirs, + subname, dir_subdir_name_equal); + if (sub != NULL) + return sub; + + if (dir->subdirs != NULL) { + dlist_for_each_data(dir->subdirs, cursub, + struct sysfs_directory) { + if (cursub->subdirs == NULL) + continue; + sub = sysfs_get_subdirectory(cursub, subname); + if (sub != NULL) + return sub; } - cur = cur->next; } - - return 0; + return NULL; +} + +/** + * sysfs_get_subdirectory_link: looks through all subdirs for specific link. + * @dir: directory and subdirectories to search for link. + * @linkname: link name to get. + * returns reference to link or NULL if not found + */ +struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir, + unsigned char *linkname) +{ + struct sysfs_directory *cursub = NULL; + struct sysfs_link *ln = NULL; + + if (dir == NULL || dir->links == NULL || linkname == NULL) { + errno = EINVAL; + return NULL; + } + + ln = sysfs_get_directory_link(dir, linkname); + if (ln != NULL) + return ln; + + if (dir->subdirs == NULL) + return NULL; + + if (dir->subdirs != NULL) { + dlist_for_each_data(dir->subdirs, cursub, + struct sysfs_directory) { + if (cursub->subdirs == NULL) + continue; + ln = sysfs_get_subdirectory_link(cursub, linkname); + if (ln != NULL) + return ln; + } + } + return NULL; } diff --git a/libsysfs/sysfs_driver.c b/libsysfs/sysfs_driver.c index 6813c85f1..f8e842c65 100644 --- a/libsysfs/sysfs_driver.c +++ b/libsysfs/sysfs_driver.c @@ -3,7 +3,7 @@ * * Driver utility functions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,19 +23,46 @@ #include "libsysfs.h" #include "sysfs.h" +static void sysfs_close_driver_by_name_dev(void *device) +{ + sysfs_close_device((struct sysfs_device *)device); +} + /** * sysfs_close_driver: closes and cleans up driver structure + * NOTE: This routine does not deallocate devices list * @driver: driver to close */ void sysfs_close_driver(struct sysfs_driver *driver) { if (driver != NULL) { + if (driver->devices != NULL) { + dlist_for_each(driver->devices) + dlist_shift(driver->devices); + free(driver->devices); + driver->devices = NULL; + } if (driver->directory != NULL) sysfs_close_directory(driver->directory); free(driver); } } +/** + * sysfs_close_driver_by_name: closes driver and deletes device lists too + * @driver: driver to close + */ +void sysfs_close_driver_by_name(struct sysfs_driver *driver) +{ + if (driver != NULL) { + if (driver->devices != NULL) + dlist_destroy(driver->devices); + if (driver->directory != NULL) + sysfs_close_directory(driver->directory); + free(driver); + } +} + /** * alloc_driver: allocates and initializes driver * returns struct sysfs_driver with success and NULL with error. @@ -50,11 +77,10 @@ static struct sysfs_driver *alloc_driver(void) * @path: path to driver directory * returns struct sysfs_driver with success and NULL with error */ -struct sysfs_driver *sysfs_open_driver(const char *path) +struct sysfs_driver *sysfs_open_driver(const unsigned char *path) { struct sysfs_driver *driver = NULL; struct sysfs_directory *sdir = NULL; - char devname[SYSFS_NAME_LEN]; if (path == NULL) { errno = EINVAL; @@ -62,28 +88,280 @@ struct sysfs_driver *sysfs_open_driver(const char *path) } sdir = sysfs_open_directory(path); if (sdir == NULL) { - dprintf (stderr, "Error opening directory %s\n", path); + dprintf("Error opening directory %s\n", path); return NULL; } if ((sysfs_read_directory(sdir)) != 0) { - dprintf (stderr, "Error reading directory %s\n", path); + dprintf("Error reading directory %s\n", path); sysfs_close_directory(sdir); return NULL; } driver = alloc_driver(); if (driver == NULL) { - dprintf(stderr, "Error allocating driver at %s\n", path); + dprintf("Error allocating driver at %s\n", path); sysfs_close_directory(sdir); return NULL; } - if ((sysfs_get_name_from_path(path, devname, SYSFS_NAME_LEN)) != 0) { - dprintf (stderr, "Error reading directory %s\n", path); - sysfs_close_directory(sdir); - free(driver); - return NULL; - } - strncpy(driver->name, devname, sizeof(driver->name)); + strcpy(driver->name, sdir->name); driver->directory = sdir; + strcpy(driver->path, sdir->path); return driver; } + +/** + * sysfs_get_driver_attributes: gets list of attributes for the given driver + * @driver: sysfs_driver for which attributes are required + * returns a dlist of attributes corresponding to the driver if present + * NULL otherwise + */ +struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver) +{ + if (driver == NULL || driver->directory == NULL) + return NULL; + + return(driver->directory->attributes); +} + +/** + * sysfs_get_driver_attr: searches driver's attributes by name + * @drv: driver to look through + * @name: attribute name to get + * returns sysfs_attribute reference on success or NULL with error + */ +struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv, + const unsigned char *name) +{ + struct sysfs_attribute *cur = NULL; + + if (drv == NULL || drv->directory == NULL + || drv->directory->attributes == NULL || name == NULL) { + errno = EINVAL; + return NULL; + } + + cur = sysfs_get_directory_attribute(drv->directory, + (unsigned char *)name); + if (cur != NULL) + return cur; + + return NULL; +} + +/** + * sysfs_get_driver_links: gets list of links from the given driver + * @driver: sysfs_driver for which links list is required + * returns a dlist of links corresponding to the driver if present + * NULL otherwise + */ +struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver) +{ + if (driver == NULL || driver->directory == NULL) + return NULL; + + return(driver->directory->links); +} + +/** + * sysfs_open_driver_by_name: open a driver by name and return the bus + * the driver is on. + * @drv_name: driver to open + * @bus: the driver bus + * @bsize: size of bus buffer + * returns struct sysfs_driver if found, NULL otherwise + * NOTE: + * 1. Need to call sysfs_close_driver_by_name to free up memory + * 2. Bus the driver is registered with must be supplied. + * Use sysfs_find_driver_bus() to obtain the bus name + */ +struct sysfs_driver *sysfs_open_driver_by_name(const unsigned char *drv_name, + const unsigned char *bus, size_t bsize) +{ + struct sysfs_driver *driver = NULL; + struct sysfs_device *device = NULL; + struct sysfs_link *curlink = NULL; + unsigned char path[SYSFS_PATH_MAX]; + + if (drv_name == NULL || bus == NULL) { + errno = EINVAL; + return NULL; + } + + memset(path, 0, SYSFS_PATH_MAX); + if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) { + dprintf("Error getting sysfs mount path\n"); + return NULL; + } + strcat(path, SYSFS_BUS_DIR); + strcat(path, "/"); + strcat(path, bus); + strcat(path, SYSFS_DRIVERS_DIR); + strcat(path, "/"); + strcat(path, drv_name); + driver = sysfs_open_driver(path); + if (driver == NULL) { + dprintf("Could not open driver %s\n", drv_name); + return NULL; + } + if (driver->directory->links != NULL) { + dlist_for_each_data(driver->directory->links, curlink, + struct sysfs_link) { + device = sysfs_open_device(curlink->target); + if (device == NULL) { + dprintf("Error opening device at %s\n", + curlink->target); + sysfs_close_driver_by_name(driver); + return NULL; + } + strcpy(device->driver_name, drv_name); + if (driver->devices == NULL) + driver->devices = dlist_new_with_delete + (sizeof(struct sysfs_device), + sysfs_close_driver_by_name_dev); + dlist_unshift(driver->devices, device); + } + } + return driver; +} + +/** + * get_driver_path: looks up the bus the driver is on and builds path to + * the driver. + * @drv: driver to look for + * @path: buffer to return path to driver + * @psize: size of "path" + * Returns 0 on success and -1 on error + */ +static int get_driver_path(const unsigned char *drv, + unsigned char *path, size_t psize) +{ + unsigned char bus_name[SYSFS_NAME_LEN]; + + if (drv == NULL || path == NULL) { + errno = EINVAL; + return -1; + } + memset(bus_name, 0, SYSFS_NAME_LEN); + memset(path, 0, SYSFS_PATH_MAX); + if ((sysfs_find_driver_bus(drv, bus_name, SYSFS_NAME_LEN)) < 0) { + dprintf("Driver %s not found\n", drv); + return -1; + } + if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) { + dprintf("Error getting sysfs mount path\n"); + return -1; + } + strcat(path, SYSFS_BUS_DIR); + strcat(path, "/"); + strcat(path, bus_name); + strcat(path, SYSFS_DRIVERS_DIR); + strcat(path, "/"); + strcat(path, drv); + fprintf(stdout, "get_driver_path %s\n", path); + return 0; +} + +/** + * sysfs_write_driver_attr: modify "writable" driver attribute + * @drv: driver whose attribute has to be modified + * @attrib: Attribute to be modified + * @value: Value to change to + * Returns 0 on success -1 on failure + */ +int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib, + unsigned char *value, size_t len) +{ + struct sysfs_attribute *attribute = NULL; + unsigned char path[SYSFS_PATH_MAX]; + + if (drv == NULL || attrib == NULL || value == NULL) { + errno = EINVAL; + return -1; + } + + memset(path, 0, SYSFS_PATH_MAX); + if ((get_driver_path(drv, path, SYSFS_PATH_MAX)) != 0) { + dprintf("Error getting to driver %s\n", drv); + return -1; + } + strcat(path, "/"); + strcat(path, attrib); + attribute = sysfs_open_attribute(path); + if (attribute == NULL) { + dprintf("Attribute %s could not be retrieved for driver %s\n", + attrib, drv); + return -1; + } + if (attribute->method & SYSFS_METHOD_SHOW) { + if ((sysfs_read_attribute(attribute)) != 0) { + dprintf("Error reading attribute %s for driver %s\n", + attrib, drv); + sysfs_close_attribute(attribute); + return -1; + } + } + if ((sysfs_write_attribute(attribute, value, len)) < 0) { + dprintf("Error setting %s to %s\n", attrib, value); + sysfs_close_attribute(attribute); + return -1; + } + sysfs_close_attribute(attribute); + return 0; +} + +/** + * sysfs_read_driver_attr: read the user supplied driver attribute + * @drv: driver whose attribute has to be read + * @attrib: Attribute to be read + * @value: Buffer to return the read value + * @len: Length of the buffer "value" + * Returns 0 on success -1 on failure + */ +int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib, + unsigned char *value, size_t len) +{ + struct sysfs_attribute *attribute = NULL; + unsigned char path[SYSFS_PATH_MAX]; + + if (drv == NULL || attrib == NULL || value == NULL) { + errno = EINVAL; + return -1; + } + + memset(path, 0, SYSFS_NAME_LEN); + if ((get_driver_path(drv, path, SYSFS_PATH_MAX)) != 0) { + dprintf("Error getting to driver %s\n", drv); + return -1; + } + strcat(path, "/"); + strcat(path, attrib); + attribute = sysfs_open_attribute(path); + if (attribute == NULL) { + dprintf("Error opening attribute %s for driver %s\n", + attrib, drv); + return -1; + } + if (!(attribute->method & SYSFS_METHOD_SHOW)) { + dprintf("Show method not supported for attribute %s\n", + attrib); + sysfs_close_attribute(attribute); + return -1; + } + if ((sysfs_read_attribute(attribute)) != 0) { + dprintf("Error reading attribute %s for driver %s\n", + attrib, drv); + sysfs_close_attribute(attribute); + return -1; + } + if (attribute->len > len) { + dprintf("Value length %d is larger than supplied buffer %d\n", + attribute->len, len); + sysfs_close_attribute(attribute); + return -1; + } + strncpy(value, attribute->value, attribute->len); + value[(attribute->len)+1] = 0; + sysfs_close_attribute(attribute); + return 0; +} + diff --git a/libsysfs/sysfs_utils.c b/libsysfs/sysfs_utils.c index a2410abe6..447534243 100644 --- a/libsysfs/sysfs_utils.c +++ b/libsysfs/sysfs_utils.c @@ -3,7 +3,7 @@ * * System utility functions for libsysfs * - * Copyright (C) 2003 International Business Machines, Inc. + * Copyright (C) IBM Corp. 2003 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -30,8 +30,8 @@ * @len: size of mnt_path * returns 0 with success and -1 with error. */ -static int sysfs_get_fs_mnt_path(const char *fs_type, char *mnt_path, - size_t len) +static int sysfs_get_fs_mnt_path(const unsigned char *fs_type, + unsigned char *mnt_path, size_t len) { FILE *mnt; struct mntent *mntent; @@ -45,7 +45,7 @@ static int sysfs_get_fs_mnt_path(const char *fs_type, char *mnt_path, } if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) { - dprintf(stderr, "Error getting mount information\n"); + dprintf("Error getting mount information\n"); return -1; } while (ret == 0 && dirlen == 0 && (mntent = getmntent(mnt)) != NULL) { @@ -54,15 +54,14 @@ static int sysfs_get_fs_mnt_path(const char *fs_type, char *mnt_path, if (dirlen <= (len - 1)) { strcpy(mnt_path, mntent->mnt_dir); } else { - dprintf(stderr, - "Error - mount path too long\n"); + dprintf("Error - mount path too long\n"); ret = -1; } } } endmntent(mnt); if (dirlen == 0 && ret == 0) { - dprintf(stderr, "Filesystem %s not found!\n", fs_type); + dprintf("Filesystem %s not found!\n", fs_type); errno = EINVAL; ret = -1; } @@ -75,7 +74,7 @@ static int sysfs_get_fs_mnt_path(const char *fs_type, char *mnt_path, * @len: size of mnt_path * returns 0 with success and -1 with error. */ -int sysfs_get_mnt_path(char *mnt_path, size_t len) +int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len) { int ret = -1; @@ -93,9 +92,10 @@ int sysfs_get_mnt_path(char *mnt_path, size_t len) * @name: where to put name * @len: size of name */ -int sysfs_get_name_from_path(const char *path, char *name, size_t len) +int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name, + size_t len) { - char *n = NULL; + unsigned char *n = NULL; if (path == NULL || name == NULL) { errno = EINVAL; @@ -118,11 +118,11 @@ int sysfs_get_name_from_path(const char *path, char *name, size_t len) * @target: where to put name * @len: size of name */ -int sysfs_get_link(const char *path, char *target, size_t len) +int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len) { - char devdir[SYSFS_PATH_MAX]; - char linkpath[SYSFS_PATH_MAX]; - char *d = NULL; + unsigned char devdir[SYSFS_PATH_MAX]; + unsigned char linkpath[SYSFS_PATH_MAX]; + unsigned char *d = NULL; if (path == NULL || target == NULL) { errno = EINVAL; @@ -133,7 +133,7 @@ int sysfs_get_link(const char *path, char *target, size_t len) memset(linkpath, 0, SYSFS_PATH_MAX); if ((sysfs_get_mnt_path(devdir, SYSFS_PATH_MAX)) != 0) { - dprintf(stderr, "Sysfs not supported on this system\n"); + dprintf("Sysfs not supported on this system\n"); return -1; } @@ -154,3 +154,136 @@ int sysfs_get_link(const char *path, char *target, size_t len) return 0; } + + +/** + * sysfs_del_name: free function for sysfs_open_subsystem_list + * @name: memory area to be freed + */ +void sysfs_del_name(void *name) +{ + free(name); +} + + +/** + * sysfs_close_list: generic list free routine + * @list: dlist to free + * Returns nothing + */ +void sysfs_close_list(struct dlist *list) +{ + if (list != NULL) + dlist_destroy(list); +} + +/** + * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem + * details from the system + * @name: name of the subsystem, eg., "bus", "class", "devices" + * Returns a dlist of supported names or NULL if subsystem not supported + */ +struct dlist *sysfs_open_subsystem_list(unsigned char *name) +{ + unsigned char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL; + struct sysfs_directory *dir = NULL, *cur = NULL; + struct dlist *list = NULL; + + if (name == NULL) + return NULL; + + if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) { + dprintf("Error getting sysfs mount point\n"); + return NULL; + } + + strcat(sysfs_path, name); + dir = sysfs_open_directory(sysfs_path); + if (dir == NULL) { + dprintf("Error opening sysfs_directory at %s\n", sysfs_path); + return NULL; + } + + if (sysfs_read_directory(dir) != 0) { + dprintf("Error reading sysfs_directory at %s\n", sysfs_path); + sysfs_close_directory(dir); + return NULL; + } + + if (dir->subdirs != NULL) { + list = dlist_new_with_delete(SYSFS_NAME_LEN, + sysfs_del_name); + if (list == NULL) { + dprintf("Error creating list\n"); + sysfs_close_directory(dir); + return NULL; + } + + dlist_for_each_data(dir->subdirs, cur, + struct sysfs_directory) { + subsys_name = (char *)calloc(1, SYSFS_NAME_LEN); + strcpy(subsys_name, cur->name); + dlist_unshift(list, subsys_name); + } + } + sysfs_close_directory(dir); + return list; +} + + +/** + * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus + * @name: name of the subsystem, eg., "pci", "scsi", "usb" + * Returns a dlist of supported names or NULL if subsystem not supported + */ +struct dlist *sysfs_open_bus_devices_list(unsigned char *name) +{ + unsigned char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL; + struct sysfs_directory *dir = NULL; + struct sysfs_link *cur = NULL; + struct dlist *list = NULL; + + if (name == NULL) + return NULL; + + if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) { + dprintf("Error getting sysfs mount point\n"); + return NULL; + } + + strcat(sysfs_path, SYSFS_BUS_DIR); + strcat(sysfs_path, "/"); + strcat(sysfs_path, name); + strcat(sysfs_path, SYSFS_DEVICES_DIR); + dir = sysfs_open_directory(sysfs_path); + if (dir == NULL) { + dprintf("Error opening sysfs_directory at %s\n", sysfs_path); + return NULL; + } + + if (sysfs_read_directory(dir) != 0) { + dprintf("Error reading sysfs_directory at %s\n", sysfs_path); + sysfs_close_directory(dir); + return NULL; + } + + if (dir->links != NULL) { + list = dlist_new_with_delete(SYSFS_NAME_LEN, + sysfs_del_name); + if (list == NULL) { + dprintf("Error creating list\n"); + sysfs_close_directory(dir); + return NULL; + } + + dlist_for_each_data(dir->links, cur, + struct sysfs_link) { + device_name = (char *)calloc(1, SYSFS_NAME_LEN); + strcpy(device_name, cur->name); + dlist_unshift(list, device_name); + } + } + sysfs_close_directory(dir); + return list; +} + diff --git a/namedev.c b/namedev.c index ac05fd88e..c21a5be7a 100644 --- a/namedev.c +++ b/namedev.c @@ -547,7 +547,7 @@ static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *at attr->mode = 0; if (class_dev->sysdevice) { - dbg_parse("class_dev->sysdevice->directory->path = '%s'", class_dev->sysdevice->directory->path); + dbg_parse("class_dev->sysdevice->path = '%s'", class_dev->sysdevice->path); dbg_parse("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id); } else { dbg_parse("class_dev->name = '%s'", class_dev->name); @@ -557,19 +557,21 @@ static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *at switch (dev->type) { case LABEL: { - char *temp; + struct sysfs_attribute *tmpattr = NULL; + struct sysfs_class_device *class_dev_parent = NULL; + char *temp = NULL; dbg_parse("LABEL: match file '%s' with value '%s'", dev->sysfs_file, dev->sysfs_value); /* try to find the attribute in the class device directory */ - temp = sysfs_get_value_from_attributes(class_dev->directory->attributes, dev->sysfs_file); - if (temp) + tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file); + if (tmpattr) goto label_found; /* look in the class device device directory if present */ if (class_dev->sysdevice) { - temp = sysfs_get_value_from_attributes(class_dev->sysdevice->directory->attributes, dev->sysfs_file); - if (temp) + tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file); + if (tmpattr) goto label_found; } @@ -577,14 +579,13 @@ static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *at * as block partitions don't point to the physical device. Need to fix that * up in the kernel... */ - if (strstr(class_dev->directory->path, "block")) { + if (strstr(class_dev->path, "block")) { dbg_parse("looking at block device..."); - if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) { + if (isdigit(class_dev->path[strlen(class_dev->path)-1])) { char path[SYSFS_PATH_MAX]; - struct sysfs_class_device *class_dev_parent; dbg_parse("really is a partition..."); - strcpy(path, class_dev->directory->path); + strcpy(path, class_dev->path); temp = strrchr(path, '/'); *temp = 0x00; dbg_parse("looking for a class device at '%s'", path); @@ -596,35 +597,36 @@ static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *at dbg_parse("class_dev_parent->name = %s", class_dev_parent->name); /* try to find the attribute in the class device directory */ - temp = sysfs_get_value_from_attributes(class_dev_parent->directory->attributes, dev->sysfs_file); - if (temp) { - //sysfs_close_class_device(class_dev_parent); + tmpattr = sysfs_get_classdev_attr(class_dev_parent, dev->sysfs_file); + if (tmpattr) goto label_found; - } /* look in the class device device directory if present */ if (class_dev_parent->sysdevice) { - temp = sysfs_get_value_from_attributes(class_dev_parent->sysdevice->directory->attributes, dev->sysfs_file); - if (temp) { - // sysfs_close_class_device(class_dev_parent); + tmpattr = sysfs_get_classdev_attr(class_dev_parent, dev->sysfs_file); + if (tmpattr) goto label_found; - } } } } + if (class_dev_parent) + sysfs_close_class_device(class_dev_parent); + continue; label_found: - temp[strlen(temp)-1] = 0x00; - dbg_parse("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, temp, dev->sysfs_value); - if (strcmp(dev->sysfs_value, temp) != 0) + tmpattr->value[strlen(tmpattr->value)-1] = 0x00; + dbg_parse("file '%s' found with value '%s' compare with '%s'", dev->sysfs_file, tmpattr->value, dev->sysfs_value); + if (strcmp(dev->sysfs_value, tmpattr->value) != 0) { + if (class_dev_parent) + sysfs_close_class_device(class_dev_parent); continue; + } strcpy(attr->name, dev->attr.name); - if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) { - temp[0] = class_dev->directory->path[strlen(class_dev->directory->path)-1]; - temp[1] = 0x00; + if (isdigit(class_dev->path[strlen(class_dev->path)-1])) { + temp = &class_dev->path[strlen(class_dev->path)-1]; strcat(attr->name, temp); } if (dev->attr.mode != 0) { @@ -635,6 +637,8 @@ label_found: dbg_parse("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o", dev->sysfs_file, dev->sysfs_value, attr->name, dev->attr.owner, dev->attr.group, dev->attr.mode); + if (class_dev_parent) + sysfs_close_class_device(class_dev_parent); goto done; break; } @@ -646,7 +650,7 @@ label_found: found = 0; if (!class_dev->sysdevice) continue; - strcpy(path, class_dev->sysdevice->directory->path); + strcpy(path, class_dev->sysdevice->path); temp = strrchr(path, '/'); dbg_parse("NUMBER path = '%s'", path); dbg_parse("NUMBER temp = '%s' id = '%s'", temp, dev->id); @@ -682,7 +686,7 @@ label_found: if (!class_dev->sysdevice) continue; found = 0; - strcpy(path, class_dev->sysdevice->directory->path); + strcpy(path, class_dev->sysdevice->path); temp = strrchr(path, '/'); dbg_parse("TOPOLOGY path = '%s'", path); dbg_parse("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place); -- 2.30.2