nsenter.go 4.5 KB

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