nsenter.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package namespaces
  2. /*
  3. #include <dirent.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <linux/sched.h>
  7. #include <signal.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. static const kBufSize = 256;
  15. void get_args(int *argc, char ***argv) {
  16. // Read argv
  17. int fd = open("/proc/self/cmdline", O_RDONLY);
  18. // Read the whole commandline.
  19. ssize_t contents_size = 0;
  20. ssize_t contents_offset = 0;
  21. char *contents = NULL;
  22. ssize_t bytes_read = 0;
  23. do {
  24. contents_size += kBufSize;
  25. contents = (char *) realloc(contents, contents_size);
  26. bytes_read = read(fd, contents + contents_offset, contents_size - contents_offset);
  27. contents_offset += bytes_read;
  28. } while (bytes_read > 0);
  29. close(fd);
  30. // Parse the commandline into an argv. /proc/self/cmdline has \0 delimited args.
  31. ssize_t i;
  32. *argc = 0;
  33. for (i = 0; i < contents_offset; i++) {
  34. if (contents[i] == '\0') {
  35. (*argc)++;
  36. }
  37. }
  38. *argv = (char **) malloc(sizeof(char *) * ((*argc) + 1));
  39. int idx;
  40. for (idx = 0; idx < (*argc); idx++) {
  41. (*argv)[idx] = contents;
  42. contents += strlen(contents) + 1;
  43. }
  44. (*argv)[*argc] = NULL;
  45. }
  46. void nsenter() {
  47. int argc;
  48. char **argv;
  49. get_args(&argc, &argv);
  50. // Ignore if this is not for us.
  51. if (argc < 2 || strcmp(argv[1], "nsenter") != 0) {
  52. return;
  53. }
  54. // USAGE: <binary> nsenter <PID> <process label> <container JSON> <argv>...
  55. if (argc < 6) {
  56. fprintf(stderr, "nsenter: Incorrect usage, not enough arguments\n");
  57. exit(1);
  58. }
  59. pid_t init_pid = strtol(argv[2], NULL, 10);
  60. if (errno != 0 || init_pid <= 0) {
  61. fprintf(stderr, "nsenter: Failed to parse PID from \"%s\" with error: \"%s\"\n", argv[2], strerror(errno));
  62. exit(1);
  63. }
  64. argc -= 3;
  65. argv += 3;
  66. // Setns on all supported namespaces.
  67. char ns_dir[kBufSize];
  68. memset(ns_dir, 0, kBufSize);
  69. if (snprintf(ns_dir, kBufSize - 1, "/proc/%d/ns/", init_pid) < 0) {
  70. fprintf(stderr, "nsenter: Error getting ns dir path with error: \"%s\"\n", strerror(errno));
  71. exit(1);
  72. }
  73. struct dirent *dent;
  74. DIR *dir = opendir(ns_dir);
  75. if (dir == NULL) {
  76. fprintf(stderr, "nsenter: Failed to open directory \"%s\" with error: \"%s\"\n", ns_dir, strerror(errno));
  77. exit(1);
  78. }
  79. while((dent = readdir(dir)) != NULL) {
  80. if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0 || strcmp(dent->d_name, "user") == 0) {
  81. continue;
  82. }
  83. // Get and open the namespace for the init we are joining..
  84. char buf[kBufSize];
  85. memset(buf, 0, kBufSize);
  86. strncat(buf, ns_dir, kBufSize - 1);
  87. strncat(buf, dent->d_name, kBufSize - 1);
  88. int fd = open(buf, O_RDONLY);
  89. if (fd == -1) {
  90. fprintf(stderr, "nsenter: Failed to open ns file \"%s\" for ns \"%s\" with error: \"%s\"\n", buf, dent->d_name, strerror(errno));
  91. exit(1);
  92. }
  93. // Set the namespace.
  94. if (setns(fd, 0) == -1) {
  95. fprintf(stderr, "nsenter: Failed to setns for \"%s\" with error: \"%s\"\n", dent->d_name, strerror(errno));
  96. exit(1);
  97. }
  98. close(fd);
  99. }
  100. closedir(dir);
  101. // We must fork to actually enter the PID namespace.
  102. int child = fork();
  103. if (child == 0) {
  104. // Finish executing, let the Go runtime take over.
  105. return;
  106. } else {
  107. // Parent, wait for the child.
  108. int status = 0;
  109. if (waitpid(child, &status, 0) == -1) {
  110. fprintf(stderr, "nsenter: Failed to waitpid with error: \"%s\"\n", strerror(errno));
  111. exit(1);
  112. }
  113. // Forward the child's exit code or re-send its death signal.
  114. if (WIFEXITED(status)) {
  115. exit(WEXITSTATUS(status));
  116. } else if (WIFSIGNALED(status)) {
  117. kill(getpid(), WTERMSIG(status));
  118. }
  119. exit(1);
  120. }
  121. return;
  122. }
  123. __attribute__((constructor)) init() {
  124. nsenter();
  125. }
  126. */
  127. import "C"