File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / dl.c
Revision 1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Wed Nov 23 08:50:03 2011 UTC (12 years, 7 months ago) by misho
Branches: mqtt1_0
rework again RTLM management
add rtl module for log

    1: #include "global.h"
    2: #include "rtlm.h"
    3: 
    4: 
    5: static void *acc_rtlm, *pub_rtlm, *log_rtlm;
    6: 
    7: 
    8: /*
    9:  * mqttLog() Log message to syslog
   10:  *
   11:  * @fmt = format string
   12:  * @... = argument list
   13:  * return: none
   14:  */
   15: void
   16: mqttLog(const char *fmt, ...)
   17: {
   18: 	va_list lst;
   19: 
   20: 	va_start(lst, fmt);
   21: 	vsyslog(LOG_ERR, fmt, lst);
   22: 	va_end(lst);
   23: }
   24: 
   25: void *
   26: mqttLoadRTLM(sl_config *cfg, int modtype)
   27: {
   28: 	const char *str, *attr;
   29: 	void *rtlm = NULL;
   30: 	void *(*mqttOpenRTLM)(sl_config *);
   31: 	void (*mqttCloseRTLM)(void *);
   32: 
   33: 	if (!cfg)
   34: 		return NULL;
   35: 
   36: 	switch (modtype) {
   37: 		case 0:
   38: 			attr = "acc_file";
   39: 			break;
   40: 		case 1:
   41: 			attr = "pub_file";
   42: 			break;
   43: 		default:
   44: 			attr = "log_file";
   45: 			break;
   46: 	}
   47: 
   48: 	str = (const char*) CFG(cfg_GetAttribute(cfg, CFG("mqttd"), CFG(attr)));
   49: 	if (!str) {
   50: 		mqttLog("Error:: RTL module not found\n");
   51: 		return NULL;
   52: 	}
   53: 
   54: 	rtlm = dlopen(str, RTLD_LAZY);
   55: 	if (!rtlm) {
   56: 		mqttLog("Error:: RTL module not found %s\n", dlerror());
   57: 		return NULL;
   58: 	}
   59: 
   60: 	mqttOpenRTLM = dlsym(rtlm, "mqtt_rtlm_open");
   61: 	if (!mqttOpenRTLM) {
   62: 		mqttLog("Error:: Can't found mqtt_db_open call\n");
   63: 		dlclose(rtlm);
   64: 		return NULL;
   65: 	}
   66: 	mqttCloseRTLM = dlsym(rtlm, "mqtt_rtlm_close");
   67: 	if (!mqttCloseRTLM) {
   68: 		mqttLog("Error:: Can't found mqtt_db_close call\n");
   69: 		dlclose(rtlm);
   70: 		return NULL;
   71: 	}
   72: 
   73: 	switch (modtype) {
   74: 		case 0:
   75: 			acc_rtlm = rtlm;
   76: 			mqttOpenACC = mqttOpenRTLM;
   77: 			mqttCloseACC = mqttCloseRTLM;
   78: 			break;
   79: 		case 1:
   80: 			pub_rtlm = rtlm;
   81: 			mqttOpenPUB = mqttOpenRTLM;
   82: 			mqttClosePUB = mqttCloseRTLM;
   83: 			break;
   84: 		default:
   85: 			log_rtlm = rtlm;
   86: 			mqttOpenLOG = mqttOpenRTLM;
   87: 			mqttCloseLOG = mqttCloseRTLM;
   88: 			break;
   89: 	}
   90: 
   91: 	return rtlm;
   92: }
   93: 
   94: void
   95: mqttUnloadRTLM(int modtype)
   96: {
   97: 	switch (modtype) {
   98: 		case 0:
   99: 			dlclose(acc_rtlm);
  100: 			break;
  101: 		case 1:
  102: 			dlclose(pub_rtlm);
  103: 			break;
  104: 		default:
  105: 			dlclose(log_rtlm);
  106: 			break;
  107: 	}
  108: }
  109: 
  110: inline int
  111: mqttMkDir(sl_config *cfg)
  112: {
  113: 	const char *str;
  114: 
  115: 	if (!cfg)
  116: 		return -1;
  117: 
  118: 	str = (const char*) cfg_GetAttribute(cfg, CFG("mqttd"), CFG("statedir"));
  119: 	if (!str)
  120: 		return -1;
  121: 
  122: 	if (mkdir(str, 0600) == -1 && errno != EEXIST)
  123: 		return -1;
  124: 
  125: 	return 0;
  126: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>