Example #1
0
//
// Functions to copy settings from libconfig/contrib
//
void config_setting_copy_simple(config_setting_t *parent, const config_setting_t *src) {
	if (config_setting_is_aggregate(src)) {
		libconfig->setting_copy_aggregate(parent, src);
	}
	else {
		config_setting_t *set;

		if( libconfig->setting_get_member(parent, config_setting_name(src)) != NULL )
			return;

		if ((set = libconfig->setting_add(parent, config_setting_name(src), config_setting_type(src))) == NULL)
			return;

		if (CONFIG_TYPE_INT == config_setting_type(src)) {
			libconfig->setting_set_int(set, libconfig->setting_get_int(src));
			libconfig->setting_set_format(set, src->format);
		} else if (CONFIG_TYPE_INT64 == config_setting_type(src)) {
			libconfig->setting_set_int64(set, libconfig->setting_get_int64(src));
			libconfig->setting_set_format(set, src->format);
		} else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) {
			libconfig->setting_set_float(set, libconfig->setting_get_float(src));
		} else if (CONFIG_TYPE_STRING == config_setting_type(src)) {
			libconfig->setting_set_string(set, libconfig->setting_get_string(src));
		} else if (CONFIG_TYPE_BOOL == config_setting_type(src)) {
			libconfig->setting_set_bool(set, libconfig->setting_get_bool(src));
		}
	}
}
Example #2
0
ret_code_t entry_copy_aggregate(config_setting_t * source, config_setting_t * dest, int type)
{
	const char * setting_name;
	config_setting_t * new_dest;
	int index=0;
	char tag[7];

	setting_name = config_setting_name(source);
	if(setting_name == NULL) {
		return RET_NOK;
	}

	new_dest=config_setting_add(dest,setting_name,type);
	if( new_dest == NULL ) {
		// Try to find an available name
		sprintf(tag,"A%05x",index);

		while((new_dest=config_setting_add(dest,setting_name,type))== NULL) {
			index++;
			sprintf(tag,"A%05x",index);
		}
	}

	if(!entry_copy_config(source,new_dest)) {
		config_setting_remove(dest,setting_name);
		return RET_NOK;
	}
	return RET_OK;
}
static int processRequest(char *request) {
    int count = 0;
    int responseTotal = 0;
    
    config_setting_t *responseConfig = NULL;
    config_setting_t *responseCurrent = NULL;
    const char *responseValue = NULL;
    const char *requestName = NULL;
    const char *requestValue = NULL;
    long volume;
    
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    pthread_mutex_lock(&lockConfig);
    
    responseConfig = config_lookup(&config, "response");
    responseTotal = config_setting_length(responseConfig);
    
    for(count = 0; count < responseTotal; count++) {
        responseCurrent = config_setting_get_elem(responseConfig, count);
        if((responseValue = config_setting_get_string_elem(responseCurrent, 1)) != NULL &&
        strcmp(responseValue, request) == 0) {
            responseValue = config_setting_get_string_elem(responseCurrent, 2);
            if(config_setting_get_bool_elem(responseCurrent, 0) == 1) { // formulating default response
            
                pthread_mutex_unlock(&lockConfig);
                pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
                
                common_data.interface->reply(responseValue, strlen(responseValue));
            }
            else { // attempt to formulate custom response
                requestName = config_setting_name(responseCurrent);
                
                pthread_mutex_unlock(&lockConfig);
                pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
                
                if(strcmp(requestName, "volume") == 0) {
                    if(getMixer(&volume) == EXIT_FAILURE) {
                        return EXIT_FAILURE;
                    }
                    replyVolumeCommand(&volume);
                }
                else {
                    statusInfo.retrieve(requestName, &requestValue);
                    if(requestValue != NULL)
                        replyDeviceCommand((char *)requestName, (char *)requestValue);
                    else // custom response not possible, reverting to default value
                        replyDeviceCommand((char *)requestName, (char *)responseValue);
                }
            }
            syslog(LOG_DEBUG, "Successfully processed request: %s", request);
            return EXIT_SUCCESS; // command is matched, returning
        }
        else {
            pthread_mutex_unlock(&lockConfig);
            pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        }
    }
    syslog(LOG_DEBUG, "Could not identify request: %s", request);
    return EXIT_SUCCESS;
}
Example #4
0
void config_setting_copy_aggregate(config_setting_t *parent, const config_setting_t *src) {
	config_setting_t *newAgg;
	int i, n;

	if( libconfig->setting_get_member(parent, config_setting_name(src)) != NULL )
		return;

	newAgg = libconfig->setting_add(parent, config_setting_name(src), config_setting_type(src));

	if (newAgg == NULL)
		return;

	n = config_setting_length(src);

	for (i = 0; i < n; i++) {
		if (config_setting_is_group(src)) {
			libconfig->setting_copy_simple(newAgg, libconfig->setting_get_elem(src, i));
		} else {
			libconfig->setting_copy_elem(newAgg, libconfig->setting_get_elem(src, i));
		}
	}
}
Example #5
0
/*******************************************************************************
 函数名称  : webauth_rdx_read_setting
 功能描述  : 读取服务器配置
 输入参数  : 无
 输出参数  : 服务器配置列表
 返 回 值  :
--------------------------------------------------------------------------------
 最近一次修改记录 :
 修改作者   :      王群
 修改目的   :      新添加函数
 修改日期   :      2010-08-16
*******************************************************************************/
webauth_rdx_server_conf_t *webauth_rdx_read_setting (void)
{
    config_t cfg;
    webauth_rdx_server_conf_t *server_cfg, *head;
    config_setting_t *server_set;
    config_setting_t *root_set;
    int server_cnt, i;
    int max_index;

    config_init (&cfg);

    head = NULL;
    if (0 == config_read_file (&cfg, RADIUS_CFG_FILE))
    {
        config_destroy (&cfg);
        return head;
    }

    root_set = config_root_setting (&cfg);
    server_cnt = config_setting_length (root_set);
    max_index = 0;
    for (i = 0; i < server_cnt; i++)
    {
        /* 遍历所有的RADIUS服务器配置,找到指定的服务器 */
        server_set = config_setting_get_elem (root_set, (unsigned int) i);
        if (NULL != server_set)
        {
            server_cfg = webauth_rdx_new_setting (config_setting_name (server_set));
            if (NULL != server_cfg)
            {
                webauth_rdx_read_server_setting (server_set, server_cfg);
                server_cfg->next = head;
                head = server_cfg;
                if (max_index < server_cfg->index)
                {
                    max_index = server_cfg->index;
                }
            }
        }
    }
    /* 如果配置的server没有指定index, 则指定之 */
    for (server_cfg = head; NULL != server_cfg; server_cfg = server_cfg->next)
    {
        if (-1 == server_cfg->index)
        {
            server_cfg->index = ++max_index;
        }
    }
    config_destroy (&cfg);
    return head;
}
Example #6
0
/**********************
Return a list of the name of the elements in the specified group
res must be freed  (deep_free)
 return RET_NOK on error
**********************/
ret_code_t entry_get_group_list(const char * table, const char * file, char *** res, ...)
{
	char * path;
	const config_t * config;
	va_list ap;
	config_setting_t * setting;
	config_setting_t * elem_setting;
	int index = 0;

	*res = NULL;

	SDL_LockMutex(entry_mutex);
	config = get_config(table,file);
	if(config==NULL) {
		SDL_UnlockMutex(entry_mutex);
		return RET_NOK;
	}

	va_start(ap,res);
	path = get_path(ap);
	va_end(ap);

	if(path != NULL && path[0] != 0) {
		setting = config_lookup(config,path);
		free(path);
	} else {
		setting = config_root_setting(config);
	}

	/* The path does not exist in the conf file */
	if(setting == NULL ) {
		SDL_UnlockMutex(entry_mutex);
		return RET_NOK;
	}

	while((elem_setting=config_setting_get_elem(setting,index))!= NULL ) {
		index++;
		*res = (char **)realloc(*res,(index+1)*sizeof(char *));
		(*res)[index-1] = strdup(config_setting_name(elem_setting));
		(*res)[index] = NULL;
	}

	SDL_UnlockMutex(entry_mutex);

	if( *res == NULL) {
		return RET_NOK;
	}

	return RET_OK;
}
Example #7
0
// {{{ session_get_user()
int session_get_user(const char *sessid, char *user, size_t len)
{
   struct config_t cfg;
   config_init(&cfg);
   config_setting_t *cs;
   config_setting_t *vs;

   if(!config_read_file(&cfg, OD_SESSION_FILE))
      return -1;

   int i=0;
   for(i=0; ;i++)
   {
      if( !(cs = config_setting_get_elem(cfg.root, i)) )
         break;

      if( !(vs = config_setting_get_member(cs, "sessid")) )
         continue;

      char *session_user = config_setting_name(cs);
      if(!session_user)
         continue;

      const char *res = config_setting_get_string(vs);
      if(res)
         if(strcmp(res, sessid)==0)
         {
            sstrncpy(user, session_user, len);
            config_destroy(&cfg);
            return 0;
         }

   }

   config_destroy(&cfg);
   return -1;
}
Example #8
0
/**
 * Print a configuration setting. 
 * @param f File stream to print to
 * @param cs Configuration setting
 * @param d Current depth.
 */
