xref: /linux/tools/perf/util/namespaces.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f3b3614aSHari Bathini /*
3f3b3614aSHari Bathini  *
4f3b3614aSHari Bathini  * Copyright (C) 2017 Hari Bathini, IBM Corporation
5f3b3614aSHari Bathini  */
6f3b3614aSHari Bathini 
7f3b3614aSHari Bathini #include "namespaces.h"
8f3b3614aSHari Bathini #include "util.h"
9f3b3614aSHari Bathini #include "event.h"
10843ff37bSKrister Johansen #include <sys/types.h>
11843ff37bSKrister Johansen #include <sys/stat.h>
12c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h>
13544abd44SKrister Johansen #include <limits.h>
14843ff37bSKrister Johansen #include <sched.h>
15f3b3614aSHari Bathini #include <stdlib.h>
16f3b3614aSHari Bathini #include <stdio.h>
1772f7c4d2SArnaldo Carvalho de Melo #include <string.h>
18843ff37bSKrister Johansen #include <unistd.h>
19b01c1f69SJiri Olsa #include <asm/bug.h>
20f3b3614aSHari Bathini 
21f3b3614aSHari Bathini struct namespaces *namespaces__new(struct namespaces_event *event)
22f3b3614aSHari Bathini {
23f3b3614aSHari Bathini 	struct namespaces *namespaces;
24f3b3614aSHari Bathini 	u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
25f3b3614aSHari Bathini 			      sizeof(struct perf_ns_link_info));
26f3b3614aSHari Bathini 
27f3b3614aSHari Bathini 	namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
28f3b3614aSHari Bathini 	if (!namespaces)
29f3b3614aSHari Bathini 		return NULL;
30f3b3614aSHari Bathini 
31f3b3614aSHari Bathini 	namespaces->end_time = -1;
32f3b3614aSHari Bathini 
33f3b3614aSHari Bathini 	if (event)
34f3b3614aSHari Bathini 		memcpy(namespaces->link_info, event->link_info, link_info_size);
35f3b3614aSHari Bathini 
36f3b3614aSHari Bathini 	return namespaces;
37f3b3614aSHari Bathini }
38f3b3614aSHari Bathini 
39f3b3614aSHari Bathini void namespaces__free(struct namespaces *namespaces)
40f3b3614aSHari Bathini {
41f3b3614aSHari Bathini 	free(namespaces);
42f3b3614aSHari Bathini }
43843ff37bSKrister Johansen 
44bf2e710bSKrister Johansen int nsinfo__init(struct nsinfo *nsi)
45843ff37bSKrister Johansen {
46843ff37bSKrister Johansen 	char oldns[PATH_MAX];
47bf2e710bSKrister Johansen 	char spath[PATH_MAX];
48843ff37bSKrister Johansen 	char *newns = NULL;
49bf2e710bSKrister Johansen 	char *statln = NULL;
50843ff37bSKrister Johansen 	struct stat old_stat;
51843ff37bSKrister Johansen 	struct stat new_stat;
52bf2e710bSKrister Johansen 	FILE *f = NULL;
53bf2e710bSKrister Johansen 	size_t linesz = 0;
54bf2e710bSKrister Johansen 	int rv = -1;
55843ff37bSKrister Johansen 
56843ff37bSKrister Johansen 	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
57bf2e710bSKrister Johansen 		return rv;
58843ff37bSKrister Johansen 
59843ff37bSKrister Johansen 	if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
60bf2e710bSKrister Johansen 		return rv;
61843ff37bSKrister Johansen 
62843ff37bSKrister Johansen 	if (stat(oldns, &old_stat) < 0)
63843ff37bSKrister Johansen 		goto out;
64843ff37bSKrister Johansen 
65843ff37bSKrister Johansen 	if (stat(newns, &new_stat) < 0)
66843ff37bSKrister Johansen 		goto out;
67843ff37bSKrister Johansen 
68843ff37bSKrister Johansen 	/* Check if the mount namespaces differ, if so then indicate that we
69843ff37bSKrister Johansen 	 * want to switch as part of looking up dso/map data.
70843ff37bSKrister Johansen 	 */
71843ff37bSKrister Johansen 	if (old_stat.st_ino != new_stat.st_ino) {
72843ff37bSKrister Johansen 		nsi->need_setns = true;
73843ff37bSKrister Johansen 		nsi->mntns_path = newns;
74843ff37bSKrister Johansen 		newns = NULL;
75843ff37bSKrister Johansen 	}
76843ff37bSKrister Johansen 
77bf2e710bSKrister Johansen 	/* If we're dealing with a process that is in a different PID namespace,
78bf2e710bSKrister Johansen 	 * attempt to work out the innermost tgid for the process.
79bf2e710bSKrister Johansen 	 */
80bf2e710bSKrister Johansen 	if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
81bf2e710bSKrister Johansen 		goto out;
82bf2e710bSKrister Johansen 
83bf2e710bSKrister Johansen 	f = fopen(spath, "r");
84bf2e710bSKrister Johansen 	if (f == NULL)
85bf2e710bSKrister Johansen 		goto out;
86bf2e710bSKrister Johansen 
87bf2e710bSKrister Johansen 	while (getline(&statln, &linesz, f) != -1) {
88bf2e710bSKrister Johansen 		/* Use tgid if CONFIG_PID_NS is not defined. */
89bf2e710bSKrister Johansen 		if (strstr(statln, "Tgid:") != NULL) {
90bf2e710bSKrister Johansen 			nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
91bf2e710bSKrister Johansen 						     NULL, 10);
92bf2e710bSKrister Johansen 			nsi->nstgid = nsi->tgid;
93bf2e710bSKrister Johansen 		}
94bf2e710bSKrister Johansen 
95bf2e710bSKrister Johansen 		if (strstr(statln, "NStgid:") != NULL) {
96bf2e710bSKrister Johansen 			nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'),
97bf2e710bSKrister Johansen 						     NULL, 10);
98bf2e710bSKrister Johansen 			break;
99bf2e710bSKrister Johansen 		}
100bf2e710bSKrister Johansen 	}
101bf2e710bSKrister Johansen 	rv = 0;
102bf2e710bSKrister Johansen 
103843ff37bSKrister Johansen out:
104bf2e710bSKrister Johansen 	if (f != NULL)
105bf2e710bSKrister Johansen 		(void) fclose(f);
106bf2e710bSKrister Johansen 	free(statln);
107843ff37bSKrister Johansen 	free(newns);
108bf2e710bSKrister Johansen 	return rv;
109843ff37bSKrister Johansen }
110843ff37bSKrister Johansen 
111843ff37bSKrister Johansen struct nsinfo *nsinfo__new(pid_t pid)
112843ff37bSKrister Johansen {
113bf2e710bSKrister Johansen 	struct nsinfo *nsi;
114843ff37bSKrister Johansen 
115bf2e710bSKrister Johansen 	if (pid == 0)
116bf2e710bSKrister Johansen 		return NULL;
117bf2e710bSKrister Johansen 
118bf2e710bSKrister Johansen 	nsi = calloc(1, sizeof(*nsi));
119843ff37bSKrister Johansen 	if (nsi != NULL) {
120843ff37bSKrister Johansen 		nsi->pid = pid;
121bf2e710bSKrister Johansen 		nsi->tgid = pid;
122bf2e710bSKrister Johansen 		nsi->nstgid = pid;
123843ff37bSKrister Johansen 		nsi->need_setns = false;
124bf2e710bSKrister Johansen 		/* Init may fail if the process exits while we're trying to look
125bf2e710bSKrister Johansen 		 * at its proc information.  In that case, save the pid but
126bf2e710bSKrister Johansen 		 * don't try to enter the namespace.
127bf2e710bSKrister Johansen 		 */
128bf2e710bSKrister Johansen 		if (nsinfo__init(nsi) == -1)
129bf2e710bSKrister Johansen 			nsi->need_setns = false;
130bf2e710bSKrister Johansen 
131843ff37bSKrister Johansen 		refcount_set(&nsi->refcnt, 1);
132843ff37bSKrister Johansen 	}
133843ff37bSKrister Johansen 
134843ff37bSKrister Johansen 	return nsi;
135843ff37bSKrister Johansen }
136843ff37bSKrister Johansen 
137bf2e710bSKrister Johansen struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
138bf2e710bSKrister Johansen {
139bf2e710bSKrister Johansen 	struct nsinfo *nnsi;
140bf2e710bSKrister Johansen 
1413f4417d6SBenno Evers 	if (nsi == NULL)
1423f4417d6SBenno Evers 		return NULL;
1433f4417d6SBenno Evers 
144bf2e710bSKrister Johansen 	nnsi = calloc(1, sizeof(*nnsi));
145bf2e710bSKrister Johansen 	if (nnsi != NULL) {
146bf2e710bSKrister Johansen 		nnsi->pid = nsi->pid;
147bf2e710bSKrister Johansen 		nnsi->tgid = nsi->tgid;
148bf2e710bSKrister Johansen 		nnsi->nstgid = nsi->nstgid;
149bf2e710bSKrister Johansen 		nnsi->need_setns = nsi->need_setns;
150bf2e710bSKrister Johansen 		if (nsi->mntns_path) {
151bf2e710bSKrister Johansen 			nnsi->mntns_path = strdup(nsi->mntns_path);
152bf2e710bSKrister Johansen 			if (!nnsi->mntns_path) {
153bf2e710bSKrister Johansen 				free(nnsi);
154bf2e710bSKrister Johansen 				return NULL;
155bf2e710bSKrister Johansen 			}
156bf2e710bSKrister Johansen 		}
157bf2e710bSKrister Johansen 		refcount_set(&nnsi->refcnt, 1);
158bf2e710bSKrister Johansen 	}
159bf2e710bSKrister Johansen 
160bf2e710bSKrister Johansen 	return nnsi;
161bf2e710bSKrister Johansen }
162bf2e710bSKrister Johansen 
163843ff37bSKrister Johansen void nsinfo__delete(struct nsinfo *nsi)
164843ff37bSKrister Johansen {
165843ff37bSKrister Johansen 	zfree(&nsi->mntns_path);
166843ff37bSKrister Johansen 	free(nsi);
167843ff37bSKrister Johansen }
168843ff37bSKrister Johansen 
169843ff37bSKrister Johansen struct nsinfo *nsinfo__get(struct nsinfo *nsi)
170843ff37bSKrister Johansen {
171843ff37bSKrister Johansen 	if (nsi)
172843ff37bSKrister Johansen 		refcount_inc(&nsi->refcnt);
173843ff37bSKrister Johansen 	return nsi;
174843ff37bSKrister Johansen }
175843ff37bSKrister Johansen 
176843ff37bSKrister Johansen void nsinfo__put(struct nsinfo *nsi)
177843ff37bSKrister Johansen {
178843ff37bSKrister Johansen 	if (nsi && refcount_dec_and_test(&nsi->refcnt))
179843ff37bSKrister Johansen 		nsinfo__delete(nsi);
180843ff37bSKrister Johansen }
181843ff37bSKrister Johansen 
182bf2e710bSKrister Johansen void nsinfo__mountns_enter(struct nsinfo *nsi,
183bf2e710bSKrister Johansen 				  struct nscookie *nc)
184843ff37bSKrister Johansen {
185843ff37bSKrister Johansen 	char curpath[PATH_MAX];
186843ff37bSKrister Johansen 	int oldns = -1;
187843ff37bSKrister Johansen 	int newns = -1;
188b01c1f69SJiri Olsa 	char *oldcwd = NULL;
189843ff37bSKrister Johansen 
190843ff37bSKrister Johansen 	if (nc == NULL)
191843ff37bSKrister Johansen 		return;
192843ff37bSKrister Johansen 
193843ff37bSKrister Johansen 	nc->oldns = -1;
194843ff37bSKrister Johansen 	nc->newns = -1;
195843ff37bSKrister Johansen 
196843ff37bSKrister Johansen 	if (!nsi || !nsi->need_setns)
197843ff37bSKrister Johansen 		return;
198843ff37bSKrister Johansen 
199843ff37bSKrister Johansen 	if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
200843ff37bSKrister Johansen 		return;
201843ff37bSKrister Johansen 
202b01c1f69SJiri Olsa 	oldcwd = get_current_dir_name();
203b01c1f69SJiri Olsa 	if (!oldcwd)
204b01c1f69SJiri Olsa 		return;
205b01c1f69SJiri Olsa 
206843ff37bSKrister Johansen 	oldns = open(curpath, O_RDONLY);
207843ff37bSKrister Johansen 	if (oldns < 0)
208b01c1f69SJiri Olsa 		goto errout;
209843ff37bSKrister Johansen 
210843ff37bSKrister Johansen 	newns = open(nsi->mntns_path, O_RDONLY);
211843ff37bSKrister Johansen 	if (newns < 0)
212843ff37bSKrister Johansen 		goto errout;
213843ff37bSKrister Johansen 
214843ff37bSKrister Johansen 	if (setns(newns, CLONE_NEWNS) < 0)
215843ff37bSKrister Johansen 		goto errout;
216843ff37bSKrister Johansen 
217b01c1f69SJiri Olsa 	nc->oldcwd = oldcwd;
218843ff37bSKrister Johansen 	nc->oldns = oldns;
219843ff37bSKrister Johansen 	nc->newns = newns;
220843ff37bSKrister Johansen 	return;
221843ff37bSKrister Johansen 
222843ff37bSKrister Johansen errout:
223b01c1f69SJiri Olsa 	free(oldcwd);
224843ff37bSKrister Johansen 	if (oldns > -1)
225843ff37bSKrister Johansen 		close(oldns);
226843ff37bSKrister Johansen 	if (newns > -1)
227843ff37bSKrister Johansen 		close(newns);
228843ff37bSKrister Johansen }
229843ff37bSKrister Johansen 
230843ff37bSKrister Johansen void nsinfo__mountns_exit(struct nscookie *nc)
231843ff37bSKrister Johansen {
232b01c1f69SJiri Olsa 	if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
233843ff37bSKrister Johansen 		return;
234843ff37bSKrister Johansen 
235843ff37bSKrister Johansen 	setns(nc->oldns, CLONE_NEWNS);
236843ff37bSKrister Johansen 
237b01c1f69SJiri Olsa 	if (nc->oldcwd) {
238b01c1f69SJiri Olsa 		WARN_ON_ONCE(chdir(nc->oldcwd));
239b01c1f69SJiri Olsa 		zfree(&nc->oldcwd);
240b01c1f69SJiri Olsa 	}
241b01c1f69SJiri Olsa 
242843ff37bSKrister Johansen 	if (nc->oldns > -1) {
243843ff37bSKrister Johansen 		close(nc->oldns);
244843ff37bSKrister Johansen 		nc->oldns = -1;
245843ff37bSKrister Johansen 	}
246843ff37bSKrister Johansen 
247843ff37bSKrister Johansen 	if (nc->newns > -1) {
248843ff37bSKrister Johansen 		close(nc->newns);
249843ff37bSKrister Johansen 		nc->newns = -1;
250843ff37bSKrister Johansen 	}
251843ff37bSKrister Johansen }
252544abd44SKrister Johansen 
253544abd44SKrister Johansen char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
254544abd44SKrister Johansen {
255544abd44SKrister Johansen 	char *rpath;
256544abd44SKrister Johansen 	struct nscookie nsc;
257544abd44SKrister Johansen 
258544abd44SKrister Johansen 	nsinfo__mountns_enter(nsi, &nsc);
259544abd44SKrister Johansen 	rpath = realpath(path, NULL);
260544abd44SKrister Johansen 	nsinfo__mountns_exit(&nsc);
261544abd44SKrister Johansen 
262544abd44SKrister Johansen 	return rpath;
263544abd44SKrister Johansen }
264