blob: dd536220cdb9e8526b1f713d0e1d06701873f40c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Hari Bathinif3b36142017-03-08 02:11:43 +05302/*
Hari Bathinif3b36142017-03-08 02:11:43 +05303 *
4 * Copyright (C) 2017 Hari Bathini, IBM Corporation
5 */
6
7#include "namespaces.h"
Hari Bathinif3b36142017-03-08 02:11:43 +05308#include "event.h"
Arnaldo Carvalho de Meloe5653eb2019-07-05 14:16:15 -03009#include "get_current_dir_name.h"
Krister Johansen843ff372017-07-05 18:48:08 -070010#include <sys/types.h>
11#include <sys/stat.h>
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -030012#include <fcntl.h>
Krister Johansen544abd42017-07-05 18:48:10 -070013#include <limits.h>
Krister Johansen843ff372017-07-05 18:48:08 -070014#include <sched.h>
Hari Bathinif3b36142017-03-08 02:11:43 +053015#include <stdlib.h>
16#include <stdio.h>
Arnaldo Carvalho de Melo72f7c4d2017-04-19 19:06:30 -030017#include <string.h>
Krister Johansen843ff372017-07-05 18:48:08 -070018#include <unistd.h>
Jiri Olsab01c1f692018-11-01 18:00:01 +010019#include <asm/bug.h>
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -030020#include <linux/kernel.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030021#include <linux/zalloc.h>
Hari Bathinif3b36142017-03-08 02:11:43 +053022
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -030023static 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
33const 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
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -030040struct namespaces *namespaces__new(struct perf_record_namespaces *event)
Hari Bathinif3b36142017-03-08 02:11:43 +053041{
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
58void namespaces__free(struct namespaces *namespaces)
59{
60 free(namespaces);
61}
Krister Johansen843ff372017-07-05 18:48:08 -070062
Leo Yan5d28a172021-12-12 21:47:20 +080063static 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);
Ian Rogersbcaf0a92022-02-11 02:34:06 -080079 nsi->nstgid = nsinfo__tgid(nsi);
Leo Yan5d28a172021-12-12 21:47:20 +080080 }
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
Krister Johansenbf2e7102017-07-05 18:48:09 -070099int nsinfo__init(struct nsinfo *nsi)
Krister Johansen843ff372017-07-05 18:48:08 -0700100{
101 char oldns[PATH_MAX];
Krister Johansenbf2e7102017-07-05 18:48:09 -0700102 char spath[PATH_MAX];
Krister Johansen843ff372017-07-05 18:48:08 -0700103 char *newns = NULL;
104 struct stat old_stat;
105 struct stat new_stat;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700106 int rv = -1;
Krister Johansen843ff372017-07-05 18:48:08 -0700107
108 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
Krister Johansenbf2e7102017-07-05 18:48:09 -0700109 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -0700110
Ian Rogersbcaf0a92022-02-11 02:34:06 -0800111 if (asprintf(&newns, "/proc/%d/ns/mnt", nsinfo__pid(nsi)) == -1)
Krister Johansenbf2e7102017-07-05 18:48:09 -0700112 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -0700113
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
Krister Johansenbf2e7102017-07-05 18:48:09 -0700129 /* 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 */
Ian Rogersbcaf0a92022-02-11 02:34:06 -0800132 if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsinfo__pid(nsi)) >= PATH_MAX)
Krister Johansenbf2e7102017-07-05 18:48:09 -0700133 goto out;
134
Leo Yan5d28a172021-12-12 21:47:20 +0800135 rv = nsinfo__get_nspid(nsi, spath);
Krister Johansenbf2e7102017-07-05 18:48:09 -0700136
Krister Johansen843ff372017-07-05 18:48:08 -0700137out:
138 free(newns);
Krister Johansenbf2e7102017-07-05 18:48:09 -0700139 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -0700140}
141
142struct nsinfo *nsinfo__new(pid_t pid)
143{
Krister Johansenbf2e7102017-07-05 18:48:09 -0700144 struct nsinfo *nsi;
Krister Johansen843ff372017-07-05 18:48:08 -0700145
Krister Johansenbf2e7102017-07-05 18:48:09 -0700146 if (pid == 0)
147 return NULL;
148
149 nsi = calloc(1, sizeof(*nsi));
Krister Johansen843ff372017-07-05 18:48:08 -0700150 if (nsi != NULL) {
151 nsi->pid = pid;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700152 nsi->tgid = pid;
153 nsi->nstgid = pid;
Krister Johansen843ff372017-07-05 18:48:08 -0700154 nsi->need_setns = false;
Yonatan Goldschmidt2b51c712020-11-05 03:54:18 +0200155 nsi->in_pidns = false;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700156 /* 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
Krister Johansen843ff372017-07-05 18:48:08 -0700163 refcount_set(&nsi->refcnt, 1);
164 }
165
166 return nsi;
167}
168
Ian Rogersbcaf0a92022-02-11 02:34:06 -0800169struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
Krister Johansenbf2e7102017-07-05 18:48:09 -0700170{
171 struct nsinfo *nnsi;
172
Benno Evers3f4417d2018-08-10 15:36:13 +0200173 if (nsi == NULL)
174 return NULL;
175
Krister Johansenbf2e7102017-07-05 18:48:09 -0700176 nnsi = calloc(1, sizeof(*nnsi));
177 if (nnsi != NULL) {
Ian Rogersbcaf0a92022-02-11 02:34:06 -0800178 nnsi->pid = nsinfo__pid(nsi);
179 nnsi->tgid = nsinfo__tgid(nsi);
180 nnsi->nstgid = nsinfo__nstgid(nsi);
181 nnsi->need_setns = nsinfo__need_setns(nsi);
182 nnsi->in_pidns = nsinfo__in_pidns(nsi);
Krister Johansenbf2e7102017-07-05 18:48:09 -0700183 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
Ian Rogersbcaf0a92022-02-11 02:34:06 -0800196static void nsinfo__delete(struct nsinfo *nsi)
Krister Johansen843ff372017-07-05 18:48:08 -0700197{
198 zfree(&nsi->mntns_path);
199 free(nsi);
200}
201
202struct nsinfo *nsinfo__get(struct nsinfo *nsi)
203{
204 if (nsi)
205 refcount_inc(&nsi->refcnt);
206 return nsi;
207}
208
209void nsinfo__put(struct nsinfo *nsi)
210{
211 if (nsi && refcount_dec_and_test(&nsi->refcnt))
212 nsinfo__delete(nsi);
213}
214
Ian Rogersbcaf0a92022-02-11 02:34:06 -0800215bool nsinfo__need_setns(const struct nsinfo *nsi)
216{
217 return nsi->need_setns;
218}
219
220void nsinfo__clear_need_setns(struct nsinfo *nsi)
221{
222 nsi->need_setns = false;
223}
224
225pid_t nsinfo__tgid(const struct nsinfo *nsi)
226{
227 return nsi->tgid;
228}
229
230pid_t nsinfo__nstgid(const struct nsinfo *nsi)
231{
232 return nsi->nstgid;
233}
234
235pid_t nsinfo__pid(const struct nsinfo *nsi)
236{
237 return nsi->pid;
238}
239
240pid_t nsinfo__in_pidns(const struct nsinfo *nsi)
241{
242 return nsi->in_pidns;
243}
244
Krister Johansenbf2e7102017-07-05 18:48:09 -0700245void nsinfo__mountns_enter(struct nsinfo *nsi,
246 struct nscookie *nc)
Krister Johansen843ff372017-07-05 18:48:08 -0700247{
248 char curpath[PATH_MAX];
249 int oldns = -1;
250 int newns = -1;
Jiri Olsab01c1f692018-11-01 18:00:01 +0100251 char *oldcwd = NULL;
Krister Johansen843ff372017-07-05 18:48:08 -0700252
253 if (nc == NULL)
254 return;
255
256 nc->oldns = -1;
257 nc->newns = -1;
258
259 if (!nsi || !nsi->need_setns)
260 return;
261
262 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
263 return;
264
Jiri Olsab01c1f692018-11-01 18:00:01 +0100265 oldcwd = get_current_dir_name();
266 if (!oldcwd)
267 return;
268
Krister Johansen843ff372017-07-05 18:48:08 -0700269 oldns = open(curpath, O_RDONLY);
270 if (oldns < 0)
Jiri Olsab01c1f692018-11-01 18:00:01 +0100271 goto errout;
Krister Johansen843ff372017-07-05 18:48:08 -0700272
273 newns = open(nsi->mntns_path, O_RDONLY);
274 if (newns < 0)
275 goto errout;
276
277 if (setns(newns, CLONE_NEWNS) < 0)
278 goto errout;
279
Jiri Olsab01c1f692018-11-01 18:00:01 +0100280 nc->oldcwd = oldcwd;
Krister Johansen843ff372017-07-05 18:48:08 -0700281 nc->oldns = oldns;
282 nc->newns = newns;
283 return;
284
285errout:
Jiri Olsab01c1f692018-11-01 18:00:01 +0100286 free(oldcwd);
Krister Johansen843ff372017-07-05 18:48:08 -0700287 if (oldns > -1)
288 close(oldns);
289 if (newns > -1)
290 close(newns);
291}
292
293void nsinfo__mountns_exit(struct nscookie *nc)
294{
Jiri Olsab01c1f692018-11-01 18:00:01 +0100295 if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
Krister Johansen843ff372017-07-05 18:48:08 -0700296 return;
297
298 setns(nc->oldns, CLONE_NEWNS);
299
Jiri Olsab01c1f692018-11-01 18:00:01 +0100300 if (nc->oldcwd) {
301 WARN_ON_ONCE(chdir(nc->oldcwd));
302 zfree(&nc->oldcwd);
303 }
304
Krister Johansen843ff372017-07-05 18:48:08 -0700305 if (nc->oldns > -1) {
306 close(nc->oldns);
307 nc->oldns = -1;
308 }
309
310 if (nc->newns > -1) {
311 close(nc->newns);
312 nc->newns = -1;
313 }
314}
Krister Johansen544abd42017-07-05 18:48:10 -0700315
316char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
317{
318 char *rpath;
319 struct nscookie nsc;
320
321 nsinfo__mountns_enter(nsi, &nsc);
322 rpath = realpath(path, NULL);
323 nsinfo__mountns_exit(&nsc);
324
325 return rpath;
326}
Yonatan Goldschmidt67dec922020-11-05 03:56:04 +0200327
328int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi)
329{
330 int ret;
331 struct nscookie nsc;
332
333 nsinfo__mountns_enter(nsi, &nsc);
334 ret = stat(filename, st);
335 nsinfo__mountns_exit(&nsc);
336
337 return ret;
338}
Leo Yan5d28a172021-12-12 21:47:20 +0800339
340bool nsinfo__is_in_root_namespace(void)
341{
342 struct nsinfo nsi;
343
344 memset(&nsi, 0x0, sizeof(nsi));
345 nsinfo__get_nspid(&nsi, "/proc/self/status");
346 return !nsi.in_pidns;
347}