namespaces.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2017 Hari Bathini, IBM Corporation
  7. */
  8. #include "namespaces.h"
  9. #include "util.h"
  10. #include "event.h"
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <limits.h>
  14. #include <sched.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <asm/bug.h>
  20. struct namespaces *namespaces__new(struct namespaces_event *event)
  21. {
  22. struct namespaces *namespaces;
  23. u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
  24. sizeof(struct perf_ns_link_info));
  25. namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
  26. if (!namespaces)
  27. return NULL;
  28. namespaces->end_time = -1;
  29. if (event)
  30. memcpy(namespaces->link_info, event->link_info, link_info_size);
  31. return namespaces;
  32. }
  33. void namespaces__free(struct namespaces *namespaces)
  34. {
  35. free(namespaces);
  36. }
  37. int nsinfo__init(struct nsinfo *nsi)
  38. {
  39. char oldns[PATH_MAX];
  40. char spath[PATH_MAX];
  41. char *newns = NULL;
  42. char *statln = NULL;
  43. struct stat old_stat;
  44. struct stat new_stat;
  45. FILE *f = NULL;
  46. size_t linesz = 0;
  47. int rv = -1;
  48. if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
  49. return rv;
  50. if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
  51. return rv;
  52. if (stat(oldns, &old_stat) < 0)
  53. goto out;
  54. if (stat(newns, &new_stat) < 0)
  55. goto out;
  56. /* Check if the mount namespaces differ, if so then indicate that we
  57. * want to switch as part of looking up dso/map data.
  58. */
  59. if (old_stat.st_ino != new_stat.st_ino) {
  60. nsi->need_setns = true;
  61. nsi->mntns_path = newns;
  62. newns = NULL;
  63. }
  64. /* If we're dealing with a process that is in a different PID namespace,
  65. * attempt to work out the innermost tgid for the process.
  66. */
  67. if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
  68. goto out;
  69. f = fopen(spath, "r");
  70. if (f == NULL)
  71. goto out;
  72. while (getline(&statln, &linesz, f) != -1) {
  73. /* Use tgid if CONFIG_PID_NS is not defined. */
  74. if (strstr(statln, "Tgid:") != NULL) {
  75. nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
  76. NULL, 10);
  77. nsi->nstgid = nsi->tgid;
  78. }
  79. if (strstr(statln, "NStgid:") != NULL) {
  80. nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'),
  81. NULL, 10);
  82. break;
  83. }
  84. }
  85. rv = 0;
  86. out:
  87. if (f != NULL)
  88. (void) fclose(f);
  89. free(statln);
  90. free(newns);
  91. return rv;
  92. }
  93. struct nsinfo *nsinfo__new(pid_t pid)
  94. {
  95. struct nsinfo *nsi;
  96. if (pid == 0)
  97. return NULL;
  98. nsi = calloc(1, sizeof(*nsi));
  99. if (nsi != NULL) {
  100. nsi->pid = pid;
  101. nsi->tgid = pid;
  102. nsi->nstgid = pid;
  103. nsi->need_setns = false;
  104. /* Init may fail if the process exits while we're trying to look
  105. * at its proc information. In that case, save the pid but
  106. * don't try to enter the namespace.
  107. */
  108. if (nsinfo__init(nsi) == -1)
  109. nsi->need_setns = false;
  110. refcount_set(&nsi->refcnt, 1);
  111. }
  112. return nsi;
  113. }
  114. struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
  115. {
  116. struct nsinfo *nnsi;
  117. if (nsi == NULL)
  118. return NULL;
  119. nnsi = calloc(1, sizeof(*nnsi));
  120. if (nnsi != NULL) {
  121. nnsi->pid = nsi->pid;
  122. nnsi->tgid = nsi->tgid;
  123. nnsi->nstgid = nsi->nstgid;
  124. nnsi->need_setns = nsi->need_setns;
  125. if (nsi->mntns_path) {
  126. nnsi->mntns_path = strdup(nsi->mntns_path);
  127. if (!nnsi->mntns_path) {
  128. free(nnsi);
  129. return NULL;
  130. }
  131. }
  132. refcount_set(&nnsi->refcnt, 1);
  133. }
  134. return nnsi;
  135. }
  136. void nsinfo__delete(struct nsinfo *nsi)
  137. {
  138. zfree(&nsi->mntns_path);
  139. free(nsi);
  140. }
  141. struct nsinfo *nsinfo__get(struct nsinfo *nsi)
  142. {
  143. if (nsi)
  144. refcount_inc(&nsi->refcnt);
  145. return nsi;
  146. }
  147. void nsinfo__put(struct nsinfo *nsi)
  148. {
  149. if (nsi && refcount_dec_and_test(&nsi->refcnt))
  150. nsinfo__delete(nsi);
  151. }
  152. void nsinfo__mountns_enter(struct nsinfo *nsi,
  153. struct nscookie *nc)
  154. {
  155. char curpath[PATH_MAX];
  156. int oldns = -1;
  157. int newns = -1;
  158. char *oldcwd = NULL;
  159. if (nc == NULL)
  160. return;
  161. nc->oldns = -1;
  162. nc->newns = -1;
  163. if (!nsi || !nsi->need_setns)
  164. return;
  165. if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
  166. return;
  167. oldcwd = get_current_dir_name();
  168. if (!oldcwd)
  169. return;
  170. oldns = open(curpath, O_RDONLY);
  171. if (oldns < 0)
  172. goto errout;
  173. newns = open(nsi->mntns_path, O_RDONLY);
  174. if (newns < 0)
  175. goto errout;
  176. if (setns(newns, CLONE_NEWNS) < 0)
  177. goto errout;
  178. nc->oldcwd = oldcwd;
  179. nc->oldns = oldns;
  180. nc->newns = newns;
  181. return;
  182. errout:
  183. free(oldcwd);
  184. if (oldns > -1)
  185. close(oldns);
  186. if (newns > -1)
  187. close(newns);
  188. }
  189. void nsinfo__mountns_exit(struct nscookie *nc)
  190. {
  191. if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
  192. return;
  193. setns(nc->oldns, CLONE_NEWNS);
  194. if (nc->oldcwd) {
  195. WARN_ON_ONCE(chdir(nc->oldcwd));
  196. zfree(&nc->oldcwd);
  197. }
  198. if (nc->oldns > -1) {
  199. close(nc->oldns);
  200. nc->oldns = -1;
  201. }
  202. if (nc->newns > -1) {
  203. close(nc->newns);
  204. nc->newns = -1;
  205. }
  206. }
  207. char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
  208. {
  209. char *rpath;
  210. struct nscookie nsc;
  211. nsinfo__mountns_enter(nsi, &nsc);
  212. rpath = realpath(path, NULL);
  213. nsinfo__mountns_exit(&nsc);
  214. return rpath;
  215. }