Skip to content

Commit 4fd9a45

Browse files
committed
CHANGES: snmpd: Stop reading and writing the mib_indexes/* files
Caching directory contents is something the operating system should do and is not something Net-SNMP should do. Instead of storing a copy of the directory contents in ${tmp_dir}/mib_indexes/${n}, always scan a MIB directory.
1 parent 5ed1bc5 commit 4fd9a45

File tree

5 files changed

+4
-207
lines changed

5 files changed

+4
-207
lines changed

Diff for: .gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ Makefile
7575
man/*.[1358]
7676
man/default_store.3.h
7777
man/manaliases
78-
mibs/.index
7978
mk/
8079
module_tmp_header.h
8180
net-snmp-5*

Diff for: include/net-snmp/library/mib.h

-3
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ SOFTWARE.
124124
NETSNMP_IMPORT
125125
char *netsnmp_get_mib_directory(void);
126126
void netsnmp_fixup_mib_directory(void);
127-
void netsnmp_mibindex_load( void );
128-
char * netsnmp_mibindex_lookup( const char * );
129-
FILE * netsnmp_mibindex_new( const char * );
130127
int sprint_realloc_description(u_char ** buf, size_t * buf_len,
131128
size_t * out_len, int allow_realloc,
132129
oid * objid, size_t objidlen, int width);

Diff for: include/net-snmp/library/parse.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ SOFTWARE.
201201
#endif
202202
void netsnmp_init_mib_internals(void);
203203
void unload_all_mibs(void);
204-
int add_mibfile(const char*, const char*, FILE *);
204+
int add_mibfile(const char*, const char*);
205205
int which_module(const char *);
206206
NETSNMP_IMPORT
207207
char *module_name(int, char *);

Diff for: snmplib/mib.c

+1-147
Original file line numberDiff line numberDiff line change
@@ -2720,7 +2720,6 @@ netsnmp_init_mib(void)
27202720
env_var = strdup(netsnmp_get_mib_directory());
27212721
if (!env_var)
27222722
return;
2723-
netsnmp_mibindex_load();
27242723

27252724
DEBUGMSGTL(("init_mib",
27262725
"Seen MIBDIRS: Looking in '%s' for mib dirs ...\n",
@@ -2740,7 +2739,7 @@ netsnmp_init_mib(void)
27402739
else
27412740
entry = strtok_r(env_var, ENV_SEPARATOR, &st);
27422741
while (entry) {
2743-
add_mibfile(entry, NULL, NULL);
2742+
add_mibfile(entry, NULL);
27442743
entry = strtok_r(NULL, ENV_SEPARATOR, &st);
27452744
}
27462745
}
@@ -2891,142 +2890,6 @@ init_mib(void)
28912890
#endif
28922891

28932892

2894-
/*
2895-
* Handle MIB indexes centrally
2896-
*/
2897-
static int _mibindex = 0; /* Last index in use */
2898-
static int _mibindex_max = 0; /* Size of index array */
2899-
char **_mibindexes = NULL;
2900-
2901-
int _mibindex_add( const char *dirname, int i );
2902-
void
2903-
netsnmp_mibindex_load( void )
2904-
{
2905-
DIR *dir;
2906-
struct dirent *file;
2907-
FILE *fp;
2908-
char tmpbuf[ 300];
2909-
char tmpbuf2[300];
2910-
int i;
2911-
char *cp;
2912-
2913-
/*
2914-
* Open the MIB index directory, or create it (empty)
2915-
*/
2916-
snprintf( tmpbuf, sizeof(tmpbuf), "%s/mib_indexes",
2917-
get_persistent_directory());
2918-
tmpbuf[sizeof(tmpbuf)-1] = 0;
2919-
dir = opendir( tmpbuf );
2920-
if ( dir == NULL ) {
2921-
DEBUGMSGTL(("mibindex", "load: (new)\n"));
2922-
mkdirhier( tmpbuf, NETSNMP_AGENT_DIRECTORY_MODE, 0);
2923-
return;
2924-
}
2925-
2926-
/*
2927-
* Create a list of which directory each file refers to
2928-
*/
2929-
while ((file = readdir( dir ))) {
2930-
if ( !isdigit((unsigned char)(file->d_name[0])))
2931-
continue;
2932-
i = atoi( file->d_name );
2933-
2934-
snprintf( tmpbuf, sizeof(tmpbuf), "%s/mib_indexes/%d",
2935-
get_persistent_directory(), i );
2936-
tmpbuf[sizeof(tmpbuf)-1] = 0;
2937-
fp = fopen( tmpbuf, "r" );
2938-
if (!fp)
2939-
continue;
2940-
cp = fgets( tmpbuf2, sizeof(tmpbuf2), fp );
2941-
fclose( fp );
2942-
if ( !cp ) {
2943-
DEBUGMSGTL(("mibindex", "Empty MIB index (%d)\n", i));
2944-
continue;
2945-
}
2946-
if ( strncmp( tmpbuf2, "DIR ", 4 ) != 0 ) {
2947-
DEBUGMSGTL(("mibindex", "Malformed MIB index (%d)\n", i));
2948-
continue;
2949-
}
2950-
tmpbuf2[strlen(tmpbuf2)-1] = 0;
2951-
DEBUGMSGTL(("mibindex", "load: (%d) %s\n", i, tmpbuf2));
2952-
(void)_mibindex_add( tmpbuf2+4, i ); /* Skip 'DIR ' */
2953-
}
2954-
closedir( dir );
2955-
}
2956-
2957-
char *
2958-
netsnmp_mibindex_lookup( const char *dirname )
2959-
{
2960-
int i;
2961-
static char tmpbuf[300];
2962-
2963-
for (i=0; i<_mibindex; i++) {
2964-
if ( _mibindexes[i] &&
2965-
strcmp( _mibindexes[i], dirname ) == 0) {
2966-
snprintf(tmpbuf, sizeof(tmpbuf), "%s/mib_indexes/%d",
2967-
get_persistent_directory(), i);
2968-
tmpbuf[sizeof(tmpbuf)-1] = 0;
2969-
DEBUGMSGTL(("mibindex", "lookup: %s (%d) %s\n", dirname, i, tmpbuf ));
2970-
return tmpbuf;
2971-
}
2972-
}
2973-
DEBUGMSGTL(("mibindex", "lookup: (none)\n"));
2974-
return NULL;
2975-
}
2976-
2977-
int
2978-
_mibindex_add( const char *dirname, int i )
2979-
{
2980-
const int old_mibindex_max = _mibindex_max;
2981-
2982-
DEBUGMSGTL(("mibindex", "add: %s (%d)\n", dirname, i ));
2983-
if ( i == -1 )
2984-
i = _mibindex++;
2985-
if ( i >= _mibindex_max ) {
2986-
/*
2987-
* If the index array is full (or non-existent)
2988-
* then expand (or create) it
2989-
*/
2990-
_mibindex_max = i + 10;
2991-
_mibindexes = realloc(_mibindexes,
2992-
_mibindex_max * sizeof(_mibindexes[0]));
2993-
netsnmp_assert(_mibindexes);
2994-
memset(_mibindexes + old_mibindex_max, 0,
2995-
(_mibindex_max - old_mibindex_max) * sizeof(_mibindexes[0]));
2996-
}
2997-
2998-
_mibindexes[ i ] = strdup( dirname );
2999-
if ( i >= _mibindex )
3000-
_mibindex = i+1;
3001-
3002-
DEBUGMSGTL(("mibindex", "add: %d/%d/%d\n", i, _mibindex, _mibindex_max ));
3003-
return i;
3004-
}
3005-
3006-
FILE *
3007-
netsnmp_mibindex_new( const char *dirname )
3008-
{
3009-
FILE *fp;
3010-
char tmpbuf[300];
3011-
char *cp;
3012-
int i;
3013-
3014-
cp = netsnmp_mibindex_lookup( dirname );
3015-
if (!cp) {
3016-
i = _mibindex_add( dirname, -1 );
3017-
snprintf( tmpbuf, sizeof(tmpbuf), "%s/mib_indexes/%d",
3018-
get_persistent_directory(), i );
3019-
tmpbuf[sizeof(tmpbuf)-1] = 0;
3020-
cp = tmpbuf;
3021-
}
3022-
DEBUGMSGTL(("mibindex", "new: %s (%s)\n", dirname, cp ));
3023-
fp = fopen( cp, "w" );
3024-
if (fp)
3025-
fprintf( fp, "DIR %s\n", dirname );
3026-
return fp;
3027-
}
3028-
3029-
30302893
/**
30312894
* Unloads all mibs.
30322895
*/
@@ -3041,15 +2904,6 @@ shutdown_mib(void)
30412904
}
30422905
tree_head = NULL;
30432906
Mib = NULL;
3044-
if (_mibindexes) {
3045-
int i;
3046-
for (i = 0; i < _mibindex; ++i)
3047-
SNMP_FREE(_mibindexes[i]);
3048-
free(_mibindexes);
3049-
_mibindex = 0;
3050-
_mibindex_max = 0;
3051-
_mibindexes = NULL;
3052-
}
30532907
if (Prefix != NULL && Prefix != &Standard_Prefix[0])
30542908
SNMP_FREE(Prefix);
30552909
if (Prefix)

