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>
2006-01-09 23:18:00 +03:00
# include <stddef.h>
# include <unistd.h>
2004-02-13 06:48:07 +03:00
# 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
# include "udev.h"
2005-03-13 00:36:32 +03:00
# include "udev_rules.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-02-13 06:48:07 +03:00
char * devpath ;
2005-03-07 06:29:43 +03:00
char temp [ PATH_SIZE ] ;
2006-01-09 23:18:00 +03:00
struct udevice * udev ;
struct sysfs_device * dev ;
int retval ;
int rc = 0 ;
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 */
2006-01-09 23:18:00 +03:00
udev_config_init ( ) ;
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
2006-01-09 23:18:00 +03:00
if ( argc ! = 2 ) {
info ( " Usage: udevtest <devpath> " ) ;
2005-03-29 13:30:51 +04:00
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 )
2006-01-09 23:18:00 +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 ] ;
2006-01-09 23:18:00 +03:00
sysfs_init ( ) ;
2005-11-05 22:00:31 +03:00
udev_rules_init ( & rules , 0 ) ;
2004-02-13 06:48:07 +03:00
2006-01-09 23:18:00 +03:00
dev = sysfs_device_get ( devpath ) ;
if ( dev = = NULL ) {
info ( " unable to open '%s' " , devpath ) ;
rc = 2 ;
goto exit ;
}
2004-10-19 06:11:51 +04:00
2006-01-09 23:18:00 +03:00
udev = udev_device_init ( ) ;
if ( udev = = NULL ) {
info ( " can't open device " ) ;
rc = 3 ;
goto exit ;
2004-10-30 15:44:46 +04:00
}
2004-10-19 06:11:51 +04:00
2006-01-09 23:18:00 +03:00
/* override built-in sysfs device */
udev - > dev = dev ;
strcpy ( udev - > action , " add " ) ;
udev - > devt = udev_device_get_devt ( udev ) ;
2005-06-22 03:31:24 +04:00
2004-10-19 06:28:39 +04:00
/* simulate node creation with test flag */
2006-01-09 23:18:00 +03:00
udev - > test_run = 1 ;
setenv ( " DEVPATH " , udev - > dev - > devpath , 1 ) ;
setenv ( " SUBSYSTEM " , udev - > dev - > subsystem , 1 ) ;
setenv ( " ACTION " , " add " , 1 ) ;
info ( " looking at device '%s' from subsystem '%s' " , udev - > dev - > devpath , udev - > dev - > subsystem ) ;
retval = udev_device_event ( & rules , udev ) ;
if ( retval = = 0 ) {
struct name_entry * name_loop ;
list_for_each_entry ( name_loop , & udev - > run_list , node )
info ( " run: '%s' " , name_loop - > name ) ;
}
exit :
udev_rules_cleanup ( & rules ) ;
sysfs_cleanup ( ) ;
return rc ;
2004-02-13 06:48:07 +03:00
}