2003-12-03 12:08:46 +03:00
/*
2006-08-28 02:29:11 +04:00
* Copyright ( C ) 2003 - 2004 Greg Kroah - Hartman < greg @ kroah . com >
* Copyright ( C ) 2004 - 2005 Kay Sievers < kay . sievers @ vrfy . org >
2003-12-03 12:08:46 +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 . ,
2006-08-28 02:29:11 +04:00
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
2003-12-03 12:08:46 +03:00
*
*/
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <fcntl.h>
# include <unistd.h>
# include <errno.h>
# include <ctype.h>
2005-03-27 03:11:03 +04:00
# include <syslog.h>
2003-12-03 12:08:46 +03:00
# include "udev.h"
/* global variables */
2005-03-07 06:29:43 +03:00
char udev_root [ PATH_SIZE ] ;
char udev_config_filename [ PATH_SIZE ] ;
2005-03-27 03:11:03 +04:00
char udev_rules_filename [ PATH_SIZE ] ;
int udev_log_priority ;
2005-04-02 19:45:35 +04:00
int udev_run ;
2003-12-03 12:08:46 +03:00
2005-03-13 07:46:31 +03:00
static int get_key ( char * * line , char * * key , char * * value )
{
char * linepos ;
char * temp ;
linepos = * line ;
if ( ! linepos )
return - 1 ;
/* skip whitespace */
while ( isspace ( linepos [ 0 ] ) )
linepos + + ;
/* get the key */
* key = linepos ;
while ( 1 ) {
linepos + + ;
if ( linepos [ 0 ] = = ' \0 ' )
return - 1 ;
if ( isspace ( linepos [ 0 ] ) )
break ;
if ( linepos [ 0 ] = = ' = ' )
break ;
}
/* terminate key */
linepos [ 0 ] = ' \0 ' ;
linepos + + ;
/* skip whitespace */
while ( isspace ( linepos [ 0 ] ) )
linepos + + ;
/* get the value*/
if ( linepos [ 0 ] = = ' " ' )
linepos + + ;
else
return - 1 ;
* value = linepos ;
temp = strchr ( linepos , ' " ' ) ;
if ( ! temp )
return - 1 ;
temp [ 0 ] = ' \0 ' ;
return 0 ;
}
2003-12-03 12:08:46 +03:00
static int parse_config_file ( void )
{
2004-09-11 07:54:04 +04:00
char line [ LINE_SIZE ] ;
char * bufline ;
2005-03-13 07:46:31 +03:00
char * linepos ;
2003-12-03 12:08:46 +03:00
char * variable ;
char * value ;
2004-03-23 09:22:20 +03:00
char * buf ;
size_t bufsize ;
size_t cur ;
size_t count ;
int lineno ;
2003-12-03 12:08:46 +03:00
int retval = 0 ;
2004-03-23 09:22:20 +03:00
2005-03-07 06:29:43 +03:00
if ( file_map ( udev_config_filename , & buf , & bufsize ) ! = 0 ) {
2005-11-07 20:44:18 +03:00
err ( " can't open '%s' as config file: %s " , udev_config_filename , strerror ( errno ) ) ;
2003-12-03 12:08:46 +03:00
return - ENODEV ;
}
/* loop through the whole file */
2004-03-23 09:22:20 +03:00
lineno = 0 ;
cur = 0 ;
2004-09-11 07:54:04 +04:00
while ( cur < bufsize ) {
2004-03-23 09:22:20 +03:00
count = buf_get_line ( buf , bufsize , cur ) ;
2004-09-11 07:54:04 +04:00
bufline = & buf [ cur ] ;
2004-03-23 09:22:20 +03:00
cur + = count + 1 ;
2004-09-11 07:54:04 +04:00
lineno + + ;
2003-12-03 12:08:46 +03:00
2005-03-07 06:29:43 +03:00
if ( count > = sizeof ( line ) ) {
2005-03-27 03:11:03 +04:00
err ( " line too long, conf line skipped %s, line %d " , udev_config_filename , lineno ) ;
2004-09-11 07:54:04 +04:00
continue ;
}
2003-12-03 12:08:46 +03:00
2004-09-11 07:54:04 +04:00
/* eat the whitespace */
2004-09-15 04:45:48 +04:00
while ( ( count > 0 ) & & isspace ( bufline [ 0 ] ) ) {
2004-09-11 07:54:04 +04:00
bufline + + ;
count - - ;
}
2004-09-15 04:45:48 +04:00
if ( count = = 0 )
continue ;
2004-09-11 07:54:04 +04:00
2003-12-03 12:08:46 +03:00
/* see if this is a comment */
2004-09-11 07:54:04 +04:00
if ( bufline [ 0 ] = = COMMENT_CHARACTER )
2003-12-03 12:08:46 +03:00
continue ;
2005-08-08 04:21:55 +04:00
memcpy ( line , bufline , count ) ;
line [ count ] = ' \0 ' ;
2004-09-11 07:54:04 +04:00
2005-03-13 07:46:31 +03:00
linepos = line ;
retval = get_key ( & linepos , & variable , & value ) ;
if ( retval ! = 0 ) {
2006-01-13 15:17:10 +03:00
err ( " error parsing %s, line %d:%d " , udev_config_filename , lineno , ( int ) ( linepos - line ) ) ;
2005-03-13 07:46:31 +03:00
continue ;
}
2004-04-24 08:50:27 +04:00
if ( strcasecmp ( variable , " udev_root " ) = = 0 ) {
2005-03-07 06:29:43 +03:00
strlcpy ( udev_root , value , sizeof ( udev_root ) ) ;
2005-08-29 01:15:51 +04:00
remove_trailing_chars ( udev_root , ' / ' ) ;
2004-04-24 08:50:27 +04:00
continue ;
}
if ( strcasecmp ( variable , " udev_rules " ) = = 0 ) {
2005-03-07 06:29:43 +03:00
strlcpy ( udev_rules_filename , value , sizeof ( udev_rules_filename ) ) ;
2005-08-29 01:15:51 +04:00
remove_trailing_chars ( udev_rules_filename , ' / ' ) ;
2004-04-24 08:50:27 +04:00
continue ;
}
if ( strcasecmp ( variable , " udev_log " ) = = 0 ) {
2005-03-27 03:11:03 +04:00
udev_log_priority = log_priority ( value ) ;
2004-04-24 08:50:27 +04:00
continue ;
}
2003-12-03 12:08:46 +03:00
}
2004-03-23 09:22:20 +03:00
file_unmap ( buf , bufsize ) ;
2003-12-03 12:08:46 +03:00
return retval ;
}
2006-01-09 23:18:00 +03:00
void udev_config_init ( void )
2003-12-03 12:08:46 +03:00
{
2005-03-27 03:11:03 +04:00
const char * env ;
2003-12-03 12:08:46 +03:00
2005-03-27 03:11:03 +04:00
strcpy ( udev_root , UDEV_ROOT ) ;
strcpy ( udev_config_filename , UDEV_CONFIG_FILE ) ;
strcpy ( udev_rules_filename , UDEV_RULES_FILE ) ;
udev_log_priority = LOG_ERR ;
2005-04-02 19:45:35 +04:00
udev_run = 1 ;
2003-12-03 12:08:46 +03:00
2005-04-02 19:45:35 +04:00
/* disable RUN key execution */
env = getenv ( " UDEV_RUN " ) ;
if ( env & & ! string_is_true ( env ) )
udev_run = 0 ;
2005-03-27 03:11:03 +04:00
env = getenv ( " UDEV_CONFIG_FILE " ) ;
if ( env ) {
strlcpy ( udev_config_filename , env , sizeof ( udev_config_filename ) ) ;
2005-08-29 01:15:51 +04:00
remove_trailing_chars ( udev_config_filename , ' / ' ) ;
2005-03-27 03:11:03 +04:00
}
2003-12-03 12:08:46 +03:00
parse_config_file ( ) ;
2005-03-27 03:11:03 +04:00
2005-11-25 20:56:06 +03:00
env = getenv ( " UDEV_ROOT " ) ;
if ( env ) {
strlcpy ( udev_root , env , sizeof ( udev_root ) ) ;
remove_trailing_chars ( udev_root , ' / ' ) ;
}
2005-03-27 03:11:03 +04:00
env = getenv ( " UDEV_LOG " ) ;
if ( env )
udev_log_priority = log_priority ( env ) ;
dbg ( " UDEV_CONFIG_FILE='%s' " , udev_config_filename ) ;
2004-12-20 09:38:33 +03:00
dbg ( " udev_root='%s' " , udev_root ) ;
2005-03-27 03:11:03 +04:00
dbg ( " udev_rules='%s' " , udev_rules_filename ) ;
dbg ( " udev_log=%d " , udev_log_priority ) ;
2003-12-03 12:08:46 +03:00
}