nsenter.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // +build linux
  2. package namespaces
  3. /*
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <linux/limits.h>
  7. #include <linux/sched.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #include <getopt.h>
  15. static const kBufSize = 256;
  16. void get_args(int *argc, char ***argv) {
  17. // Read argv
  18. int fd = open("/proc/self/cmdline", O_RDONLY);
  19. // Read the whole commandline.
  20. ssize_t contents_size = 0;
  21. ssize_t contents_offset = 0;
  22. char *contents = NULL;
  23. ssize_t bytes_read = 0;
  24. do {
  25. contents_size += kBufSize;
  26. contents = (char *) realloc(contents, contents_size);
  27. bytes_read = read(fd, contents + contents_offset, contents_size - contents_offset);
  28. contents_offset += bytes_read;
  29. } while (bytes_read > 0);
  30. close(fd);
  31. // Parse the commandline into an argv. /proc/self/cmdline has \0 delimited args.
  32. ssize_t i;
  33. *argc = 0;
  34. for (i = 0; i < contents_offset; i++) {
  35. if (contents[i] == '\0') {
  36. (*argc)++;
  37. }
  38. }
  39. *argv = (char **) malloc(sizeof(char *) * ((*argc) + 1));
  40. int idx;
  41. for (idx = 0; idx < (*argc); idx++) {
  42. (*argv)[idx] = contents;
  43. contents += strlen(contents) + 1;
  44. }
  45. (*argv)[*argc] = NULL;
  46. }
  47. // Use raw setns syscall for versions of glibc that don't include it (namely glibc-2.12)
  48. #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 14
  49. #define _GNU_SOURCE
  50. #include <sched.h>
  51. #include "syscall.h"
  52. #ifdef SYS_setns
  53. int setns(int fd, int nstype) {
  54. return syscall(SYS_setns, fd, nstype);
  55. }
  56. #endif
  57. #endif
  58. void print_usage() {
  59. fprintf(stderr, "<binary> nsenter --nspid <pid> --containerjson <container_json> -- cmd1 arg1 arg2...\n");
  60. }
  61. void nsenter() {
  62. int argc;
  63. char **argv;
  64. get_args(&argc, &argv);
  65. // Ignore if this is not for us.
  66. if (argc < 2 || strcmp(argv[1], "nsenter") != 0) {
  67. return;
  68. }
  69. // USAGE: <binary> nsenter <PID> <process label> <container JSON> <argv>...
  70. if (argc < 6) {
  71. fprintf(stderr, "nsenter: Incorrect usage, not enough arguments\n");
  72. exit(1);
  73. }
  74. static const struct option longopts[] = {
  75. { "nspid", required_argument, NULL, 'n' },
  76. { "containerjson", required_argument, NULL, 'c' },
  77. { "console", required_argument, NULL, 't' },
  78. { NULL, 0, NULL, 0 }
  79. };
  80. int c;
  81. pid_t init_pid = -1;
  82. char *init_pid_str = NULL;
  83. char *container_json = NULL;
  84. char *console = NULL;
  85. while ((c = getopt_long_only(argc, argv, "n:s:c:", longopts, NULL)) != -1) {
  86. switch (c) {
  87. case 'n':
  88. init_pid_str = optarg;
  89. break;
  90. case 'c':
  91. container_json = optarg;
  92. break;
  93. case 't':
  94. console = optarg;
  95. break;
  96. }
  97. }
  98. if (container_json == NULL || init_pid_str == NULL) {
  99. print_usage();
  100. exit(1);
  101. }
  102. init_pid = strtol(init_pid_str, NULL, 10);
  103. if (errno != 0 || init_pid <= 0) {
  104. fprintf(stderr, "nsenter: Failed to parse PID from \"%s\" with error: \"%s\"\n", init_pid_str, strerror(errno));
  105. print_usage();
  106. exit(1);
  107. }
  108. argc -= 3;
  109. argv += 3;
  110. if (setsid() == -1) {
  111. fprintf(stderr, "setsid failed. Error: %s\n", strerror(errno));
  112. exit(1);
  113. }
  114. // before we setns we need to dup the console
  115. int consolefd = -1;
  116. if (console != NULL) {
  117. consolefd = open(console, O_RDWR);
  118. if (consolefd < 0) {
  119. fprintf(stderr, "nsenter: failed to open console %s %s\n", console, strerror(errno));
  120. exit(1);
  121. }
  122. }
  123. // Setns on all supported namespaces.
  124. char ns_dir[PATH_MAX];
  125. memset(ns_dir, 0, PATH_MAX);
  126. snprintf(ns_dir, PATH_MAX - 1, "/proc/%d/ns/", init_pid);
  127. char* namespaces[] = {"ipc", "uts", "net", "pid", "mnt"};
  128. const int num = sizeof(namespaces) / sizeof(char*);
  129. int i;
  130. for (i = 0; i < num; i++) {
  131. char buf[PATH_MAX];
  132. memset(buf, 0, PATH_MAX);
  133. snprintf(buf, PATH_MAX - 1, "%s%s", ns_dir, namespaces[i]);
  134. int fd = open(buf, O_RDONLY);
  135. if (fd == -1) {
  136. // Ignore nonexistent namespaces.
  137. if (errno == ENOENT)
  138. continue;
  139. fprintf(stderr, "nsenter: Failed to open ns file \"%s\" for ns \"%s\" with error: \"%s\"\n", buf, namespaces[i], strerror(errno));
  140. exit(1);
  141. }
  142. // Set the namespace.
  143. if (setns(fd, 0) == -1) {
  144. fprintf(stderr, "nsenter: Failed to setns for \"%s\" with error: \"%s\"\n", namespaces[i], strerror(errno));
  145. exit(1);
  146. }
  147. close(fd);
  148. }
  149. // We must fork to actually enter the PID namespace.
  150. int child = fork();
  151. if (child == 0) {
  152. if (consolefd != -1) {
  153. if (dup2(consolefd, STDIN_FILENO) != 0) {
  154. fprintf(stderr, "nsenter: failed to dup 0 %s\n", strerror(errno));
  155. exit(1);
  156. }
  157. if (dup2(consolefd, STDOUT_FILENO) != STDOUT_FILENO) {
  158. fprintf(stderr, "nsenter: failed to dup 1 %s\n", strerror(errno));
  159. exit(1);
  160. }
  161. if (dup2(consolefd, STDERR_FILENO) != STDERR_FILENO) {
  162. fprintf(stderr, "nsenter: failed to dup 2 %s\n", strerror(errno));
  163. exit(1);
  164. }
  165. }
  166. // Finish executing, let the Go runtime take over.
  167. return;
  168. } else {
  169. // Parent, wait for the child.
  170. int status = 0;
  171. if (waitpid(child, &status, 0) == -1) {
  172. fprintf(stderr, "nsenter: Failed to waitpid with error: \"%s\"\n", strerror(errno));
  173. exit(1);
  174. }
  175. // Forward the child's exit code or re-send its death signal.
  176. if (WIFEXITED(status)) {
  177. exit(WEXITSTATUS(status));
  178. } else if (WIFSIGNALED(status)) {
  179. kill(getpid(), WTERMSIG(status));
  180. }
  181. exit(1);
  182. }
  183. return;
  184. }
  185. __attribute__((constructor)) init() {
  186. nsenter();
  187. }
  188. */
  189. import "C"