projects
/
collectd.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
snmp_agent: Replace strndup() with internal implementation
[collectd.git]
/
src
/
snmp_agent.c
diff --git
a/src/snmp_agent.c
b/src/snmp_agent.c
index
8f556d8
..
aeb0158
100644
(file)
--- a/
src/snmp_agent.c
+++ b/
src/snmp_agent.c
@@
-1,7
+1,7
@@
/**
* collectd - src/snmp_agent.c
*
/**
* collectd - src/snmp_agent.c
*
- * Copyright(c) 2017 Intel Corporation. All rights reserved.
+ * Copyright(c) 2017
-2018
Intel Corporation. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@
-101,7
+101,7
@@
struct table_definition_s {
will be split into sepearate
tokens */
will be split into sepearate
tokens */
-
_Bool tokens_done; /* Set to 1
when all tokens are generated */
+
bool tokens_done; /* Set to true
when all tokens are generated */
};
typedef struct table_definition_s table_definition_t;
};
typedef struct table_definition_s table_definition_t;
@@
-134,7
+134,7
@@
struct snmp_agent_ctx_s {
typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
static snmp_agent_ctx_t *g_agent;
typedef struct snmp_agent_ctx_s snmp_agent_ctx_t;
static snmp_agent_ctx_t *g_agent;
-
const char *const
index_opts[MAX_KEY_SOURCES] = {
+
static const char *
index_opts[MAX_KEY_SOURCES] = {
"Hostname", "Plugin", "PluginInstance", "Type", "TypeInstance"};
#define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \
"Hostname", "Plugin", "PluginInstance", "Type", "TypeInstance"};
#define CHECK_DD_TYPE(_dd, _p, _pi, _t, _ti) \
@@
-340,9
+340,8
@@
static int snmp_agent_validate_config(void) {
return 0;
}
return 0;
}
-static int snmp_agent_parse_index_key(const char *input, char *regex,
- regex_t *regex_info, int gi,
- regmatch_t *m) {
+static int snmp_agent_parse_index_key(const char *input, regex_t *regex_info,
+ int gi, regmatch_t *m) {
regmatch_t matches[MAX_MATCHES];
int ret = regexec(regex_info, input, MAX_MATCHES, matches, 0);
regmatch_t matches[MAX_MATCHES];
int ret = regexec(regex_info, input, MAX_MATCHES, matches, 0);
@@
-384,11
+383,20
@@
static int snmp_agent_create_token(char const *input, int t_off, int n,
int ret = 0;
token->key = index_key;
int ret = 0;
token->key = index_key;
- token->str = strndup(input + t_off, n);
+ /* copy at most n bytes from input with offset t_off into token->str */
+ input += t_off;
+ size_t len = strlen(input);
+ if (n < len)
+ len = n;
+
+ token->str = malloc(len + 1);
if (token->str == NULL)
goto free_offset_error;
if (token->str == NULL)
goto free_offset_error;
+ memcpy(token->str, input, len);
+ token->str[len] = '\0';
+
*offset = t_off;
ret = c_avl_insert(tree, (void *)offset, (void *)token);
*offset = t_off;
ret = c_avl_insert(tree, (void *)offset, (void *)token);
@@
-526,8
+534,7
@@
static int snmp_agent_fill_index_list(table_definition_t *td,
regmatch_t m = {-1, -1};
/* Parsing input string */
regmatch_t m = {-1, -1};
/* Parsing input string */
- ret = snmp_agent_parse_index_key(ptr, td->index_keys[i].regex,
- &td->index_keys[i].regex_info,
+ ret = snmp_agent_parse_index_key(ptr, &td->index_keys[i].regex_info,
td->index_keys[i].group, &m);
if (ret != 0) {
ERROR(PLUGIN_NAME ": Error executing regex");
td->index_keys[i].group, &m);
if (ret != 0) {
ERROR(PLUGIN_NAME ": Error executing regex");
@@
-535,7
+542,7
@@
static int snmp_agent_fill_index_list(table_definition_t *td,
}
/* Tokenizing input string if not done yet */
}
/* Tokenizing input string if not done yet */
- if (td->tokens_done ==
0
)
+ if (td->tokens_done ==
false
)
ret = snmp_agent_tokenize(ptr, tokens, &m, key);
if (ret != 0)
ret = snmp_agent_tokenize(ptr, tokens, &m, key);
if (ret != 0)
@@
-543,17
+550,31
@@
static int snmp_agent_fill_index_list(table_definition_t *td,
if (td->index_keys[i].type == ASN_INTEGER) {
int val = strtol(ptr + m.rm_so, NULL, 0);
if (td->index_keys[i].type == ASN_INTEGER) {
int val = strtol(ptr + m.rm_so, NULL, 0);
+
+#ifdef HAVE_NETSNMP_OLD_API
+ ret = snmp_set_var_value(key, (const u_char *)&val, sizeof(val));
+#else
ret = snmp_set_var_value(key, &val, sizeof(val));
ret = snmp_set_var_value(key, &val, sizeof(val));
+#endif
} else
} else
+#ifdef HAVE_NETSNMP_OLD_API
+ ret = snmp_set_var_value(key, (const u_char *)(ptr + m.rm_so),
+ m.rm_eo - m.rm_so);
+#else
ret = snmp_set_var_value(key, ptr + m.rm_so, m.rm_eo - m.rm_so);
ret = snmp_set_var_value(key, ptr + m.rm_so, m.rm_eo - m.rm_so);
+#endif
} else
} else
+#ifdef HAVE_NETSNMP_OLD_API
+ ret = snmp_set_var_value(key, (const u_char *)ptr, strlen(ptr));
+#else
ret = snmp_set_var_value(key, ptr, strlen(ptr));
ret = snmp_set_var_value(key, ptr, strlen(ptr));
+#endif
key = key->next_variable;
}
/* Tokens for all source strings are generated */
for (i = 0; i < MAX_KEY_SOURCES; i++)
key = key->next_variable;
}
/* Tokens for all source strings are generated */
for (i = 0; i < MAX_KEY_SOURCES; i++)
- td->tokens_done =
1
;
+ td->tokens_done =
true
;
return 0;
}
return 0;
}
@@
-1041,12
+1062,24
@@
static int snmp_agent_form_reply(struct netsnmp_request_info_s *requests,
requests->requestvb->type = td->index_keys[dd->index_key_pos].type;
if (requests->requestvb->type == ASN_INTEGER)
requests->requestvb->type = td->index_keys[dd->index_key_pos].type;
if (requests->requestvb->type == ASN_INTEGER)
+#ifdef HAVE_NETSNMP_OLD_API
+ snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
+ (const u_char *)key->val.integer,
+ sizeof(*key->val.integer));
+#else
snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
key->val.integer, sizeof(*key->val.integer));
snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
key->val.integer, sizeof(*key->val.integer));
+#endif
else /* OCTET_STR */
else /* OCTET_STR */
+#ifdef HAVE_NETSNMP_OLD_API
snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
(const u_char *)key->val.string,
strlen((const char *)key->val.string));
snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
(const u_char *)key->val.string,
strlen((const char *)key->val.string));
+#else
+ snmp_set_var_typed_value(requests->requestvb, requests->requestvb->type,
+ key->val.string,
+ strlen((const char *)key->val.string));
+#endif
pthread_mutex_unlock(&g_agent->lock);
pthread_mutex_unlock(&g_agent->lock);
@@
-1476,7
+1509,7
@@
static int snmp_agent_config_index_key_source(table_definition_t *td,
if (ret != 0)
return -1;
if (ret != 0)
return -1;
-
_Bool match = 0
;
+
bool match = false
;
for (int i = 0; i < MAX_KEY_SOURCES; i++) {
if (strcasecmp(index_opts[i], (const char *)val) == 0) {
for (int i = 0; i < MAX_KEY_SOURCES; i++) {
if (strcasecmp(index_opts[i], (const char *)val) == 0) {
@@
-1496,7
+1529,7
@@
static int snmp_agent_config_index_key_source(table_definition_t *td,
sfree(val);
dd->index_key_pos = td->index_keys_len++;
sfree(val);
dd->index_key_pos = td->index_keys_len++;
- dd->is_index_key =
1
;
+ dd->is_index_key =
true
;
return 0;
}
return 0;
}
@@
-1575,14
+1608,14
@@
static int snmp_agent_config_table_column(table_definition_t *td,
dd->shift = 0.0;
/* NULL if it's a scalar */
dd->table = td;
dd->shift = 0.0;
/* NULL if it's a scalar */
dd->table = td;
- dd->is_index_key =
0
;
+ dd->is_index_key =
false
;
for (int i = 0; i < ci->children_num; i++) {
oconfig_item_t *option = ci->children + i;
/* First 3 options are reserved for table entry only */
if (td != NULL && strcasecmp("IndexKey", option->key) == 0) {
for (int i = 0; i < ci->children_num; i++) {
oconfig_item_t *option = ci->children + i;
/* First 3 options are reserved for table entry only */
if (td != NULL && strcasecmp("IndexKey", option->key) == 0) {
- dd->is_index_key =
1
;
+ dd->is_index_key =
true
;
option_tmp = option;
} else if (strcasecmp("Plugin", option->key) == 0)
ret = cf_util_get_string(option, &dd->plugin);
option_tmp = option;
} else if (strcasecmp("Plugin", option->key) == 0)
ret = cf_util_get_string(option, &dd->plugin);
@@
-1681,7
+1714,7
@@
static int snmp_agent_config_table(oconfig_item_t *ci) {
for (int i = 0; i < MAX_KEY_SOURCES; i++)
td->tokens[i] = NULL;
for (int i = 0; i < MAX_KEY_SOURCES; i++)
td->tokens[i] = NULL;
- td->tokens_done =
0
;
+ td->tokens_done =
false
;
for (int i = 0; i < ci->children_num; i++) {
oconfig_item_t *option = ci->children + i;
for (int i = 0; i < ci->children_num; i++) {
oconfig_item_t *option = ci->children + i;
@@
-1856,11
+1889,11
@@
static int snmp_agent_update_index(data_definition_t *dd,
int ret;
int *index = NULL;
int *value = NULL;
int ret;
int *index = NULL;
int *value = NULL;
-
_Bool free_index_oid = 1
;
+
bool do_free_index_oid = true
;
if (c_avl_get(td->instance_index, (void *)*index_oid, (void **)&index) != 0) {
/* Processing new instance */
if (c_avl_get(td->instance_index, (void *)*index_oid, (void **)&index) != 0) {
/* Processing new instance */
-
free_index_oid = 0
;
+
do_free_index_oid = false
;
/* need to generate index for the table */
if (td->index_oid.oid_len) {
/* need to generate index for the table */
if (td->index_oid.oid_len) {
@@
-1974,7
+2007,7
@@
static int snmp_agent_update_index(data_definition_t *dd,
plugin_dispatch_notification(&n);
}
plugin_dispatch_notification(&n);
}
- if (free_index_oid)
+ if (
do_
free_index_oid)
sfree(*index_oid);
return 0;
sfree(*index_oid);
return 0;