2008-08-27 19:11:58 +04:00
/*
* test - libudev
*
* Copyright ( C ) 2008 Kay Sievers < kay . sievers @ vrfy . org >
*
2009-03-26 21:29:36 +03:00
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
2008-08-27 19:11:58 +04:00
*/
# include <stdio.h>
# include <stdarg.h>
2008-10-01 11:34:07 +04:00
# include <stdlib.h>
2008-08-29 01:05:01 +04:00
# include <unistd.h>
# include <errno.h>
# include <string.h>
2008-09-12 02:58:40 +04:00
# include <getopt.h>
# include <syslog.h>
2008-09-22 10:28:56 +04:00
# include <fcntl.h>
2008-08-29 01:05:01 +04:00
# include <sys/select.h>
2008-08-27 19:11:58 +04:00
# include "libudev.h"
static void log_fn ( struct udev * udev ,
int priority , const char * file , int line , const char * fn ,
const char * format , va_list args )
{
printf ( " test-libudev: %s %s:%d " , fn , file , line ) ;
vprintf ( format , args ) ;
}
2008-08-29 01:05:01 +04:00
static void print_device ( struct udev_device * device )
{
const char * str ;
2008-09-26 01:26:22 +04:00
dev_t devnum ;
2008-08-29 01:05:01 +04:00
int count ;
2008-09-28 03:34:55 +04:00
struct udev_list_entry * list_entry ;
2008-08-29 01:05:01 +04:00
printf ( " *** device: %p *** \n " , device ) ;
2008-09-16 05:01:15 +04:00
str = udev_device_get_action ( device ) ;
2008-09-28 03:34:55 +04:00
if ( str ! = NULL )
printf ( " action: '%s' \n " , str ) ;
2008-09-16 04:12:47 +04:00
str = udev_device_get_syspath ( device ) ;
printf ( " syspath: '%s' \n " , str ) ;
2008-09-28 03:34:55 +04:00
2008-10-17 15:54:14 +04:00
str = udev_device_get_sysname ( device ) ;
printf ( " sysname: '%s' \n " , str ) ;
str = udev_device_get_sysnum ( device ) ;
if ( str ! = NULL )
printf ( " sysnum: '%s' \n " , str ) ;
2008-08-29 01:05:01 +04:00
str = udev_device_get_devpath ( device ) ;
printf ( " devpath: '%s' \n " , str ) ;
2008-09-28 03:34:55 +04:00
2008-08-29 01:05:01 +04:00
str = udev_device_get_subsystem ( device ) ;
2008-09-28 03:34:55 +04:00
if ( str ! = NULL )
printf ( " subsystem: '%s' \n " , str ) ;
2009-01-02 06:14:47 +03:00
str = udev_device_get_devtype ( device ) ;
if ( str ! = NULL )
printf ( " devtype: '%s' \n " , str ) ;
2008-09-09 20:14:54 +04:00
str = udev_device_get_driver ( device ) ;
2008-09-28 03:34:55 +04:00
if ( str ! = NULL )
printf ( " driver: '%s' \n " , str ) ;
2008-09-20 11:01:20 +04:00
str = udev_device_get_devnode ( device ) ;
2008-09-28 03:34:55 +04:00
if ( str ! = NULL )
printf ( " devname: '%s' \n " , str ) ;
2008-09-26 01:26:22 +04:00
devnum = udev_device_get_devnum ( device ) ;
2008-09-28 03:34:55 +04:00
if ( major ( devnum ) > 0 )
printf ( " devnum: %u:%u \n " , major ( devnum ) , minor ( devnum ) ) ;
2008-09-25 15:20:27 +04:00
count = 0 ;
2008-10-02 13:54:33 +04:00
udev_list_entry_foreach ( list_entry , udev_device_get_devlinks_list_entry ( device ) ) {
2008-09-28 03:34:55 +04:00
printf ( " link: '%s' \n " , udev_list_entry_get_name ( list_entry ) ) ;
2008-09-25 15:20:27 +04:00
count + + ;
}
2008-09-28 03:34:55 +04:00
if ( count > 0 )
printf ( " found %i links \n " , count ) ;
2008-09-25 15:20:27 +04:00
count = 0 ;
2008-10-02 13:54:33 +04:00
udev_list_entry_foreach ( list_entry , udev_device_get_properties_list_entry ( device ) ) {
2008-09-28 03:34:55 +04:00
printf ( " property: '%s=%s' \n " ,
udev_list_entry_get_name ( list_entry ) ,
udev_list_entry_get_value ( list_entry ) ) ;
2008-09-25 15:20:27 +04:00
count + + ;
}
2008-09-28 03:34:55 +04:00
if ( count > 0 )
printf ( " found %i properties \n " , count ) ;
2008-09-25 15:20:27 +04:00
2009-01-06 18:52:40 +03:00
str = udev_device_get_property_value ( device , " MAJOR " ) ;
if ( str ! = NULL )
printf ( " MAJOR: '%s' \n " , str ) ;
2008-10-20 20:12:36 +04:00
str = udev_device_get_sysattr_value ( device , " dev " ) ;
2008-09-28 03:34:55 +04:00
if ( str ! = NULL )
printf ( " attr{dev}: '%s' \n " , str ) ;
2008-09-25 15:20:27 +04:00
2008-08-29 01:05:01 +04:00
printf ( " \n " ) ;
}
2008-09-16 04:12:47 +04:00
static int test_device ( struct udev * udev , const char * syspath )
2008-08-29 01:05:01 +04:00
{
struct udev_device * device ;
2008-09-16 04:12:47 +04:00
printf ( " looking at device: %s \n " , syspath ) ;
device = udev_device_new_from_syspath ( udev , syspath ) ;
2008-08-29 01:05:01 +04:00
if ( device = = NULL ) {
2008-10-03 12:33:15 +04:00
printf ( " no device found \n " ) ;
2008-08-29 01:05:01 +04:00
return - 1 ;
}
print_device ( device ) ;
udev_device_unref ( device ) ;
2008-08-27 19:11:58 +04:00
return 0 ;
}
2008-09-16 04:12:47 +04:00
static int test_device_parents ( struct udev * udev , const char * syspath )
2008-09-11 19:08:12 +04:00
{
struct udev_device * device ;
2008-09-12 02:58:40 +04:00
struct udev_device * device_parent ;
2008-09-11 19:08:12 +04:00
2008-09-16 04:12:47 +04:00
printf ( " looking at device: %s \n " , syspath ) ;
device = udev_device_new_from_syspath ( udev , syspath ) ;
2008-09-12 02:58:40 +04:00
if ( device = = NULL )
return - 1 ;
2008-09-25 15:20:27 +04:00
printf ( " looking at parents \n " ) ;
2008-09-12 02:58:40 +04:00
device_parent = device ;
do {
print_device ( device_parent ) ;
device_parent = udev_device_get_parent ( device_parent ) ;
} while ( device_parent ! = NULL ) ;
2008-09-25 15:20:27 +04:00
printf ( " looking at parents again \n " ) ;
2008-09-12 02:58:40 +04:00
device_parent = device ;
do {
print_device ( device_parent ) ;
device_parent = udev_device_get_parent ( device_parent ) ;
} while ( device_parent ! = NULL ) ;
udev_device_unref ( device ) ;
2008-09-11 19:08:12 +04:00
return 0 ;
}
2008-09-22 10:28:56 +04:00
static int test_device_devnum ( struct udev * udev )
{
dev_t devnum = makedev ( 1 , 3 ) ;
struct udev_device * device ;
printf ( " looking up device: %u:%u \n " , major ( devnum ) , minor ( devnum ) ) ;
device = udev_device_new_from_devnum ( udev , ' c ' , devnum ) ;
if ( device = = NULL )
return - 1 ;
print_device ( device ) ;
udev_device_unref ( device ) ;
return 0 ;
}
2008-10-07 22:20:34 +04:00
static int test_device_subsys_name ( struct udev * udev )
{
struct udev_device * device ;
printf ( " looking up device: 'block':'sda' \n " ) ;
device = udev_device_new_from_subsystem_sysname ( udev , " block " , " sda " ) ;
if ( device = = NULL )
return - 1 ;
print_device ( device ) ;
udev_device_unref ( device ) ;
printf ( " looking up device: 'subsystem':'pci' \n " ) ;
device = udev_device_new_from_subsystem_sysname ( udev , " subsystem " , " pci " ) ;
if ( device = = NULL )
return - 1 ;
print_device ( device ) ;
udev_device_unref ( device ) ;
printf ( " looking up device: 'drivers':'scsi:sd' \n " ) ;
device = udev_device_new_from_subsystem_sysname ( udev , " drivers " , " scsi:sd " ) ;
if ( device = = NULL )
return - 1 ;
print_device ( device ) ;
udev_device_unref ( device ) ;
printf ( " looking up device: 'module':'printk' \n " ) ;
device = udev_device_new_from_subsystem_sysname ( udev , " module " , " printk " ) ;
if ( device = = NULL )
return - 1 ;
print_device ( device ) ;
udev_device_unref ( device ) ;
return 0 ;
}
2008-09-28 19:39:31 +04:00
static int test_enumerate_print_list ( struct udev_enumerate * enumerate )
2008-08-27 19:11:58 +04:00
{
2008-09-28 03:34:55 +04:00
struct udev_list_entry * list_entry ;
2008-09-25 15:20:27 +04:00
int count = 0 ;
2008-08-29 01:05:01 +04:00
2008-09-28 19:39:31 +04:00
udev_list_entry_foreach ( list_entry , udev_enumerate_get_list_entry ( enumerate ) ) {
2008-09-25 15:20:27 +04:00
struct udev_device * device ;
2008-09-28 19:39:31 +04:00
device = udev_device_new_from_syspath ( udev_enumerate_get_udev ( enumerate ) ,
udev_list_entry_get_name ( list_entry ) ) ;
2008-09-25 15:20:27 +04:00
if ( device ! = NULL ) {
2008-10-01 11:34:07 +04:00
printf ( " device: '%s' (%s) \n " ,
2008-09-25 15:20:27 +04:00
udev_device_get_syspath ( device ) ,
2008-10-01 11:34:07 +04:00
udev_device_get_subsystem ( device ) ) ;
2008-09-25 15:20:27 +04:00
udev_device_unref ( device ) ;
count + + ;
}
}
2008-08-29 01:05:01 +04:00
printf ( " found %i devices \n \n " , count ) ;
return count ;
}
2009-04-18 15:23:10 +04:00
static int test_monitor ( struct udev * udev )
2008-08-29 01:05:01 +04:00
{
struct udev_monitor * udev_monitor ;
fd_set readfds ;
int fd ;
2009-04-18 15:23:10 +04:00
udev_monitor = udev_monitor_new_from_netlink ( udev , " udev " ) ;
2008-08-29 01:05:01 +04:00
if ( udev_monitor = = NULL ) {
printf ( " no socket \n " ) ;
return - 1 ;
}
2009-04-23 05:24:38 +04:00
if ( udev_monitor_filter_add_match_subsystem_devtype ( udev_monitor , " block " , NULL ) < 0 | |
udev_monitor_filter_add_match_subsystem_devtype ( udev_monitor , " tty " , NULL ) < 0 | |
udev_monitor_filter_add_match_subsystem_devtype ( udev_monitor , " usb " , " usb_device " ) < 0 ) {
2009-04-22 05:50:11 +04:00
printf ( " filter failed \n " ) ;
return - 1 ;
}
2008-09-08 19:59:00 +04:00
if ( udev_monitor_enable_receiving ( udev_monitor ) < 0 ) {
printf ( " bind failed \n " ) ;
return - 1 ;
}
2008-08-29 01:05:01 +04:00
fd = udev_monitor_get_fd ( udev_monitor ) ;
FD_ZERO ( & readfds ) ;
while ( 1 ) {
struct udev_device * device ;
int fdcount ;
FD_SET ( STDIN_FILENO , & readfds ) ;
FD_SET ( fd , & readfds ) ;
2009-04-18 15:23:10 +04:00
printf ( " waiting for events from udev, press ENTER to exit \n " ) ;
2008-08-29 01:05:01 +04:00
fdcount = select ( fd + 1 , & readfds , NULL , NULL , NULL ) ;
printf ( " select fd count: %i \n " , fdcount ) ;
if ( FD_ISSET ( fd , & readfds ) ) {
2008-09-08 19:59:00 +04:00
device = udev_monitor_receive_device ( udev_monitor ) ;
2008-08-29 01:05:01 +04:00
if ( device = = NULL ) {
printf ( " no device from socket \n " ) ;
continue ;
}
print_device ( device ) ;
udev_device_unref ( device ) ;
}
if ( FD_ISSET ( STDIN_FILENO , & readfds ) ) {
printf ( " exiting loop \n " ) ;
break ;
}
}
udev_monitor_unref ( udev_monitor ) ;
2008-08-27 19:11:58 +04:00
return 0 ;
}
2008-10-01 11:34:07 +04:00
static int test_queue ( struct udev * udev )
{
struct udev_queue * udev_queue ;
unsigned long long int seqnum ;
struct udev_list_entry * list_entry ;
udev_queue = udev_queue_new ( udev ) ;
if ( udev_queue = = NULL )
return - 1 ;
seqnum = udev_queue_get_kernel_seqnum ( udev_queue ) ;
printf ( " seqnum kernel: %llu \n " , seqnum ) ;
seqnum = udev_queue_get_udev_seqnum ( udev_queue ) ;
printf ( " seqnum udev : %llu \n " , seqnum ) ;
if ( udev_queue_get_queue_is_empty ( udev_queue ) )
printf ( " queue is empty \n " ) ;
printf ( " get queue list \n " ) ;
udev_list_entry_foreach ( list_entry , udev_queue_get_queued_list_entry ( udev_queue ) )
printf ( " queued: '%s' [%s] \n " , udev_list_entry_get_name ( list_entry ) , udev_list_entry_get_value ( list_entry ) ) ;
printf ( " \n " ) ;
printf ( " get queue list again \n " ) ;
udev_list_entry_foreach ( list_entry , udev_queue_get_queued_list_entry ( udev_queue ) )
printf ( " queued: '%s' [%s] \n " , udev_list_entry_get_name ( list_entry ) , udev_list_entry_get_value ( list_entry ) ) ;
printf ( " \n " ) ;
printf ( " get failed list \n " ) ;
udev_list_entry_foreach ( list_entry , udev_queue_get_failed_list_entry ( udev_queue ) )
printf ( " failed: '%s' \n " , udev_list_entry_get_name ( list_entry ) ) ;
printf ( " \n " ) ;
list_entry = udev_queue_get_queued_list_entry ( udev_queue ) ;
if ( list_entry ! = NULL ) {
printf ( " event [%llu] is queued \n " , seqnum ) ;
seqnum = strtoull ( udev_list_entry_get_value ( list_entry ) , NULL , 10 ) ;
if ( udev_queue_get_seqnum_is_finished ( udev_queue , seqnum ) )
printf ( " event [%llu] is not finished \n " , seqnum ) ;
else
printf ( " event [%llu] is finished \n " , seqnum ) ;
}
printf ( " \n " ) ;
udev_queue_unref ( udev_queue ) ;
return 0 ;
}
2008-10-07 22:20:34 +04:00
static int test_enumerate ( struct udev * udev , const char * subsystem )
{
struct udev_enumerate * udev_enumerate ;
printf ( " enumerate '%s' \n " , subsystem = = NULL ? " <all> " : subsystem ) ;
udev_enumerate = udev_enumerate_new ( udev ) ;
if ( udev_enumerate = = NULL )
return - 1 ;
udev_enumerate_add_match_subsystem ( udev_enumerate , subsystem ) ;
udev_enumerate_scan_devices ( udev_enumerate ) ;
test_enumerate_print_list ( udev_enumerate ) ;
udev_enumerate_unref ( udev_enumerate ) ;
2009-07-08 04:04:49 +04:00
printf ( " enumerate 'net' + duplicated scan + null + zero \n " ) ;
udev_enumerate = udev_enumerate_new ( udev ) ;
if ( udev_enumerate = = NULL )
return - 1 ;
udev_enumerate_add_match_subsystem ( udev_enumerate , " net " ) ;
udev_enumerate_scan_devices ( udev_enumerate ) ;
udev_enumerate_scan_devices ( udev_enumerate ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/zero " ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/null " ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/zero " ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/null " ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/zero " ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/null " ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/null " ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/zero " ) ;
udev_enumerate_add_syspath ( udev_enumerate , " /sys/class/mem/zero " ) ;
udev_enumerate_scan_devices ( udev_enumerate ) ;
test_enumerate_print_list ( udev_enumerate ) ;
udev_enumerate_unref ( udev_enumerate ) ;
2008-10-07 22:20:34 +04:00
printf ( " enumerate 'block' \n " ) ;
udev_enumerate = udev_enumerate_new ( udev ) ;
if ( udev_enumerate = = NULL )
return - 1 ;
udev_enumerate_add_match_subsystem ( udev_enumerate , " block " ) ;
udev_enumerate_scan_devices ( udev_enumerate ) ;
test_enumerate_print_list ( udev_enumerate ) ;
udev_enumerate_unref ( udev_enumerate ) ;
printf ( " enumerate 'not block' \n " ) ;
udev_enumerate = udev_enumerate_new ( udev ) ;
if ( udev_enumerate = = NULL )
return - 1 ;
udev_enumerate_add_nomatch_subsystem ( udev_enumerate , " block " ) ;
udev_enumerate_scan_devices ( udev_enumerate ) ;
test_enumerate_print_list ( udev_enumerate ) ;
udev_enumerate_unref ( udev_enumerate ) ;
printf ( " enumerate 'pci, mem, vc' \n " ) ;
udev_enumerate = udev_enumerate_new ( udev ) ;
if ( udev_enumerate = = NULL )
return - 1 ;
udev_enumerate_add_match_subsystem ( udev_enumerate , " pci " ) ;
udev_enumerate_add_match_subsystem ( udev_enumerate , " mem " ) ;
udev_enumerate_add_match_subsystem ( udev_enumerate , " vc " ) ;
udev_enumerate_scan_devices ( udev_enumerate ) ;
test_enumerate_print_list ( udev_enumerate ) ;
udev_enumerate_unref ( udev_enumerate ) ;
printf ( " enumerate 'subsystem' \n " ) ;
udev_enumerate = udev_enumerate_new ( udev ) ;
if ( udev_enumerate = = NULL )
return - 1 ;
udev_enumerate_scan_subsystems ( udev_enumerate ) ;
test_enumerate_print_list ( udev_enumerate ) ;
udev_enumerate_unref ( udev_enumerate ) ;
2008-12-29 09:42:19 +03:00
printf ( " enumerate 'property IF_FS_*=filesystem' \n " ) ;
udev_enumerate = udev_enumerate_new ( udev ) ;
if ( udev_enumerate = = NULL )
return - 1 ;
udev_enumerate_add_match_property ( udev_enumerate , " ID_FS* " , " filesystem " ) ;
udev_enumerate_scan_devices ( udev_enumerate ) ;
test_enumerate_print_list ( udev_enumerate ) ;
udev_enumerate_unref ( udev_enumerate ) ;
2008-10-07 22:20:34 +04:00
return 0 ;
}
int main ( int argc , char * argv [ ] )
2008-08-27 19:11:58 +04:00
{
2008-09-12 02:58:40 +04:00
struct udev * udev = NULL ;
static const struct option options [ ] = {
2008-10-02 18:49:05 +04:00
{ " syspath " , required_argument , NULL , ' p ' } ,
{ " subsystem " , required_argument , NULL , ' s ' } ,
{ " debug " , no_argument , NULL , ' d ' } ,
{ " help " , no_argument , NULL , ' h ' } ,
{ " version " , no_argument , NULL , ' V ' } ,
2008-09-12 02:58:40 +04:00
{ }
} ;
2008-09-16 04:12:47 +04:00
const char * syspath = " /devices/virtual/mem/null " ;
2008-08-27 19:11:58 +04:00
const char * subsystem = NULL ;
2008-09-16 04:12:47 +04:00
char path [ 1024 ] ;
2008-08-29 01:05:01 +04:00
const char * str ;
2008-08-27 19:11:58 +04:00
udev = udev_new ( ) ;
printf ( " context: %p \n " , udev ) ;
if ( udev = = NULL ) {
printf ( " no context \n " ) ;
return 1 ;
}
udev_set_log_fn ( udev , log_fn ) ;
printf ( " set log: %p \n " , log_fn ) ;
2008-09-12 02:58:40 +04:00
while ( 1 ) {
int option ;
option = getopt_long ( argc , argv , " +dhV " , options , NULL ) ;
if ( option = = - 1 )
break ;
switch ( option ) {
case ' p ' :
2008-09-16 04:12:47 +04:00
syspath = optarg ;
2008-09-12 02:58:40 +04:00
break ;
case ' s ' :
subsystem = optarg ;
break ;
case ' d ' :
if ( udev_get_log_priority ( udev ) < LOG_INFO )
udev_set_log_priority ( udev , LOG_INFO ) ;
break ;
case ' h ' :
2009-04-18 15:23:10 +04:00
printf ( " --debug --syspath= --subsystem= --help \n " ) ;
2008-09-12 02:58:40 +04:00
goto out ;
case ' V ' :
printf ( " %s \n " , VERSION ) ;
goto out ;
default :
goto out ;
}
}
2008-08-27 19:11:58 +04:00
str = udev_get_sys_path ( udev ) ;
2008-08-29 01:05:01 +04:00
printf ( " sys_path: '%s' \n " , str ) ;
2008-08-27 19:11:58 +04:00
str = udev_get_dev_path ( udev ) ;
2008-08-29 01:05:01 +04:00
printf ( " dev_path: '%s' \n " , str ) ;
2008-08-27 19:11:58 +04:00
2008-09-16 04:12:47 +04:00
/* add sys path if needed */
if ( strncmp ( syspath , udev_get_sys_path ( udev ) , strlen ( udev_get_sys_path ( udev ) ) ) ! = 0 ) {
snprintf ( path , sizeof ( path ) , " %s%s " , udev_get_sys_path ( udev ) , syspath ) ;
syspath = path ;
}
test_device ( udev , syspath ) ;
2008-09-22 10:28:56 +04:00
test_device_devnum ( udev ) ;
2008-10-07 22:20:34 +04:00
test_device_subsys_name ( udev ) ;
2008-09-16 04:12:47 +04:00
test_device_parents ( udev , syspath ) ;
2008-09-28 19:39:31 +04:00
2008-10-07 22:20:34 +04:00
test_enumerate ( udev , subsystem ) ;
2008-09-29 00:18:40 +04:00
2008-10-01 11:34:07 +04:00
test_queue ( udev ) ;
2009-04-18 15:23:10 +04:00
test_monitor ( udev ) ;
2008-09-12 02:58:40 +04:00
out :
2008-08-27 19:11:58 +04:00
udev_unref ( udev ) ;
return 0 ;
}