restore cgroup-network operation - also netdata reads /proc/1/net/dev when host prefix is defined to allow it read the network interfaces of the host
Costa Tsaousis (ktsaou) committed
Apr 20, 2018 at 01:15 UTC
8e363c3a61c5331e90f1e8e25582410b56f2edac
3 files changed
+24
-12
src/cgroup-network.c
+20
-11
@@ -39,8 +39,10 @@ struct iface {
39
};
40
41
unsigned int read_iface_iflink(const char *prefix, const char *iface) {
42
+ if(!prefix) prefix = "";
43
+
44
char filename[FILENAME_MAX + 1];
43
- snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/iflink", prefix?prefix:"", iface);
45
+ snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/iflink", prefix, iface);
46
47
unsigned long long iflink = 0;
48
int ret = read_single_number_file(filename, &iflink);
@@ -50,8 +52,10 @@ unsigned int read_iface_iflink(const char *prefix, const char *iface) {
52
}
53
54
unsigned int read_iface_ifindex(const char *prefix, const char *iface) {
55
+ if(!prefix) prefix = "";
56
+
57
char filename[FILENAME_MAX + 1];
54
- snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/ifindex", prefix?prefix:"", iface);
58
+ snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/ifindex", prefix, iface);
59
60
unsigned long long ifindex = 0;
61
int ret = read_single_number_file(filename, &ifindex);
@@ -61,10 +65,12 @@ unsigned int read_iface_ifindex(const char *prefix, const char *iface) {
65
}
66
67
struct iface *read_proc_net_dev(const char *prefix) {
68
+ if(!prefix) prefix = "";
69
+
70
procfile *ff = NULL;
71
char filename[FILENAME_MAX + 1];
72
67
- snprintfz(filename, FILENAME_MAX, "%s%s", prefix?prefix:"", "/proc/net/dev");
73
+ snprintfz(filename, FILENAME_MAX, "%s%s", prefix, (*prefix)?"/proc/1/net/dev":"/proc/net/dev");
74
ff = procfile_open(filename, " \t,:|", PROCFILE_FLAG_DEFAULT);
75
if(unlikely(!ff)) {
76
error("Cannot open file '%s'", filename);
@@ -161,12 +167,14 @@ static void continue_as_child(void) {
167
}
168
169
int proc_pid_fd(const char *prefix, const char *ns, pid_t pid) {
170
+ if(!prefix) prefix = "";
171
+
172
char filename[FILENAME_MAX + 1];
165
- snprintfz(filename, FILENAME_MAX, "%s/proc/%d/%s", prefix?prefix:"", (int)pid, ns);
166
- int fd = open(filename, procfile_open_flags);
173
+ snprintfz(filename, FILENAME_MAX, "%s/proc/%d/%s", prefix, (int)pid, ns);
174
+ int fd = open(filename, O_RDONLY);
175
176
if(fd == -1)
169
- error("Cannot open file '%s'", filename);
177
+ error("Cannot open proc_pid_fd() file '%s'", filename);
178
179
return fd;
180
}
@@ -191,6 +199,8 @@ static struct ns {
199
};
200
201
int switch_namespace(const char *prefix, pid_t pid) {
202
+ if(!prefix) prefix = "";
203
+
204
#ifdef HAVE_SETNS
205
206
int i;
@@ -272,13 +282,13 @@ int switch_namespace(const char *prefix, pid_t pid) {
282
pid_t read_pid_from_cgroup_file(const char *filename) {
283
int fd = open(filename, procfile_open_flags);
284
if(fd == -1) {
275
- error("Cannot open file '%s'.", filename);
285
+ error("Cannot open pid_from_cgroup() file '%s'.", filename);
286
return 0;
287
}
288
289
FILE *fp = fdopen(fd, "r");
290
if(!fp) {
281
- error("Cannot open file '%s'.", filename);
291
+ error("Cannot upgrade fd to fp for file '%s'.", filename);
292
return 0;
293
}
294
@@ -392,9 +402,8 @@ int send_devices(void) {
402
403
void detect_veth_interfaces(pid_t pid) {
404
struct iface *host, *cgroup, *h, *c;
395
- const char *prefix = getenv("NETDATA_HOST_PREFIX");
405
397
- host = read_proc_net_dev(prefix);
406
+ host = read_proc_net_dev(netdata_configured_host_prefix);
407
if(!host) {
408
errno = 0;
409
error("cannot read host interface list.");
@@ -407,7 +416,7 @@ void detect_veth_interfaces(pid_t pid) {
416
goto cleanup;
417
}
418
410
- if(switch_namespace(prefix, pid)) {
419
+ if(switch_namespace(netdata_configured_host_prefix, pid)) {
420
errno = 0;
421
error("cannot switch to the namespace of pid %u", (unsigned int) pid);
422
goto cleanup;
src/common.c
+3
@@ -1419,6 +1419,9 @@ int verify_netdata_host_prefix() {
1419
if(is_virtual_filesystem(path, &reason) == -1)
1420
goto failed;
1421
1422
+ if(netdata_configured_host_prefix && *netdata_configured_host_prefix)
1423
+ info("Using host prefix directory '%s'", netdata_configured_host_prefix);
1424
+
1425
return 0;
1426
1427
failed:
src/proc_net_dev.c
+1
-1
@@ -452,7 +452,7 @@ int do_proc_net_dev(int update_every, usec_t dt) {
452
453
if(unlikely(!ff)) {
454
char filename[FILENAME_MAX + 1];
455
- snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/net/dev");
455
+ snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, (*netdata_configured_host_prefix)?"/proc/1/net/dev":"/proc/net/dev");
456
ff = procfile_open(config_get("plugin:proc:/proc/net/dev", "filename to monitor", filename), " \t,:|", PROCFILE_FLAG_DEFAULT);
457
if(unlikely(!ff)) return 1;
458
}