summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Eitzenberger <heitzenberger@astaro.com>2009-10-09 09:17:09 +0200
committerHarald Welte <laforge@gnumonks.org>2010-10-21 19:15:36 +0200
commit02515010001b7b2d79d74c601b71b2c68a0c0770 (patch)
treea2ca14e5faccbb5e7e04d821a86895f4ebe6ed20
parenteeef45a47330a67fadb155c9fec985da98305215 (diff)
Make key name a pointer
Signed-off-by: Holger Eitzenberger <holger@eitzenberger.org>
-rw-r--r--include/ulogd/plugin.h20
-rw-r--r--output/pgsql/ulogd_output_PGSQL.c9
-rw-r--r--src/db.c5
-rw-r--r--src/plugin.c40
4 files changed, 38 insertions, 36 deletions
diff --git a/include/ulogd/plugin.h b/include/ulogd/plugin.h
index f35e353..7c3ae6a 100644
--- a/include/ulogd/plugin.h
+++ b/include/ulogd/plugin.h
@@ -90,35 +90,33 @@ struct db_column;
/* structure describing an input / output parameter of a plugin */
struct ulogd_key {
- struct ulogd_key *source;
-
- uint16_t flags;
-
struct ulogd_value val;
+ struct ulogd_key *source;
+ unsigned flags;
/*
* Map to database column
*/
struct db_column *col;
- /* name of this key */
- char name[ULOGD_MAX_KEYLEN+1];
+ char *name;
- /* IETF IPFIX attribute ID */
- struct {
+ struct { /* IETF IPFIX attribute ID */
uint32_t vendor;
uint16_t field_id;
} ipfix;
- /* length of the returned value (for variable-length types) */
- uint32_t len;
+ uint32_t len; /* for variable-length types */
};
+#define KEYSET_F_ALLOC 0x0001
+
struct ulogd_keyset {
/* possible input keys of this interpreter */
struct ulogd_key *keys;
/* number of input keys */
unsigned int num_keys;
+ unsigned int flags;
/* bitmask of possible types */
unsigned int type;
};
@@ -226,7 +224,7 @@ key_src_valid(const struct ulogd_key *key)
}
int ulogd_key_size(const struct ulogd_key *key);
-struct ulogd_key *ulogd_alloc_keyset(int n);
+int ulogd_init_keyset(struct ulogd_keyset *, unsigned);
void ulogd_free_keyset(struct ulogd_keyset *);
void ulogd_dump_keyset(const struct ulogd_keyset *);
struct ulogd_key *ulogd_key_find(const struct ulogd_keyset *,
diff --git a/output/pgsql/ulogd_output_PGSQL.c b/output/pgsql/ulogd_output_PGSQL.c
index 74d8dc0..e4f3d10 100644
--- a/output/pgsql/ulogd_output_PGSQL.c
+++ b/output/pgsql/ulogd_output_PGSQL.c
@@ -224,19 +224,14 @@ pgsql_get_columns(struct ulogd_pluginstance *upi)
upi_log(upi, ULOGD_DEBUG, "using %d/%d columns of table\n",
upi->input.num_keys, tuples);
- upi->input.keys = ulogd_alloc_keyset(upi->input.num_keys);
- if (upi->input.keys == NULL) {
- upi_log(upi, ULOGD_ERROR, "error allocating keyset: %m\n");
-
- upi->input.num_keys = 0;
+ if (ulogd_init_keyset(&upi->input, KEYSET_F_ALLOC) < 0) {
PQclear(priv->pgres);
-
goto err_again;
}
in = upi->input.keys;
for (i = k = 0; i < upi->input.num_keys; i++) {
- strncpy(in[k].name, PQgetvalue(priv->pgres, i, 0), ULOGD_MAX_KEYLEN);
+ in[k].name = strdup(PQgetvalue(priv->pgres, i, 0));
/* skip columns with leading underscore */
if (in[k].name[0] == '_') {
diff --git a/src/db.c b/src/db.c
index e1b2483..2ac79d9 100644
--- a/src/db.c
+++ b/src/db.c
@@ -412,7 +412,8 @@ keymap_map_keys(const char *str, struct ulogd_keyset *set,
case 0:
if (tok != TOK_NAME)
goto err_inval;
- xstrncpy(set->keys[keyno].name, keymap_lexbuf, ULOGD_MAX_KEYLEN);
+ BUG_ON(!(set->flags & KEYSET_F_ALLOC));
+ set->keys[keyno].name = strdup(keymap_lexbuf);
state++;
break;
@@ -515,7 +516,7 @@ ulogd_db_map_keys(struct ulogd_pluginstance *pi)
if ((set->num_keys = keymap_check(keymap, &di->num_cols)) < 0)
return -1;
- if ((set->keys = ulogd_alloc_keyset(set->num_keys)) == NULL)
+ if (ulogd_init_keyset(set, KEYSET_F_ALLOC) < 0)
return -1;
if (db_alloc_columns(di, di->num_cols) < 0)
diff --git a/src/plugin.c b/src/plugin.c
index c546bc0..b5946d0 100644
--- a/src/plugin.c
+++ b/src/plugin.c
@@ -1194,12 +1194,18 @@ key_free(struct ulogd_key *key)
void
key_reset(struct ulogd_key *key)
{
+ enum ulogd_ktype type;
+
if (!(key->flags & ULOGD_RETF_VALID))
return;
if (key->flags & ULOGD_RETF_FREE)
key_free(key);
+ type = key->val.type;
+ memset(&key->val, 0, sizeof(key->val));
+ key->val.type = type;
+
key->flags &= ~ULOGD_RETF_VALID;
}
@@ -1273,26 +1279,21 @@ ulogd_key_size(const struct ulogd_key *key)
return ret;
}
-/**
- * Allocate a keyset for use with ulogd_pluginstance. The keys are
- * optionally setup with private data.
- *
- * @arg num_keys Number of keys to use.
- * @arg priv_size Size of private area per key.
- * @return Newly allocated key space or %NULL.
- */
-struct ulogd_key *
-ulogd_alloc_keyset(int num_keys)
+int
+ulogd_init_keyset(struct ulogd_keyset *set, unsigned flags)
{
- struct ulogd_key *keys;
+ if (!set || set->num_keys <= 0)
+ return -1;
- if (num_keys <= 0)
- return NULL;
+ set->keys = calloc(set->num_keys, sizeof(struct ulogd_key));
+ if (!set->keys) {
+ ulogd_log(ULOGD_FATAL, "%s: %s\n", __func__, strerror(ENOMEM));
+ return -1;
+ }
- if ((keys = calloc(num_keys, sizeof(struct ulogd_key))) == NULL)
- return NULL;
+ set->flags = flags;
- return keys;
+ return 0;
}
void
@@ -1313,6 +1314,13 @@ ulogd_dump_keyset(const struct ulogd_keyset *set)
void
ulogd_free_keyset(struct ulogd_keyset *set)
{
+ int i;
+
+ if (set->flags & KEYSET_F_ALLOC) {
+ for (i = 0; i < set->num_keys; i++)
+ free(&set->keys[i]);
+ }
+
free(set->keys);
set->num_keys = 0;
}