2005-08-01 22:22:46 +04:00
/*
2008-09-10 04:40:42 +04:00
* Copyright ( C ) 2004 - 2008 Kay Sievers < kay . sievers @ vrfy . org >
2005-08-01 22:22:46 +04:00
*
2008-09-10 04:40:42 +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 .
2005-08-01 22:22:46 +04:00
*
2008-09-10 04:40:42 +04:00
* 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/>.
2005-08-01 22:22:46 +04:00
*/
# include <unistd.h>
# include <stdio.h>
# include <stdlib.h>
2006-05-01 22:36:21 +04:00
# include <stddef.h>
2005-08-01 22:22:46 +04:00
# include <string.h>
# include <fcntl.h>
# include <errno.h>
2005-08-25 02:38:25 +04:00
# include <signal.h>
2007-03-10 17:12:42 +03:00
# include <getopt.h>
2005-11-07 04:23:06 +03:00
# include <sys/time.h>
2005-08-01 22:22:46 +04:00
# include <sys/socket.h>
# include <sys/un.h>
# include <sys/select.h>
2005-11-12 07:09:34 +03:00
# include <linux/types.h>
2005-08-01 22:22:46 +04:00
# include <linux/netlink.h>
# include "udev.h"
2008-09-09 16:07:08 +04:00
static int udev_exit ;
2005-08-01 22:22:46 +04:00
2005-08-25 02:38:25 +04:00
static void asmlinkage sig_handler ( int signum )
{
if ( signum = = SIGINT | | signum = = SIGTERM )
udev_exit = 1 ;
}
2008-09-28 03:34:55 +04:00
static void print_device ( struct udev_device * device , const char * source , int env )
{
struct timeval tv ;
struct timezone tz ;
gettimeofday ( & tv , & tz ) ;
printf ( " %-6s[%llu.%06u] %-8s %s (%s) \n " ,
source ,
( unsigned long long ) tv . tv_sec , ( unsigned int ) tv . tv_usec ,
udev_device_get_action ( device ) ,
udev_device_get_devpath ( device ) ,
udev_device_get_subsystem ( device ) ) ;
if ( env ) {
struct udev_list_entry * list_entry ;
udev_list_entry_foreach ( list_entry , udev_device_get_properties_list_entry ( device ) )
printf ( " %s=%s \n " ,
udev_list_entry_get_name ( list_entry ) ,
udev_list_entry_get_value ( list_entry ) ) ;
printf ( " \n " ) ;
}
}
2008-09-06 17:45:31 +04:00
int udevadm_monitor ( struct udev * udev , int argc , char * argv [ ] )
2005-08-01 22:22:46 +04:00
{
2005-08-25 02:38:25 +04:00
struct sigaction act ;
2007-03-10 17:12:42 +03:00
int option ;
2005-08-01 22:22:46 +04:00
int env = 0 ;
2008-09-06 17:45:31 +04:00
int print_kernel = 0 ;
int print_udev = 0 ;
2009-04-22 07:00:15 +04:00
struct udev_list_node subsystem_match_list ;
2008-09-09 16:07:08 +04:00
struct udev_monitor * udev_monitor = NULL ;
2008-09-09 19:41:17 +04:00
struct udev_monitor * kernel_monitor = NULL ;
2005-08-01 22:22:46 +04:00
fd_set readfds ;
2008-09-09 16:07:08 +04:00
int rc = 0 ;
2005-08-01 22:22:46 +04:00
2007-03-10 17:12:42 +03:00
static const struct option options [ ] = {
2008-10-02 18:49:05 +04:00
{ " environment " , no_argument , NULL , ' e ' } ,
{ " kernel " , no_argument , NULL , ' k ' } ,
{ " udev " , no_argument , NULL , ' u ' } ,
2009-04-22 07:00:15 +04:00
{ " subsystem-match " , required_argument , NULL , ' s ' } ,
2008-10-02 18:49:05 +04:00
{ " help " , no_argument , NULL , ' h ' } ,
2007-03-10 17:12:42 +03:00
{ }
} ;
2009-04-22 07:00:15 +04:00
udev_list_init ( & subsystem_match_list ) ;
2007-03-10 17:12:42 +03:00
while ( 1 ) {
2009-04-22 07:00:15 +04:00
option = getopt_long ( argc , argv , " ekus:h " , options , NULL ) ;
2007-03-10 17:12:42 +03:00
if ( option = = - 1 )
break ;
switch ( option ) {
case ' e ' :
2005-08-11 22:34:24 +04:00
env = 1 ;
2007-03-10 17:12:42 +03:00
break ;
case ' k ' :
2008-09-06 17:45:31 +04:00
print_kernel = 1 ;
2007-03-10 17:12:42 +03:00
break ;
case ' u ' :
2008-09-06 17:45:31 +04:00
print_udev = 1 ;
2007-03-10 17:12:42 +03:00
break ;
2009-04-22 07:00:15 +04:00
case ' s ' :
udev_list_entry_add ( udev , & subsystem_match_list , optarg , NULL , 1 , 0 ) ;
break ;
2007-03-10 17:12:42 +03:00
case ' h ' :
2007-11-08 19:51:59 +03:00
printf ( " Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help] \n "
2007-03-10 17:12:42 +03:00
" --env print the whole event environment \n "
" --kernel print kernel uevents \n "
" --udev print udev events \n "
2009-03-07 04:32:40 +03:00
" --help \n \n " ) ;
2007-03-10 17:12:42 +03:00
default :
goto out ;
2005-08-11 22:34:24 +04:00
}
}
2008-09-06 17:45:31 +04:00
if ( ! print_kernel & & ! print_udev ) {
print_kernel = 1 ;
print_udev = 1 ;
2007-03-10 17:12:42 +03:00
}
2005-08-25 02:38:25 +04:00
/* set signal handlers */
memset ( & act , 0x00 , sizeof ( struct sigaction ) ) ;
act . sa_handler = ( void ( * ) ( int ) ) sig_handler ;
sigemptyset ( & act . sa_mask ) ;
act . sa_flags = SA_RESTART ;
sigaction ( SIGINT , & act , NULL ) ;
sigaction ( SIGTERM , & act , NULL ) ;
2008-09-06 17:45:31 +04:00
printf ( " monitor will print the received events for: \n " ) ;
if ( print_udev ) {
2009-04-22 07:00:15 +04:00
struct udev_list_entry * entry ;
2009-03-29 22:08:52 +04:00
udev_monitor = udev_monitor_new_from_netlink ( udev , " udev " ) ;
2008-09-09 16:07:08 +04:00
if ( udev_monitor = = NULL ) {
2009-04-22 06:11:18 +04:00
fprintf ( stderr , " error: unable to create netlink socket \n " ) ;
2008-09-09 16:07:08 +04:00
rc = 1 ;
goto out ;
}
2009-04-22 07:00:15 +04:00
udev_list_entry_foreach ( entry , udev_list_get_entry ( & subsystem_match_list ) ) {
const char * subsys = udev_list_entry_get_name ( entry ) ;
2009-04-23 05:24:38 +04:00
if ( udev_monitor_filter_add_match_subsystem_devtype ( udev_monitor , subsys , NULL ) < 0 )
2009-04-22 07:00:15 +04:00
fprintf ( stderr , " error: unable to apply subsystem filter '%s' \n " , subsys ) ;
}
2008-09-09 16:07:08 +04:00
if ( udev_monitor_enable_receiving ( udev_monitor ) < 0 ) {
2009-04-22 06:11:18 +04:00
fprintf ( stderr , " error: unable to subscribe to udev events \n " ) ;
2008-09-09 16:07:08 +04:00
rc = 2 ;
2007-03-10 17:12:42 +03:00
goto out ;
2008-09-09 16:07:08 +04:00
}
2009-03-18 20:47:23 +03:00
printf ( " UDEV - the event which udev sends out after rule processing \n " ) ;
2007-03-10 17:12:42 +03:00
}
2008-09-06 17:45:31 +04:00
if ( print_kernel ) {
2009-03-29 22:08:52 +04:00
kernel_monitor = udev_monitor_new_from_netlink ( udev , " kernel " ) ;
2008-09-09 19:41:17 +04:00
if ( kernel_monitor = = NULL ) {
2009-04-22 06:11:18 +04:00
fprintf ( stderr , " error: unable to create netlink socket \n " ) ;
2008-09-09 16:07:08 +04:00
rc = 3 ;
2007-03-10 17:12:42 +03:00
goto out ;
2008-09-09 16:07:08 +04:00
}
2008-09-09 19:41:17 +04:00
if ( udev_monitor_enable_receiving ( kernel_monitor ) < 0 ) {
2009-04-22 06:11:18 +04:00
fprintf ( stderr , " error: unable to subscribe to kernel events \n " ) ;
2008-09-09 19:41:17 +04:00
rc = 4 ;
goto out ;
}
2009-03-29 22:08:52 +04:00
printf ( " KERNEL - the kernel uevent \n " ) ;
2007-03-10 17:12:42 +03:00
}
printf ( " \n " ) ;
2005-08-11 22:34:24 +04:00
2005-08-25 02:38:25 +04:00
while ( ! udev_exit ) {
2005-08-15 13:57:04 +04:00
int fdcount ;
2005-08-01 22:22:46 +04:00
2005-08-15 13:57:04 +04:00
FD_ZERO ( & readfds ) ;
2008-09-09 19:41:17 +04:00
if ( kernel_monitor ! = NULL )
FD_SET ( udev_monitor_get_fd ( kernel_monitor ) , & readfds ) ;
2008-09-09 16:07:08 +04:00
if ( udev_monitor ! = NULL )
FD_SET ( udev_monitor_get_fd ( udev_monitor ) , & readfds ) ;
2005-08-15 13:57:04 +04:00
2008-09-09 19:41:17 +04:00
fdcount = select ( UDEV_MAX ( udev_monitor_get_fd ( kernel_monitor ) , udev_monitor_get_fd ( udev_monitor ) ) + 1 ,
2008-09-09 16:07:08 +04:00
& readfds , NULL , NULL , NULL ) ;
2005-08-15 13:57:04 +04:00
if ( fdcount < 0 ) {
2005-08-01 22:22:46 +04:00
if ( errno ! = EINTR )
2008-09-29 19:01:32 +04:00
fprintf ( stderr , " error receiving uevent message: %m \n " ) ;
2005-08-01 22:22:46 +04:00
continue ;
}
2008-09-09 19:41:17 +04:00
if ( ( kernel_monitor ! = NULL ) & & FD_ISSET ( udev_monitor_get_fd ( kernel_monitor ) , & readfds ) ) {
2008-09-28 03:34:55 +04:00
struct udev_device * device ;
device = udev_monitor_receive_device ( kernel_monitor ) ;
2008-09-09 19:41:17 +04:00
if ( device = = NULL )
2005-08-01 22:22:46 +04:00
continue ;
2009-03-29 22:08:52 +04:00
print_device ( device , " KERNEL " , env ) ;
2008-09-09 19:41:17 +04:00
udev_device_unref ( device ) ;
2005-08-01 22:22:46 +04:00
}
2008-09-09 16:07:08 +04:00
if ( ( udev_monitor ! = NULL ) & & FD_ISSET ( udev_monitor_get_fd ( udev_monitor ) , & readfds ) ) {
2008-09-28 03:34:55 +04:00
struct udev_device * device ;
device = udev_monitor_receive_device ( udev_monitor ) ;
2008-09-09 19:41:17 +04:00
if ( device = = NULL )
2005-08-01 22:22:46 +04:00
continue ;
2008-09-28 03:34:55 +04:00
print_device ( device , " UDEV " , env ) ;
2008-09-09 19:41:17 +04:00
udev_device_unref ( device ) ;
2005-08-01 22:22:46 +04:00
}
}
2005-08-15 13:57:04 +04:00
out :
2008-09-09 16:07:08 +04:00
udev_monitor_unref ( udev_monitor ) ;
2008-09-09 19:41:17 +04:00
udev_monitor_unref ( kernel_monitor ) ;
2009-04-22 07:00:15 +04:00
udev_list_cleanup_entries ( udev , & subsystem_match_list ) ;
2008-09-09 16:07:08 +04:00
return rc ;
2005-08-01 22:22:46 +04:00
}