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