Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / util / namespaces.c
blobaed170bd4384ef72a012453a578497bfe912ae61
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.
6 * Copyright (C) 2017 Hari Bathini, IBM Corporation
7 */
9 #include "namespaces.h"
10 #include "util.h"
11 #include "event.h"
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <limits.h>
16 #include <sched.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <asm/bug.h>
23 struct namespaces *namespaces__new(struct namespaces_event *event)
25 struct namespaces *namespaces;
26 u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
27 sizeof(struct perf_ns_link_info));
29 namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
30 if (!namespaces)
31 return NULL;
33 namespaces->end_time = -1;
35 if (event)
36 memcpy(namespaces->link_info, event->link_info, link_info_size);
38 return namespaces;
41 void namespaces__free(struct namespaces *namespaces)
43 free(namespaces);
46 int nsinfo__init(struct nsinfo *nsi)
48 char oldns[PATH_MAX];
49 char spath[PATH_MAX];
50 char *newns = NULL;
51 char *statln = NULL;
52 struct stat old_stat;
53 struct stat new_stat;
54 FILE *f = NULL;
55 size_t linesz = 0;
56 int rv = -1;
58 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
59 return rv;
61 if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
62 return rv;
64 if (stat(oldns, &old_stat) < 0)
65 goto out;
67 if (stat(newns, &new_stat) < 0)
68 goto out;
70 /* Check if the mount namespaces differ, if so then indicate that we
71 * want to switch as part of looking up dso/map data.
73 if (old_stat.st_ino != new_stat.st_ino) {
74 nsi->need_setns = true;
75 nsi->mntns_path = newns;
76 newns = NULL;
79 /* If we're dealing with a process that is in a different PID namespace,
80 * attempt to work out the innermost tgid for the process.
82 if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
83 goto out;
85 f = fopen(spath, "r");
86 if (f == NULL)
87 goto out;
89 while (getline(&statln, &linesz, f) != -1) {
90 /* Use tgid if CONFIG_PID_NS is not defined. */
91 if (strstr(statln, "Tgid:") != NULL) {
92 nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
93 NULL, 10);
94 nsi->nstgid = nsi->tgid;
97 if (strstr(statln, "NStgid:") != NULL) {
98 nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'),
99 NULL, 10);
100 break;
103 rv = 0;
105 out:
106 if (f != NULL)
107 (void) fclose(f);
108 free(statln);
109 free(newns);
110 return rv;
113 struct nsinfo *nsinfo__new(pid_t pid)
115 struct nsinfo *nsi;
117 if (pid == 0)
118 return NULL;
120 nsi = calloc(1, sizeof(*nsi));
121 if (nsi != NULL) {
122 nsi->pid = pid;
123 nsi->tgid = pid;
124 nsi->nstgid = pid;
125 nsi->need_setns = false;
126 /* Init may fail if the process exits while we're trying to look
127 * at its proc information. In that case, save the pid but
128 * don't try to enter the namespace.
130 if (nsinfo__init(nsi) == -1)
131 nsi->need_setns = false;
133 refcount_set(&nsi->refcnt, 1);
136 return nsi;
139 struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
141 struct nsinfo *nnsi;
143 if (nsi == NULL)
144 return NULL;
146 nnsi = calloc(1, sizeof(*nnsi));
147 if (nnsi != NULL) {
148 nnsi->pid = nsi->pid;
149 nnsi->tgid = nsi->tgid;
150 nnsi->nstgid = nsi->nstgid;
151 nnsi->need_setns = nsi->need_setns;
152 if (nsi->mntns_path) {
153 nnsi->mntns_path = strdup(nsi->mntns_path);
154 if (!nnsi->mntns_path) {
155 free(nnsi);
156 return NULL;
159 refcount_set(&nnsi->refcnt, 1);
162 return nnsi;
165 void nsinfo__delete(struct nsinfo *nsi)
167 zfree(&nsi->mntns_path);
168 free(nsi);
171 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
173 if (nsi)
174 refcount_inc(&nsi->refcnt);
175 return nsi;
178 void nsinfo__put(struct nsinfo *nsi)
180 if (nsi && refcount_dec_and_test(&nsi->refcnt))
181 nsinfo__delete(nsi);
184 void nsinfo__mountns_enter(struct nsinfo *nsi,
185 struct nscookie *nc)
187 char curpath[PATH_MAX];
188 int oldns = -1;
189 int newns = -1;
190 char *oldcwd = NULL;
192 if (nc == NULL)
193 return;
195 nc->oldns = -1;
196 nc->newns = -1;
198 if (!nsi || !nsi->need_setns)
199 return;
201 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
202 return;
204 oldcwd = get_current_dir_name();
205 if (!oldcwd)
206 return;
208 oldns = open(curpath, O_RDONLY);
209 if (oldns < 0)
210 goto errout;
212 newns = open(nsi->mntns_path, O_RDONLY);
213 if (newns < 0)
214 goto errout;
216 if (setns(newns, CLONE_NEWNS) < 0)
217 goto errout;
219 nc->oldcwd = oldcwd;
220 nc->oldns = oldns;
221 nc->newns = newns;
222 return;
224 errout:
225 free(oldcwd);
226 if (oldns > -1)
227 close(oldns);
228 if (newns > -1)
229 close(newns);
232 void nsinfo__mountns_exit(struct nscookie *nc)
234 if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
235 return;
237 setns(nc->oldns, CLONE_NEWNS);
239 if (nc->oldcwd) {
240 WARN_ON_ONCE(chdir(nc->oldcwd));
241 zfree(&nc->oldcwd);
244 if (nc->oldns > -1) {
245 close(nc->oldns);
246 nc->oldns = -1;
249 if (nc->newns > -1) {
250 close(nc->newns);
251 nc->newns = -1;
255 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
257 char *rpath;
258 struct nscookie nsc;
260 nsinfo__mountns_enter(nsi, &nsc);
261 rpath = realpath(path, NULL);
262 nsinfo__mountns_exit(&nsc);
264 return rpath;