2004-02-13 06:48:07 +03:00
/*
2005-03-07 06:29:43 +03:00
* udevtest . c
2004-02-13 06:48:07 +03:00
*
2005-07-22 20:35:58 +04:00
* Copyright ( C ) 2003 - 2004 Greg Kroah - Hartman < greg @ kroah . com >
2004-02-13 06:48:07 +03:00
*
* 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 <errno.h>
# include <ctype.h>
# include <signal.h>
2005-03-27 03:11:03 +04:00
# include <syslog.h>
2004-02-13 06:48:07 +03:00
2004-02-24 06:07:25 +03:00
# include "libsysfs/sysfs/libsysfs.h"
2004-02-13 06:48:07 +03:00
# include "udev.h"
2004-12-12 04:32:25 +03:00
# include "udev_sysfs.h"
2004-11-25 04:44:38 +03:00
# include "udev_utils.h"
2004-02-13 06:48:07 +03:00
# include "udev_version.h"
2005-03-13 00:36:32 +03:00
# include "udev_rules.h"
2004-11-12 08:32:19 +03:00
# include "logging.h"
2004-02-13 06:48:07 +03:00
2004-03-23 09:18:34 +03:00
2005-03-06 12:15:51 +03:00
# ifdef USE_LOG
2005-03-27 03:11:03 +04:00
void log_message ( int priority , const char * format , . . . )
2004-02-13 06:48:07 +03:00
{
2004-03-04 11:57:29 +03:00
va_list args ;
2004-02-13 06:48:07 +03:00
2005-03-27 03:11:03 +04:00
if ( priority > udev_log_priority )
return ;
2004-02-13 06:48:07 +03:00
va_start ( args , format ) ;
2004-02-13 07:19:21 +03:00
vprintf ( format , args ) ;
2004-02-13 06:48:07 +03:00
va_end ( args ) ;
2004-02-13 07:19:21 +03:00
if ( format [ strlen ( format ) - 1 ] ! = ' \n ' )
printf ( " \n " ) ;
2004-02-13 06:48:07 +03:00
}
# endif
2004-03-23 09:18:34 +03:00
int main ( int argc , char * argv [ ] , char * envp [ ] )
2004-02-13 06:48:07 +03:00
{
2005-07-05 17:24:41 +04:00
struct udev_rules rules ;
2004-10-19 06:11:51 +04:00
struct sysfs_class_device * class_dev ;
2004-02-13 06:48:07 +03:00
char * devpath ;
2005-03-07 06:29:43 +03:00
char path [ PATH_SIZE ] ;
char temp [ PATH_SIZE ] ;
2004-10-19 06:11:51 +04:00
struct udevice udev ;
2004-11-28 15:41:15 +03:00
char * subsystem = NULL ;
2004-03-23 09:18:34 +03:00
info ( " version %s " , UDEV_VERSION ) ;
2004-02-13 06:48:07 +03:00
2004-11-28 15:41:15 +03:00
/* initialize our configuration */
udev_init_config ( ) ;
2005-03-27 03:11:03 +04:00
if ( udev_log_priority < LOG_INFO )
udev_log_priority = LOG_INFO ;
2004-11-28 15:41:15 +03:00
2005-03-29 13:30:51 +04:00
if ( argc ! = 3 ) {
info ( " Usage: udevtest <devpath> <subsystem> " ) ;
return 1 ;
}
2004-03-23 09:18:34 +03:00
/* remove sysfs_path if given */
2005-03-07 06:29:43 +03:00
if ( strncmp ( argv [ 1 ] , sysfs_path , strlen ( sysfs_path ) ) = = 0 )
2004-11-28 15:41:15 +03:00
devpath = & argv [ 1 ] [ strlen ( sysfs_path ) ] ;
2004-03-23 09:18:34 +03:00
else
if ( argv [ 1 ] [ 0 ] ! = ' / ' ) {
/* prepend '/' if missing */
2005-03-07 06:29:43 +03:00
snprintf ( temp , sizeof ( temp ) , " /%s " , argv [ 1 ] ) ;
temp [ sizeof ( temp ) - 1 ] = ' \0 ' ;
2004-03-23 09:18:34 +03:00
devpath = temp ;
2005-03-07 06:29:43 +03:00
} else
2004-03-23 09:18:34 +03:00
devpath = argv [ 1 ] ;
2005-03-29 13:25:52 +04:00
subsystem = argv [ 2 ] ;
setenv ( " DEVPATH " , devpath , 1 ) ;
setenv ( " SUBSYSTEM " , subsystem , 1 ) ;
setenv ( " ACTION " , " add " , 1 ) ;
info ( " looking at device '%s' from subsystem '%s' " , devpath , subsystem ) ;
2004-02-13 06:48:07 +03:00
/* initialize the naming deamon */
2005-07-05 17:24:41 +04:00
udev_rules_init ( & rules , 0 ) ;
2004-02-13 06:48:07 +03:00
2004-10-19 06:11:51 +04:00
/* fill in values and test_run flag*/
2005-03-29 13:25:52 +04:00
udev_init_device ( & udev , devpath , subsystem , " add " ) ;
2004-10-19 06:11:51 +04:00
/* open the device */
2005-03-07 06:29:43 +03:00
snprintf ( path , sizeof ( path ) , " %s%s " , sysfs_path , udev . devpath ) ;
path [ sizeof ( path ) - 1 ] = ' \0 ' ;
2004-10-19 06:11:51 +04:00
class_dev = sysfs_open_class_device_path ( path ) ;
2004-10-30 15:44:46 +04:00
if ( class_dev = = NULL ) {
info ( " sysfs_open_class_device_path failed " ) ;
return 1 ;
}
2004-12-12 04:03:14 +03:00
info ( " opened class_dev->name='%s' " , class_dev - > name ) ;
2004-10-19 06:11:51 +04:00
2005-06-22 03:31:24 +04:00
if ( udev . type = = DEV_BLOCK | | udev . type = = DEV_CLASS )
udev . devt = get_devt ( class_dev ) ;
2004-10-19 06:28:39 +04:00
/* simulate node creation with test flag */
udev . test_run = 1 ;
2005-06-22 03:31:24 +04:00
if ( udev . type = = DEV_NET | | udev . devt ) {
2005-07-05 17:24:41 +04:00
udev_rules_get_name ( & rules , & udev , class_dev ) ;
2005-06-22 03:31:24 +04:00
udev_add_device ( & udev , class_dev ) ;
} else
info ( " only char and block devices with a dev-file are supported by this test program " ) ;
2004-10-19 06:28:39 +04:00
sysfs_close_class_device ( class_dev ) ;
2004-03-23 09:18:34 +03:00
return 0 ;
2004-02-13 06:48:07 +03:00
}