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