Beispiel #1
0
void udev_config_init(void) {
    const char* env;

    strcpy(udev_root, UDEV_ROOT);
    strcpy(udev_config_filename, UDEV_CONFIG_FILE);
    strcpy(udev_rules_dir, UDEV_RULES_DIR);
    udev_log_priority = LOG_ERR;
    udev_run = 1;

    /* disable RUN key execution */
    env = getenv("UDEV_RUN");

    if (env && !string_is_true(env)) {
        udev_run = 0;
    }

    env = getenv("UDEV_CONFIG_FILE");

    if (env) {
        strlcpy(udev_config_filename, env, sizeof(udev_config_filename));
        remove_trailing_chars(udev_config_filename, '/');
    }

    parse_config_file();

    env = getenv("UDEV_ROOT");

    if (env) {
        strlcpy(udev_root, env, sizeof(udev_root));
        remove_trailing_chars(udev_root, '/');
    }

    env = getenv("UDEV_LOG");

    if (env) {
        udev_log_priority = log_priority(env);
    }

    dbg("UDEV_CONFIG_FILE='%s'", udev_config_filename);
    dbg("udev_root='%s'", udev_root);
    dbg("udev_rules='%s'", udev_rules_dir);
    dbg("udev_log=%d", udev_log_priority);
}
Beispiel #2
0
static char *mydatetime(const struct timeval *tv)
{
	static char datetime[26]; /* length imposed by ctime_r */
	time_t time;

	time = tv->tv_sec;
	ctime_r(&time, datetime);
	datetime[sizeof(datetime) - 1] = '\0';
	remove_trailing_chars(datetime, '\n');
	return datetime;
}
Beispiel #3
0
int parse_config_file(char *path_to_config_file)
{
	char line[LINE_SIZE + 2];
	char *bufline;
	char *linepos;
	char *variable;
	char *value;
	char *buf;
	size_t bufsize;
	size_t cur;
	size_t count;
	int lineno;
	int retval = 0;
    FILE *cfg_file=fopen(path_to_config_file, "r");
    if (NULL==cfg_file)
    {
    	printf("can't open '%s' as config file: %s", path_to_config_file, strerror(errno));
		goto EXIT;
	}
		 
   		 /* loop through the whole file */ 
	lineno=0;
	cur=0;
	while(NULL!=fgets(line, sizeof(line), cfg_file))
    { 	
    	lineno++;
		bufline=line;
		//printf("1_bufline:\t%s\n",bufline);
		count=strlen(line);
    	if (count>LINE_SIZE)
        {
       		printf("line too long, conf line skipped %s, line %d", path_to_config_file, lineno);
			continue;
	  	}
   
		 while ((count>0)&&isspace(bufline[0]))	 /*eat the whitespace */
   		 { 
    		bufline++;
			count--;
		 }
	 
		 if (count == 0)
		 {
			continue;
		 }
    		/* see if this is a comment */ 
	     if (bufline[0]==COMMENT_CHARACTER)
	     {
			continue;
		 }
    	 
    	 memcpy(line, bufline, count);
		 line[count]='\0';
     	 linepos=line;
		 retval=get_key(&linepos, &variable, &value);
		 if (retval!=0)
   		 { 
			printf("error parsing %s, line %d:%d\n", path_to_config_file, lineno, (int)(linepos-line));
			continue;
		 }
   		 if (g_var_num>=MAX_VAR_NUM)
   		 { 
			printf("too many vars in  %s, line %d:%d", path_to_config_file, lineno, (int)(linepos-line));
			continue;
		 }
    	if (strlen(variable) > MAX_VAR_NAME_LEN)
   		{
    		printf("var name to long %s, line %d:%d", path_to_config_file, lineno, (int)(linepos-line));
			continue;
		}
   		if (strlen(value) > MAX_VAR_VALUE_LEN)
   		{ 
			printf("value to long %s, line %d:%d", path_to_config_file, lineno, (int)(linepos-line));
			continue;
		}
		strncpy(ga_variables[g_var_num], variable, sizeof(ga_variables[g_var_num]));
		remove_trailing_chars(value, '/');
		strncpy(ga_values[g_var_num], value, sizeof(ga_values[g_var_num]));
		g_var_num++;
		continue;
   	}
	EXIT:
		 fclose(cfg_file);
	return g_var_num;
}
int parse_config_file(char *path_to_config_file, int need_update)
{
	char line[LINE_SIZE + 2];
	char *bufline;
	char *linepos;
	char *variable;
	char *value;

	size_t count;
	int lineno;
	int retval = 0;

    FILE *cfg_file;

    strcpy(config_file_path, path_to_config_file);
    cfg_file = fopen(config_file_path, "r");
	if (NULL == cfg_file) 
    {
		ErrSysLog("can't open '%s' as config file", path_to_config_file);
		goto EXIT;
	}
    
    ga_variables = malloc(VAR_NAME_SIZE);
    ga_values    = malloc(VAR_VALUE_SIZE);
    if (need_update)
        ga_values_backup = malloc(VAR_VALUE_SIZE);

    if (NULL==ga_variables || NULL==ga_values || (need_update && NULL==ga_values_backup))
    {
        ErrSysLog("malloc failed");
        goto EXIT;
    }

	/* loop through the whole file */
	lineno = 0;

	while (NULL != fgets(line, sizeof(line), cfg_file)) 
    {
		lineno++;
		bufline = line;
		count = strlen(line);

		if (count > LINE_SIZE) 
        {
			ErrSysLog("line too long, conf line skipped %s, line %d", path_to_config_file, lineno);
			continue;
		}

		/* eat the whitespace */
		while ((count > 0) && isspace(bufline[0])) 
        {
			bufline++;
			count--;
		}
		if (count == 0)
			continue;

		/* see if this is a comment */
		if (bufline[0] == COMMENT_CHARACTER)
			continue;

		memcpy(line, bufline, count);
		line[count] = '\0';

		linepos = line;
		retval = get_key(&linepos, &variable, &value);
		if (retval != 0) 
        {
			ErrSysLog("error parsing %s, line %d:%d", path_to_config_file, lineno, (int)(linepos-line));
			continue;
		}

        if (g_var_num >= MAX_VAR_NUM)
        {
			ErrSysLog("too many vars in  %s, line %d:%d", path_to_config_file, lineno, (int)(linepos-line));
			continue;
		}

        if (strlen(variable) > MAX_VAR_NAME_LEN)
        {
			ErrSysLog("var name to long %s, line %d:%d", path_to_config_file, lineno, (int)(linepos-line));
			continue;
		}

        if (strlen(value) > MAX_VAR_VALUE_LEN)
        {
			ErrSysLog("value to long %s, line %d:%d", path_to_config_file, lineno, (int)(linepos-line));
			continue;
		}

		strncpy(ga_variables[g_var_num], variable, sizeof(ga_variables[g_var_num]));
        remove_trailing_chars(value, '/');
		strncpy(ga_values[g_var_num], value, sizeof(ga_values[g_var_num]));
        g_var_num++;
        continue;

	}

    if (ga_values_backup) 
        value_copy(ga_values_backup, ga_values, g_var_num);
EXIT:
	fclose(cfg_file);
	return g_var_num;
}
Beispiel #5
0
static int parse_config_file(void) {
    char line[LINE_SIZE];
    char* bufline;
    char* linepos;
    char* variable;
    char* value;
    char* buf;
    size_t bufsize;
    size_t cur;
    size_t count;
    int lineno;
    int retval = 0;

    if (file_map(udev_config_filename, &buf, &bufsize) != 0) {
        err("can't open '%s' as config file: %s", udev_config_filename, strerror(errno));
        return -ENODEV;
    }

    /* loop through the whole file */
    lineno = 0;
    cur = 0;

    while (cur < bufsize) {
        count = buf_get_line(buf, bufsize, cur);
        bufline = &buf[cur];
        cur += count + 1;
        lineno++;

        if (count >= sizeof(line)) {
            err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
            continue;
        }

        /* eat the whitespace */
        while ((count > 0) && isspace(bufline[0])) {
            bufline++;
            count--;
        }

        if (count == 0) {
            continue;
        }

        /* see if this is a comment */
        if (bufline[0] == COMMENT_CHARACTER) {
            continue;
        }

        memcpy(line, bufline, count);
        line[count] = '\0';

        linepos = line;
        retval = get_key(&linepos, &variable, &value);

        if (retval != 0) {
            err("error parsing %s, line %d:%d", udev_config_filename, lineno, (int)(linepos - line));
            continue;
        }

        if (strcasecmp(variable, "udev_root") == 0) {
            strlcpy(udev_root, value, sizeof(udev_root));
            remove_trailing_chars(udev_root, '/');
            continue;
        }

        if (strcasecmp(variable, "udev_rules") == 0) {
            strlcpy(udev_rules_dir, value, sizeof(udev_rules_dir));
            remove_trailing_chars(udev_rules_dir, '/');
            continue;
        }

        if (strcasecmp(variable, "udev_log") == 0) {
            udev_log_priority = log_priority(value);
            continue;
        }
    }

    file_unmap(buf, bufsize);
    return retval;
}