2003-07-21 07:48:48 +04:00
/*
* udev - remove . c
*
* Userspace devfs
*
* Copyright ( C ) 2003 Greg Kroah - Hartman < greg @ kroah . com >
*
*
* This program is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License .
*
* This program 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
* General Public License for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
* 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*
*/
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <fcntl.h>
# include <unistd.h>
# include <errno.h>
# include "udev.h"
# include "udev_version.h"
# include "namedev.h"
2003-08-06 10:57:23 +04:00
# include "udevdb.h"
2003-07-21 07:48:48 +04:00
# include "libsysfs/libsysfs.h"
2003-12-07 20:12:07 +03:00
static int delete_path ( char * path )
2003-07-21 07:48:48 +04:00
{
2003-12-07 20:12:07 +03:00
char * pos ;
int retval ;
2003-07-21 07:48:48 +04:00
2003-12-07 20:12:07 +03:00
pos = strrchr ( path , ' / ' ) ;
while ( 1 ) {
* pos = ' \0 ' ;
pos = strrchr ( path , ' / ' ) ;
/* don't remove the last one */
if ( ( pos = = path ) | | ( pos = = NULL ) )
break ;
/* remove if empty */
retval = rmdir ( path ) ;
if ( retval ) {
if ( errno = = ENOTEMPTY )
return 0 ;
dbg ( " rmdir(%s) failed with error '%s' " ,
path , strerror ( errno ) ) ;
break ;
}
dbg ( " removed '%s' " , path ) ;
2003-10-21 09:48:44 +04:00
}
2003-12-07 20:12:07 +03:00
return 0 ;
2003-07-21 07:48:48 +04:00
}
2003-12-07 20:12:07 +03:00
static int delete_node ( struct udevice * dev )
2003-07-21 07:48:48 +04:00
{
char filename [ 255 ] ;
2003-11-12 14:48:01 +03:00
int retval ;
2003-07-21 07:48:48 +04:00
2003-10-22 07:19:09 +04:00
strncpy ( filename , udev_root , sizeof ( filename ) ) ;
2003-12-07 20:12:07 +03:00
strncat ( filename , dev - > name , sizeof ( filename ) ) ;
2003-07-21 07:48:48 +04:00
2003-12-07 20:12:07 +03:00
dbg ( " unlinking node '%s' " , filename ) ;
2003-11-12 14:48:01 +03:00
retval = unlink ( filename ) ;
if ( retval ) {
dbg ( " unlink(%s) failed with error '%s' " ,
filename , strerror ( errno ) ) ;
return retval ;
}
/* remove subdirectories */
2003-12-07 20:12:07 +03:00
if ( strchr ( dev - > name , ' / ' ) )
delete_path ( filename ) ;
if ( * dev - > symlink ) {
strncpy ( filename , udev_root , sizeof ( filename ) ) ;
strncat ( filename , dev - > symlink , sizeof ( filename ) ) ;
dbg ( " unlinking symlink '%s' " , filename ) ;
retval = unlink ( filename ) ;
if ( retval ) {
dbg ( " unlink(%s) failed with error '%s' " ,
filename , strerror ( errno ) ) ;
return retval ;
}
if ( strchr ( dev - > symlink , ' / ' ) ) {
delete_path ( filename ) ;
2003-11-12 14:48:01 +03:00
}
}
2003-12-07 20:12:07 +03:00
2003-11-12 14:48:01 +03:00
return retval ;
2003-07-21 07:48:48 +04:00
}
2003-12-07 20:12:07 +03:00
/*
* Look up the sysfs path in the database to see if we have named this device
* something different from the kernel name . If we have , us it . If not , use
* the default kernel name for lack of anything else to know to do .
*/
int udev_remove_device ( char * path , char * subsystem )
2003-07-21 07:48:48 +04:00
{
2003-12-07 20:12:07 +03:00
char name [ 100 ] ;
struct udevice * dev ;
char * temp ;
2003-07-21 07:48:48 +04:00
2003-12-07 20:12:07 +03:00
dev = udevdb_get_dev ( path ) ;
if ( dev = = NULL ) {
dbg ( " '%s' not found in database, falling back on default name " , path ) ;
temp = strrchr ( path , ' / ' ) ;
if ( temp = = NULL )
return - ENODEV ;
strncpy ( name , & temp [ 1 ] , sizeof ( name ) ) ;
2003-07-21 07:48:48 +04:00
}
2003-12-07 20:12:07 +03:00
dbg ( " name is '%s' " , dev - > name ) ;
udevdb_delete_dev ( path ) ;
return delete_node ( dev ) ;
2003-07-21 07:48:48 +04:00
}