2003-07-19 07:08:00 +04:00
/*
* sysfs_driver . c
*
* Driver utility functions for libsysfs
*
2005-02-23 14:21:39 +03:00
* Copyright ( C ) IBM Corp . 2003 - 2005
2003-07-19 07:08:00 +04: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 15:15:26 +04:00
# include "libsysfs.h"
2003-07-19 07:08:00 +04:00
# include "sysfs.h"
2003-12-16 08:53:28 +03:00
static void sysfs_close_driver_device ( void * device )
2003-10-21 12:19:14 +04:00
{
sysfs_close_device ( ( struct sysfs_device * ) device ) ;
}
2005-02-23 14:21:39 +03:00
/**
2003-12-16 08:53:28 +03:00
* sysfs_close_driver : closes driver and deletes device lists too
2003-10-21 12:19:14 +04:00
* @ driver : driver to close
2005-02-23 14:21:39 +03:00
*/
2003-12-16 08:53:28 +03:00
void sysfs_close_driver ( struct sysfs_driver * driver )
2003-10-21 12:19:14 +04:00
{
2005-02-23 14:21:39 +03:00
if ( driver ) {
if ( driver - > devices )
2003-10-21 12:19:14 +04:00
dlist_destroy ( driver - > devices ) ;
2005-02-23 14:21:39 +03:00
if ( driver - > attrlist )
dlist_destroy ( driver - > attrlist ) ;
2003-10-21 12:19:14 +04:00
free ( driver ) ;
}
}
2003-12-16 08:53:28 +03:00
2003-07-19 07:08:00 +04:00
/**
* alloc_driver : allocates and initializes driver
* returns struct sysfs_driver with success and NULL with error .
*/
static struct sysfs_driver * alloc_driver ( void )
{
return ( struct sysfs_driver * ) calloc ( 1 , sizeof ( struct sysfs_driver ) ) ;
}
/**
2005-02-23 14:21:39 +03:00
* get_driver_bus : gets bus the driver is on
* Returns 0 on success and 1 on error
2003-07-19 07:08:00 +04:00
*/
2005-02-23 14:21:39 +03:00
static int get_driver_bus ( struct sysfs_driver * drv )
2003-07-19 07:08:00 +04:00
{
2005-02-23 14:21:39 +03:00
char drvpath [ SYSFS_PATH_MAX ] , * c = NULL ;
2003-07-19 07:08:00 +04:00
2005-02-23 14:21:39 +03:00
if ( ! drv ) {
2003-12-16 08:53:28 +03:00
errno = EINVAL ;
2005-02-23 14:21:39 +03:00
return 1 ;
2003-12-16 08:53:28 +03:00
}
2003-10-21 12:19:14 +04:00
2005-02-23 14:21:39 +03:00
safestrcpy ( drvpath , drv - > path ) ;
c = strstr ( drvpath , SYSFS_DRIVERS_NAME ) ;
if ( c = = NULL )
return 1 ;
* - - c = ' \0 ' ;
c = strstr ( drvpath , SYSFS_BUS_NAME ) ;
if ( c = = NULL )
return 1 ;
c = strstr ( c , " / " ) ;
if ( c = = NULL )
return 1 ;
c + + ;
safestrcpy ( drv - > bus , c ) ;
return 0 ;
2004-01-15 09:21:38 +03:00
}
2003-10-21 12:19:14 +04:00
/**
2005-02-23 14:21:39 +03:00
* sysfs_get_driver_attr : searches drv ' s attributes by name
2003-10-21 12:19:14 +04:00
* @ drv : driver to look through
* @ name : attribute name to get
2005-02-23 14:21:39 +03:00
* returns sysfs_attribute reference with success or NULL with error .
*/
2003-10-21 12:19:14 +04:00
struct sysfs_attribute * sysfs_get_driver_attr ( struct sysfs_driver * drv ,
2005-02-23 14:21:39 +03:00
const char * name )
2003-10-21 12:19:14 +04:00
{
2005-02-23 14:21:39 +03:00
if ( ! drv | | ! name ) {
errno = EINVAL ;
2004-01-15 09:21:38 +03:00
return NULL ;
2005-02-23 14:21:39 +03:00
}
return get_attribute ( drv , ( char * ) name ) ;
2003-10-21 12:19:14 +04:00
}
/**
2005-02-23 14:21:39 +03:00
* sysfs_get_driver_attributes : gets list of driver attributes
* @ dev : driver whose attributes list is needed
* returns dlist of attributes on success or NULL on error
2003-10-21 12:19:14 +04:00
*/
2005-02-23 14:21:39 +03:00
struct dlist * sysfs_get_driver_attributes ( struct sysfs_driver * drv )
2003-10-21 12:19:14 +04:00
{
2005-02-23 14:21:39 +03:00
if ( ! drv ) {
2003-12-16 08:53:28 +03:00
errno = EINVAL ;
2003-10-21 12:19:14 +04:00
return NULL ;
2003-12-16 08:53:28 +03:00
}
2005-02-23 14:21:39 +03:00
return get_attributes_list ( drv ) ;
2003-10-21 12:19:14 +04:00
}
2003-12-16 08:53:28 +03:00
/**
2005-02-23 14:21:39 +03:00
* sysfs_open_driver_path : opens and initializes driver structure
* @ path : path to driver directory
* returns struct sysfs_driver with success and NULL with error
*/
struct sysfs_driver * sysfs_open_driver_path ( const char * path )
2003-12-16 08:53:28 +03:00
{
2005-02-23 14:21:39 +03:00
struct sysfs_driver * driver = NULL ;
2003-12-16 08:53:28 +03:00
2005-02-23 14:21:39 +03:00
if ( ! path ) {
2003-12-16 08:53:28 +03:00
errno = EINVAL ;
return NULL ;
}
2005-02-23 14:21:39 +03:00
if ( sysfs_path_is_dir ( path ) ) {
dprintf ( " Invalid path to driver: %s \n " , path ) ;
return NULL ;
2003-12-16 08:53:28 +03:00
}
2005-02-23 14:21:39 +03:00
driver = alloc_driver ( ) ;
if ( ! driver ) {
dprintf ( " Error allocating driver at %s \n " , path ) ;
2004-01-15 09:21:38 +03:00
return NULL ;
}
2005-02-23 14:21:39 +03:00
if ( sysfs_get_name_from_path ( path , driver - > name , SYSFS_NAME_LEN ) ) {
dprintf ( " Error getting driver name from path \n " ) ;
free ( driver ) ;
return NULL ;
2004-01-15 09:21:38 +03:00
}
2005-02-23 14:21:39 +03:00
safestrcpy ( driver - > path , path ) ;
if ( sysfs_remove_trailing_slash ( driver - > path ) ) {
dprintf ( " Invalid path to driver %s \n " , driver - > path ) ;
sysfs_close_driver ( driver ) ;
2004-01-15 09:21:38 +03:00
return NULL ;
}
2005-02-23 14:21:39 +03:00
if ( get_driver_bus ( driver ) ) {
dprintf ( " Could not get the bus driver is on \n " ) ;
sysfs_close_driver ( driver ) ;
2003-12-16 08:53:28 +03:00
return NULL ;
}
2005-02-23 14:21:39 +03:00
return driver ;
2003-12-16 08:53:28 +03:00
}
2003-11-25 10:47:43 +03:00
/**
* get_driver_path : looks up the bus the driver is on and builds path to
* the driver .
* @ bus : bus on which to search
* @ drv : driver to look for
* @ path : buffer to return path to driver
* @ psize : size of " path "
* Returns 0 on success and - 1 on error
*/
2004-03-12 11:57:30 +03:00
static int get_driver_path ( const char * bus , const char * drv ,
char * path , size_t psize )
2003-11-25 10:47:43 +03:00
{
2005-02-23 14:21:39 +03:00
if ( ! bus | | ! drv | | ! path | | psize = = 0 ) {
2003-11-25 10:47:43 +03:00
errno = EINVAL ;
return - 1 ;
}
2005-02-23 14:21:39 +03:00
if ( sysfs_get_mnt_path ( path , psize ) ) {
2003-11-25 10:47:43 +03:00
dprintf ( " Error getting sysfs mount path \n " ) ;
return - 1 ;
}
[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 11:57:36 +03:00
safestrcatmax ( path , " / " , psize ) ;
safestrcatmax ( path , SYSFS_BUS_NAME , psize ) ;
safestrcatmax ( path , " / " , psize ) ;
safestrcatmax ( path , bus , psize ) ;
safestrcatmax ( path , " / " , psize ) ;
safestrcatmax ( path , SYSFS_DRIVERS_NAME , psize ) ;
safestrcatmax ( path , " / " , psize ) ;
safestrcatmax ( path , drv , psize ) ;
2003-11-25 10:47:43 +03:00
return 0 ;
}
2003-12-20 05:29:10 +03:00
/**
* sysfs_open_driver : open driver by name , given its bus
* @ bus_name : Name of the bus
2004-03-12 11:57:30 +03:00
* @ drv_name : Name of the driver
2003-12-20 05:29:10 +03:00
* Returns the sysfs_driver reference on success and NULL on failure
*/
2004-03-12 11:57:30 +03:00
struct sysfs_driver * sysfs_open_driver ( const char * bus_name ,
const char * drv_name )
2003-12-20 05:29:10 +03:00
{
2004-03-12 11:57:30 +03:00
char path [ SYSFS_PATH_MAX ] ;
2003-12-20 05:29:10 +03:00
struct sysfs_driver * driver = NULL ;
2005-02-23 14:21:39 +03:00
if ( ! drv_name | | ! bus_name ) {
2003-12-20 05:29:10 +03:00
errno = EINVAL ;
return NULL ;
}
memset ( path , 0 , SYSFS_PATH_MAX ) ;
2005-02-23 14:21:39 +03:00
if ( get_driver_path ( bus_name , drv_name , path , SYSFS_PATH_MAX ) ) {
2003-12-20 05:29:10 +03:00
dprintf ( " Error getting to driver %s \n " , drv_name ) ;
return NULL ;
}
driver = sysfs_open_driver_path ( path ) ;
2005-02-23 14:21:39 +03:00
if ( ! driver ) {
2003-12-20 05:29:10 +03:00
dprintf ( " Error opening driver at %s \n " , path ) ;
return NULL ;
}
return driver ;
}
2005-02-23 14:21:39 +03:00
/**
* sysfs_get_driver_devices : gets list of devices that use the driver
* @ drv : sysfs_driver whose device list is needed
* Returns dlist of struct sysfs_device on success and NULL on failure
*/
struct dlist * sysfs_get_driver_devices ( struct sysfs_driver * drv )
{
char * ln = NULL ;
struct dlist * linklist = NULL ;
struct sysfs_device * dev = NULL ;
if ( ! drv ) {
errno = EINVAL ;
return NULL ;
}
linklist = read_dir_links ( drv - > path ) ;
if ( linklist ) {
dlist_for_each_data ( linklist , ln , char ) {
if ( ! strncmp ( ln , SYSFS_MODULE_NAME , strlen ( ln ) ) )
continue ;
dev = sysfs_open_device ( drv - > bus , ln ) ;
if ( ! dev ) {
dprintf ( " Error opening driver's device \n " ) ;
sysfs_close_list ( linklist ) ;
return NULL ;
}
if ( ! drv - > devices ) {
drv - > devices = dlist_new_with_delete
( sizeof ( struct sysfs_device ) ,
sysfs_close_driver_device ) ;
if ( ! drv - > devices ) {
dprintf ( " Error creating device list \n " ) ;
sysfs_close_list ( linklist ) ;
return NULL ;
}
}
dlist_unshift_sorted ( drv - > devices , dev , sort_list ) ;
}
sysfs_close_list ( linklist ) ;
}
return drv - > devices ;
}