2005-04-27 07:59:47 +04:00
/*
* udev . 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>
2003-04-10 21:53:46 +04:00
# include <stdio.h>
# include <fcntl.h>
# include <unistd.h>
# include <errno.h>
2005-04-27 07:59:47 +04:00
# include "udev.h"
# include "udev_version.h"
2003-07-17 12:24:51 +04:00
# include "namedev.h"
2003-10-15 10:20:53 +04:00
# include "udevdb.h"
2003-07-19 16:06:55 +04:00
# include "libsysfs/libsysfs.h"
2005-04-27 07:59:47 +04:00
2003-10-22 07:19:09 +04:00
/* global variables */
char * * main_argv ;
char * * main_envp ;
char sysfs_path [ SYSFS_PATH_MAX ] ;
2003-10-22 08:41:35 +04:00
char * udev_config_dir = UDEV_CONFIG_DIR ;
char * udev_root = UDEV_ROOT ;
2003-10-22 07:19:09 +04:00
char udev_db_filename [ PATH_MAX + NAME_MAX ] ;
char udev_config_permission_filename [ PATH_MAX + NAME_MAX ] ;
char udev_config_filename [ PATH_MAX + NAME_MAX ] ;
2005-04-27 07:59:47 +04:00
2003-10-22 07:19:09 +04:00
static inline char * get_action ( void )
2005-04-27 07:59:47 +04:00
{
char * action ;
action = getenv ( " ACTION " ) ;
return action ;
}
2003-10-22 07:19:09 +04:00
static inline char * get_devpath ( void )
{
char * devpath ;
devpath = getenv ( " DEVPATH " ) ;
return devpath ;
}
2005-04-27 07:59:47 +04:00
2003-10-22 07:19:09 +04:00
static inline char * get_seqnum ( void )
2005-04-27 07:59:47 +04:00
{
2003-10-22 07:19:09 +04:00
char * seqnum ;
2005-04-27 07:59:47 +04:00
2003-10-22 07:19:09 +04:00
seqnum = getenv ( " SEQNUM " ) ;
return seqnum ;
2005-04-27 07:59:47 +04:00
}
2003-10-22 07:19:09 +04:00
static void get_dirs ( void )
{
char * temp ;
2003-10-22 08:41:35 +04:00
char * udev_db = UDEV_DB ;
2003-10-22 09:08:03 +04:00
char * udev_config = UDEV_CONFIG_FILE ;
char * udev_permission = UDEV_CONFIG_PERMISSION_FILE ;
2003-10-22 07:19:09 +04:00
int retval ;
2003-10-22 08:41:35 +04:00
retval = sysfs_get_mnt_path ( sysfs_path , SYSFS_PATH_MAX ) ;
if ( retval )
dbg ( " sysfs_get_mnt_path failed " ) ;
/* see if we should try to override any of the default values */
temp = getenv ( " UDEV_TEST " ) ;
if ( temp ! = NULL ) {
/* hm testing is happening, use the specified values, if they are present */
2003-10-22 07:19:09 +04:00
temp = getenv ( " UDEV_SYSFS_PATH " ) ;
2003-10-22 08:41:35 +04:00
if ( temp )
strncpy ( sysfs_path , temp , sizeof ( sysfs_path ) ) ;
temp = getenv ( " UDEV_CONFIG_DIR " ) ;
if ( temp )
udev_config_dir = temp ;
temp = getenv ( " UDEV_ROOT " ) ;
if ( temp )
udev_root = temp ;
temp = getenv ( " UDEV_DB " ) ;
if ( temp )
udev_db = temp ;
temp = getenv ( " UDEV_CONFIG_FILE " ) ;
if ( temp )
udev_config = temp ;
temp = getenv ( " UDEV_PERMISSION_FILE " ) ;
if ( temp )
udev_permission = temp ;
2003-10-22 07:19:09 +04:00
}
2003-10-22 08:41:35 +04:00
dbg ( " sysfs_path = %s " , sysfs_path ) ;
2003-10-22 07:19:09 +04:00
strncpy ( udev_db_filename , udev_config_dir , sizeof ( udev_db_filename ) ) ;
2003-10-22 08:41:35 +04:00
strncat ( udev_db_filename , udev_db , sizeof ( udev_db_filename ) ) ;
2003-10-22 07:19:09 +04:00
strncpy ( udev_config_filename , udev_config_dir , sizeof ( udev_config_filename ) ) ;
2003-10-22 08:41:35 +04:00
strncat ( udev_config_filename , udev_config , sizeof ( udev_config_filename ) ) ;
2003-10-22 07:19:09 +04:00
strncpy ( udev_config_permission_filename , udev_config_dir , sizeof ( udev_config_permission_filename ) ) ;
2003-10-22 08:41:35 +04:00
strncat ( udev_config_permission_filename , udev_permission , sizeof ( udev_config_permission_filename ) ) ;
2003-10-22 07:19:09 +04:00
}
2003-10-15 10:32:17 +04:00
int main ( int argc , char * * argv , char * * envp )
2005-04-27 07:59:47 +04:00
{
char * action ;
2003-10-22 07:19:09 +04:00
char * devpath ;
2003-07-21 07:48:48 +04:00
char * subsystem ;
2003-04-10 21:53:46 +04:00
int retval = - EINVAL ;
2005-04-27 07:59:47 +04:00
2003-10-15 10:32:17 +04:00
main_argv = argv ;
main_envp = envp ;
2003-10-16 10:51:27 +04:00
dbg ( " version %s " , UDEV_VERSION ) ;
2005-04-27 07:59:47 +04:00
if ( argc ! = 2 ) {
dbg ( " unknown number of arguments " ) ;
2003-04-10 21:53:46 +04:00
goto exit ;
2005-04-27 07:59:47 +04:00
}
2003-07-21 07:48:48 +04:00
subsystem = argv [ 1 ] ;
2003-10-22 07:19:09 +04:00
devpath = get_devpath ( ) ;
if ( ! devpath ) {
dbg ( " no devpath? " ) ;
2003-07-19 16:06:55 +04:00
goto exit ;
}
2003-10-22 07:19:09 +04:00
dbg ( " looking at %s " , devpath ) ;
2003-07-17 12:24:51 +04:00
2003-07-19 16:06:55 +04:00
/* we only care about class devices and block stuff */
2003-10-22 07:19:09 +04:00
if ( ! strstr ( devpath , " class " ) & &
! strstr ( devpath , " block " ) ) {
2003-07-19 16:06:55 +04:00
dbg ( " not block or class " ) ;
goto exit ;
}
2003-07-17 07:18:06 +04:00
2003-07-21 07:48:48 +04:00
/* but we don't care about net class devices */
if ( strcmp ( subsystem , " net " ) = = 0 ) {
dbg ( " don't care about net devices " ) ;
goto exit ;
}
2005-04-27 07:59:47 +04:00
action = get_action ( ) ;
if ( ! action ) {
dbg ( " no action? " ) ;
2003-04-10 21:53:46 +04:00
goto exit ;
2005-04-27 07:59:47 +04:00
}
2003-10-15 10:20:53 +04:00
/* initialize udev database */
2003-10-22 07:19:09 +04:00
get_dirs ( ) ;
2003-10-15 10:20:53 +04:00
retval = udevdb_init ( UDEVDB_DEFAULT ) ;
if ( retval ! = 0 ) {
dbg ( " Unable to initialize database. " ) ;
goto exit ;
}
2003-07-21 07:48:48 +04:00
/* initialize the naming deamon */
namedev_init ( ) ;
2003-04-10 22:02:32 +04:00
if ( strcmp ( action , " add " ) = = 0 )
2003-10-22 07:19:09 +04:00
retval = udev_add_device ( devpath , subsystem ) ;
2003-04-10 21:53:46 +04:00
2003-10-15 10:20:53 +04:00
else if ( strcmp ( action , " remove " ) = = 0 )
2003-10-22 07:19:09 +04:00
retval = udev_remove_device ( devpath , subsystem ) ;
2003-04-10 21:53:46 +04:00
2003-10-15 10:20:53 +04:00
else {
dbg ( " Unknown action: %s " , action ) ;
retval = - EINVAL ;
}
udevdb_exit ( ) ;
2003-04-10 21:53:46 +04:00
exit :
return retval ;
2005-04-27 07:59:47 +04:00
}