aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/namespaces.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/namespaces.c')
-rw-r--r--tools/perf/util/namespaces.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index cf8bd123cf73..aed170bd4384 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -18,6 +18,7 @@
18#include <stdio.h> 18#include <stdio.h>
19#include <string.h> 19#include <string.h>
20#include <unistd.h> 20#include <unistd.h>
21#include <asm/bug.h>
21 22
22struct namespaces *namespaces__new(struct namespaces_event *event) 23struct namespaces *namespaces__new(struct namespaces_event *event)
23{ 24{
@@ -186,6 +187,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
186 char curpath[PATH_MAX]; 187 char curpath[PATH_MAX];
187 int oldns = -1; 188 int oldns = -1;
188 int newns = -1; 189 int newns = -1;
190 char *oldcwd = NULL;
189 191
190 if (nc == NULL) 192 if (nc == NULL)
191 return; 193 return;
@@ -199,9 +201,13 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
199 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) 201 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
200 return; 202 return;
201 203
204 oldcwd = get_current_dir_name();
205 if (!oldcwd)
206 return;
207
202 oldns = open(curpath, O_RDONLY); 208 oldns = open(curpath, O_RDONLY);
203 if (oldns < 0) 209 if (oldns < 0)
204 return; 210 goto errout;
205 211
206 newns = open(nsi->mntns_path, O_RDONLY); 212 newns = open(nsi->mntns_path, O_RDONLY);
207 if (newns < 0) 213 if (newns < 0)
@@ -210,11 +216,13 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
210 if (setns(newns, CLONE_NEWNS) < 0) 216 if (setns(newns, CLONE_NEWNS) < 0)
211 goto errout; 217 goto errout;
212 218
219 nc->oldcwd = oldcwd;
213 nc->oldns = oldns; 220 nc->oldns = oldns;
214 nc->newns = newns; 221 nc->newns = newns;
215 return; 222 return;
216 223
217errout: 224errout:
225 free(oldcwd);
218 if (oldns > -1) 226 if (oldns > -1)
219 close(oldns); 227 close(oldns);
220 if (newns > -1) 228 if (newns > -1)
@@ -223,11 +231,16 @@ errout:
223 231
224void nsinfo__mountns_exit(struct nscookie *nc) 232void nsinfo__mountns_exit(struct nscookie *nc)
225{ 233{
226 if (nc == NULL || nc->oldns == -1 || nc->newns == -1) 234 if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
227 return; 235 return;
228 236
229 setns(nc->oldns, CLONE_NEWNS); 237 setns(nc->oldns, CLONE_NEWNS);
230 238
239 if (nc->oldcwd) {
240 WARN_ON_ONCE(chdir(nc->oldcwd));
241 zfree(&nc->oldcwd);
242 }
243
231 if (nc->oldns > -1) { 244 if (nc->oldns > -1) {
232 close(nc->oldns); 245 close(nc->oldns);
233 nc->oldns = -1; 246 nc->oldns = -1;