diff --git a/plugins/in_cpu/cpu.c b/plugins/in_cpu/cpu.c index be7c71e7a21..050f5d9d56a 100644 --- a/plugins/in_cpu/cpu.c +++ b/plugins/in_cpu/cpu.c @@ -523,7 +523,7 @@ static int cb_cpu_init(struct flb_input_instance *in, int ret; struct flb_cpu *ctx; (void) data; - + /* Allocate space for the configuration */ ctx = flb_calloc(1, sizeof(struct flb_cpu)); if (!ctx) { @@ -531,7 +531,7 @@ static int cb_cpu_init(struct flb_input_instance *in, return -1; } ctx->ins = in; - + ret = flb_input_config_map_set(in, (void *)ctx); if (ret == -1) { flb_free(ctx); @@ -559,6 +559,7 @@ static int cb_cpu_init(struct flb_input_instance *in, /* Get CPU load, ready to be updated once fired the calc callback */ if (ctx->pid > 0) { ret = proc_cpu_pid_load(ctx, ctx->pid, &ctx->cstats); + flb_plg_info(in, "monitoring PID %i", ctx->pid); } else { ret = proc_cpu_load(ctx->n_processors, &ctx->cstats); diff --git a/src/flb_env.c b/src/flb_env.c index 4758214d408..55610b78ddf 100644 --- a/src/flb_env.c +++ b/src/flb_env.c @@ -63,11 +63,26 @@ static int env_preset(struct flb_env *env) } } + /* Set the current process ID */ + ret = snprintf(tmp, sizeof(tmp) - 1, "%i", getpid()); + if (ret <= 0) { + flb_error("[env] could not set ${FLUENT_BIT_PID}"); + return -1; + } + tmp[ret] = '\0'; + + ret = flb_env_set(env, "FLUENT_BIT_PID", tmp); + if (ret < 0) { + flb_error("[env] could not set ${FLUENT_BIT_PID}"); + return -1; + } + return 0; } struct flb_env *flb_env_create() { + int ret; struct flb_env *env; struct flb_hash_table *ht; @@ -86,7 +101,11 @@ struct flb_env *flb_env_create() env->warn_unused = FLB_TRUE; env->ht = ht; - env_preset(env); + ret = env_preset(env); + if (ret == -1) { + flb_env_destroy(env); + return NULL; + } return env; } @@ -109,6 +128,8 @@ int flb_env_set(struct flb_env *env, const char *key, const char *val) klen = strlen(key); vlen = strlen(val); + printf("[env set] key: %s, val: %s (vlen: %i)\n", key, val, vlen); + /* Check if the key is already set */ id = flb_hash_table_get(env->ht, key, klen, &out_buf, &out_size); if (id >= 0) { diff --git a/src/flb_hash_table.c b/src/flb_hash_table.c index b9b702e93d4..44366b7d08b 100644 --- a/src/flb_hash_table.c +++ b/src/flb_hash_table.c @@ -255,7 +255,7 @@ static int entry_set_value(struct flb_hash_table_entry *entry, void *val, size_t * If the entry already contains a previous value in the heap, just remove * the previously assigned memory. */ - if (entry->val_size > 0) { + if (entry->val_size > 0 && entry->val) { flb_free(entry->val); } @@ -268,6 +268,7 @@ static int entry_set_value(struct flb_hash_table_entry *entry, void *val, size_t entry->val = flb_malloc(val_size + 1); if (!entry->val) { flb_errno(); + entry->val_size = 0; return -1; } diff --git a/tests/internal/env.c b/tests/internal/env.c index c0a72f0a896..cb89909be82 100644 --- a/tests/internal/env.c +++ b/tests/internal/env.c @@ -85,7 +85,31 @@ void test_translate_long_env() } +static void test_internal_vars() +{ + int64_t pid; + char *tmp; + struct flb_env *env; + + env = flb_env_create(); + TEST_CHECK(env != NULL); + + /* HOSTNAME */ + tmp = flb_env_get(env, "HOSTNAME"); + TEST_CHECK(tmp != NULL); + + /* FLUENT_BIT_PID */ + tmp = flb_env_get(env, "FLUENT_BIT_PID"); + TEST_CHECK(tmp != NULL); + + pid = getpid(); + TEST_CHECK(pid == atoll(tmp)); + + flb_env_destroy(env); +} + TEST_LIST = { { "translate_long_env" , test_translate_long_env}, + { "internal_vars" , test_internal_vars}, { NULL, NULL } };