⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snap_svc_library_handler.c

📁 一个学习SNMP项目:tmoerlan.
💻 C
字号:
/*	snap service handler	library handler source file	(c) 2003 Willem de Bruijn	all code falls under the BSD License*//*dlopen() API programming tips (from http://www.linux.se/doc/HOWTO/Program-Library-HOWTO/dl-libraries.html)if a library exports a function named '_init' this is executed before dlopen(..) returnsequally, '_fini' will be executed before dlclose(..) returns (if it exists)unfortunately, this behaviour is currently broken under Linux due to crt.o exporting the same symbolslink the ELF object compiled using the dlopen() API againts libdl (add '-ldl' to your gcc statements)optionally (and preferably ;) you can also add the '-Wl,--export-dynamic' statementcheck the exports from a library using 'nm' (man page under nm(1))*/#include <stdlib.h>#include <dlfcn.h>  /* for the dlopen() API*/#include <dirent.h> /* for scandir() */#include <string.h>#include <errno.h>#include "d_printf.h"#include "snap_svc_library_handler.h"# define _D_EXACT_NAMLEN(d) ((d)->d_namlen)/*    LOW LEVEL dlopen() API FUNCTIONS */int snap_svc_logerrors(int dLineNo){    char* strError;	int dErrorCount = 0;		do {	    strError = (char *) dlerror();		if (strError){		    d_printf(20,"\nsnap_svc : encountered dlopen API error at line %d: %s\n", dLineNo, strError);			dErrorCount++;		}	} while (strError);		return dErrorCount;}/* open a library, return OK ? false : true */int snap_svc_open(void** hDll, char* strLib){	*hDll = dlopen(strLib,RTLD_NOW);    if (! *hDll)	    return snap_svc_logerrors(__LINE__);    else		return 0;}/* bind a library function to a pointer */int snap_svc_bind(void* hDll, char* strFunc, void** hFunc){    int dErrorsOccurred = 0;		if (!hDll){	    d_printf(20,"snap_svc : trying to bind from closed library at %d\n", __LINE__);		return -1;	}	    		*hFunc = dlsym(hDll,strFunc);	if (!hFunc)	    /* 		    it is possible to retrieve NULL handles 			therefore we test for errors before exiting with a failure		*/		dErrorsOccurred = snap_svc_logerrors(__LINE__);		return dErrorsOccurred;}/* close a library */int snap_svc_close(void** hDll){    int dErrorsOccurred = 0;	    if (! *hDll)	    d_printf(20,"snap_svc : trying to close a library with a NULL handle at %d\n", __LINE__);		    if (dlclose(*hDll))	    dErrorsOccurred = snap_svc_logerrors(__LINE__);    	*hDll = NULL;		return dErrorsOccurred;}/*    DERIVED FUNCTIONS*//* helper functions for snap_svc_openmultiple */int snap_svc_openmultiple_selector_empty(const struct dirent* curentry){	return (!strncmp(".so",&curentry->d_name[_D_EXACT_NAMLEN(curentry) - 3],3));}int snap_svc_openmultiple_selector_snapsvc(const struct dirent* curentry){		/* the macro below uses different calculation methods based on the underlying OS */    if (_D_EXACT_NAMLEN(curentry) <= (12 + 3) ) /* shorter than or equal to strncmp item1 + ".so" ? fail */	    return 0;	else	    if (strncmp(".so",&curentry->d_name[_D_EXACT_NAMLEN(curentry) - 3],3))		    return 0;		else{			if (!strncmp("libsnap_svc_", curentry->d_name, 12))			    d_printf(100,"\nsnap_svc : found a library: %s\n",curentry->d_name);            return (!strncmp("libsnap_svc_", curentry->d_name, 12));		}    }/*     open all libraries in directory strDir that are accepted by the fileselector function	returns the number of succesfully opened libraries, -1 on error*/int snap_svc_openmultiple(void*** hDllList, char* strDir, snap_svc_fileselector thisSelector){    struct dirent** returnEntries;	int dEntryCount, dCurEntry=0, dSuccessCount=0;	if (*hDllList){	    d_printf(50,"snap_svc : warning : received a non NULL pointer in snap_svc_openmultiple at lineno %d\n", __LINE__);	        *hDllList = NULL;	}		dEntryCount = scandir(strDir, &returnEntries, thisSelector, alphasort);	if (dEntryCount == -1){	    d_printf(20,"snap_svc : an error occurred while scanning a directory at lineno %d : dir %s error %d",__LINE__, strDir, errno);		return -1;	}	d_printf(100, "snap_svc : found %d lib entries\n", dEntryCount);		if (dEntryCount == 0)	    return 0;			/* try to load the files */	*hDllList = (void**) calloc( (dEntryCount + 1) , sizeof(void*) );	d_printf(100, "snap_svc : calloc'ed hDllList = %p\n", *hDllList);	for (; dCurEntry < dEntryCount; dCurEntry++){		d_printf(100, "snap_svc : doing entry %d\n", dCurEntry);	    if (! snap_svc_open(&(*hDllList)[dCurEntry], returnEntries[dCurEntry]->d_name) )		    dSuccessCount++;		if ((int) (*hDllList)[dCurEntry] == -1)		    d_printf(20,"snap_svc : warning : a libpointer to -1 was returned at line %d, this will break memory mgmt\n",__LINE__);		free (returnEntries[dCurEntry]);	}	(int) ((*hDllList)[dEntryCount]) = -1;	free (returnEntries);		d_printf(100, "snap_svc: returning dSuccessCount = %d from snap_svc_openmultiple(...)\n", dSuccessCount);	return dSuccessCount;}/* try to close multiple libraries */int snap_svc_closemultiple(void*** hDllList){    int dCurEntry = 0, dErrorCount = 0;		if (!*hDllList)	    return 0;		    while (((int) ((*hDllList)[dCurEntry])) != -1){ /* while not at end of queue */		if ( (*hDllList)[dCurEntry] ){		    dErrorCount = snap_svc_close(&(*hDllList)[dCurEntry]) % 1;			(*hDllList)[dCurEntry] = NULL;		}	    dCurEntry++;	}	free (*hDllList);	hDllList = NULL;		return dErrorCount;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -