cgroup-network.c 20 KB

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