2004-01-20 06:46:26 +03:00
/*
2005-08-29 17:01:08 +04:00
* udevinfo . c - fetches stored device information or sysfs attributes
2004-01-20 06:46:26 +03:00
*
2005-08-29 17:01:08 +04:00
* Copyright ( C ) 2004 - 2005 Kay Sievers < kay . sievers @ vrfy . org >
2004-01-20 06:46:26 +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>
2004-01-23 14:01:02 +03:00
# include <ctype.h>
2004-01-27 05:20:12 +03:00
# include <stdarg.h>
# include <unistd.h>
2006-01-09 23:18:00 +03:00
# include <dirent.h>
2004-01-27 05:20:12 +03:00
# include <errno.h>
2004-01-20 06:46:26 +03:00
2004-01-27 05:55:37 +03:00
# include "udev.h"
2004-01-20 06:46:26 +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-12 09:10:26 +03:00
{
2004-02-13 05:57:06 +03:00
va_list args ;
2005-03-27 03:11:03 +04:00
if ( priority > udev_log_priority )
return ;
2004-02-13 05:57:06 +03:00
va_start ( args , format ) ;
2005-03-27 03:11:03 +04:00
vsyslog ( priority , format , args ) ;
2004-02-13 05:57:06 +03:00
va_end ( args ) ;
2004-02-12 09:10:26 +03:00
}
2004-02-13 05:57:06 +03:00
# endif
2004-02-12 09:10:26 +03:00
2006-01-09 23:18:00 +03:00
static void print_all_attributes ( const char * devpath )
2004-01-20 06:46:26 +03:00
{
2006-01-09 23:18:00 +03:00
char path [ PATH_SIZE ] ;
DIR * dir ;
struct dirent * dent ;
strlcpy ( path , sysfs_path , sizeof ( path ) ) ;
strlcat ( path , devpath , sizeof ( path ) ) ;
dir = opendir ( path ) ;
if ( dir ! = NULL ) {
for ( dent = readdir ( dir ) ; dent ! = NULL ; dent = readdir ( dir ) ) {
char * attr_value ;
char value [ NAME_SIZE ] ;
size_t len ;
attr_value = sysfs_attr_get_value ( devpath , dent - > d_name ) ;
if ( attr_value = = NULL )
continue ;
len = strlcpy ( value , attr_value , sizeof ( value ) ) ;
dbg ( " attr '%s'='%s'(%zi) " , dent - > d_name , value , len ) ;
/* remove trailing newlines */
while ( len & & value [ len - 1 ] = = ' \n ' )
value [ - - len ] = ' \0 ' ;
/* skip nonprintable attributes */
while ( len & & isprint ( value [ len - 1 ] ) )
len - - ;
if ( len ) {
dbg ( " attribute value of '%s' non-printable, skip " , dent - > d_name ) ;
continue ;
}
2005-03-27 03:29:10 +04:00
2006-01-09 23:18:00 +03:00
replace_untrusted_chars ( value ) ;
printf ( " SYSFS{%s}== \" %s \" \n " , dent - > d_name , value ) ;
2005-03-27 03:29:10 +04:00
}
2004-01-20 06:46:26 +03:00
}
printf ( " \n " ) ;
}
2006-01-09 23:18:00 +03:00
static int print_device_chain ( const char * devpath )
2004-01-27 05:20:12 +03:00
{
2006-01-09 23:18:00 +03:00
struct sysfs_device * dev ;
2005-03-05 07:35:31 +03:00
2006-01-09 23:18:00 +03:00
printf ( " \n "
2006-06-16 13:11:07 +04:00
" Udevinfo starts with the device specified by the devpath and then \n "
" walks up the chain of parent devices. It prints for every device \n "
" found, all possible attributes in the udev rules key format. \n "
" A rule to match, can be composed by the attributes of the device \n "
" and the attributes from one single parent device. \n "
2004-03-02 11:08:28 +03:00
" \n " ) ;
[PATCH] hmm, handle net devices with udev?
Hmm, Arndt Bergmann sent a patch like this one a few weeks ago and
I want to bring the question back, if we want to handle net device
naming with udev.
With this patch it is actually possible to specify something like this
in udev.rules:
KERNEL="dummy*", SYSFS{address}="00:00:00:00:00:00", SYSFS{features}="0x0", NAME="blind%n"
KERNEL="eth*", SYSFS{address}="00:0d:60:77:30:91", NAME="private"
and you will get:
[root@pim udev.kay]# cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo: 1500 30 0 0 0 0 0 0 1500 30 0 0 0 0 0 0
private: 278393 1114 0 0 0 0 0 0 153204 1468 0 0 0 0 0 0
sit0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
blind0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The udevinfo program is also working:
[root@pim udev.kay]# ./udevinfo -a -p /sys/class/net/private
looking at class device '/sys/class/net/private':
SYSFS{addr_len}="6"
SYSFS{address}="00:0d:60:77:30:91"
SYSFS{broadcast}="ff:ff:ff:ff:ff:ff"
SYSFS{features}="0x3a9"
SYSFS{flags}="0x1003"
SYSFS{ifindex}="2"
SYSFS{iflink}="2"
SYSFS{mtu}="1500"
SYSFS{tx_queue_len}="1000"
SYSFS{type}="1"
follow the class device's "device"
looking at the device chain at '/sys/devices/pci0000:00/0000:00:1e.0/0000:02:01.0':
BUS="pci"
ID="0000:02:01.0"
SYSFS{class}="0x020000"
SYSFS{detach_state}="0"
SYSFS{device}="0x101e"
SYSFS{irq}="11"
SYSFS{subsystem_device}="0x0549"
SYSFS{subsystem_vendor}="0x1014"
SYSFS{vendor}="0x8086"
The matching device will be renamed to the given name. The device name
will not be put into the udev database, cause the kernel renames the
device and the sysfs name disappears.
I like it, cause it plugs in nicely. We have all the naming features
and sysfs queries and walks inside of udev. The sysfs timing races
are already solved and the management tools are working for net devices
too. nameif can only match the MAC address now. udev can match any sysfs
value of the device tree the net device is connected to.
But right, net devices do not have device nodes :)
2004-03-25 10:19:39 +03:00
2006-01-09 23:18:00 +03:00
dev = sysfs_device_get ( devpath ) ;
if ( dev = = NULL )
return - 1 ;
2005-02-23 14:21:39 +03:00
2006-01-09 23:18:00 +03:00
printf ( " looking at device '%s': \n " , dev - > devpath ) ;
printf ( " KERNEL== \" %s \" \n " , dev - > kernel_name ) ;
printf ( " SUBSYSTEM== \" %s \" \n " , dev - > subsystem ) ;
2006-07-30 17:54:28 +04:00
printf ( " DRIVER== \" %s \" \n " , dev - > driver ) ;
2006-01-09 23:18:00 +03:00
print_all_attributes ( dev - > devpath ) ;
2004-01-23 14:01:02 +03:00
2006-01-09 23:18:00 +03:00
/* walk up the chain of devices */
while ( 1 ) {
dev = sysfs_device_get_parent ( dev ) ;
if ( dev = = NULL )
2004-01-23 14:01:02 +03:00
break ;
2006-06-16 13:11:07 +04:00
printf ( " looking at parent device '%s': \n " , dev - > devpath ) ;
2006-01-09 23:18:00 +03:00
printf ( " ID== \" %s \" \n " , dev - > kernel_name ) ;
printf ( " BUS== \" %s \" \n " , dev - > subsystem ) ;
printf ( " DRIVER== \" %s \" \n " , dev - > driver ) ;
print_all_attributes ( dev - > devpath ) ;
2004-01-20 06:46:26 +03:00
}
2006-01-09 23:18:00 +03:00
return 0 ;
}
static void print_record ( struct udevice * udev )
{
struct name_entry * name_loop ;
printf ( " P: %s \n " , udev - > dev - > devpath ) ;
printf ( " N: %s \n " , udev - > name ) ;
list_for_each_entry ( name_loop , & udev - > symlink_list , node )
printf ( " S: %s \n " , name_loop - > name ) ;
list_for_each_entry ( name_loop , & udev - > env_list , node )
printf ( " E: %s \n " , name_loop - > name ) ;
2004-01-20 06:46:26 +03:00
}
2004-01-27 05:20:12 +03:00
2005-08-30 06:56:24 +04:00
static void export_name_devpath ( struct udevice * udev ) {
2006-01-09 23:18:00 +03:00
printf ( " %s=%s/%s \n " , udev - > dev - > devpath , udev_root , udev - > name ) ;
2005-08-29 17:01:08 +04:00
}
2005-08-30 06:56:24 +04:00
static void export_record ( struct udevice * udev ) {
2005-08-29 17:01:08 +04:00
print_record ( udev ) ;
printf ( " \n " ) ;
}
2005-08-30 06:56:24 +04:00
static void export_db ( void fnct ( struct udevice * udev ) ) {
2005-08-27 04:59:20 +04:00
LIST_HEAD ( name_list ) ;
struct name_entry * name_loop ;
udev_db_get_all_entries ( & name_list ) ;
2005-08-27 18:15:41 +04:00
list_for_each_entry ( name_loop , & name_list , node ) {
2006-01-09 23:18:00 +03:00
struct udevice * udev_db ;
2005-08-27 04:59:20 +04:00
2006-01-09 23:18:00 +03:00
udev_db = udev_device_init ( ) ;
if ( udev_db = = NULL )
continue ;
if ( udev_db_get_device ( udev_db , name_loop - > name ) = = 0 )
fnct ( udev_db ) ;
udev_device_cleanup ( udev_db ) ;
2005-08-27 04:59:20 +04:00
}
2005-08-27 18:15:41 +04:00
name_list_cleanup ( & name_list ) ;
2005-02-25 09:40:14 +03:00
}
2005-08-29 17:01:08 +04:00
static void print_help ( void )
{
fprintf ( stderr , " Usage: udevinfo [-anpqrVh] \n "
" -q TYPE query database for the specified value: \n "
" 'name' name of device node \n "
" 'symlink' pointing to node \n "
" 'path' sysfs device path \n "
" 'env' the device related imported environment \n "
" 'all' all values \n "
" \n "
" -p PATH sysfs device path used for query or chain \n "
" -n NAME node/symlink name used for query \n "
" \n "
2005-08-30 06:56:24 +04:00
" -r prepend to query result or print udev_root \n "
2005-08-29 17:01:08 +04:00
" -a print all SYSFS_attributes along the device chain \n "
2005-08-30 06:56:24 +04:00
" -e export the content of the udev database \n "
2005-08-29 17:01:08 +04:00
" -V print udev version \n "
" -h print this help text \n "
" \n " ) ;
}
2005-03-05 07:35:31 +03:00
int main ( int argc , char * argv [ ] , char * envp [ ] )
2004-01-27 05:20:12 +03:00
{
2005-08-31 04:35:35 +04:00
static const char short_options [ ] = " aden:p:q:rVh " ;
2004-01-27 05:20:12 +03:00
int option ;
2006-01-09 23:18:00 +03:00
struct udevice * udev ;
2004-01-27 05:20:12 +03:00
int root = 0 ;
2005-08-29 17:01:08 +04:00
enum action_type {
ACTION_NONE ,
ACTION_QUERY ,
ACTION_ATTRIBUTE_WALK ,
ACTION_ROOT ,
} action = ACTION_NONE ;
2005-06-26 20:55:24 +04:00
enum query_type {
QUERY_NONE ,
QUERY_NAME ,
QUERY_PATH ,
QUERY_SYMLINK ,
QUERY_ENV ,
QUERY_ALL ,
} query = QUERY_NONE ;
2005-08-29 17:01:08 +04:00
2005-03-07 06:29:43 +03:00
char path [ PATH_SIZE ] = " " ;
char name [ PATH_SIZE ] = " " ;
2005-03-05 07:35:31 +03:00
struct name_entry * name_loop ;
2006-01-09 23:18:00 +03:00
int rc = 0 ;
2005-03-05 07:35:31 +03:00
logging_init ( " udevinfo " ) ;
2006-01-09 23:18:00 +03:00
udev_config_init ( ) ;
sysfs_init ( ) ;
udev = udev_device_init ( ) ;
if ( udev = = NULL ) {
rc = 1 ;
goto exit ;
}
2004-01-27 05:20:12 +03:00
/* get command line options */
while ( 1 ) {
2004-11-12 08:20:22 +03:00
option = getopt ( argc , argv , short_options ) ;
2004-01-27 05:20:12 +03:00
if ( option = = - 1 )
break ;
dbg ( " option '%c' " , option ) ;
switch ( option ) {
case ' n ' :
2006-08-18 04:33:46 +04:00
/* remove /dev if given */
if ( strncmp ( optarg , udev_root , strlen ( udev_root ) ) = = 0 )
strlcpy ( name , & optarg [ strlen ( udev_root ) + 1 ] , sizeof ( name ) ) ;
else
strlcpy ( name , optarg , sizeof ( name ) ) ;
dbg ( " name: %s \n " , name ) ;
2004-01-27 05:20:12 +03:00
break ;
case ' p ' :
2006-08-18 04:33:46 +04:00
/* remove /sys if given */
2006-01-09 23:18:00 +03:00
if ( strncmp ( optarg , sysfs_path , strlen ( sysfs_path ) ) = = 0 )
strlcpy ( path , & optarg [ strlen ( sysfs_path ) ] , sizeof ( path ) ) ;
else
strlcpy ( path , optarg , sizeof ( path ) ) ;
2006-08-18 04:33:46 +04:00
dbg ( " path: %s \n " , path ) ;
2004-01-27 05:20:12 +03:00
break ;
case ' q ' :
dbg ( " udev query: %s \n " , optarg ) ;
2005-08-29 17:01:08 +04:00
action = ACTION_QUERY ;
2004-01-27 05:20:12 +03:00
if ( strcmp ( optarg , " name " ) = = 0 ) {
2005-06-26 20:55:24 +04:00
query = QUERY_NAME ;
2004-01-27 05:20:12 +03:00
break ;
}
if ( strcmp ( optarg , " symlink " ) = = 0 ) {
2005-06-26 20:55:24 +04:00
query = QUERY_SYMLINK ;
2004-01-27 05:20:12 +03:00
break ;
}
if ( strcmp ( optarg , " path " ) = = 0 ) {
2005-06-26 20:55:24 +04:00
query = QUERY_PATH ;
break ;
}
if ( strcmp ( optarg , " env " ) = = 0 ) {
query = QUERY_ENV ;
2004-01-27 05:20:12 +03:00
break ;
}
2004-03-02 10:47:59 +03:00
if ( strcmp ( optarg , " all " ) = = 0 ) {
2005-06-26 20:55:24 +04:00
query = QUERY_ALL ;
2004-03-02 10:47:59 +03:00
break ;
}
2005-03-27 03:12:55 +04:00
fprintf ( stderr , " unknown query type \n " ) ;
2006-01-09 23:18:00 +03:00
rc = 2 ;
2005-03-05 07:35:31 +03:00
goto exit ;
2004-01-27 05:20:12 +03:00
case ' r ' :
2005-08-29 17:01:08 +04:00
if ( action = = ACTION_NONE )
action = ACTION_ROOT ;
2004-01-27 05:20:12 +03:00
root = 1 ;
break ;
case ' a ' :
2005-08-29 17:01:08 +04:00
action = ACTION_ATTRIBUTE_WALK ;
2004-01-27 05:20:12 +03:00
break ;
2005-02-25 09:40:14 +03:00
case ' d ' :
2005-08-30 06:56:24 +04:00
export_db ( export_name_devpath ) ;
2005-08-29 17:01:08 +04:00
goto exit ;
case ' e ' :
2005-08-30 06:56:24 +04:00
export_db ( export_record ) ;
2005-03-05 07:35:31 +03:00
goto exit ;
2004-01-27 05:20:12 +03:00
case ' V ' :
2004-01-27 05:55:37 +03:00
printf ( " udevinfo, version %s \n " , UDEV_VERSION ) ;
2005-03-05 07:35:31 +03:00
goto exit ;
2004-01-27 05:20:12 +03:00
case ' h ' :
case ' ? ' :
default :
2005-08-29 17:01:08 +04:00
print_help ( ) ;
goto exit ;
2004-01-27 05:20:12 +03:00
}
}
2005-08-29 17:01:08 +04:00
/* run action */
switch ( action ) {
case ACTION_QUERY :
2006-08-18 04:33:46 +04:00
/* needs devpath or node/symlink name for query */
2004-01-27 05:20:12 +03:00
if ( path [ 0 ] ! = ' \0 ' ) {
2006-08-18 04:33:46 +04:00
if ( udev_db_get_device ( udev , path ) ! = 0 ) {
fprintf ( stderr , " no record for '%s' in database \n " , path ) ;
2006-01-09 23:18:00 +03:00
rc = 3 ;
2004-01-27 05:20:12 +03:00
goto exit ;
}
2005-08-29 17:01:08 +04:00
} else if ( name [ 0 ] ! = ' \0 ' ) {
2005-03-07 06:29:43 +03:00
char devpath [ PATH_SIZE ] ;
2004-11-06 16:28:01 +03:00
2006-08-18 04:33:46 +04:00
if ( udev_db_lookup_name ( name , devpath , sizeof ( devpath ) ) ! = 0 ) {
fprintf ( stderr , " node name not found \n " ) ;
rc = 4 ;
2004-01-27 05:20:12 +03:00
goto exit ;
}
2006-01-09 23:18:00 +03:00
udev_db_get_device ( udev , devpath ) ;
2005-08-29 17:01:08 +04:00
} else {
fprintf ( stderr , " query needs device path(-p) or node name(-n) specified \n " ) ;
2006-01-09 23:18:00 +03:00
rc = 4 ;
2005-08-29 17:01:08 +04:00
goto exit ;
2004-01-27 05:20:12 +03:00
}
switch ( query ) {
2005-06-26 20:55:24 +04:00
case QUERY_NAME :
2005-03-05 07:35:31 +03:00
if ( root )
2006-01-09 23:18:00 +03:00
printf ( " %s/%s \n " , udev_root , udev - > name ) ;
2005-03-05 07:35:31 +03:00
else
2006-01-09 23:18:00 +03:00
printf ( " %s \n " , udev - > name ) ;
2005-08-29 17:01:08 +04:00
break ;
2005-06-26 20:55:24 +04:00
case QUERY_SYMLINK :
2006-01-09 23:18:00 +03:00
if ( list_empty ( & udev - > symlink_list ) )
2005-08-10 19:06:01 +04:00
goto exit ;
2005-03-05 07:35:31 +03:00
if ( root )
2006-01-09 23:18:00 +03:00
list_for_each_entry ( name_loop , & udev - > symlink_list , node )
2005-03-05 07:35:31 +03:00
printf ( " %s/%s " , udev_root , name_loop - > name ) ;
else
2006-01-09 23:18:00 +03:00
list_for_each_entry ( name_loop , & udev - > symlink_list , node )
2005-03-05 07:35:31 +03:00
printf ( " %s " , name_loop - > name ) ;
printf ( " \n " ) ;
2005-08-29 17:01:08 +04:00
break ;
2005-06-26 20:55:24 +04:00
case QUERY_PATH :
2006-01-09 23:18:00 +03:00
printf ( " %s \n " , udev - > dev - > devpath ) ;
2005-03-05 07:35:31 +03:00
goto exit ;
2005-06-26 20:55:24 +04:00
case QUERY_ENV :
2006-01-09 23:18:00 +03:00
list_for_each_entry ( name_loop , & udev - > env_list , node )
2005-06-26 20:55:24 +04:00
printf ( " %s \n " , name_loop - > name ) ;
2005-08-29 17:01:08 +04:00
break ;
2005-06-26 20:55:24 +04:00
case QUERY_ALL :
2006-01-09 23:18:00 +03:00
print_record ( udev ) ;
2005-08-29 17:01:08 +04:00
break ;
2004-01-27 05:20:12 +03:00
default :
2005-08-29 17:01:08 +04:00
print_help ( ) ;
break ;
2004-01-27 05:20:12 +03:00
}
2005-08-29 17:01:08 +04:00
break ;
case ACTION_ATTRIBUTE_WALK :
2006-08-18 04:31:37 +04:00
if ( path [ 0 ] ! = ' \0 ' ) {
2004-01-27 05:20:12 +03:00
print_device_chain ( path ) ;
2006-08-18 04:31:37 +04:00
} else if ( name [ 0 ] ! = ' \0 ' ) {
char devpath [ PATH_SIZE ] ;
if ( udev_db_lookup_name ( name , devpath , sizeof ( devpath ) ) ! = 0 ) {
fprintf ( stderr , " node name not found \n " ) ;
rc = 4 ;
goto exit ;
}
print_device_chain ( devpath ) ;
} else {
fprintf ( stderr , " attribute walk needs device path(-p) or node name(-n) specified \n " ) ;
rc = 5 ;
goto exit ;
}
2005-08-29 17:01:08 +04:00
break ;
case ACTION_ROOT :
2004-01-27 05:20:12 +03:00
printf ( " %s \n " , udev_root ) ;
2005-08-29 17:01:08 +04:00
break ;
default :
print_help ( ) ;
2006-01-09 23:18:00 +03:00
rc = 1 ;
2005-08-29 17:01:08 +04:00
break ;
2004-01-27 05:20:12 +03:00
}
2005-03-05 07:35:31 +03:00
exit :
2006-01-09 23:18:00 +03:00
udev_device_cleanup ( udev ) ;
sysfs_cleanup ( ) ;
2004-10-19 05:15:10 +04:00
logging_close ( ) ;
2006-01-09 23:18:00 +03:00
return rc ;
2004-01-27 05:20:12 +03:00
}