static void config_setting_fprint(FILE *f, config_setting_t * cs, int d)
{
    assert(cs && d >= 0);

    int i;
    for (i = 0; i < d; i++)
        fprintf(f, "  ");

    char *n = config_setting_name(cs);

    switch (config_setting_type(cs)) {
    case CONFIG_TYPE_GROUP:
        if (d > 0)
            fprintf(f, "%s = {\n", n);

        for (i = 0; i < config_setting_length(cs); i++)
            config_setting_fprint(f, config_setting_get_elem(cs, i), d + 1);

        if (d > 0) {
            for (i = 0; i < d; i++)
                fprintf(f, "  ");
            fprintf(f, "};\n");
        }
        break;
    case CONFIG_TYPE_STRING:
        fprintf(f, "%s\t= \"%s\";\n", n, config_setting_get_string(cs));
        break;
    case CONFIG_TYPE_FLOAT:
        fprintf(f, "%s\t= %7.5f;\n", n, config_setting_get_float(cs));
        break;
    case CONFIG_TYPE_INT:
        fprintf(f, "%s\t= %d;\n", n, config_setting_get_int(cs));
        break;
    default:
        error("Unsupported type for configuration setting '%s'", n);
    }
}
Example #9
0
void load_message_file_source(void)
{
#define ARRAYLENGTH(A) ( sizeof(A)/sizeof((A)[0]) )
	config_setting_t *main_group = NULL;
	config_t group_ext;
	int index=0;
	
	if (libconfig->read_file(&group_ext, bra_config.lang_file))
		return;
	
	memset(lang_s, 0, sizeof(LANG));
	
	if((main_group = config_lookup(&group_ext, "Source"))) {
		int i, h, k = 0, groups_count = libconfig->setting_length(main_group);
		config_setting_t *group_ele;
		config_setting_t *groups;
		
		for(i = 0; i < groups_count; ++i) {
			group_ele = libconfig->setting_get_elem(main_group, i);

			while(k < ARRAYLENGTH(Source) && (groups = libconfig->setting_get_member(group_ele, Source[k]))) {
				int group_count = config_setting_length(groups);

				for(h = 0; h < group_count; h++) {
					config_setting_t *group_e = libconfig->setting_get_elem(groups, h);

					copy_to_list((char *)config_setting_name(group_e), (char *)config_setting_get_string_elem(groups, h), index);
					index++;
				}
				k++;
			}
		}
	}
	
	ShowConf("Leitura de '"CL_WHITE"%d"CL_RESET"' mensagens em '"CL_WHITE"%s"CL_RESET"'.\n", index, bra_config.lang_file);
}
Example #10
0
/*********************
return RET_NOK on error
*********************/
ret_code_t entry_copy_config(config_setting_t * source, config_setting_t * dest)
{
	config_setting_t * new_source;
	config_setting_t * new_dest;
	int index = -1;
	int int_value;
	long long long_value;
	double double_value;
	const char * string;

	while((new_source=config_setting_get_elem(source,index+1))!= NULL ) {
		index++;
		if(config_setting_is_group(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_GROUP)) {
				return RET_NOK;
			}
		}

		else if(config_setting_is_array(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_ARRAY)) {
				return RET_NOK;
			}
		}

		else if(config_setting_is_list(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_LIST)) {
				return RET_NOK;
			}
		}

		else {
			switch(config_setting_type(new_source)) {
			case CONFIG_TYPE_INT:
				int_value = config_setting_get_int(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_INT);
				config_setting_set_int(new_dest,int_value);
				continue;
			case CONFIG_TYPE_INT64:
				long_value = config_setting_get_int64(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_INT64);
				config_setting_set_int64(new_dest,long_value);
				continue;
			case CONFIG_TYPE_FLOAT:
				double_value = config_setting_get_float(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_FLOAT);
				config_setting_set_float(new_dest,double_value);
				continue;
			case CONFIG_TYPE_BOOL:
				int_value = config_setting_get_bool(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_BOOL);
				config_setting_set_bool(new_dest,int_value);
				continue;
			case CONFIG_TYPE_STRING:
				string = config_setting_get_string(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_STRING);
				config_setting_set_string(new_dest,string);
				continue;
			default:
				return RET_NOK;
			}
		}
	}

	return RET_OK;
}
Example #11
0
static const char *test_libconfig_setting_get(void)
{
	struct config_t config;
	struct config_setting_t *t = NULL;
	double f;
	const char *str;
	const char *input = "/* Test File */\n"
		"Setting_Int: 1;\n"
		"Setting_Int64: 1L;\n"
		"Setting_Float: 1.0;\n"
		"Setting_Bool: true;\n"
		"Setting_String: \"1\";\n"
		"Setting_Array: [ ];\n"
		"Setting_Group: { };\n"
		"Setting_List: ( );\n"
		"/* End test file */\n";

	if (libconfig->read_string(&config, input) == CONFIG_FALSE) {
		libconfig->destroy(&config);
		return "Unable to parse configuration.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Int")) == NULL || libconfig->setting_get_int(t) != 1) {
		libconfig->destroy(&config);
		return "libconfig->setting_get_int failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Int64")) == NULL || libconfig->setting_get_int64(t) != 1) {
		libconfig->destroy(&config);
		return "libconfig->lookup_int64 failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Float")) == NULL || (f = libconfig->setting_get_float(t)) < 1.0 - 0.1 || f > 1.0 + 0.1) {
		libconfig->destroy(&config);
		return "libconfig->lookup_float failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Bool")) == NULL || libconfig->setting_get_bool(t) != 1) {
		libconfig->destroy(&config);
		return "libconfig->lookup_bool failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_String")) == NULL || (str = libconfig->setting_get_string(t)) == NULL || str[0] != '1' || str[1] != '\0') {
		libconfig->destroy(&config);
		return "libconfig->lookup_string failed.";
	}

	t = config_root_setting(&config);

	if (libconfig->setting_get_int_elem(t, 0) != 1) {
		libconfig->destroy(&config);
		return "libconfig->setting_get_int_elem failed.";
	}

	if (libconfig->setting_get_int64_elem(t, 1) != 1) {
		libconfig->destroy(&config);
		return "libconfig->setting_get_int64_elem failed.";
	}

	if ((f = libconfig->setting_get_float_elem(t, 2)) < 1.0 - 0.1 || f > 1.0 + 0.1) {
		libconfig->destroy(&config);
		return "libconfig->setting_get_float_elem failed.";
	}

	if (libconfig->setting_get_bool_elem(t, 3) != 1) {
		libconfig->destroy(&config);
		return "libconfig->setting_get_bool_elem failed.";
	}

	if ((str = libconfig->setting_get_string_elem(t, 4)) == NULL || str[0] != '1' || str[1] != '\0') {
		libconfig->destroy(&config);
		return "libconfig->setting_get_string_elem failed.";
	}

	if ((t = libconfig->setting_get_elem(config.root, 0)) == NULL || libconfig->setting_get_int(t) != 1) {
		libconfig->destroy(&config);
		return "libconfig->setting_get_elem failed.";
	}

	if ((t = libconfig->setting_get_member(config.root, "Setting_Int")) == NULL || libconfig->setting_get_int(t) != 1 || strcmp(config_setting_name(t), "Setting_Int") != 0) {
		libconfig->destroy(&config);
		return "libconfig->setting_get_member failed.";
	}

	if ((t = libconfig->setting_get_elem(config.root, 0)) == NULL || strcmp(config_setting_name(t), "Setting_Int") != 0) {
		libconfig->destroy(&config);
		return "config_setting_name failed.";
	}

	if ((t = libconfig->setting_get_member(config.root, "Setting_Int")) == NULL || libconfig->setting_index(t) != 0) {
		libconfig->destroy(&config);
		return "libconfig->setting_index failed.";
	}

	if (libconfig->setting_length(config.root) != 8) {
		libconfig->destroy(&config);
		return "libconfig->setting_length failed.";
	}

	libconfig->destroy(&config);
	return NULL;
}
Example #12
0
static void ya_setup_bar(config_setting_t * set) {
	int retcnf, retint;
	const char *retstr;
	ya_bar_t *inherit_bar;
	ya_bar_t *bar = calloc(1, sizeof(ya_bar_t));
	if (ya.curbar) {
		bar->prev_bar = ya.curbar;
		ya.curbar->next_bar = bar;
	}
	ya.curbar = bar;
	bar->name = strdup(config_setting_name(set));
	retcnf = config_setting_lookup_string(set, "inherit", &retstr);
	if(retcnf == CONFIG_TRUE) {
		if (ya_inherit_bar(bar, retstr, false, NULL) == -1) {
			fprintf(stderr, "Skipping bar (%s).\n", bar->name);
			free(bar->name);
			free(bar);
			return;
		}
	}
	retcnf = config_setting_lookup_string(set, "inherit-all", &retstr);
	if(retcnf == CONFIG_TRUE) {
		if (ya_inherit_bar(bar, retstr, true, &inherit_bar) == -1) {
			fprintf(stderr, "Skipping bar (%s).\n", bar->name);
			free(bar->name);
			free(bar);
			return;
		}
	}
	retcnf = config_setting_lookup_string(set, "font", &retstr);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BAR(bar))
			bar->desc = pango_font_description_from_string(YA_DEF_FONT);
	}
	else {
		bar->desc = pango_font_description_from_string(retstr);
	}

	retcnf = config_setting_lookup_string(set, "position", &retstr);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BAR(bar))
			bar->position = YA_TOP;
	}
	else {
		if(strcmp(retstr, "top")==0) {
			bar->position = YA_TOP;
		}
		else if(strcmp(retstr, "bottom")==0) {
			bar->position = YA_BOTTOM;
		}
		else if(strcmp(retstr, "left")==0) {
			bar->position = YA_LEFT;
		}
		else if(strcmp(retstr, "right")==0) {
			bar->position = YA_RIGHT;
		}
		else{
			bar->position = YA_TOP;
		}
	}
	retcnf = config_setting_lookup_string(set, "monitor", &retstr);
	if(retcnf == CONFIG_FALSE) {
		//If not explicitly specified, fall back to the first active monitor.
		if((ya.gen_flag & GEN_RANDR) && NOT_INHERIT_BAR(bar)) {
			for(bar->mon= ya.curmon; bar->mon->prev_mon; bar->mon = bar->mon->prev_mon);
		}
	}
	else {
		if((ya.gen_flag & GEN_RANDR)) {
			bar->mon = ya_get_monitor_from_name(retstr);
			//If null, fall back to the first active monitor
			if(bar->mon == NULL)
				for(bar->mon= ya.curmon; bar->mon->prev_mon; bar->mon = bar->mon->prev_mon);
		}
	}

	bool is_gap_horizontal_defined = false;
	retcnf = config_setting_lookup_int(set, "gap-horizontal", &retint);
	if(retcnf == CONFIG_TRUE) {
		bar->hgap = retint;
		is_gap_horizontal_defined = true;
	}

	retcnf = config_setting_lookup_int(set, "gap-vertical", &retint);
	if(retcnf == CONFIG_TRUE) {
		bar->vgap = retint;
	}
	retcnf = config_setting_lookup_int(set, "height", &retint);
	if(retcnf == CONFIG_FALSE) {
		if (NOT_INHERIT_BAR(bar))
			bar->height = 20;
	}
	else {
		bar->height = retint;
	}
	retcnf = config_setting_lookup_int(set, "width", &retint);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BAR(bar)) {
			if(bar->mon) {
				bar->width = bar->mon->pos.width - 2*(bar->hgap);
			}
			else {
				bar->width = ya.scr->width_in_pixels - 2*(bar->hgap);
			}
		}
	}
	else {
		bar->width = retint;
		if(!is_gap_horizontal_defined) {
			if((ya.gen_flag & GEN_RANDR)) {
				bar->hgap = (bar->mon->pos.width - bar->width) /2;
			}
			else {
				bar->hgap = (ya.scr->width_in_pixels - bar->width) /2;
			}
		}
	}
	retcnf = config_setting_lookup_int(set, "underline-size", &retint);
	if(retcnf == CONFIG_TRUE) {
		bar->ulsize = retint;
	}
	retcnf = config_setting_lookup_int(set, "overline-size", &retint);
	if(retcnf == CONFIG_TRUE) {
		bar->olsize = retint;
	}
	retcnf = config_setting_lookup_int(set, "background-color-argb", &retint);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BAR(bar))
			bar->bgcolor = 0xff1d1d1d;
	}
	else {
		bar->bgcolor = retint;
	}
	retcnf = config_setting_lookup_int(set, "background-color-rgb", &retint);
	if(retcnf == CONFIG_TRUE) {
		bar->bgcolor = retint | 0xff000000;
	}
	retcnf = config_setting_lookup_int(set, "slack-size", &retint);
	if(retcnf == CONFIG_TRUE) {
		bar->slack = retint;
	}
	retcnf = config_setting_lookup_int(set, "border-size", &retint);
	if(retcnf == CONFIG_TRUE) {
		bar->brsize = retint;
		retcnf = config_setting_lookup_int(set, "border-color-rgb", &retint);
		if(retcnf == CONFIG_TRUE) {
			bar->brcolor = retint;
		}
	}

	retcnf = config_setting_lookup_string(set, "command-button1", &retstr);
	if(retcnf == CONFIG_TRUE) {
		bar->button_cmd[0] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "command-button2", &retstr);
	if(retcnf == CONFIG_TRUE) {
		bar->button_cmd[1] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "command-button3", &retstr);
	if(retcnf == CONFIG_TRUE) {
		bar->button_cmd[2] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "command-button4", &retstr);
	if(retcnf == CONFIG_TRUE) {
		bar->button_cmd[3] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "command-button5", &retstr);
	if(retcnf == CONFIG_TRUE) {
		bar->button_cmd[4] = strdup(retstr);
	}


	ya_create_bar(bar);
	if(bar->attr & BARA_INHERIT_ALL) {
		ya_block_t * dstblk, *srcblk;
		for(int i=0; i <3; i++) {
			if((srcblk = inherit_bar->curblk[i])) {
				for(;srcblk->prev_blk; srcblk = srcblk->prev_blk);
				for(;srcblk; srcblk = srcblk->next_blk) {
					dstblk = calloc(1, sizeof(ya_block_t));
					ya_copy_blk_members(dstblk, srcblk);
					dstblk->buf = calloc(1, dstblk->bufsize);
					ya_create_block(dstblk);
				}
			}
		}
	}
}
Example #13
0
static int processCommand(void *event_header, void *event) {
    int count = 0;
    int entry_count = 0;
    int total_root_entries = 0;
    int total_child_entries = 0;
    
    config_setting_t *config_root = NULL;
    config_setting_t *config_child = NULL;
    config_setting_t *config_entry = NULL;
    const char *current_header = NULL;
    const char *current_event = NULL;
    int int_setting = 0;

    int status_file;
    char status_file_path[MSQ_SIZE];
    
    config_root = config_root_setting(&config);
    total_root_entries = config_setting_length(config_root);
    
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    pthread_mutex_lock(&lockConfig);
    
    for(count = 0; count < total_root_entries; count++) {
        config_child = config_setting_get_elem(config_root, count);
        if(!config_setting_is_group(config_child) ||
        !config_setting_lookup_bool(config_child, "register", &int_setting) || int_setting == 0 ||
        !(config_entry = config_setting_get_member(config_child, "header")) || 
        (current_header = config_setting_get_string_elem(config_entry, common_data.diff_commands)) == NULL || 
        strcmp(current_header, (char *)event_header) != 0)
            continue;
        
        current_header = config_setting_name(config_child);
        total_child_entries = config_setting_length(config_child);
        
        for(entry_count = 0; entry_count < total_child_entries; entry_count++) {
            config_entry = config_setting_get_elem(config_child, entry_count);
            if((current_event = config_setting_get_string_elem(config_entry, common_data.diff_commands)) == 0 || 
            strcmp(current_event, (char *)event) != 0)
                continue;
            
            snprintf(status_file_path, MAX_PATH_LENGTH, "%s/%s.%s", TEMPLOCATION, PROGRAM_NAME, current_header);
            if((status_file = open(status_file_path, O_RDWR|O_CREAT|O_CLOEXEC, LOCKMODE)) < 0) {
            
                pthread_mutex_unlock(&lockConfig);
                pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
                
                return EXIT_FAILURE;
            }
    
            current_event = config_setting_name(config_entry);
            ftruncate(status_file, 0);
            write(status_file, current_event, strlen(current_event));
            close(status_file);
            
            statusInfo.update(current_header, current_event);
            syslog(LOG_DEBUG, "Status update event (header): %s (%s)", current_event, current_header);
            
            pthread_mutex_unlock(&lockConfig);
            pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
            
            return EXIT_SUCCESS;
        }
    }
    
    pthread_mutex_unlock(&lockConfig);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    
    return EXIT_SUCCESS;
} /* serial_process_ascii */
Example #14
0
File: vifset.c Project: ikob/i-Path
int qtbl_init(char *file, struct qtbl_t *qtbl)
{
	int i, j;
	int t_long;
	char *t_str;
	int iif, nif = 0;
	config_t cf;
	config_setting_t *ifsp, *tcfsp;
	FILE *FP;
	struct if_srvarreq ifsrr;

	glocation.flag = QDISABLE;

	gsnmpoid.flag = QDISABLE;
	strcpy(gsnmpoid.data.snmp.community, "public");

	for (i = 0 ; i < MAX_IF ; i++){
		for( j = 0 ; j < SIRENS_PMAX ; j++){ 
			qtbl[i].in[j].flag = QDISABLE;
			qtbl[i].out[j].flag = QDISABLE;
		}
	}

	if((FP = fopen(file, "r")) == NULL){
		printf("file read error \"%s\"\n", file);
		exit(1);
	}

	config_init(&cf);
	
	if(CONFIG_TRUE != config_read(&cf, FP) ){
		printf("config error line:%d %s\n", config_error_line(&cf), config_error_text(&cf));
		return(1);
	}

	fclose(FP);

/* Check global parameters, query interval ... */

	if(config_lookup_int(&cf, "default.polling", &t_long) == CONFIG_TRUE){
		interval = t_long;
	}
	if(t_long < 0 ){
		printf("config error : invalid update interval %ld\n", t_long);
		return(1);
	}

/* Check default parameters, query interval ... */
	if(config_lookup_string(&cf, "default.community", (const char **)&t_str) == CONFIG_TRUE){
		if(t_str != NULL){
			strcpy(gsnmpoid.data.snmp.community, t_str);
		}
	}
	if(vifsetdebug)
		printf("vifset default community: %s\n", gsnmpoid.data.snmp.community);

	if(config_lookup_string(&cf, "default.host", (const char **)&t_str) == CONFIG_TRUE){
		if(t_str != NULL){
			strcpy(gsnmpoid.data.snmp.host, t_str);
		}
	}
	if(vifsetdebug)
		printf("vifset default host: %s\n", gsnmpoid.data.snmp.host);
	gsnmpoid.flag = QSNMPOID;

	if((tcfsp = config_lookup(&cf, "default.location")) != NULL){
		glocation = qtblloc_set(tcfsp);
	}
	if(vifsetdebug && glocation.flag == QSTATIC)
		printf("vifset default location: %08x\n", glocation.data.val);

/* check if's */
	if((ifsp = config_lookup(&cf, "interface")) == NULL){
		printf("config error not defined any interface\n");
		exit(1);
	}
	for( iif = 0 , nif = 0; nif < MAX_IF ; iif++){
		char *iftok;
		tcfsp = config_setting_get_elem(ifsp, iif);
		if(tcfsp == NULL) break;

/* replace '_' with '.', due to libconfig restrictuon */
		strncpy(qtbl[nif].ifname, config_setting_name(tcfsp), IFNAMSIZ);
		if((iftok = strchr(qtbl[nif].ifname, '_')) != NULL){
			*iftok ='.';
		}

/* checking actual i/f or not */
		bzero(&ifsrr, sizeof(struct if_srvarreq));
#if defined(__APPLE__)
		strncpy(ifsrr.ifr_name, qtbl[nif].ifname, IFNAMSIZ);
		if(ioctl(fd, SIOCGSRVAR, &ifsrr) < 0){
			printf("failed in %s\n", qtbl[nif].ifname);
			continue;
		};
#endif /* defined(__APPLE__) */
#if defined(__linux__) || defined(__FreeBSD__)
		strncpy(ifsrr.ifrname, qtbl[nif].ifname, IFNAMSIZ);
	{
		socklen_t slen = sizeof(ifsrr);
		if(getsockopt(fd, IPPROTO_IP, IPSIRENS_SRVAR, &ifsrr, &slen)
				< 0){
			printf("failed in %s\n", qtbl[nif].ifname);
			continue;
		};
		if(setsockopt(fd, IPPROTO_IP, IPSIRENS_SRFIL, &i, sizeof(i)) < 0){
			printf("failed in IPSIRENS_SRFIL\n");
		}
	}
#endif /* defined(__linux__) || defined (__FreeBSD__)*/
		if(qtbl_if_set(tcfsp, &(qtbl[nif]), gsnmpoid, glocation) == 0)
			continue;
		nif++;
	}
	if(nif == MAX_IF){
		printf("vifset Too much if's %d >  %d\n", nif, MAX_IF);
		exit(1);
	}
	return nif;
}
Example #15
0
/**
 * Loads group configuration from config file into memory.
 * @private
 */
