Skip to content

Commit 7dff9c0

Browse files
committed
in_docker: add helper for container name parsing
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 0fec7f7 commit 7dff9c0

File tree

4 files changed

+52
-62
lines changed

4 files changed

+52
-62
lines changed

plugins/in_docker/cgroup_v1.c

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -213,36 +213,6 @@ static char *get_config_file(struct flb_docker *ctx, char *id)
213213
return path;
214214
}
215215

216-
static char *extract_name(char *line, char *start)
217-
{
218-
int skip = 9;
219-
int len = 0;
220-
char *name;
221-
char buff[256];
222-
char *curr;
223-
224-
if (start != NULL) {
225-
curr = start + skip;
226-
while (*curr != '"') {
227-
buff[len++] = *curr;
228-
curr++;
229-
}
230-
231-
if (len > 0) {
232-
name = (char *) flb_calloc(len + 1, sizeof(char));
233-
if (!name) {
234-
flb_errno();
235-
return NULL;
236-
}
237-
memcpy(name, buff, len);
238-
239-
return name;
240-
}
241-
}
242-
243-
return NULL;
244-
}
245-
246216
static char *get_container_name(struct flb_docker *ctx, char *id)
247217
{
248218
char *container_name = NULL;
@@ -266,7 +236,7 @@ static char *get_container_name(struct flb_docker *ctx, char *id)
266236
while ((line = read_line(f))) {
267237
char *index = strstr(line, DOCKER_NAME_ARG);
268238
if (index != NULL) {
269-
container_name = extract_name(line, index);
239+
container_name = docker_extract_name(line, index);
270240
flb_free(line);
271241
break;
272242
}

plugins/in_docker/cgroup_v2.c

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -230,36 +230,6 @@ static char *get_config_file(struct flb_docker *ctx, char *id)
230230
return path;
231231
}
232232

233-
static char *extract_name(char *line, char *start)
234-
{
235-
int skip = 9;
236-
int len = 0;
237-
char *name;
238-
char buff[256];
239-
char *curr;
240-
241-
if (start != NULL) {
242-
curr = start + skip;
243-
while (*curr != '"') {
244-
buff[len++] = *curr;
245-
curr++;
246-
}
247-
248-
if (len > 0) {
249-
name = (char *) flb_calloc(len + 1, sizeof(char));
250-
if (!name) {
251-
flb_errno();
252-
return NULL;
253-
}
254-
memcpy(name, buff, len);
255-
256-
return name;
257-
}
258-
}
259-
260-
return NULL;
261-
}
262-
263233
static char *get_container_name(struct flb_docker *ctx, char *id)
264234
{
265235
char *container_name = NULL;
@@ -283,7 +253,7 @@ static char *get_container_name(struct flb_docker *ctx, char *id)
283253
while ((line = read_line(f))) {
284254
char *index = strstr(line, DOCKER_NAME_ARG);
285255
if (index != NULL) {
286-
container_name = extract_name(line, index);
256+
container_name = docker_extract_name(line, index);
287257
flb_free(line);
288258
break;
289259
}

plugins/in_docker/docker.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,57 @@
2929
#include <string.h>
3030
#include <stdlib.h>
3131
#include <stdint.h>
32+
#include <ctype.h>
3233

3334
#include "docker.h"
3435

36+
char *docker_extract_name(const char *line, const char *start)
37+
{
38+
const char *curr;
39+
const char *end;
40+
size_t len;
41+
char *name;
42+
43+
if (line == NULL || start == NULL) {
44+
return NULL;
45+
}
46+
47+
curr = start + strlen(DOCKER_NAME_ARG);
48+
if (*curr != ':') {
49+
curr = strchr(curr, ':');
50+
if (curr == NULL) {
51+
return NULL;
52+
}
53+
}
54+
55+
curr++;
56+
while (*curr != '\0' && isspace((unsigned char) *curr)) {
57+
curr++;
58+
}
59+
60+
if (*curr != '"') {
61+
return NULL;
62+
}
63+
64+
curr++;
65+
end = strchr(curr, '"');
66+
if (end == NULL || end <= curr) {
67+
return NULL;
68+
}
69+
70+
len = end - curr;
71+
name = flb_malloc(len + 1);
72+
if (name == NULL) {
73+
flb_errno();
74+
return NULL;
75+
}
76+
77+
memcpy(name, curr, len);
78+
name[len] = '\0';
79+
80+
return name;
81+
}
82+
3583
static int cb_docker_collect(struct flb_input_instance *i_ins,
3684
struct flb_config *config, void *in_context);
3785

plugins/in_docker/docker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,6 @@ struct flb_docker {
119119
int in_docker_collect(struct flb_input_instance *i_ins,
120120
struct flb_config *config, void *in_context);
121121
docker_info *in_docker_init_docker_info(char *id);
122+
char *docker_extract_name(const char *line, const char *start);
123+
122124
#endif

0 commit comments

Comments
 (0)