2005-04-27 07:59:47 +04:00
/*
2005-07-22 20:35:58 +04:00
* Copyright ( C ) 2003 - 2004 Greg Kroah - Hartman < greg @ kroah . com >
2006-08-28 02:29:11 +04:00
* Copyright ( C ) 2004 - 2006 Kay Sievers < kay . sievers @ vrfy . org >
2005-04-27 07:59:47 +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 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 . ,
2006-08-28 02:29:11 +04:00
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
2005-04-27 07:59:47 +04:00
*
*/
2004-03-23 09:22:20 +03:00
# include <stdio.h>
# include <stddef.h>
2005-04-27 07:59:47 +04:00
# include <stdlib.h>
# include <string.h>
2004-11-23 09:34:56 +03:00
# include <fcntl.h>
2003-12-03 12:08:46 +03:00
# include <ctype.h>
2004-03-23 09:22:20 +03:00
# include <errno.h>
2003-12-30 12:21:06 +03:00
# include <signal.h>
2004-10-19 10:14:20 +04:00
# include <unistd.h>
2005-08-11 19:32:59 +04:00
# include <syslog.h>
2003-04-10 21:53:46 +04:00
2005-04-27 07:59:47 +04:00
# include "udev.h"
2005-03-13 00:36:32 +03:00
# include "udev_rules.h"
2006-08-18 05:47:59 +04:00
# include "udev_selinux.h"
2005-04-27 07:59:47 +04: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-27 06:40:32 +03:00
va_list args ;
2004-02-13 05:57:06 +03:00
2005-03-27 03:11:03 +04:00
if ( priority > udev_log_priority )
2004-02-13 05:57:06 +03:00
return ;
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
2004-10-14 09:38:15 +04:00
static void asmlinkage sig_handler ( int signum )
2003-12-30 12:21:06 +03:00
{
switch ( signum ) {
2004-10-14 10:13:26 +04:00
case SIGALRM :
2004-11-06 16:28:01 +03:00
exit ( 1 ) ;
2003-12-30 12:21:06 +03:00
case SIGINT :
case SIGTERM :
exit ( 20 + signum ) ;
}
}
[PATCH] Fix naming ethernet devices in udevstart
On Tue, Sep 07, 2004 at 12:46:43PM +0200, Kay Sievers wrote:
> On Mon, 2004-09-06 at 20:18 -0700, Tom Rini wrote:
> > I noticed somewhat recently that my enet devices weren't being renamed
> > on boot anymore. I don't quite know when this got broken (or rather, if
> > it was supposed to be working. I swear it worked for me once..), but
> > the following seems to do it.
>
> I think it never worked in the udevstart case. It worked only with the
> hotplug-event-udev, I expect.
>
> > In udev_scan_class(), look for not just
> > %s/%s/dev (which everything with a dev node has), but %s/%s/dev* (both
> > of my enet devices, sis900 & 3c59x only have device) and if that
> > exists, pass this along to udev.
>
> Yeah, network devices don't have a devnode and therefore no "dev", but
> they are all in /sys/class/net/. We may just test if we are there
> instead of the "device" match.
How about something like this. It adds all the net devices without
looking at the attributes and keeps the remaining logic like it is.
It also removes certain levels of indirection and much simplifies the
udevstart process. We surely don't need to open and close the udevdb
for every node while iterating over the list. (We are about 5% faster on
my box)
It's not well tested, so it would be nice if someone can have a look
at it, before a broken udevstart renders any system unbootable.
2004-09-11 08:09:25 +04:00
int main ( int argc , char * argv [ ] , char * envp [ ] )
2003-12-31 09:31:37 +03:00
{
2006-01-09 23:18:00 +03:00
struct sysfs_device * dev ;
struct udevice * udev ;
const char * maj , * min ;
2005-07-05 17:24:41 +04:00
struct udev_rules rules ;
2004-12-12 04:54:55 +03:00
const char * action ;
const char * devpath ;
const char * subsystem ;
2005-03-04 23:13:18 +03:00
struct sigaction act ;
2005-08-18 15:07:15 +04:00
int devnull ;
2004-12-12 04:54:55 +03:00
int retval = - EINVAL ;
2003-12-31 09:31:37 +03:00
2004-12-12 04:27:50 +03:00
if ( argc = = 2 & & strcmp ( argv [ 1 ] , " -V " ) = = 0 ) {
printf ( " %s \n " , UDEV_VERSION ) ;
exit ( 0 ) ;
}
2006-08-21 04:38:20 +04:00
/* set std fd's to /dev/null, /sbin/hotplug forks us, we don't have them at all */
2005-08-18 15:07:15 +04:00
devnull = open ( " /dev/null " , O_RDWR ) ;
if ( devnull > = 0 ) {
if ( devnull ! = STDIN_FILENO )
dup2 ( devnull , STDIN_FILENO ) ;
if ( devnull ! = STDOUT_FILENO )
dup2 ( devnull , STDOUT_FILENO ) ;
if ( devnull ! = STDERR_FILENO )
dup2 ( devnull , STDERR_FILENO ) ;
if ( devnull > STDERR_FILENO )
close ( devnull ) ;
}
2004-10-19 05:15:10 +04:00
logging_init ( " udev " ) ;
2005-08-18 15:07:15 +04:00
if ( devnull < 0 )
2006-08-21 04:38:20 +04:00
err ( " open /dev/null failed: %s " , strerror ( errno ) ) ;
2006-01-09 23:18:00 +03:00
udev_config_init ( ) ;
2006-08-18 05:47:59 +04:00
selinux_init ( ) ;
2005-03-27 03:11:03 +04:00
dbg ( " version %s " , UDEV_VERSION ) ;
2003-07-17 07:18:06 +04:00
2004-06-07 13:56:47 +04:00
/* set signal handlers */
2005-04-27 10:15:56 +04:00
memset ( & act , 0x00 , sizeof ( act ) ) ;
2005-03-27 03:11:03 +04:00
act . sa_handler = ( void ( * ) ( int ) ) sig_handler ;
2004-02-12 09:29:15 +03:00
sigemptyset ( & act . sa_mask ) ;
2004-11-05 15:16:32 +03:00
act . sa_flags = 0 ;
2004-10-14 10:13:26 +04:00
sigaction ( SIGALRM , & act , NULL ) ;
2004-02-12 09:29:15 +03:00
sigaction ( SIGINT , & act , NULL ) ;
sigaction ( SIGTERM , & act , NULL ) ;
2003-12-30 12:29:16 +03:00
2004-11-29 04:15:55 +03:00
/* trigger timeout to prevent hanging processes */
2005-07-06 16:42:26 +04:00
alarm ( UDEV_ALARM_TIMEOUT ) ;
2004-10-14 10:13:26 +04:00
2004-12-12 05:29:59 +03:00
action = getenv ( " ACTION " ) ;
devpath = getenv ( " DEVPATH " ) ;
subsystem = getenv ( " SUBSYSTEM " ) ;
/* older kernels passed the SUBSYSTEM only as argument */
2006-01-09 23:18:00 +03:00
if ( subsystem = = NULL & & argc = = 2 )
2004-12-12 05:29:59 +03:00
subsystem = argv [ 1 ] ;
2006-01-09 23:18:00 +03:00
if ( action = = NULL | | subsystem = = NULL | | devpath = = NULL ) {
2005-03-27 03:11:03 +04:00
err ( " action, subsystem or devpath missing " ) ;
2005-06-05 17:55:29 +04:00
goto exit ;
2004-11-23 09:34:56 +03:00
}
2004-10-19 15:37:22 +04:00
2005-06-16 03:58:47 +04:00
/* export log_priority , as called programs may want to do the same as udev */
2005-03-27 03:11:03 +04:00
if ( udev_log_priority ) {
char priority [ 32 ] ;
sprintf ( priority , " %i " , udev_log_priority ) ;
setenv ( " UDEV_LOG " , priority , 1 ) ;
}
2004-11-23 09:34:56 +03:00
2006-01-09 23:18:00 +03:00
sysfs_init ( ) ;
2005-11-05 22:00:31 +03:00
udev_rules_init ( & rules , 0 ) ;
2005-04-02 19:45:35 +04:00
2006-01-09 23:18:00 +03:00
dev = sysfs_device_get ( devpath ) ;
if ( dev = = NULL ) {
info ( " unable to open '%s' " , devpath ) ;
goto fail ;
}
2007-03-17 12:08:25 +03:00
udev = udev_device_init ( NULL ) ;
2006-01-09 23:18:00 +03:00
if ( udev = = NULL )
goto fail ;
/* override built-in sysfs device */
udev - > dev = dev ;
strlcpy ( udev - > action , action , sizeof ( udev - > action ) ) ;
/* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
maj = getenv ( " MAJOR " ) ;
min = getenv ( " MINOR " ) ;
if ( maj ! = NULL & & min ! = NULL )
udev - > devt = makedev ( atoi ( maj ) , atoi ( min ) ) ;
else
udev - > devt = udev_device_get_devt ( udev ) ;
retval = udev_device_event ( & rules , udev ) ;
2004-03-24 05:54:34 +03:00
2006-01-25 04:21:07 +03:00
if ( retval = = 0 & & ! udev - > ignore_device & & udev_run ) {
2005-06-05 17:55:29 +04:00
struct name_entry * name_loop ;
dbg ( " executing run list " ) ;
2006-01-09 23:18:00 +03:00
list_for_each_entry ( name_loop , & udev - > run_list , node ) {
2005-08-01 07:07:19 +04:00
if ( strncmp ( name_loop - > name , " socket: " , strlen ( " socket: " ) ) = = 0 )
pass_env_to_socket ( & name_loop - > name [ strlen ( " socket: " ) ] , devpath , action ) ;
2006-01-24 22:10:48 +03:00
else {
char program [ PATH_SIZE ] ;
strlcpy ( program , name_loop - > name , sizeof ( program ) ) ;
2006-01-25 03:28:31 +03:00
udev_rules_apply_format ( udev , program , sizeof ( program ) ) ;
2007-07-14 22:43:43 +04:00
run_program ( program , udev - > dev - > subsystem , NULL , 0 , NULL ) ;
2006-01-24 22:10:48 +03:00
}
2005-08-01 07:07:19 +04:00
}
2005-06-05 17:55:29 +04:00
}
2004-11-23 10:01:57 +03:00
2006-01-09 23:18:00 +03:00
udev_device_cleanup ( udev ) ;
fail :
udev_rules_cleanup ( & rules ) ;
sysfs_cleanup ( ) ;
2007-03-16 23:13:07 +03:00
selinux_exit ( ) ;
2005-03-29 13:25:52 +04:00
2005-06-14 18:12:31 +04:00
exit :
2004-10-19 05:15:10 +04:00
logging_close ( ) ;
2006-01-25 04:21:07 +03:00
if ( retval ! = 0 )
return 1 ;
return 0 ;
2005-04-27 07:59:47 +04:00
}