static void read_config(void)
{
	config_setting_t *groups = NULL;
	const char *config_filename = "conf/groups.conf"; // FIXME hardcoded name
	int group_count = 0;
	
	if (conf_read_file(&pc_group_config, config_filename))
		return;

	groups = config_lookup(&pc_group_config, "groups");

	if (groups != NULL) {
		GroupSettings *group_settings = NULL;
		DBIterator *iter = NULL;
		int i, loop = 0;

		group_count = config_setting_length(groups);
		for (i = 0; i < group_count; ++i) {
			int id = 0, level = 0;
			const char *groupname = NULL;
			int log_commands = 0;
			config_setting_t *group = config_setting_get_elem(groups, i);

			if (!config_setting_lookup_int(group, "id", &id)) {
				ShowConfigWarning(group, "pc_groups:read_config: \"groups\" list member #%d has undefined id, removing...", i);
				config_setting_remove_elem(groups, i);
				--i;
				--group_count;
				continue;
			}

			if (id2group(id) != NULL) {
				ShowConfigWarning(group, "pc_groups:read_config: duplicate group id %d, removing...", i);
				config_setting_remove_elem(groups, i);
				--i;
				--group_count;
				continue;
			}

			config_setting_lookup_int(group, "level", &level);
			config_setting_lookup_bool(group, "log_commands", &log_commands);

			if (!config_setting_lookup_string(group, "name", &groupname)) {
				char temp[20];
				config_setting_t *name = NULL;
				snprintf(temp, sizeof(temp), "Group %d", id);
				if ((name = config_setting_add(group, "name", CONFIG_TYPE_STRING)) == NULL ||
				    !config_setting_set_string(name, temp)) {
					ShowError("pc_groups:read_config: failed to set missing group name, id=%d, skipping... (%s:%d)\n",
					          id, config_setting_source_file(group), config_setting_source_line(group));
					continue;
				}
				config_setting_lookup_string(group, "name", &groupname); // Retrieve the pointer
			}

			if (name2group(groupname) != NULL) {
				ShowConfigWarning(group, "pc_groups:read_config: duplicate group name %s, removing...", groupname);
				config_setting_remove_elem(groups, i);
				--i;
				--group_count;
				continue;
			}

			CREATE(group_settings, GroupSettings, 1);
			group_settings->id = id;
			group_settings->level = level;
			group_settings->name = groupname;
			group_settings->log_commands = (bool)log_commands;
			group_settings->inherit = config_setting_get_member(group, "inherit");
			group_settings->commands = config_setting_get_member(group, "commands");
			group_settings->permissions = config_setting_get_member(group, "permissions");
			group_settings->inheritance_done = false;
			group_settings->root = group;
			group_settings->group_pos = i;

			strdb_put(pc_groupname_db, groupname, group_settings);
			idb_put(pc_group_db, id, group_settings);
			
		}
		group_count = config_setting_length(groups); // Save number of groups
		
		// Check if all commands and permissions exist
		iter = db_iterator(pc_group_db);
		for (group_settings = dbi_first(iter); dbi_exists(iter); group_settings = dbi_next(iter)) {
			config_setting_t *commands = group_settings->commands, *permissions = group_settings->permissions;
			int count = 0, j;

			// Make sure there is "commands" group
			if (commands == NULL)
				commands = group_settings->commands = config_setting_add(group_settings->root, "commands", CONFIG_TYPE_GROUP);
			count = config_setting_length(commands);

			for (j = 0; j < count; ++j) {
				config_setting_t *command = config_setting_get_elem(commands, j);
				const char *name = config_setting_name(command);
				if (!atcommand_exists(name)) {
					ShowConfigWarning(command, "pc_groups:read_config: non-existent command name '%s', removing...", name);
					config_setting_remove(commands, name);
					--j;
					--count;
				}
			}

			// Make sure there is "permissions" group
			if (permissions == NULL)
				permissions = group_settings->permissions = config_setting_add(group_settings->root, "permissions", CONFIG_TYPE_GROUP);
			count = config_setting_length(permissions);

			for(j = 0; j < count; ++j) {
				config_setting_t *permission = config_setting_get_elem(permissions, j);
				const char *name = config_setting_name(permission);
				int p;

				ARR_FIND(0, ARRAYLENGTH(pc_g_permission_name), p, strcmp(pc_g_permission_name[p].name, name) == 0);
				if (p == ARRAYLENGTH(pc_g_permission_name)) {
					ShowConfigWarning(permission, "pc_groups:read_config: non-existent permission name '%s', removing...", name);
					config_setting_remove(permissions, name);
					--p;
					--count;
				}
			}
		}
		dbi_destroy(iter);

		// Apply inheritance
		i = 0; // counter for processed groups
		while (i < group_count) {
			iter = db_iterator(pc_group_db);
			for (group_settings = dbi_first(iter); dbi_exists(iter); group_settings = dbi_next(iter)) {
				config_setting_t *inherit = NULL,
				                 *commands = group_settings->commands,
					             *permissions = group_settings->permissions;
				int j, inherit_count = 0, done = 0;
				
				if (group_settings->inheritance_done) // group already processed
					continue; 

				if ((inherit = group_settings->inherit) == NULL ||
				    (inherit_count = config_setting_length(inherit)) <= 0) { // this group does not inherit from others
					++i;
					group_settings->inheritance_done = true;
					continue;
				}
				
				for (j = 0; j < inherit_count; ++j) {
					GroupSettings *inherited_group = NULL;
					const char *groupname = config_setting_get_string_elem(inherit, j);

					if (groupname == NULL) {
						ShowConfigWarning(inherit, "pc_groups:read_config: \"inherit\" array member #%d is not a name, removing...", j);
						config_setting_remove_elem(inherit,j);
						continue;
					}
					if ((inherited_group = name2group(groupname)) == NULL) {
						ShowConfigWarning(inherit, "pc_groups:read_config: non-existent group name \"%s\", removing...", groupname);
						config_setting_remove_elem(inherit,j);
						continue;
					}
					if (!inherited_group->inheritance_done)
						continue; // we need to do that group first

					// Copy settings (commands/permissions) that are not defined yet
					if (inherited_group->commands != NULL) {
						int l = 0, commands_count = config_setting_length(inherited_group->commands);
						for (l = 0; l < commands_count; ++l)
							config_setting_copy(commands, config_setting_get_elem(inherited_group->commands, l));
					}

					if (inherited_group->permissions != NULL) {
						int l = 0, permissions_count = config_setting_length(inherited_group->permissions);
						for (l = 0; l < permissions_count; ++l)
							config_setting_copy(permissions, config_setting_get_elem(inherited_group->permissions, l));
					}

					++done; // copied commands and permissions from one of inherited groups
				}
				
				if (done == inherit_count) { // copied commands from all of inherited groups
					++i;
					group_settings->inheritance_done = true; // we're done with this group
				}
			}
			dbi_destroy(iter);

			if (++loop > group_count) {
				ShowWarning("pc_groups:read_config: Could not process inheritance rules, check your config '%s' for cycles...\n",
				            config_filename);
				break;
			}
		} // while(i < group_count)

		// Pack permissions into GroupSettings.e_permissions for faster checking
		iter = db_iterator(pc_group_db);
		for (group_settings = dbi_first(iter); dbi_exists(iter); group_settings = dbi_next(iter)) {
			config_setting_t *permissions = group_settings->permissions;
			int c, count = config_setting_length(permissions);

			for (c = 0; c < count; ++c) {
				config_setting_t *perm = config_setting_get_elem(permissions, c);
				const char *name = config_setting_name(perm);
				int val = config_setting_get_bool(perm);
				int j;

				if (val == 0) // does not have this permission
					continue;
				ARR_FIND(0, ARRAYLENGTH(pc_g_permission_name), j, strcmp(pc_g_permission_name[j].name, name) == 0);
				group_settings->e_permissions |= pc_g_permission_name[j].permission;
			}
		}
		dbi_destroy(iter);
	}

	ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' groups in '"CL_WHITE"%s"CL_RESET"'.\n", group_count, config_filename);

	
	if( ( pc_group_max = group_count ) ) {
		DBIterator *iter = db_iterator(pc_group_db);
		GroupSettings *group_settings = NULL;
		int* group_ids = aMalloc( pc_group_max * sizeof(int) );
		int i = 0;
		for (group_settings = dbi_first(iter); dbi_exists(iter); group_settings = dbi_next(iter)) {
			group_ids[i++] = group_settings->id;
		}
		
		atcommand_db_load_groups(group_ids);
		
		aFree(group_ids);
		
		dbi_destroy(iter);
	}
}
Example #16
0
static void ya_setup_block(config_setting_t * set) {
	struct ya_block * blk = calloc(1,sizeof(ya_block_t));
	int retcnf, retint;
	const char *retstr;

	blk->pid = -1;
	blk->name = strdup(config_setting_name(set));

	retcnf = config_setting_lookup_string(set, "inherit", &retstr);
	if(retcnf == CONFIG_TRUE) {
		if (ya_inherit_blk(blk, retstr) == -1) {
			fprintf(stderr, "Skipping block (%s.%s)\n", blk->bar->name, blk->name);
			free(blk->name);
			free(blk);
			return;
		}
	}

	retcnf = config_setting_lookup_string(set, "exec", &retstr);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BLK(blk)) {
			fprintf(stderr, "No exec is defined for block: %s.%s. Skipping this block...\n", blk->bar->name, blk->name);
			free(blk->name);
			free(blk);
			return;
		}
	}
	else if(strlen(retstr) < 1) {
		fprintf(stderr, "exec entry is empty for block: %s.%s. Skipping this block...\n", blk->bar->name, blk->name);
		free(blk->name);
		free(blk);
		return;
	
	}
	else {
#ifdef YA_INTERNAL
		//check if internal found, otherwise set external
		ya_check_blk_internal(blk, set, retstr);
#else
		//just set it external
		blk->attr |= BLKA_EXTERNAL;
#endif //YA_INTERNAL
		if (blk->attr & BLKA_EXTERNAL) {
				blk->cmd = strdup(retstr);
			}
	}
	if (blk->attr & BLKA_INTERNAL)
		goto skip_type;
	retcnf = config_setting_lookup_string(set, "type", &retstr);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BLK(blk)) {
			fprintf(stderr, "No type is defined for block: %s. Skipping this block...\n", config_setting_name(set));
			free(blk->name);
			free(blk);
			return;
		}
	}
	else {
		if(strcmp(retstr, "persist")==0) {
			blk->attr |= BLKA_PERSIST;
		}
		else if(strcmp(retstr, "once")==0) {
			blk->attr |= BLKA_ONCE;
		}
		else if(strcmp(retstr, "periodic")==0) {
			blk->attr |= BLKA_PERIODIC;
		}
		else {
			//TODO handle
		}
	}
