xref: /openbmc/linux/tools/perf/util/namespaces.c (revision f94c21dfd02e98aa0fcb9b453a1198e76ede60e7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2017 Hari Bathini, IBM Corporation
5  */
6 
7 #include "namespaces.h"
8 #include "event.h"
9 #include "get_current_dir_name.h"
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <limits.h>
14 #include <sched.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <asm/bug.h>
20 #include <linux/kernel.h>
21 #include <linux/zalloc.h>
22 
23 static const char *perf_ns__names[] = {
24 	[NET_NS_INDEX]		= "net",
25 	[UTS_NS_INDEX]		= "uts",
26 	[IPC_NS_INDEX]		= "ipc",
27 	[PID_NS_INDEX]		= "pid",
28 	[USER_NS_INDEX]		= "user",
29 	[MNT_NS_INDEX]		= "mnt",
30 	[CGROUP_NS_INDEX]	= "cgroup",
31 };
32 
33 const char *perf_ns__name(unsigned int id)
34 {
35 	if (id >= ARRAY_SIZE(perf_ns__names))
36 		return "UNKNOWN";
37 	return perf_ns__names[id];
38 }
39 
40 struct namespaces *namespaces__new(struct perf_record_namespaces *event)
41 {
42 	struct namespaces *namespaces;
43 	u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
44 			      sizeof(struct perf_ns_link_info));
45 
46 	namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
47 	if (!namespaces)
48 		return NULL;
49 
50 	namespaces->end_time = -1;
51 
52 	if (event)
53 		memcpy(namespaces->link_info, event->link_info, link_info_size);
54 
55 	return namespaces;
56 }
57 
58 void namespaces__free(struct namespaces *namespaces)
59 {
60 	free(namespaces);
61 }
62 
63 static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const char *path)
64 {
65 	FILE *f = NULL;
66 	char *statln = NULL;
67 	size_t linesz = 0;
68 	char *nspid;
69 
70 	f = fopen(path, "r");
71 	if (f == NULL)
72 		return -1;
73 
74 	while (getline(&statln, &linesz, f) != -1) {
75 		/* Use tgid if CONFIG_PID_NS is not defined. */
76 		if (strstr(statln, "Tgid:") != NULL) {
77 			*tgid = (pid_t)strtol(strrchr(statln, '\t'), NULL, 10);
78 			*nstgid = *tgid;
79 		}
80 
81 		if (strstr(statln, "NStgid:") != NULL) {
82 			nspid = strrchr(statln, '\t');
83 			*nstgid = (pid_t)strtol(nspid, NULL, 10);
84 			/*
85 			 * If innermost tgid is not the first, process is in a different
86 			 * PID namespace.
87 			 */
88 			*in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
89 			break;
90 		}
91 	}
92 
93 	fclose(f);
94 	free(statln);
95 	return 0;
96 }
97 
98 int nsinfo__init(struct nsinfo *nsi)
99 {
100 	char oldns[PATH_MAX];
101 	char spath[PATH_MAX];
102 	char *newns = NULL;
103 	struct stat old_stat;
104 	struct stat new_stat;
105 	int rv = -1;
106 
107 	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
108 		return rv;
109 
110 	if (asprintf(&newns, "/proc/%d/ns/mnt", nsinfo__pid(nsi)) == -1)
111 		return rv;
112 
113 	if (stat(oldns, &old_stat) < 0)
114 		goto out;
115 
116 	if (stat(newns, &new_stat) < 0)
117 		goto out;
118 
119 	/* Check if the mount namespaces differ, if so then indicate that we
120 	 * want to switch as part of looking up dso/map data.
121 	 */
122 	if (old_stat.st_ino != new_stat.st_ino) {
123 		RC_CHK_ACCESS(nsi)->need_setns = true;
124 		RC_CHK_ACCESS(nsi)->mntns_path = newns;
125 		newns = NULL;
126 	}
127 
128 	/* If we're dealing with a process that is in a different PID namespace,
129 	 * attempt to work out the innermost tgid for the process.
130 	 */
131 	if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsinfo__pid(nsi)) >= PATH_MAX)
132 		goto out;
133 
134 	rv = nsinfo__get_nspid(&RC_CHK_ACCESS(nsi)->tgid, &RC_CHK_ACCESS(nsi)->nstgid,
135 			       &RC_CHK_ACCESS(nsi)->in_pidns, spath);
136 
137 out:
138 	free(newns);
139 	return rv;
140 }
141 
142 static struct nsinfo *nsinfo__alloc(void)
143 {
144 	struct nsinfo *res;
145 	RC_STRUCT(nsinfo) *nsi;
146 
147 	nsi = calloc(1, sizeof(*nsi));
148 	if (ADD_RC_CHK(res, nsi))
149 		refcount_set(&nsi->refcnt, 1);
150 
151 	return res;
152 }
153 
154 struct nsinfo *nsinfo__new(pid_t pid)
155 {
156 	struct nsinfo *nsi;
157 
158 	if (pid == 0)
159 		return NULL;
160 
161 	nsi = nsinfo__alloc();
162 	if (!nsi)
163 		return NULL;
164 
165 	RC_CHK_ACCESS(nsi)->pid = pid;
166 	RC_CHK_ACCESS(nsi)->tgid = pid;
167 	RC_CHK_ACCESS(nsi)->nstgid = pid;
168 	nsinfo__clear_need_setns(nsi);
169 	RC_CHK_ACCESS(nsi)->in_pidns = false;
170 	/* Init may fail if the process exits while we're trying to look at its
171 	 * proc information. In that case, save the pid but don't try to enter
172 	 * the namespace.
173 	 */
174 	if (nsinfo__init(nsi) == -1)
175 		nsinfo__clear_need_setns(nsi);
176 
177 	return nsi;
178 }
179 
180 struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
181 {
182 	struct nsinfo *nnsi;
183 
184 	if (nsi == NULL)
185 		return NULL;
186 
187 	nnsi = nsinfo__alloc();
188 	if (!nnsi)
189 		return NULL;
190 
191 	RC_CHK_ACCESS(nnsi)->pid = nsinfo__pid(nsi);
192 	RC_CHK_ACCESS(nnsi)->tgid = nsinfo__tgid(nsi);
193 	RC_CHK_ACCESS(nnsi)->nstgid = nsinfo__nstgid(nsi);
194 	RC_CHK_ACCESS(nnsi)->need_setns = nsinfo__need_setns(nsi);
195 	RC_CHK_ACCESS(nnsi)->in_pidns = nsinfo__in_pidns(nsi);
196 	if (RC_CHK_ACCESS(nsi)->mntns_path) {
197 		RC_CHK_ACCESS(nnsi)->mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
198 		if (!RC_CHK_ACCESS(nnsi)->mntns_path) {
199 			nsinfo__put(nnsi);
200 			return NULL;
201 		}
202 	}
203 
204 	return nnsi;
205 }
206 
207 static refcount_t *nsinfo__refcnt(struct nsinfo *nsi)
208 {
209 	return &RC_CHK_ACCESS(nsi)->refcnt;
210 }
211 
212 static void nsinfo__delete(struct nsinfo *nsi)
213 {
214 	if (nsi) {
215 		WARN_ONCE(refcount_read(nsinfo__refcnt(nsi)) != 0, "nsinfo refcnt unbalanced\n");
216 		zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
217 		RC_CHK_FREE(nsi);
218 	}
219 }
220 
221 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
222 {
223 	struct nsinfo *result;
224 
225 	if (RC_CHK_GET(result, nsi))
226 		refcount_inc(nsinfo__refcnt(nsi));
227 
228 	return result;
229 }
230 
231 void nsinfo__put(struct nsinfo *nsi)
232 {
233 	if (nsi && refcount_dec_and_test(nsinfo__refcnt(nsi)))
234 		nsinfo__delete(nsi);
235 	else
236 		RC_CHK_PUT(nsi);
237 }
238 
239 bool nsinfo__need_setns(const struct nsinfo *nsi)
240 {
241 	return RC_CHK_ACCESS(nsi)->need_setns;
242 }
243 
244 void nsinfo__clear_need_setns(struct nsinfo *nsi)
245 {
246 	RC_CHK_ACCESS(nsi)->need_setns = false;
247 }
248 
249 pid_t nsinfo__tgid(const struct nsinfo  *nsi)
250 {
251 	return RC_CHK_ACCESS(nsi)->tgid;
252 }
253 
254 pid_t nsinfo__nstgid(const struct nsinfo  *nsi)
255 {
256 	return RC_CHK_ACCESS(nsi)->nstgid;
257 }
258 
259 pid_t nsinfo__pid(const struct nsinfo  *nsi)
260 {
261 	return RC_CHK_ACCESS(nsi)->pid;
262 }
263 
264 pid_t nsinfo__in_pidns(const struct nsinfo  *nsi)
265 {
266 	return RC_CHK_ACCESS(nsi)->in_pidns;
267 }
268 
269 void nsinfo__mountns_enter(struct nsinfo *nsi,
270 				  struct nscookie *nc)
271 {
272 	char curpath[PATH_MAX];
273 	int oldns = -1;
274 	int newns = -1;
275 	char *oldcwd = NULL;
276 
277 	if (nc == NULL)
278 		return;
279 
280 	nc->oldns = -1;
281 	nc->newns = -1;
282 
283 	if (!nsi || !nsinfo__need_setns(nsi))
284 		return;
285 
286 	if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
287 		return;
288 
289 	oldcwd = get_current_dir_name();
290 	if (!oldcwd)
291 		return;
292 
293 	oldns = open(curpath, O_RDONLY);
294 	if (oldns < 0)
295 		goto errout;
296 
297 	newns = open(RC_CHK_ACCESS(nsi)->mntns_path, O_RDONLY);
298 	if (newns < 0)
299 		goto errout;
300 
301 	if (setns(newns, CLONE_NEWNS) < 0)
302 		goto errout;
303 
304 	nc->oldcwd = oldcwd;
305 	nc->oldns = oldns;
306 	nc->newns = newns;
307 	return;
308 
309 errout:
310 	free(oldcwd);
311 	if (oldns > -1)
312 		close(oldns);
313 	if (newns > -1)
314 		close(newns);
315 }
316 
317 void nsinfo__mountns_exit(struct nscookie *nc)
318 {
319 	if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
320 		return;
321 
322 	setns(nc->oldns, CLONE_NEWNS);
323 
324 	if (nc->oldcwd) {
325 		WARN_ON_ONCE(chdir(nc->oldcwd));
326 		zfree(&nc->oldcwd);
327 	}
328 
329 	if (nc->oldns > -1) {
330 		close(nc->oldns);
331 		nc->oldns = -1;
332 	}
333 
334 	if (nc->newns > -1) {
335 		close(nc->newns);
336 		nc->newns = -1;
337 	}
338 }
339 
340 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
341 {
342 	char *rpath;
343 	struct nscookie nsc;
344 
345 	nsinfo__mountns_enter(nsi, &nsc);
346 	rpath = realpath(path, NULL);
347 	nsinfo__mountns_exit(&nsc);
348 
349 	return rpath;
350 }
351 
352 int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi)
353 {
354 	int ret;
355 	struct nscookie nsc;
356 
357 	nsinfo__mountns_enter(nsi, &nsc);
358 	ret = stat(filename, st);
359 	nsinfo__mountns_exit(&nsc);
360 
361 	return ret;
362 }
363 
364 bool nsinfo__is_in_root_namespace(void)
365 {
366 	pid_t tgid = 0, nstgid = 0;
367 	bool in_pidns = false;
368 
369 	nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, "/proc/self/status");
370 	return !in_pidns;
371 }
372