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

📄 read_config.c

📁 snmp up 2
💻 C
📖 第 1 页 / 共 4 页
字号:
    }    /*     * use the original string instead since strtok messed up the original      */    line = skip_white(line + (cptr - buf) + strlen(cptr) + 1);    return (run_config_handler(lptr, cptr, line, when));}intnetsnmp_config(char *line){    int             ret = SNMP_ERR_NOERROR;    DEBUGMSGTL(("snmp_config", "remembering line \"%s\"\n", line));    netsnmp_config_remember(line);      /* always remember it so it's read                                         * processed after a free_config()                                         * call */    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 			       NETSNMP_DS_LIB_HAVE_READ_CONFIG)) {        DEBUGMSGTL(("snmp_config", "  ... processing it now\n"));        ret = snmp_config_when(line, NORMAL_CONFIG);    }    return ret;}voidnetsnmp_config_remember_in_list(char *line,                                struct read_config_memory **mem){    if (mem == NULL)        return;    while (*mem != NULL)        mem = &((*mem)->next);    *mem = SNMP_MALLOC_STRUCT(read_config_memory);    if (line)        (*mem)->line = strdup(line);}voidnetsnmp_config_remember_free_list(struct read_config_memory **mem){    struct read_config_memory *tmpmem;    while (*mem) {        SNMP_FREE((*mem)->line);        tmpmem = (*mem)->next;        free(*mem);        *mem = NULL;        mem = &tmpmem;    }}voidnetsnmp_config_process_memory_list(struct read_config_memory **memp,                                   int when, int clear){    struct read_config_memory *mem;    if (!memp)        return;    mem = *memp;    while (mem) {        DEBUGMSGTL(("read_config", "processing memory: %s\n", mem->line));        snmp_config_when(mem->line, when);        mem = mem->next;    }    if (clear)        netsnmp_config_remember_free_list(memp);}/* * default storage location implementation  */static struct read_config_memory *memorylist = NULL;voidnetsnmp_config_remember(char *line){    netsnmp_config_remember_in_list(line, &memorylist);}voidnetsnmp_config_process_memories(void){    netsnmp_config_process_memory_list(&memorylist, EITHER_CONFIG, 1);}voidnetsnmp_config_process_memories_when(int when, int clear){    netsnmp_config_process_memory_list(&memorylist, when, clear);}/*******************************************************************-o-****** * read_config * * Parameters: *	*filename *	*line_handler *	 when * * Read <filename> and process each line in accordance with the list of * <line_handler> functions. * * * For each line in <filename>, search the list of <line_handler>'s  * for an entry that matches the first token on the line.  This comparison is * case insensitive. * * For each match, check that <when> is the designated time for the * <line_handler> function to be executed before processing the line. */voidread_config(const char *filename,            struct config_line *line_handler, int when){    FILE           *ifile;    char            line[STRINGMAX], token[STRINGMAX], tmpbuf[STRINGMAX];    char           *cptr;    int             i;    struct config_line *lptr;    linecount = 0;    curfilename = filename;    if ((ifile = fopen(filename, "r")) == NULL) {#ifdef ENOENT        if (errno == ENOENT) {            DEBUGMSGTL(("read_config", "%s: %s\n", filename,                        strerror(errno)));        } else#endif                          /* ENOENT */#ifdef EACCES        if (errno == EACCES) {            DEBUGMSGTL(("read_config", "%s: %s\n", filename,                        strerror(errno)));        } else#endif                          /* EACCES */#if defined(ENOENT) || defined(EACCES)        {            snmp_log_perror(filename);        }#else                           /* defined(ENOENT) || defined(EACCES) */            snmp_log_perror(filename);#endif                          /* ENOENT */        return;    } else {        DEBUGMSGTL(("read_config", "Reading configuration %s\n",                    filename));    }    while (fgets(line, sizeof(line), ifile) != NULL) {        lptr = line_handler;        linecount++;        cptr = line;        i = strlen(line) - 1;        if (line[i] == '\n')            line[i] = 0;        /*         * check blank line or # comment          */        if ((cptr = skip_white(cptr))) {            cptr = copy_nword(cptr, token, sizeof(token));            if (token[0] == '[') {                token[strlen(token) - 1] = '\0';                lptr = read_config_get_handlers(&token[1]);                if (lptr == NULL) {                    snprintf(tmpbuf, sizeof(tmpbuf),                            "No handlers regestered for type %s.",                            &token[1]);                    tmpbuf[ sizeof(tmpbuf)-1 ] = 0;                    config_perror(tmpbuf);                    continue;                }                DEBUGMSGTL(("read_config",                            "Switching to new context: %s%s\n",                            ((cptr) ? "(this line only) " : ""),                            &token[1]));                if (cptr == NULL) {                    /*                     * change context permanently                      */                    line_handler = lptr;                    continue;                } else {                    /*                     * the rest of this line only applies.                      */                    cptr = copy_nword(cptr, token, sizeof(token));                }            } else {                lptr = line_handler;            }            if (cptr == NULL) {                snprintf(tmpbuf, sizeof(tmpbuf),                        "Blank line following %s token.", token);                tmpbuf[ sizeof(tmpbuf)-1 ] = 0;                config_perror(tmpbuf);            } else {                DEBUGMSGTL(("read_config", "%s:%d examining: %s\n",                            filename, linecount, line));                run_config_handler(lptr, token, cptr, when);            }        }    }    fclose(ifile);    return;}                               /* end read_config() */voidfree_config(void){    struct config_files *ctmp = config_files;    struct config_line *ltmp;    for (; ctmp != NULL; ctmp = ctmp->next)        for (ltmp = ctmp->start; ltmp != NULL; ltmp = ltmp->next)            if (ltmp->free_func)                (*(ltmp->free_func)) ();}voidread_configs(void){    char *optional_config = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 					       NETSNMP_DS_LIB_OPTIONALCONFIG);    char *type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 				       NETSNMP_DS_LIB_APPTYPE);    DEBUGMSGTL(("read_config", "reading normal configuration tokens\n"));    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 				NETSNMP_DS_LIB_DONT_READ_CONFIGS)) {        read_config_files(NORMAL_CONFIG);    }    /*     * do this even when the normal above wasn't done      */    if (optional_config && type) {        struct stat     statbuf;        if (stat(optional_config, &statbuf)) {            DEBUGMSGTL(("read_config",                        "Optional File \"%s\" does not exist.\n",                        optional_config));            snmp_log_perror(optional_config);        } else {            DEBUGMSGTL(("read_config",                        "Reading optional config file: \"%s\"\n",                        optional_config));            read_config_with_type(optional_config, type);        }    }    netsnmp_config_process_memories_when(NORMAL_CONFIG, 1);    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, 			   NETSNMP_DS_LIB_HAVE_READ_CONFIG, 1);    snmp_call_callbacks(SNMP_CALLBACK_LIBRARY,                        SNMP_CALLBACK_POST_READ_CONFIG, NULL);}voidread_premib_configs(void){    DEBUGMSGTL(("read_config", "reading premib configuration tokens\n"));    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 				NETSNMP_DS_LIB_DONT_READ_CONFIGS)) {        read_config_files(PREMIB_CONFIG);    }    netsnmp_config_process_memories_when(PREMIB_CONFIG, 0);    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, 			   NETSNMP_DS_LIB_HAVE_READ_PREMIB_CONFIG, 1);    snmp_call_callbacks(SNMP_CALLBACK_LIBRARY,                        SNMP_CALLBACK_POST_PREMIB_READ_CONFIG, NULL);}/*******************************************************************-o-****** * set_configuration_directory * * Parameters: *      char *dir - value of the directory * Sets the configuration directory. Multiple directories can be * specified, but need to be seperated by 'ENV_SEPARATOR_CHAR'. */voidset_configuration_directory(const char *dir){    netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 			  NETSNMP_DS_LIB_CONFIGURATION_DIR, dir);}/*******************************************************************-o-****** * get_configuration_directory * * Parameters: - * Retrieve the configuration directory or directories. * (For backwards compatibility that is: *       SNMPCONFPATH, SNMPSHAREPATH, SNMPLIBPATH, HOME/.snmp * First check whether the value is set. * If not set give it the default value. * Return the value. * We always retrieve it new, since we have to do it anyway if it is just set. */const char     *get_configuration_directory(){    char            defaultPath[SPRINT_MAX_LEN];    char           *homepath;
	LPTSTR          lpszSystemInfo;     //机器system目录的完全路经   
	char           *systempath = NULL;    if (NULL == netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 				      NETSNMP_DS_LIB_CONFIGURATION_DIR)) {        homepath = getenv("HOME");        snprintf(defaultPath, sizeof(defaultPath), "%s%c%s%c%s%s%s%s",                SNMPCONFPATH, ENV_SEPARATOR_CHAR,                SNMPSHAREPATH, ENV_SEPARATOR_CHAR, SNMPLIBPATH,                ((homepath == NULL) ? "" : ENV_SEPARATOR),                ((homepath == NULL) ? "" : homepath),                ((homepath == NULL) ? "" : "/.snmp"));        
		defaultPath[ sizeof(defaultPath)-1 ] = 0;
		//point  the snmpd.conf file path as defaultPath  value
        //systempath="c:\\";
		//strcpy(defaultPath,systempath);
        //if (GetSystemDirectory(lpszSystemInfo, MAX_PATH+1)) {
        //     systempath=lpszSystemInfo; 
		//}
		strcpy(defaultPath,systempath);
        set_configuration_directory(defaultPath);    }    return (netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 				  NETSNMP_DS_LIB_CONFIGURATION_DIR));}/*******************************************************************-o-****** * set_persistent_directory * * Parameters: *      char *dir - value of the directory * Sets the configuration directory.  * No multiple directories may be specified. * (However, this is not checked) */voidset_persistent_directory(const char *dir){    netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, 			  NETSNMP_DS_LIB_PERSISTENT_DIR, dir);}/*******************************************************************-o-****** * get_persistent_directory * * Parameters: - * Function will retrieve the persisten directory value. * First check whether the value is set. * If not set give it the default value. * Return the value.  * We always retrieve it new, since we have to do it anyway if it is just set. */const char     *get_persistent_directory(){    if (NULL == netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 				      NETSNMP_DS_LIB_PERSISTENT_DIR)) {        set_persistent_directory(PERSISTENT_DIRECTORY);    }    return (netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 				  NETSNMP_DS_LIB_PERSISTENT_DIR));}/*******************************************************************-o-****** * read_config_files * * Parameters: *	when	== PREMIB_CONFIG, NORMAL_CONFIG  -or-  EITHER_CONFIG * * * Traverse the list of config file types, performing the following actions * for each -- * * First, build a search path for config files.  If the contents of  * environment variable SNMPCONFPATH are NULL, then use the following * path list (where the last entry exists only if HOME is non-null): * *	SNMPSHAREPATH:SNMPLIBPATH:${HOME}/.snmp * * Then, In each of these directories, read config files by the name of: * *	<dir>/<fileHeader>.conf		-AND- *	<dir>/<fileHeader>.local.conf * * where <fileHeader> is taken from the config file type structure. * * * PREMIB_CONFIG causes free_config() to be invoked prior to any other action. * * * EXITs if any 'config_errors' are logged while parsing config file lines. */void

⌨️ 快捷键说明

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