cgroup-network.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "libnetdata/libnetdata.h"
  3. #include "libnetdata/required_dummies.h"
  4. #ifdef HAVE_SETNS
  5. #ifndef _GNU_SOURCE
  6. #define _GNU_SOURCE /* See feature_test_macros(7) */
  7. #endif
  8. #include <sched.h>
  9. #endif
  10. char environment_variable2[FILENAME_MAX + 50] = "";
  11. char environment_variable3[FILENAME_MAX + 50] = "";
  12. char *environment[] = {
  13. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin",
  14. environment_variable2,
  15. environment_variable3,
  16. NULL
  17. };
  18. struct iface {
  19. const char *device;
  20. uint32_t hash;
  21. unsigned int ifindex;
  22. unsigned int iflink;
  23. struct iface *next;
  24. };
  25. unsigned int calc_num_ifaces(struct iface *root) {
  26. unsigned int num = 0;
  27. for (struct iface *h = root; h; h = h->next) {
  28. num++;
  29. }
  30. return num;
  31. }
  32. unsigned int read_iface_iflink(const char *prefix, const char *iface) {
  33. if(!prefix) prefix = "";
  34. char filename[FILENAME_MAX + 1];
  35. snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/iflink", prefix, iface);
  36. unsigned long long iflink = 0;
  37. int ret = read_single_number_file(filename, &iflink);
  38. if(ret) collector_error("Cannot read '%s'.", filename);
  39. return (unsigned int)iflink;
  40. }
  41. unsigned int read_iface_ifindex(const char *prefix, const char *iface) {
  42. if(!prefix) prefix = "";
  43. char filename[FILENAME_MAX + 1];
  44. snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/ifindex", prefix, iface);
  45. unsigned long long ifindex = 0;
  46. int ret = read_single_number_file(filename, &ifindex);
  47. if(ret) collector_error("Cannot read '%s'.", filename);
  48. return (unsigned int)ifindex;
  49. }
  50. struct iface *read_proc_net_dev(const char *scope __maybe_unused, const char *prefix) {
  51. if(!prefix) prefix = "";
  52. procfile *ff = NULL;
  53. char filename[FILENAME_MAX + 1];
  54. snprintfz(filename, FILENAME_MAX, "%s%s", prefix, (*prefix)?"/proc/1/net/dev":"/proc/net/dev");
  55. #ifdef NETDATA_INTERNAL_CHECKS
  56. collector_info("parsing '%s'", filename);
  57. #endif
  58. ff = procfile_open(filename, " \t,:|", PROCFILE_FLAG_DEFAULT);
  59. if(unlikely(!ff)) {
  60. collector_error("Cannot open file '%s'", filename);
  61. return NULL;
  62. }
  63. ff = procfile_readall(ff);
  64. if(unlikely(!ff)) {
  65. collector_error("Cannot read file '%s'", filename);
  66. return NULL;
  67. }
  68. size_t lines = procfile_lines(ff), l;
  69. struct iface *root = NULL;
  70. for(l = 2; l < lines ;l++) {
  71. if (unlikely(procfile_linewords(ff, l) < 1)) continue;
  72. struct iface *t = callocz(1, sizeof(struct iface));
  73. t->device = strdupz(procfile_lineword(ff, l, 0));
  74. t->hash = simple_hash(t->device);
  75. t->ifindex = read_iface_ifindex(prefix, t->device);
  76. t->iflink = read_iface_iflink(prefix, t->device);
  77. t->next = root;
  78. root = t;
  79. #ifdef NETDATA_INTERNAL_CHECKS
  80. collector_info("added %s interface '%s', ifindex %u, iflink %u", scope, t->device, t->ifindex, t->iflink);
  81. #endif
  82. }
  83. procfile_close(ff);
  84. return root;
  85. }
  86. void free_iface(struct iface *iface) {
  87. freez((void *)iface->device);
  88. freez(iface);
  89. }
  90. void free_host_ifaces(struct iface *iface) {
  91. while(iface) {
  92. struct iface *t = iface->next;
  93. free_iface(iface);
  94. iface = t;
  95. }
  96. }
  97. int iface_is_eligible(struct iface *iface) {
  98. if(iface->iflink != iface->ifindex)
  99. return 1;
  100. return 0;
  101. }
  102. int eligible_ifaces(struct iface *root) {
  103. int eligible = 0;
  104. struct iface *t;
  105. for(t = root; t ; t = t->next)
  106. if(iface_is_eligible(t))
  107. eligible++;
  108. return eligible;
  109. }
  110. static void continue_as_child(void) {
  111. pid_t child = fork();
  112. int status;
  113. pid_t ret;
  114. if (child < 0)
  115. collector_error("fork() failed");
  116. /* Only the child returns */
  117. if (child == 0)
  118. return;
  119. for (;;) {
  120. ret = waitpid(child, &status, WUNTRACED);
  121. if ((ret == child) && (WIFSTOPPED(status))) {
  122. /* The child suspended so suspend us as well */
  123. kill(getpid(), SIGSTOP);
  124. kill(child, SIGCONT);
  125. } else {
  126. break;
  127. }
  128. }
  129. /* Return the child's exit code if possible */
  130. if (WIFEXITED(status)) {
  131. exit(WEXITSTATUS(status));
  132. } else if (WIFSIGNALED(status)) {
  133. kill(getpid(), WTERMSIG(status));
  134. }
  135. exit(EXIT_FAILURE);
  136. }
  137. int proc_pid_fd(const char *prefix, const char *ns, pid_t pid) {
  138. if(!prefix) prefix = "";
  139. char filename[FILENAME_MAX + 1];
  140. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/%s", prefix, (int)pid, ns);
  141. int fd = open(filename, O_RDONLY);
  142. if(fd == -1)
  143. collector_error("Cannot open proc_pid_fd() file '%s'", filename);
  144. return fd;
  145. }
  146. static struct ns {
  147. int nstype;
  148. int fd;
  149. int status;
  150. const char *name;
  151. const char *path;
  152. } all_ns[] = {
  153. // { .nstype = CLONE_NEWUSER, .fd = -1, .status = -1, .name = "user", .path = "ns/user" },
  154. // { .nstype = CLONE_NEWCGROUP, .fd = -1, .status = -1, .name = "cgroup", .path = "ns/cgroup" },
  155. // { .nstype = CLONE_NEWIPC, .fd = -1, .status = -1, .name = "ipc", .path = "ns/ipc" },
  156. // { .nstype = CLONE_NEWUTS, .fd = -1, .status = -1, .name = "uts", .path = "ns/uts" },
  157. { .nstype = CLONE_NEWNET, .fd = -1, .status = -1, .name = "network", .path = "ns/net" },
  158. { .nstype = CLONE_NEWPID, .fd = -1, .status = -1, .name = "pid", .path = "ns/pid" },
  159. { .nstype = CLONE_NEWNS, .fd = -1, .status = -1, .name = "mount", .path = "ns/mnt" },
  160. // terminator
  161. { .nstype = 0, .fd = -1, .status = -1, .name = NULL, .path = NULL }
  162. };
  163. int switch_namespace(const char *prefix, pid_t pid) {
  164. #ifdef HAVE_SETNS
  165. int i;
  166. for(i = 0; all_ns[i].name ; i++)
  167. all_ns[i].fd = proc_pid_fd(prefix, all_ns[i].path, pid);
  168. int root_fd = proc_pid_fd(prefix, "root", pid);
  169. int cwd_fd = proc_pid_fd(prefix, "cwd", pid);
  170. setgroups(0, NULL);
  171. // 2 passes - found it at nsenter source code
  172. // this is related CLONE_NEWUSER functionality
  173. // This code cannot switch user namespace (it can all the other namespaces)
  174. // Fortunately, we don't need to switch user namespaces.
  175. int pass;
  176. for(pass = 0; pass < 2 ;pass++) {
  177. for(i = 0; all_ns[i].name ; i++) {
  178. if (all_ns[i].fd != -1 && all_ns[i].status == -1) {
  179. if(setns(all_ns[i].fd, all_ns[i].nstype) == -1) {
  180. if(pass == 1) {
  181. all_ns[i].status = 0;
  182. collector_error("Cannot switch to %s namespace of pid %d", all_ns[i].name, (int) pid);
  183. }
  184. }
  185. else
  186. all_ns[i].status = 1;
  187. }
  188. }
  189. }
  190. setgroups(0, NULL);
  191. if(root_fd != -1) {
  192. if(fchdir(root_fd) < 0)
  193. collector_error("Cannot fchdir() to pid %d root directory", (int)pid);
  194. if(chroot(".") < 0)
  195. collector_error("Cannot chroot() to pid %d root directory", (int)pid);
  196. close(root_fd);
  197. }
  198. if(cwd_fd != -1) {
  199. if(fchdir(cwd_fd) < 0)
  200. collector_error("Cannot fchdir() to pid %d current working directory", (int)pid);
  201. close(cwd_fd);
  202. }
  203. int do_fork = 0;
  204. for(i = 0; all_ns[i].name ; i++)
  205. if(all_ns[i].fd != -1) {
  206. // CLONE_NEWPID requires a fork() to become effective
  207. if(all_ns[i].nstype == CLONE_NEWPID && all_ns[i].status)
  208. do_fork = 1;
  209. close(all_ns[i].fd);
  210. }
  211. if(do_fork)
  212. continue_as_child();
  213. return 0;
  214. #else
  215. errno = ENOSYS;
  216. collector_error("setns() is missing on this system.");
  217. return 1;
  218. #endif
  219. }
  220. pid_t read_pid_from_cgroup_file(const char *filename) {
  221. int fd = open(filename, procfile_open_flags);
  222. if(fd == -1) {
  223. collector_error("Cannot open pid_from_cgroup() file '%s'.", filename);
  224. return 0;
  225. }
  226. FILE *fp = fdopen(fd, "r");
  227. if(!fp) {
  228. collector_error("Cannot upgrade fd to fp for file '%s'.", filename);
  229. return 0;
  230. }
  231. char buffer[100 + 1];
  232. pid_t pid = 0;
  233. char *s;
  234. while((s = fgets(buffer, 100, fp))) {
  235. buffer[100] = '\0';
  236. pid = atoi(s);
  237. if(pid > 0) break;
  238. }
  239. fclose(fp);
  240. #ifdef NETDATA_INTERNAL_CHECKS
  241. if(pid > 0) collector_info("found pid %d on file '%s'", pid, filename);
  242. #endif
  243. return pid;
  244. }
  245. pid_t read_pid_from_cgroup_files(const char *path) {
  246. char filename[FILENAME_MAX + 1];
  247. snprintfz(filename, FILENAME_MAX, "%s/cgroup.procs", path);
  248. pid_t pid = read_pid_from_cgroup_file(filename);
  249. if(pid > 0) return pid;
  250. snprintfz(filename, FILENAME_MAX, "%s/tasks", path);
  251. return read_pid_from_cgroup_file(filename);
  252. }
  253. pid_t read_pid_from_cgroup(const char *path) {
  254. pid_t pid = read_pid_from_cgroup_files(path);
  255. if (pid > 0) return pid;
  256. DIR *dir = opendir(path);
  257. if (!dir) {
  258. collector_error("cannot read directory '%s'", path);
  259. return 0;
  260. }
  261. struct dirent *de = NULL;
  262. while ((de = readdir(dir))) {
  263. if (de->d_type == DT_DIR
  264. && (
  265. (de->d_name[0] == '.' && de->d_name[1] == '\0')
  266. || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
  267. ))
  268. continue;
  269. if (de->d_type == DT_DIR) {
  270. char filename[FILENAME_MAX + 1];
  271. snprintfz(filename, FILENAME_MAX, "%s/%s", path, de->d_name);
  272. pid = read_pid_from_cgroup(filename);
  273. if(pid > 0) break;
  274. }
  275. }
  276. closedir(dir);
  277. return pid;
  278. }
  279. // ----------------------------------------------------------------------------
  280. // send the result to netdata
  281. struct found_device {
  282. const char *host_device;
  283. const char *guest_device;
  284. uint32_t host_device_hash;
  285. struct found_device *next;
  286. } *detected_devices = NULL;
  287. void add_device(const char *host, const char *guest) {
  288. #ifdef NETDATA_INTERNAL_CHECKS
  289. collector_info("adding device with host '%s', guest '%s'", host, guest);
  290. #endif
  291. uint32_t hash = simple_hash(host);
  292. if(guest && (!*guest || strcmp(host, guest) == 0))
  293. guest = NULL;
  294. struct found_device *f;
  295. for(f = detected_devices; f ; f = f->next) {
  296. if(f->host_device_hash == hash && !strcmp(host, f->host_device)) {
  297. if(guest && (!f->guest_device || !strcmp(f->host_device, f->guest_device))) {
  298. if(f->guest_device) freez((void *)f->guest_device);
  299. f->guest_device = strdupz(guest);
  300. }
  301. return;
  302. }
  303. }
  304. f = mallocz(sizeof(struct found_device));
  305. f->host_device = strdupz(host);
  306. f->host_device_hash = hash;
  307. f->guest_device = (guest)?strdupz(guest):NULL;
  308. f->next = detected_devices;
  309. detected_devices = f;
  310. }
  311. int send_devices(void) {
  312. int found = 0;
  313. struct found_device *f;
  314. for(f = detected_devices; f ; f = f->next) {
  315. found++;
  316. printf("%s %s\n", f->host_device, (f->guest_device)?f->guest_device:f->host_device);
  317. }
  318. return found;
  319. }
  320. // ----------------------------------------------------------------------------
  321. // this function should be called only **ONCE**
  322. // also it has to be the **LAST** to be called
  323. // since it switches namespaces, so after this call, everything is different!
  324. void detect_veth_interfaces(pid_t pid) {
  325. struct iface *cgroup = NULL;
  326. struct iface *host, *h, *c;
  327. host = read_proc_net_dev("host", netdata_configured_host_prefix);
  328. if(!host) {
  329. errno = 0;
  330. collector_error("cannot read host interface list.");
  331. goto cleanup;
  332. }
  333. if(!eligible_ifaces(host)) {
  334. errno = 0;
  335. collector_info("there are no double-linked host interfaces available.");
  336. goto cleanup;
  337. }
  338. if(switch_namespace(netdata_configured_host_prefix, pid)) {
  339. errno = 0;
  340. collector_error("cannot switch to the namespace of pid %u", (unsigned int) pid);
  341. goto cleanup;
  342. }
  343. #ifdef NETDATA_INTERNAL_CHECKS
  344. collector_info("switched to namespaces of pid %d", pid);
  345. #endif
  346. cgroup = read_proc_net_dev("cgroup", NULL);
  347. if(!cgroup) {
  348. errno = 0;
  349. collector_error("cannot read cgroup interface list.");
  350. goto cleanup;
  351. }
  352. if(!eligible_ifaces(cgroup)) {
  353. errno = 0;
  354. collector_error("there are not double-linked cgroup interfaces available.");
  355. goto cleanup;
  356. }
  357. unsigned int host_dev_num = calc_num_ifaces(host);
  358. unsigned int cgroup_dev_num = calc_num_ifaces(cgroup);
  359. // host ifaces == guest ifaces => we are still in the host namespace
  360. // and we can't really identify which ifaces belong to the cgroup (e.g. Proxmox VM).
  361. if (host_dev_num == cgroup_dev_num) {
  362. unsigned int m = 0;
  363. for (h = host; h; h = h->next) {
  364. for (c = cgroup; c; c = c->next) {
  365. if (h->ifindex == c->ifindex && h->iflink == c->iflink) {
  366. m++;
  367. break;
  368. }
  369. }
  370. }
  371. if (host_dev_num == m) {
  372. goto cleanup;
  373. }
  374. }
  375. for(h = host; h ; h = h->next) {
  376. if(iface_is_eligible(h)) {
  377. for (c = cgroup; c; c = c->next) {
  378. if(iface_is_eligible(c) && h->ifindex == c->iflink && h->iflink == c->ifindex) {
  379. add_device(h->device, c->device);
  380. }
  381. }
  382. }
  383. }
  384. cleanup:
  385. free_host_ifaces(cgroup);
  386. free_host_ifaces(host);
  387. }
  388. // ----------------------------------------------------------------------------
  389. // call the external helper
  390. #define CGROUP_NETWORK_INTERFACE_MAX_LINE 2048
  391. void call_the_helper(pid_t pid, const char *cgroup) {
  392. if(setresuid(0, 0, 0) == -1)
  393. collector_error("setresuid(0, 0, 0) failed.");
  394. char command[CGROUP_NETWORK_INTERFACE_MAX_LINE + 1];
  395. if(cgroup)
  396. snprintfz(command, CGROUP_NETWORK_INTERFACE_MAX_LINE, "exec " PLUGINS_DIR "/cgroup-network-helper.sh --cgroup '%s'", cgroup);
  397. else
  398. snprintfz(command, CGROUP_NETWORK_INTERFACE_MAX_LINE, "exec " PLUGINS_DIR "/cgroup-network-helper.sh --pid %d", pid);
  399. collector_info("running: %s", command);
  400. pid_t cgroup_pid;
  401. FILE *fp_child_input, *fp_child_output;
  402. if(cgroup) {
  403. (void)netdata_popen_raw_default_flags(&cgroup_pid, environment, &fp_child_input, &fp_child_output, PLUGINS_DIR "/cgroup-network-helper.sh", "--cgroup", cgroup);
  404. }
  405. else {
  406. char buffer[100];
  407. snprintfz(buffer, sizeof(buffer) - 1, "%d", pid);
  408. (void)netdata_popen_raw_default_flags(&cgroup_pid, environment, &fp_child_input, &fp_child_output, PLUGINS_DIR "/cgroup-network-helper.sh", "--pid", buffer);
  409. }
  410. if(fp_child_output) {
  411. char buffer[CGROUP_NETWORK_INTERFACE_MAX_LINE + 1];
  412. char *s;
  413. while((s = fgets(buffer, CGROUP_NETWORK_INTERFACE_MAX_LINE, fp_child_output))) {
  414. trim(s);
  415. if(*s && *s != '\n') {
  416. char *t = s;
  417. while(*t && *t != ' ') t++;
  418. if(*t == ' ') {
  419. *t = '\0';
  420. t++;
  421. }
  422. if(!*s || !*t) continue;
  423. add_device(s, t);
  424. }
  425. }
  426. netdata_pclose(fp_child_input, fp_child_output, cgroup_pid);
  427. }
  428. else
  429. collector_error("cannot execute cgroup-network helper script: %s", command);
  430. }
  431. int is_valid_path_symbol(char c) {
  432. switch(c) {
  433. case '/': // path separators
  434. case '\\': // needed for virsh domains \x2d1\x2dname
  435. case ' ': // space
  436. case '-': // hyphen
  437. case '_': // underscore
  438. case '.': // dot
  439. case ',': // comma
  440. return 1;
  441. default:
  442. return 0;
  443. }
  444. }
  445. // we will pass this path a shell script running as root
  446. // so, we need to make sure the path will be valid
  447. // and will not include anything that could allow
  448. // the caller use shell expansion for gaining escalated
  449. // privileges.
  450. int verify_path(const char *path) {
  451. struct stat sb;
  452. char c;
  453. const char *s = path;
  454. while((c = *s++)) {
  455. if(!( isalnum(c) || is_valid_path_symbol(c) )) {
  456. collector_error("invalid character in path '%s'", path);
  457. return -1;
  458. }
  459. }
  460. if(strstr(path, "\\") && !strstr(path, "\\x")) {
  461. collector_error("invalid escape sequence in path '%s'", path);
  462. return 1;
  463. }
  464. if(strstr(path, "/../")) {
  465. collector_error("invalid parent path sequence detected in '%s'", path);
  466. return 1;
  467. }
  468. if(path[0] != '/') {
  469. collector_error("only absolute path names are supported - invalid path '%s'", path);
  470. return -1;
  471. }
  472. if (stat(path, &sb) == -1) {
  473. collector_error("cannot stat() path '%s'", path);
  474. return -1;
  475. }
  476. if((sb.st_mode & S_IFMT) != S_IFDIR) {
  477. collector_error("path '%s' is not a directory", path);
  478. return -1;
  479. }
  480. return 0;
  481. }
  482. /*
  483. char *fix_path_variable(void) {
  484. const char *path = getenv("PATH");
  485. if(!path || !*path) return 0;
  486. char *p = strdupz(path);
  487. char *safe_path = callocz(1, strlen(p) + strlen("PATH=") + 1);
  488. strcpy(safe_path, "PATH=");
  489. int added = 0;
  490. char *ptr = p;
  491. while(ptr && *ptr) {
  492. char *s = strsep(&ptr, ":");
  493. if(s && *s) {
  494. if(verify_path(s) == -1) {
  495. collector_error("the PATH variable includes an invalid path '%s' - removed it.", s);
  496. }
  497. else {
  498. collector_info("the PATH variable includes a valid path '%s'.", s);
  499. if(added) strcat(safe_path, ":");
  500. strcat(safe_path, s);
  501. added++;
  502. }
  503. }
  504. }
  505. collector_info("unsafe PATH: '%s'.", path);
  506. collector_info(" safe PATH: '%s'.", safe_path);
  507. freez(p);
  508. return safe_path;
  509. }
  510. */
  511. // ----------------------------------------------------------------------------
  512. // main
  513. void usage(void) {
  514. fprintf(stderr, "%s [ -p PID | --pid PID | --cgroup /path/to/cgroup ]\n", program_name);
  515. exit(1);
  516. }
  517. int main(int argc, char **argv) {
  518. stderror = stderr;
  519. pid_t pid = 0;
  520. program_name = argv[0];
  521. program_version = VERSION;
  522. error_log_syslog = 0;
  523. // since cgroup-network runs as root, prevent it from opening symbolic links
  524. procfile_open_flags = O_RDONLY|O_NOFOLLOW;
  525. // ------------------------------------------------------------------------
  526. // make sure NETDATA_HOST_PREFIX is safe
  527. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  528. if(verify_netdata_host_prefix() == -1) exit(1);
  529. if(netdata_configured_host_prefix[0] != '\0' && verify_path(netdata_configured_host_prefix) == -1)
  530. fatal("invalid NETDATA_HOST_PREFIX '%s'", netdata_configured_host_prefix);
  531. // ------------------------------------------------------------------------
  532. // build a safe environment for our script
  533. // the first environment variable is a fixed PATH=
  534. snprintfz(environment_variable2, sizeof(environment_variable2) - 1, "NETDATA_HOST_PREFIX=%s", netdata_configured_host_prefix);
  535. char *s = getenv("NETDATA_LOG_SEVERITY_LEVEL");
  536. if (s)
  537. snprintfz(environment_variable3, sizeof(environment_variable3) - 1, "NETDATA_LOG_SEVERITY_LEVEL=%s", s);
  538. // ------------------------------------------------------------------------
  539. if(argc == 2 && (!strcmp(argv[1], "version") || !strcmp(argv[1], "-version") || !strcmp(argv[1], "--version") || !strcmp(argv[1], "-v") || !strcmp(argv[1], "-V"))) {
  540. fprintf(stderr, "cgroup-network %s\n", VERSION);
  541. exit(0);
  542. }
  543. if(argc != 3)
  544. usage();
  545. log_set_global_severity_for_external_plugins();
  546. int arg = 1;
  547. int helper = 1;
  548. if (getenv("KUBERNETES_SERVICE_HOST") != NULL && getenv("KUBERNETES_SERVICE_PORT") != NULL)
  549. helper = 0;
  550. if(!strcmp(argv[arg], "-p") || !strcmp(argv[arg], "--pid")) {
  551. pid = atoi(argv[arg+1]);
  552. if(pid <= 0) {
  553. errno = 0;
  554. collector_error("Invalid pid %d given", (int) pid);
  555. return 2;
  556. }
  557. if(helper) call_the_helper(pid, NULL);
  558. }
  559. else if(!strcmp(argv[arg], "--cgroup")) {
  560. char *cgroup = argv[arg+1];
  561. if(verify_path(cgroup) == -1) {
  562. collector_error("cgroup '%s' does not exist or is not valid.", cgroup);
  563. return 1;
  564. }
  565. pid = read_pid_from_cgroup(cgroup);
  566. if(helper) call_the_helper(pid, cgroup);
  567. if(pid <= 0 && !detected_devices) {
  568. errno = 0;
  569. collector_error("Cannot find a cgroup PID from cgroup '%s'", cgroup);
  570. }
  571. }
  572. else
  573. usage();
  574. if(pid > 0)
  575. detect_veth_interfaces(pid);
  576. int found = send_devices();
  577. if(found <= 0) return 1;
  578. return 0;
  579. }