skip_type:
	retcnf = config_setting_lookup_int(set, "interval", &retint);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BLK(blk))
			blk->sleep = 5;
	}
	else {
		blk->sleep = retint;
	}

	retcnf = config_setting_lookup_string(set, "command-button1", &retstr);
	if(retcnf == CONFIG_TRUE) {
		blk->button_cmd[0] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "command-button2", &retstr);
	if(retcnf == CONFIG_TRUE) {
		blk->button_cmd[1] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "command-button3", &retstr);
	if(retcnf == CONFIG_TRUE) {
		blk->button_cmd[2] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "command-button4", &retstr);
	if(retcnf == CONFIG_TRUE) {
		blk->button_cmd[3] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "command-button5", &retstr);
	if(retcnf == CONFIG_TRUE) {
		blk->button_cmd[4] = strdup(retstr);
	}
	retcnf = config_setting_lookup_string(set, "align", &retstr);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BLK(blk))
			blk->align = A_CENTER;
	}
	else {
		if(strcmp(retstr, "left")==0) {
			blk->align =	A_LEFT;
		}
		else if(strcmp(retstr, "center")==0) {
			blk->align =	A_CENTER;
		}
		else if(strcmp(retstr, "right")==0) {
			blk->align =	A_RIGHT;
		}
		else {
			blk->align = A_CENTER;
		}
	}
	retcnf = config_setting_lookup_int(set, "fixed-size", &retint);
	if(retcnf == CONFIG_FALSE) {
		if(NOT_INHERIT_BLK(blk))
			blk->width = 80;
	}
	else {
		blk->width = retint;
	}
	retcnf = config_setting_lookup_bool(set, "pango-markup", &retint);
	if((retcnf == CONFIG_TRUE) && retint) {
		blk->attr |= BLKA_MARKUP_PANGO;
	}

	retcnf = config_setting_lookup_int(set, "background-color-argb", &retint);
	if(retcnf == CONFIG_TRUE) {
		blk->bgcolor = (uint32_t) retint;
		blk->attr |= BLKA_BGCOLOR;
	}
	retcnf = config_setting_lookup_int(set, "background-color-rgb", &retint);
	if(retcnf == CONFIG_TRUE) {
		blk->bgcolor = retint | 0xff000000;
		blk->attr |= BLKA_BGCOLOR;
	}
	retcnf = config_setting_lookup_int(set, "foreground-color-argb", &retint);
	if(retcnf == CONFIG_TRUE) {
		blk->fgcolor = retint;
		blk->attr |= BLKA_FGCOLOR;
	}
	retcnf = config_setting_lookup_int(set, "foreground-color-rgb", &retint);
	if(retcnf == CONFIG_TRUE) {
		blk->fgcolor = retint | 0xff000000;
		blk->attr |= BLKA_FGCOLOR;
	}
	else {
		blk->fgcolor = 0xffffffff;
	}
	retcnf = config_setting_lookup_int(set, "underline-color-argb", &retint);
	if(retcnf == CONFIG_TRUE) {
		blk->ulcolor = retint;
		blk->attr |= BLKA_UNDERLINE;
	}
	retcnf = config_setting_lookup_int(set, "underline-color-rgb", &retint);
	if(retcnf == CONFIG_TRUE) {
		blk->ulcolor = retint | 0xff000000;
		blk->attr |= BLKA_UNDERLINE;
	}
	retcnf = config_setting_lookup_int(set, "overline-color-argb", &retint);
	if(retcnf == CONFIG_TRUE) {
		blk->olcolor = retint;
		blk->attr |= BLKA_OVERLINE;
	}
	retcnf = config_setting_lookup_int(set, "overline-color-rgb", &retint);
	if(retcnf == CONFIG_TRUE) {
		blk->olcolor = retint | 0xff000000;
		blk->attr |= BLKA_OVERLINE;
	}
	retcnf = config_setting_lookup_string(set, "justify", &retstr);
	if(retcnf == CONFIG_TRUE) {
		if(strcmp(retstr, "left")==0) {
			blk->justify =	PANGO_ALIGN_LEFT;
		}
		else if(strcmp(retstr, "center")==0) {
			blk->justify =	PANGO_ALIGN_CENTER;
		}
		else if(strcmp(retstr, "right")==0) {
			blk->justify =	PANGO_ALIGN_RIGHT;
		}
		else {
			blk->justify = PANGO_ALIGN_CENTER;
		}
	}
	else {
		if(NOT_INHERIT_BLK(blk))
			blk->justify = PANGO_ALIGN_CENTER;
	}


	if(blk->attr & BLKA_EXTERNAL) {
		if(blk->attr & BLKA_MARKUP_PANGO)
			blk->bufsize = BUFSIZE_EXT_PANGO;
		else
			blk->bufsize = BUFSIZE_EXT;
	}
	else {
		blk->bufsize = BUFSIZE_INT;
	}

	blk->buf = calloc(1, blk->bufsize);

