2011-08-05 04:00:30 +04:00
/*
2012-11-12 22:36:23 +04:00
* Copyright ( C ) 2011 Kay Sievers < kay @ vrfy . org >
2011-08-05 04:00:30 +04: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 , either version 2 of the License , or
* ( at your option ) any later version .
*
* 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 , see < http : //www.gnu.org/licenses/>.
*/
# include <stdlib.h>
# include <stddef.h>
# include <stdio.h>
# include <errno.h>
# include <getopt.h>
# include "udev.h"
2014-07-29 17:47:41 +04:00
static void help ( struct udev * udev ) {
2015-01-05 15:19:55 +03:00
printf ( " %s builtin [--help] COMMAND SYSPATH \n \n "
" Test a built-in command. \n \n "
" -h --help Print this message \n "
" --version Print version of the program \n \n "
" Commands: \n "
, program_invocation_short_name ) ;
2012-01-10 04:34:15 +04:00
udev_builtin_list ( udev ) ;
2011-08-05 04:00:30 +04:00
}
2014-07-29 17:47:41 +04:00
static int adm_builtin ( struct udev * udev , int argc , char * argv [ ] ) {
2012-01-10 04:34:15 +04:00
static const struct option options [ ] = {
{ " help " , no_argument , NULL , ' h ' } ,
{ }
} ;
char * command = NULL ;
char * syspath = NULL ;
char filename [ UTIL_PATH_SIZE ] ;
struct udev_device * dev = NULL ;
enum udev_builtin_cmd cmd ;
2013-12-18 06:48:14 +04:00
int rc = EXIT_SUCCESS , c ;
2012-01-10 04:34:15 +04:00
2013-12-18 06:48:14 +04:00
while ( ( c = getopt_long ( argc , argv , " h " , options , NULL ) ) > = 0 )
switch ( c ) {
2012-01-10 04:34:15 +04:00
case ' h ' :
help ( udev ) ;
goto out ;
}
command = argv [ optind + + ] ;
if ( command = = NULL ) {
fprintf ( stderr , " command missing \n " ) ;
help ( udev ) ;
rc = 2 ;
goto out ;
}
syspath = argv [ optind + + ] ;
if ( syspath = = NULL ) {
2013-12-18 06:48:14 +04:00
fprintf ( stderr , " syspath missing \n " ) ;
2012-01-10 04:34:15 +04:00
rc = 3 ;
goto out ;
}
udev_builtin_init ( udev ) ;
cmd = udev_builtin_lookup ( command ) ;
if ( cmd > = UDEV_BUILTIN_MAX ) {
fprintf ( stderr , " unknown command '%s' \n " , command ) ;
help ( udev ) ;
rc = 5 ;
goto out ;
}
/* add /sys if needed */
2012-04-16 22:27:44 +04:00
if ( ! startswith ( syspath , " /sys " ) )
2013-01-09 22:06:46 +04:00
strscpyl ( filename , sizeof ( filename ) , " /sys " , syspath , NULL ) ;
2012-01-10 04:34:15 +04:00
else
2013-01-09 22:06:46 +04:00
strscpy ( filename , sizeof ( filename ) , syspath ) ;
2012-01-10 04:34:15 +04:00
util_remove_trailing_chars ( filename , ' / ' ) ;
dev = udev_device_new_from_syspath ( udev , filename ) ;
if ( dev = = NULL ) {
fprintf ( stderr , " unable to open device '%s' \n \n " , filename ) ;
rc = 4 ;
goto out ;
}
2012-09-17 01:31:11 +04:00
rc = udev_builtin_run ( dev , cmd , command , true ) ;
if ( rc < 0 ) {
fprintf ( stderr , " error executing '%s', exit code %i \n \n " , command , rc ) ;
2012-01-10 04:34:15 +04:00
rc = 6 ;
}
2011-08-05 04:00:30 +04:00
out :
2012-01-10 04:34:15 +04:00
udev_device_unref ( dev ) ;
udev_builtin_exit ( udev ) ;
return rc ;
2011-08-05 04:00:30 +04:00
}
const struct udevadm_cmd udevadm_test_builtin = {
2012-01-10 04:34:15 +04:00
. name = " test-builtin " ,
. cmd = adm_builtin ,
2015-01-05 15:19:55 +03:00
. help = " Test a built-in command " ,
2012-01-10 04:34:15 +04:00
. debug = true ,
2011-08-05 04:00:30 +04:00
} ;