Diff for: snmplib/parse.c

+2-55
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,6 @@ static int read_module_replacements(const char *);
607607
static int read_import_replacements(const char *,
608608
struct module_import *);
609609

610-
static void new_module(const char *, const char *);
611-
612610
static struct node *merge_parse_objectid(struct node *, FILE *, char *);
613611
static struct index_list *getIndexes(FILE * fp, struct index_list **);
614612
static struct varbind_list *getVarbinds(FILE * fp, struct varbind_list **);
@@ -4859,7 +4857,7 @@ snmp_get_token(FILE * fp, char *token, int maxtlen)
48594857
#endif /* NETSNMP_FEATURE_REMOVE_PARSE_GET_TOKEN */
48604858

48614859
int
4862-
add_mibfile(const char* tmpstr, const char* d_name, FILE *ip )
4860+
add_mibfile(const char* tmpstr, const char* d_name)
48634861
{
48644862
FILE *fp;
48654863
char token[MAXTOKEN], token2[MAXTOKEN];
@@ -4884,8 +4882,6 @@ add_mibfile(const char* tmpstr, const char* d_name, FILE *ip )
48844882
*/
48854883
if (get_token(fp, token2, MAXTOKEN) == DEFINITIONS) {
48864884
new_module(token, tmpstr);
4887-
if (ip)
4888-
fprintf(ip, "%s %s\n", token, d_name);
48894885
fclose(fp);
48904886
return 0;
48914887
} else {
@@ -4977,71 +4973,22 @@ static int scan_directory(char ***result, const char *dirname)
49774973
int
49784974
add_mibdir(const char *dirname)
49794975
{
4980-
FILE *ip;
49814976
const char *oldFile = File;
49824977
char **filenames;
49834978
int count = 0;
49844979
int filename_count, i;
4985-
#if !(defined(WIN32) || defined(cygwin))
4986-
char *token;
4987-
char space;
4988-
char newline;
4989-
struct stat dir_stat, idx_stat;
4990-
char tmpstr[300];
4991-
char tmpstr1[300];
4992-
#endif
49934980

49944981
DEBUGMSGTL(("parse-mibs", "Scanning directory %s\n", dirname));
4995-
#if !(defined(WIN32) || defined(cygwin))
4996-
token = netsnmp_mibindex_lookup( dirname );
4997-
if (token && stat(token, &idx_stat) == 0 && stat(dirname, &dir_stat) == 0) {
4998-
if (dir_stat.st_mtime < idx_stat.st_mtime) {
4999-
DEBUGMSGTL(("parse-mibs", "The index is good\n"));
5000-
if ((ip = fopen(token, "r")) != NULL) {
5001-
fgets(tmpstr, sizeof(tmpstr), ip); /* Skip dir line */
5002-
while (fscanf(ip, "%127s%c%299[^\n]%c", token, &space, tmpstr,
5003-
&newline) == 4) {
5004-
5005-
/*
5006-
* If an overflow of the token or tmpstr buffers has been
5007-
* found log a message and break out of the while loop,
5008-
* thus the rest of the file tokens will be ignored.
5009-
*/
5010-
if (space != ' ' || newline != '\n') {
5011-
snmp_log(LOG_ERR,
5012-
"add_mibdir: strings scanned in from %s/%s " \
5013-
"are too large. count = %d\n ", dirname,
5014-
".index", count);
5015-
break;
5016-
}
5017-
5018-
snprintf(tmpstr1, sizeof(tmpstr1), "%s/%s", dirname, tmpstr);
5019-
tmpstr1[ sizeof(tmpstr1)-1 ] = 0;
5020-
new_module(token, tmpstr1);
5021-
count++;
5022-
}
5023-
fclose(ip);
5024-
return count;
5025-
} else
5026-
DEBUGMSGTL(("parse-mibs", "Can't read index\n"));
5027-
} else
5028-
DEBUGMSGTL(("parse-mibs", "Index outdated\n"));
5029-
} else
5030-
DEBUGMSGTL(("parse-mibs", "No index\n"));
5031-
#endif
50324982

50334983
filename_count = scan_directory(&filenames, dirname);
50344984

50354985
if (filename_count >= 0) {
5036-
ip = netsnmp_mibindex_new(dirname);
50374986
for (i = 0; i < filename_count; i++) {
5038-
if (add_mibfile(filenames[i], strrchr(filenames[i], '/'), ip) == 0)
4987+
if (add_mibfile(filenames[i], strrchr(filenames[i], '/')) == 0)
50394988
count++;
50404989
free(filenames[i]);
50414990
}
50424991
File = oldFile;
5043-
if (ip)
5044-
fclose(ip);
50454992
free(filenames);
50464993
return (count);
50474994
}

0 commit comments

Comments
 (0)