2003-07-18 20:08:00 -07:00
/*
* sysfs_bus . c
*
* Generic bus utility functions for libsysfs
*
2003-10-21 01:19:14 -07:00
* Copyright ( C ) IBM Corp . 2003
2003-07-18 20:08:00 -07:00
*
* 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
*
*/
2004-10-20 13:15:26 +02:00
# include "libsysfs.h"
2003-07-18 20:08:00 -07:00
# include "sysfs.h"
2003-10-21 01:19:14 -07:00
static void sysfs_close_dev ( void * dev )
{
2003-11-24 23:47:43 -08:00
sysfs_close_device ( ( struct sysfs_device * ) dev ) ;
2003-10-21 01:19:14 -07:00
}
static void sysfs_close_drv ( void * drv )
{
2003-11-24 23:47:43 -08:00
sysfs_close_driver ( ( struct sysfs_driver * ) drv ) ;
2003-10-21 01:19:14 -07:00
}
/*
* 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 ;
2004-03-12 00:57:30 -08:00
if ( strcmp ( ( ( char * ) a ) , ( ( struct sysfs_device * ) b ) - > bus_id )
2003-10-21 01:19:14 -07:00
= = 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 ;
2004-03-12 00:57:30 -08:00
if ( strcmp ( ( ( char * ) a ) , ( ( struct sysfs_driver * ) b ) - > name ) = = 0 )
2003-10-21 01:19:14 -07:00
return 1 ;
return 0 ;
}
2003-07-18 20:08:00 -07:00
/**
* sysfs_close_bus : close single bus
* @ bus : bus structure
*/
void sysfs_close_bus ( struct sysfs_bus * bus )
{
if ( bus ! = NULL ) {
if ( bus - > directory ! = NULL )
sysfs_close_directory ( bus - > directory ) ;
2003-10-21 01:19:14 -07:00
if ( bus - > devices )
dlist_destroy ( bus - > devices ) ;
if ( bus - > drivers )
dlist_destroy ( bus - > drivers ) ;
2003-07-18 20:08:00 -07:00
free ( bus ) ;
}
}
/**
* alloc_bus : mallocs new bus structure
* returns sysfs_bus_bus struct or NULL
*/
static struct sysfs_bus * alloc_bus ( void )
{
return ( struct sysfs_bus * ) calloc ( 1 , sizeof ( struct sysfs_bus ) ) ;
}
/**
2003-12-15 21:53:28 -08:00
* sysfs_get_bus_devices : gets all devices for bus
* @ bus : bus to get devices for
* returns dlist of devices with success and NULL with failure
2003-07-18 20:08:00 -07:00
*/
2003-12-15 21:53:28 -08:00
struct dlist * sysfs_get_bus_devices ( struct sysfs_bus * bus )
2003-07-18 20:08:00 -07:00
{
2003-12-15 21:53:28 -08:00
struct sysfs_device * bdev = NULL ;
struct sysfs_directory * devdir = NULL ;
struct sysfs_link * curl = NULL ;
2004-03-12 00:57:30 -08:00
char path [ SYSFS_PATH_MAX ] ;
2003-07-18 20:08:00 -07:00
2003-12-15 21:53:28 -08:00
if ( bus = = NULL ) {
2003-07-18 20:08:00 -07:00
errno = EINVAL ;
return NULL ;
}
2003-12-15 21:53:28 -08:00
memset ( path , 0 , SYSFS_PATH_MAX ) ;
2004-03-12 00:57:30 -08:00
safestrcpy ( path , bus - > path ) ;
safestrcat ( path , " / " ) ;
safestrcat ( path , SYSFS_DEVICES_NAME ) ;
2003-12-15 21:53:28 -08:00
devdir = sysfs_open_directory ( path ) ;
if ( devdir = = NULL )
2003-07-18 20:08:00 -07:00
return NULL ;
2003-12-15 21:53:28 -08:00
if ( sysfs_read_dir_links ( devdir ) ! = 0 ) {
sysfs_close_directory ( devdir ) ;
2003-07-18 20:08:00 -07:00
return NULL ;
}
2003-10-21 01:19:14 -07:00
2004-03-12 00:57:30 -08:00
if ( devdir - > links ! = NULL ) {
2003-12-19 18:29:10 -08:00
dlist_for_each_data ( devdir - > links , curl , struct sysfs_link ) {
bdev = sysfs_open_device_path ( curl - > target ) ;
if ( bdev = = NULL ) {
dprintf ( " Error opening device at %s \n " ,
curl - > target ) ;
continue ;
}
if ( bus - > devices = = NULL )
bus - > devices = dlist_new_with_delete
( sizeof ( struct sysfs_device ) ,
sysfs_close_dev ) ;
2004-03-12 00:57:30 -08:00
dlist_unshift_sorted ( bus - > devices , bdev , sort_list ) ;
2003-07-18 20:08:00 -07:00
}
}
2003-12-15 21:53:28 -08:00
sysfs_close_directory ( devdir ) ;
return ( bus - > devices ) ;
2003-07-18 20:08:00 -07:00
}
/**
2003-12-15 21:53:28 -08:00
* sysfs_get_bus_drivers : get all pci drivers
2003-07-18 20:08:00 -07:00
* @ bus : pci bus to add drivers to
2003-12-15 21:53:28 -08:00
* returns dlist of drivers with success and NULL with error
2003-07-18 20:08:00 -07:00
*/
2003-12-15 21:53:28 -08:00
struct dlist * sysfs_get_bus_drivers ( struct sysfs_bus * bus )
2003-07-18 20:08:00 -07:00
{
struct sysfs_driver * driver = NULL ;
2003-12-15 21:53:28 -08:00
struct sysfs_directory * drvdir = NULL ;
2003-10-21 01:19:14 -07:00
struct sysfs_directory * cursub = NULL ;
2004-03-12 00:57:30 -08:00
char path [ SYSFS_PATH_MAX ] ;
2003-07-18 20:08:00 -07:00
2003-12-15 21:53:28 -08:00
if ( bus = = NULL ) {
2003-07-18 20:08:00 -07:00
errno = EINVAL ;
2003-12-15 21:53:28 -08:00
return NULL ;
2003-07-18 20:08:00 -07:00
}
2003-12-15 21:53:28 -08:00
memset ( path , 0 , SYSFS_PATH_MAX ) ;
2004-03-12 00:57:30 -08:00
safestrcpy ( path , bus - > path ) ;
safestrcat ( path , " / " ) ;
safestrcat ( path , SYSFS_DRIVERS_NAME ) ;
2003-12-15 21:53:28 -08:00
drvdir = sysfs_open_directory ( path ) ;
if ( drvdir = = NULL )
return NULL ;
2003-07-18 20:08:00 -07:00
2003-12-15 21:53:28 -08:00
if ( sysfs_read_dir_subdirs ( drvdir ) ! = 0 ) {
sysfs_close_directory ( drvdir ) ;
return NULL ;
2003-07-18 20:08:00 -07:00
}
2003-12-19 18:29:10 -08:00
if ( drvdir - > subdirs ! = NULL ) {
dlist_for_each_data ( drvdir - > subdirs , cursub ,
struct sysfs_directory ) {
driver = sysfs_open_driver_path ( cursub - > path ) ;
if ( driver = = NULL ) {
dprintf ( " Error opening driver at %s \n " ,
cursub - > path ) ;
continue ;
}
if ( bus - > drivers = = NULL )
bus - > drivers = dlist_new_with_delete
( sizeof ( struct sysfs_driver ) ,
sysfs_close_drv ) ;
2004-03-12 00:57:30 -08:00
dlist_unshift_sorted ( bus - > drivers , driver , sort_list ) ;
2003-07-18 20:08:00 -07:00
}
}
2003-12-15 21:53:28 -08:00
sysfs_close_directory ( drvdir ) ;
return ( bus - > drivers ) ;
2003-07-18 20:08:00 -07:00
}
/**
* sysfs_open_bus : opens specific bus and all its devices on system
* returns sysfs_bus structure with success or NULL with error .
*/
2004-03-12 00:57:30 -08:00
struct sysfs_bus * sysfs_open_bus ( const char * name )
2003-07-18 20:08:00 -07:00
{
struct sysfs_bus * bus = NULL ;
2004-03-12 00:57:30 -08:00
char buspath [ SYSFS_PATH_MAX ] ;
2003-07-18 20:08:00 -07:00
if ( name = = NULL ) {
errno = EINVAL ;
return NULL ;
}
2003-12-15 21:53:28 -08:00
memset ( buspath , 0 , SYSFS_PATH_MAX ) ;
if ( ( sysfs_get_mnt_path ( buspath , SYSFS_PATH_MAX ) ) ! = 0 ) {
dprintf ( " Sysfs not supported on this system \n " ) ;
2003-07-18 20:08:00 -07:00
return NULL ;
}
2003-12-15 21:53:28 -08:00
2004-03-12 00:57:30 -08:00
safestrcat ( buspath , " / " ) ;
safestrcat ( buspath , SYSFS_BUS_NAME ) ;
safestrcat ( buspath , " / " ) ;
safestrcat ( buspath , name ) ;
2003-12-15 21:53:28 -08:00
if ( ( sysfs_path_is_dir ( buspath ) ) ! = 0 ) {
dprintf ( " Invalid path to bus: %s \n " , buspath ) ;
2003-07-18 20:08:00 -07:00
return NULL ;
}
2003-12-15 21:53:28 -08:00
bus = alloc_bus ( ) ;
if ( bus = = NULL ) {
dprintf ( " calloc failed \n " ) ;
2003-07-18 20:08:00 -07:00
return NULL ;
}
2004-03-12 00:57:30 -08:00
safestrcpy ( bus - > name , name ) ;
safestrcpy ( bus - > path , buspath ) ;
2004-01-14 22:21:38 -08:00
if ( ( sysfs_remove_trailing_slash ( bus - > path ) ) ! = 0 ) {
dprintf ( " Incorrect path to bus %s \n " , bus - > path ) ;
sysfs_close_bus ( bus ) ;
return NULL ;
}
2003-07-18 20:08:00 -07:00
return bus ;
}
2003-10-21 01:19:14 -07:00
/**
* 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 .
*/
2004-03-12 00:57:30 -08:00
struct sysfs_device * sysfs_get_bus_device ( struct sysfs_bus * bus , char * id )
2003-10-21 01:19:14 -07:00
{
if ( bus = = NULL | | id = = NULL ) {
errno = EINVAL ;
return NULL ;
}
2003-12-15 21:53:28 -08:00
if ( bus - > devices = = NULL ) {
bus - > devices = sysfs_get_bus_devices ( bus ) ;
if ( bus - > devices = = NULL )
return NULL ;
}
2003-10-21 01:19:14 -07:00
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 ,
2004-03-12 00:57:30 -08:00
char * drvname )
2003-10-21 01:19:14 -07:00
{
if ( bus = = NULL | | drvname = = NULL ) {
errno = EINVAL ;
return NULL ;
}
2003-12-15 21:53:28 -08:00
if ( bus - > drivers = = NULL ) {
bus - > drivers = sysfs_get_bus_drivers ( bus ) ;
if ( bus - > drivers = = NULL )
return NULL ;
}
2003-10-21 01:19:14 -07:00
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 )
{
2003-12-15 21:53:28 -08:00
if ( bus = = NULL )
2003-10-21 01:19:14 -07:00
return NULL ;
2003-12-15 21:53:28 -08:00
if ( bus - > directory = = NULL ) {
bus - > directory = sysfs_open_directory ( bus - > path ) ;
if ( bus - > directory = = NULL )
return NULL ;
}
if ( bus - > directory - > attributes = = NULL ) {
if ( ( sysfs_read_dir_attributes ( bus - > directory ) ) ! = 0 )
return NULL ;
}
2003-10-21 01:19:14 -07:00
return bus - > directory - > attributes ;
}
2004-01-14 22:21:38 -08:00
/**
* sysfs_refresh_bus_attributes : refreshes the bus ' s list of attributes
* @ bus : sysfs_bus whose attributes to refresh
*
* NOTE : Upon return , prior references to sysfs_attributes for this bus
* _may_ not be valid
*
* Returns list of attributes on success and NULL on failure
*/
struct dlist * sysfs_refresh_bus_attributes ( struct sysfs_bus * bus )
{
if ( bus = = NULL ) {
errno = EINVAL ;
return NULL ;
}
if ( bus - > directory = = NULL )
return ( sysfs_get_bus_attributes ( bus ) ) ;
if ( ( sysfs_refresh_dir_attributes ( bus - > directory ) ) ! = 0 ) {
dprintf ( " Error refreshing bus attributes \n " ) ;
return NULL ;
}
return ( bus - > directory - > attributes ) ;
}
2003-10-21 01:19:14 -07:00
/**
* 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 ,
2004-03-12 00:57:30 -08:00
char * attrname )
2003-10-21 01:19:14 -07:00
{
2003-12-15 21:53:28 -08:00
struct dlist * attrlist = NULL ;
if ( bus = = NULL ) {
2003-10-21 01:19:14 -07:00
errno = EINVAL ;
return NULL ;
}
2003-12-15 21:53:28 -08:00
attrlist = sysfs_get_bus_attributes ( bus ) ;
if ( attrlist = = NULL )
return NULL ;
2003-10-21 01:19:14 -07:00
return sysfs_get_directory_attribute ( bus - > directory , attrname ) ;
}
/**
* 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
*/
2004-03-12 00:57:30 -08:00
int sysfs_find_driver_bus ( const char * driver , char * busname , size_t bsize )
2003-10-21 01:19:14 -07:00
{
2004-03-12 00:57:30 -08:00
char subsys [ SYSFS_PATH_MAX ] , * bus = NULL , * curdrv = NULL ;
2003-10-21 01:19:14 -07:00
struct dlist * buslist = NULL , * drivers = NULL ;
if ( driver = = NULL | | busname = = NULL ) {
errno = EINVAL ;
return - 1 ;
}
memset ( subsys , 0 , SYSFS_PATH_MAX ) ;
2004-03-12 00:57:30 -08:00
safestrcpy ( subsys , SYSFS_BUS_NAME ) ;
2003-10-21 01:19:14 -07:00
buslist = sysfs_open_subsystem_list ( subsys ) ;
if ( buslist ! = NULL ) {
dlist_for_each_data ( buslist , bus , char ) {
memset ( subsys , 0 , SYSFS_PATH_MAX ) ;
2004-03-12 00:57:30 -08:00
safestrcpy ( subsys , SYSFS_BUS_NAME ) ;
safestrcat ( subsys , " / " ) ;
safestrcat ( subsys , bus ) ;
safestrcat ( subsys , " / " ) ;
safestrcat ( subsys , SYSFS_DRIVERS_NAME ) ;
2003-10-21 01:19:14 -07:00
drivers = sysfs_open_subsystem_list ( subsys ) ;
if ( drivers ! = NULL ) {
dlist_for_each_data ( drivers , curdrv , char ) {
if ( strcmp ( driver , curdrv ) = = 0 ) {
[PATCH] more Libsysfs updates
On Thu, Mar 11, 2004 at 02:36:23PM +0100, Kay Sievers wrote:
> On Thu, 2004-03-11 at 15:02, Ananth N Mavinakayanahalli wrote:
> > On Thu, Mar 11, 2004 at 02:04:36PM +0100, Kay Sievers wrote:
> > > On Thu, Mar 11, 2004 at 11:53:50AM +0500, Ananth N Mavinakayanahalli wrote:
> > >
> > > > +#define safestrcpy(to, from) strncpy(to, from, sizeof(to)-1)
> > > > +#define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1)
> > >
> > > These strings are not terminated with '\0' if from is longer than
> > > the sizeof to.
> >
> > Did not do it on purpose as the "to" elements are either calloc'd or memset to
> > '0' explicitly in the library. Thats the reason I mentioned "scaled down" :)
>
> Ahh, sounds good.
>
> > > > +#define safestrncpy(to, from, maxsize) \
> > > > +do { \
> > > > + to[maxsize-1] = '\0'; \
> > > > + strncpy(to, from, maxsize-1); \
> > > > +} while (0)
> > > > +
> > > > +#define safestrncat(to, from, maxsize) \
> > > > +do { \
> > > > + to[maxsize-1] = '\0'; \
> > > > + strncat(to, from, maxsize - strlen(to)-1); \
> > > > +} while (0)
> > >
> > > We all expect a similar behavior like strncat/strncpy according to the
> > > names, but these macros are limiting by the target size and do not limit
> > > the count of chars copied.
> > > This is confusing I think and suggest using a different name like
> > > 'safestrcopymax()' or something.
> >
> > Good point.. will make the change
>
> Nice. I've had these *n* names too and I forgot about the logic and only
> 10 days later I introduced a ugly bug cause I can't limit the count of
> copied chars :)
Inlined is the patch for this... applies on the earlier _BIG_ patch.
2004-03-12 00:57:36 -08:00
safestrcpymax ( busname ,
2004-03-12 00:57:30 -08:00
bus , bsize ) ;
2003-10-21 01:19:14 -07:00
sysfs_close_list ( drivers ) ;
sysfs_close_list ( buslist ) ;
return 0 ;
}
}
sysfs_close_list ( drivers ) ;
}
}
sysfs_close_list ( buslist ) ;
}
return - 1 ;
}