#ifdef YA_DYN_COL
	blk->bgcolor_old = blk->bgcolor;
	blk->fgcolor_old = blk->fgcolor;
	blk->ulcolor_old = blk->ulcolor;
	blk->olcolor_old = blk->olcolor;
	blk->strbuf = blk->buf;
#endif

	ya_create_block(blk);
}
Example #17
0
/* Load and initialise the modules */
eemo_rv eemo_conf_load_modules(void)
{
	unsigned int mod_count = 0;
	config_setting_t* modules_conf = NULL;
	int i = 0;
	int loaded_modules = 0;

	/* Get a reference to the module configuration */
	modules_conf = config_lookup(eemo_conf_get_config_t(), "modules");

	if (modules_conf == NULL)
	{
		ERROR_MSG("No modules have been configured");

		return ERV_NO_MODULES;
	}

	/* Initialise list of modules */
	modules = NULL;

	/* Determine the number of configured modules */
	mod_count = config_setting_length(modules_conf);

	/* Now load the configured modules one-by-one */
	for (i = 0; i < mod_count; i++)
	{
		config_setting_t* mod_conf = NULL;
		eemo_module_spec* new_mod = NULL;
		const char* lib_path = NULL;
		eemo_plugin_get_fn_table_fn mod_getfn = NULL;
		void* mod_entry = NULL;

		/* Retrieve the module to load */
		mod_conf = config_setting_get_elem(modules_conf, i);

		if (mod_conf == NULL)
		{
			ERROR_MSG("Failed to enumerate next configured module");

			continue;
		}

		/* Retrieve the module path setting */
		if ((config_setting_lookup_string(mod_conf, "lib", &lib_path) != CONFIG_TRUE) || (lib_path == NULL))
		{
			ERROR_MSG("No library specified in module section for '%s'", config_setting_name(mod_conf));

			continue;
		}

		new_mod = (eemo_module_spec*) malloc(sizeof(eemo_module_spec));

		if (new_mod == NULL)
		{
			ERROR_MSG("Error allocating memory for a new module");

			continue;
		}

		new_mod->mod_path = strdup(lib_path);

		if (new_mod->mod_path == NULL)
		{
			ERROR_MSG("Error allocating memory for a new module");

			free(new_mod);

			continue;
		}

		new_mod->mod_conf_base = (char*) malloc((strlen("modules.") + 
		                                         strlen(config_setting_name(mod_conf)) + 
							 strlen(".modconf") + 1) * sizeof(char));

		if (new_mod->mod_conf_base == NULL)
		{
			ERROR_MSG("Error allocating memory for a new module");

			free(new_mod->mod_path);
			free(new_mod);

			continue;
		}

		sprintf(new_mod->mod_conf_base, "modules.%s.modconf", config_setting_name(mod_conf));

		/* Now try to load the module */
		new_mod->mod_handle = dlopen(new_mod->mod_path, RTLD_NOW | RTLD_LOCAL);

		if (new_mod->mod_handle == NULL)
		{
			ERROR_MSG("Failed to load module %s (%s)", new_mod->mod_path, dlerror());

			free(new_mod->mod_path);
			free(new_mod->mod_conf_base);
			free(new_mod);

			continue;
		}

		INFO_MSG("Loaded module %s", new_mod->mod_path);

		/* Retrieve the module entry point */

		/* FIXME: we need to use this ugly memcpy kludge in order to avoid the compiler complaining about
		 *        data pointers getting converted to function pointers. The reason for this is that dlsym
		 *        returns a void* which is a data pointer. In effect, dlsym violates the ANSI C standard
		 *        here... See http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html */

		mod_entry = dlsym(new_mod->mod_handle, "eemo_plugin_get_fn_table");
		memcpy(&mod_getfn, &mod_entry, sizeof(void*));

		if (mod_getfn == NULL)
		{
			ERROR_MSG("Failed to resolve entry point eemo_plugin_get_fn_table in %s", new_mod->mod_path);

			free(new_mod->mod_path);
			free(new_mod->mod_conf_base);
			free(new_mod);

			continue;
		}

		/* Retrieve the module function table */
		if ((mod_getfn)(&new_mod->mod_fn_table) != ERV_OK)
		{
			ERROR_MSG("Failed to retrieve the function table in %s", new_mod->mod_path);

			dlclose(new_mod->mod_handle);
			free(new_mod->mod_path);
			free(new_mod->mod_conf_base);
			free(new_mod);

			continue;
		}

		/* Check the function table version */
		if (new_mod->mod_fn_table->fn_table_version > EEMO_PLUGIN_FN_VERSION)
		{
			ERROR_MSG("Unsupported plugin module API version %d in module %s",
				new_mod->mod_fn_table->fn_table_version,
				new_mod->mod_path);

			dlclose(new_mod->mod_handle);
			free(new_mod->mod_path);
			free(new_mod->mod_conf_base);
			free(new_mod);

			continue;
		}

		/* Initialise the module */
		if ((new_mod->mod_fn_table->plugin_init)(&eemo_function_table, new_mod->mod_conf_base) != ERV_OK)
		{
			ERROR_MSG("Failed to initialise module %s", (new_mod->mod_fn_table->plugin_getdescription)());

			dlclose(new_mod->mod_handle);
			free(new_mod->mod_path);
			free(new_mod->mod_conf_base);
			free(new_mod);

			continue;
		}

		INFO_MSG("Initialised module %s", (new_mod->mod_fn_table->plugin_getdescription)());

		/* Add it to the list of modules */
		LL_APPEND(modules, new_mod);

		loaded_modules++;
	}

	if (loaded_modules == 0)
	{
		ERROR_MSG("Failed to load any modules");

		return ERV_NO_MODULES;
	}

	return ERV_OK;
}
Example #18
0
int load_memory(void) {
    config_setting_t *pmemory;
    config_setting_t *pblock;
    config_setting_t *args;
    config_setting_t *arg;

    int memory_items = 0;
    int arg_items = 0;
    char *name;
    const char *module;

    hw_config_t *hw_config = NULL;

    INFO("Loading memory");
    pmemory = config_lookup(&main_config, "memory");
    if(!pmemory) {
        INFO("No memory blocks to laod");
        return TRUE;
    }

    while(1) {
        pblock = config_setting_get_elem(pmemory, memory_items);
        memory_items++;
        if(!pblock)
            break;

        name = config_setting_name(pblock);

        INFO("Found memory element: %s", name);

        if(!config_setting_lookup_string(pblock, "module", &module)) {
            FATAL("Missing module entry for memory item %s", name);
            exit(1);
        }

        INFO("  Module name: %s", module);
        args = config_setting_get_member(pblock, "args");

        if (!args) {
            hw_config = malloc(sizeof(hw_config_t));
            if (!hw_config) {
                FATAL("malloc");
                exit(1);
            }

            arg_items = 0;
        } else {
            arg_items = config_setting_length(args);
            INFO("  %d arg items", arg_items);
            hw_config = malloc(sizeof(hw_config_t) +
                               (arg_items * sizeof(hw_config_item_t)));
            if(!hw_config) {
                FATAL("malloc");
                exit(1);
            }
        }

        /* now walk the args and put them in a
           config struct */
        hw_config->config_items = arg_items;
        for(int x = 0; x < arg_items; x++) {
            arg = config_setting_get_elem(args, x);
            const char *key = config_setting_name(arg);
            if(!key) {
                FATAL("Bad arg key in %s", name);
                exit(1);
            }
            const char *value = NULL;
            value = config_setting_get_string(arg);

            if(!value) {
                FATAL("Bad arg value for %s in %s",
                      key, name);
                exit(1);
            }

            hw_config->item[x].key = key;
            hw_config->item[x].value = value;

            INFO("    %s => %s", key, value);
        }

        /* now, let's load it */
        memory_load(name, module, hw_config);

        if (hw_config) {
            free(hw_config);
            hw_config = NULL;
        }
    }

    INFO("Memory loaded");
    return TRUE;
}