xref: /openbmc/linux/tools/perf/util/namespaces.c (revision 593692d2)
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(struct nsinfo *nsi, 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 			nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
78 						     NULL, 10);
79 			nsi->nstgid = nsi->tgid;
80 		}
81 
82 		if (strstr(statln, "NStgid:") != NULL) {
83 			nspid = strrchr(statln, '\t');
84 			nsi->nstgid = (pid_t)strtol(nspid, NULL, 10);
85 			/*
86 			 * If innermost tgid is not the first, process is in a different
87 			 * PID namespace.
88 			 */
89 			nsi->in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
90 			break;
91 		}
92 	}
93 
94 	fclose(f);
95 	free(statln);
96 	return 0;
97 }
98 
99 int nsinfo__init(struct nsinfo *nsi)
100 {
101 	char oldns[PATH_MAX];
102 	char spath[PATH_MAX];
103 	char *newns = NULL;
104 	struct stat old_stat;
105 	struct stat new_stat;
106 	int rv = -1;
107 
108 	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
109 		return rv;
110 
111 	if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
112 		return rv;
113 
114 	if (stat(oldns, &old_stat) < 0)
115 		goto out;
116 
117 	if (stat(newns, &new_stat) < 0)
118 		goto out;
119 
120 	/* Check if the mount namespaces differ, if so then indicate that we
121 	 * want to switch as part of looking up dso/map data.
122 	 */
123 	if (old_stat.st_ino != new_stat.st_ino) {
124 		nsi->need_setns = true;
125 		nsi->mntns_path = newns;
126 		newns = NULL;
127 	}
128 
129 	/* If we're dealing with a process that is in a different PID namespace,
130 	 * attempt to work out the innermost tgid for the process.
131 	 */
132 	if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
133 		goto out;
134 
135 	rv = nsinfo__get_nspid(nsi, spath);
136 
137 out:
138 	free(newns);
139 	return rv;
140 }
141 
142 struct nsinfo *nsinfo__new(pid_t pid)
143 {
144 	struct nsinfo *nsi;
145 
146 	if (pid == 0)
147 		return NULL;
148 
149 	nsi = calloc(1, sizeof(*nsi));
150 	if (nsi != NULL) {
151 		nsi->pid = pid;
152 		nsi->tgid = pid;
153 		nsi->nstgid = pid;
154 		nsi->need_setns = false;
155 		nsi->in_pidns = false;
156 		/* Init may fail if the process exits while we're trying to look
157 		 * at its proc information.  In that case, save the pid but
158 		 * don't try to enter the namespace.
159 		 */
160 		if (nsinfo__init(nsi) == -1)
161 			nsi->need_setns = false;
162 
163 		refcount_set(&nsi->refcnt, 1);
164 	}
165 
166 	return nsi;
167 }
168 
169 struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
170 {
171 	struct nsinfo *nnsi;
172 
173 	if (nsi == NULL)
174 		return NULL;
175 
176 	nnsi = calloc(1, sizeof(*nnsi));
177 	if (nnsi != NULL) {
178 		nnsi->pid = nsi->pid;
179 		nnsi->tgid = nsi->tgid;
180 		nnsi->nstgid = nsi->nstgid;
181 		nnsi->need_setns = nsi->need_setns;
182 		nnsi->in_pidns = nsi->in_pidns;
183 		if (nsi->mntns_path) {
184 			nnsi->mntns_path = strdup(nsi->mntns_path);
185 			if (!nnsi->mntns_path) {
186 				free(nnsi);
187 				return NULL;
188 			}
189 		}
190 		refcount_set(&nnsi->refcnt, 1);
191 	}
192 
193 	return nnsi;
194 }
195 
196 void nsinfo__delete(struct nsinfo *nsi)
197 {
198 	zfree(&nsi->mntns_path);
199 	free(nsi);
200 }
201 
202 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
203 {
204 	if (nsi)
205 		refcount_inc(&nsi->refcnt);
206 	return nsi;
207 }
208 
209 void nsinfo__put(struct nsinfo *nsi)
210 {
211 	if (nsi && refcount_dec_and_test(&nsi->refcnt))
212 		nsinfo__delete(nsi);
213 }
214 
215 void nsinfo__mountns_enter(struct nsinfo *nsi,
216 				  struct nscookie *nc)
217 {
218 	char curpath[PATH_MAX];
219 	int oldns = -1;
220 	int newns = -1;
221 	char *oldcwd = NULL;
222 
223 	if (nc == NULL)
224 		return;
225 
226 	nc->oldns = -1;
227 	nc->newns = -1;
228 
229 	if (!nsi || !nsi->need_setns)
230 		return;
231 
232 	if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
233 		return;
234 
235 	oldcwd = get_current_dir_name();
236 	if (!oldcwd)
237 		return;
238 
239 	oldns = open(curpath, O_RDONLY);
240 	if (oldns < 0)
241 		goto errout;
242 
243 	newns = open(nsi->mntns_path, O_RDONLY);
244 	if (newns < 0)
245 		goto errout;
246 
247 	if (setns(newns, CLONE_NEWNS) < 0)
248 		goto errout;
249 
250 	nc->oldcwd = oldcwd;
251 	nc->oldns = oldns;
252 	nc->newns = newns;
253 	return;
254 
255 errout:
256 	free(oldcwd);
257 	if (oldns > -1)
258 		close(oldns);
259 	if (newns > -1)
260 		close(newns);
261 }
262 
263 void nsinfo__mountns_exit(struct nscookie *nc)
264 {
265 	if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
266 		return;
267 
268 	setns(nc->oldns, CLONE_NEWNS);
269 
270 	if (nc->oldcwd) {
271 		WARN_ON_ONCE(chdir(nc->oldcwd));
272 		zfree(&nc->oldcwd);
273 	}
274 
275 	if (nc->oldns > -1) {
276 		close(nc->oldns);
277 		nc->oldns = -1;
278 	}
279 
280 	if (nc->newns > -1) {
281 		close(nc->newns);
282 		nc->newns = -1;
283 	}
284 }
285 
286 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
287 {
288 	char *rpath;
289 	struct nscookie nsc;
290 
291 	nsinfo__mountns_enter(nsi, &nsc);
292 	rpath = realpath(path, NULL);
293 	nsinfo__mountns_exit(&nsc);
294 
295 	return rpath;
296 }
297 
298 int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi)
299 {
300 	int ret;
301 	struct nscookie nsc;
302 
303 	nsinfo__mountns_enter(nsi, &nsc);
304 	ret = stat(filename, st);
305 	nsinfo__mountns_exit(&nsc);
306 
307 	return ret;
308 }
309 
310 bool nsinfo__is_in_root_namespace(void)
311 {
312 	struct nsinfo nsi;
313 
314 	memset(&nsi, 0x0, sizeof(nsi));
315 	nsinfo__get_nspid(&nsi, "/proc/self/status");
316 	return !nsi.in_pidns;
317 }
318