2005-06-05 06:41:09 +04:00
/*
2008-09-10 04:40:42 +04:00
* Copyright ( C ) 2005 - 2008 Kay Sievers < kay . sievers @ vrfy . org >
2005-06-05 06:41:09 +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-06-05 06:41:09 +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 .
2005-06-05 06:41:09 +04:00
*/
# include <time.h>
# include <errno.h>
# include <stdio.h>
# include <stdlib.h>
# include <stddef.h>
# include <string.h>
# include <unistd.h>
2008-09-03 23:56:47 +04:00
# include <getopt.h>
2006-05-01 22:36:21 +04:00
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/wait.h>
# include <sys/un.h>
2005-06-05 06:41:09 +04:00
# include "udev.h"
2008-09-08 19:59:00 +04:00
static void print_help ( void )
{
printf ( " Usage: udevadm control COMMAND \n "
2011-04-13 03:17:09 +04:00
" --exit instruct the daemon to cleanup and exit \n "
2008-09-08 19:59:00 +04:00
" --log-priority=<level> set the udev log level for the daemon \n "
" --stop-exec-queue keep udevd from executing events, queue only \n "
" --start-exec-queue execute events, flush queue \n "
" --reload-rules reloads the rules files \n "
2009-06-07 04:53:35 +04:00
" --property=<KEY>=<value> set a global property for all events \n "
2010-05-27 19:27:56 +04:00
" --children-max=<N> maximum number of children \n "
2011-04-13 03:17:09 +04:00
" --timeout=<seconds> maximum time to block for a reply \n "
2008-09-08 19:59:00 +04:00
" --help print this help text \n \n " ) ;
}
2005-06-05 06:41:09 +04:00
2008-09-06 17:45:31 +04:00
int udevadm_control ( struct udev * udev , int argc , char * argv [ ] )
2008-09-03 23:56:47 +04:00
{
2008-09-08 19:59:00 +04:00
struct udev_ctrl * uctrl = NULL ;
2011-04-13 03:17:09 +04:00
int timeout = 60 ;
2008-09-03 23:56:47 +04:00
int rc = 1 ;
static const struct option options [ ] = {
2011-04-13 03:17:09 +04:00
{ " exit " , no_argument , NULL , ' e ' } ,
2008-10-02 18:49:05 +04:00
{ " log-priority " , required_argument , NULL , ' l ' } ,
{ " stop-exec-queue " , no_argument , NULL , ' s ' } ,
{ " start-exec-queue " , no_argument , NULL , ' S ' } ,
{ " reload-rules " , no_argument , NULL , ' R ' } ,
2009-06-07 04:53:35 +04:00
{ " property " , required_argument , NULL , ' p ' } ,
{ " env " , required_argument , NULL , ' p ' } ,
2010-05-27 19:27:56 +04:00
{ " children-max " , required_argument , NULL , ' m ' } ,
2011-04-13 03:17:09 +04:00
{ " timeout " , required_argument , NULL , ' t ' } ,
2008-10-02 18:49:05 +04:00
{ " help " , no_argument , NULL , ' h ' } ,
2008-09-03 23:56:47 +04:00
{ }
} ;
2005-06-05 06:41:09 +04:00
2008-09-03 23:56:47 +04:00
if ( getuid ( ) ! = 0 ) {
fprintf ( stderr , " root privileges required \n " ) ;
2009-09-14 16:29:05 +04:00
return 1 ;
2005-06-05 06:41:09 +04:00
}
2008-09-03 23:56:47 +04:00
2008-09-08 19:59:00 +04:00
uctrl = udev_ctrl_new_from_socket ( udev , UDEV_CTRL_SOCK_PATH ) ;
2009-09-14 16:29:05 +04:00
if ( uctrl = = NULL )
return 2 ;
2008-09-03 23:56:47 +04:00
2010-05-27 17:11:00 +04:00
for ( ; ; ) {
2008-09-03 23:56:47 +04:00
int option ;
2011-04-13 03:17:09 +04:00
option = getopt_long ( argc , argv , " el:sSRp:m:h " , options , NULL ) ;
2008-09-03 23:56:47 +04:00
if ( option = = - 1 )
break ;
switch ( option ) {
2011-04-13 03:17:09 +04:00
case ' e ' :
if ( udev_ctrl_send_exit ( uctrl , timeout ) < 0 )
rc = 2 ;
else
rc = 0 ;
break ;
case ' l ' : {
int i ;
2008-09-10 20:39:23 +04:00
i = util_log_priority ( optarg ) ;
2008-09-03 23:56:47 +04:00
if ( i < 0 ) {
fprintf ( stderr , " invalid number '%s' \n " , optarg ) ;
2011-04-13 03:17:09 +04:00
goto out ;
2008-09-03 23:56:47 +04:00
}
2011-04-13 03:17:09 +04:00
if ( udev_ctrl_send_set_log_level ( uctrl , util_log_priority ( optarg ) , timeout ) < 0 )
2009-09-14 16:05:31 +04:00
rc = 2 ;
else
rc = 0 ;
2008-09-03 23:56:47 +04:00
break ;
2011-04-13 03:17:09 +04:00
}
2008-09-03 23:56:47 +04:00
case ' s ' :
2011-04-13 03:17:09 +04:00
if ( udev_ctrl_send_stop_exec_queue ( uctrl , timeout ) < 0 )
2009-09-14 16:05:31 +04:00
rc = 2 ;
else
rc = 0 ;
2008-09-03 23:56:47 +04:00
break ;
case ' S ' :
2011-04-13 03:17:09 +04:00
if ( udev_ctrl_send_start_exec_queue ( uctrl , timeout ) < 0 )
2009-09-14 16:05:31 +04:00
rc = 2 ;
else
rc = 0 ;
2008-09-03 23:56:47 +04:00
break ;
case ' R ' :
2011-04-13 03:17:09 +04:00
if ( udev_ctrl_send_reload_rules ( uctrl , timeout ) < 0 )
2009-09-14 16:05:31 +04:00
rc = 2 ;
else
rc = 0 ;
2008-09-03 23:56:47 +04:00
break ;
2009-06-07 04:53:35 +04:00
case ' p ' :
2008-09-03 23:56:47 +04:00
if ( strchr ( optarg , ' = ' ) = = NULL ) {
2009-08-08 17:29:38 +04:00
fprintf ( stderr , " expect <KEY>=<value> instead of '%s' \n " , optarg ) ;
2011-04-13 03:17:09 +04:00
goto out ;
2008-09-03 23:56:47 +04:00
}
2011-04-13 03:17:09 +04:00
if ( udev_ctrl_send_set_env ( uctrl , optarg , timeout ) < 0 )
2009-09-14 16:05:31 +04:00
rc = 2 ;
else
rc = 0 ;
2008-09-03 23:56:47 +04:00
break ;
2011-04-13 03:17:09 +04:00
case ' m ' : {
char * endp ;
int i ;
2008-09-03 23:56:47 +04:00
i = strtoul ( optarg , & endp , 0 ) ;
if ( endp [ 0 ] ! = ' \0 ' | | i < 1 ) {
fprintf ( stderr , " invalid number '%s' \n " , optarg ) ;
2011-04-13 03:17:09 +04:00
goto out ;
2008-09-03 23:56:47 +04:00
}
2011-04-13 03:17:09 +04:00
if ( udev_ctrl_send_set_children_max ( uctrl , i , timeout ) < 0 )
2009-09-14 16:05:31 +04:00
rc = 2 ;
else
rc = 0 ;
2008-09-03 23:56:47 +04:00
break ;
2011-04-13 03:17:09 +04:00
}
case ' t ' : {
int seconds ;
seconds = atoi ( optarg ) ;
if ( seconds > = 0 )
timeout = seconds ;
else
fprintf ( stderr , " invalid timeout value \n " ) ;
break ;
}
2008-09-03 23:56:47 +04:00
case ' h ' :
2008-09-08 19:59:00 +04:00
print_help ( ) ;
rc = 0 ;
2009-09-14 16:29:05 +04:00
break ;
2008-09-03 23:56:47 +04:00
}
2005-06-14 00:38:42 +04:00
}
2011-04-13 03:17:09 +04:00
out :
2008-09-03 23:56:47 +04:00
udev_ctrl_unref ( uctrl ) ;
return rc ;
2005-06-05 06:41:09 +04:00
}