namespaces.c 5.3 KB

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