1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2017 Hari Bathini, IBM Corporation
7 #include "namespaces.h"
9 #include "get_current_dir_name.h"
10 #include <sys/types.h>
20 #include <linux/kernel.h>
21 #include <linux/zalloc.h>
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",
33 const char *perf_ns__name(unsigned int id)
35 if (id >= ARRAY_SIZE(perf_ns__names))
37 return perf_ns__names[id];
40 struct namespaces *namespaces__new(struct perf_record_namespaces *event)
42 struct namespaces *namespaces;
43 u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
44 sizeof(struct perf_ns_link_info));
46 namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
50 namespaces->end_time = -1;
53 memcpy(namespaces->link_info, event->link_info, link_info_size);
58 void namespaces__free(struct namespaces *namespaces)
63 static int nsinfo__get_nspid(struct nsinfo *nsi, const char *path)
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'),
79 nsi->nstgid = nsi->tgid;
82 if (strstr(statln, "NStgid:") != NULL) {
83 nspid = strrchr(statln, '\t');
84 nsi->nstgid = (pid_t)strtol(nspid, NULL, 10);
86 * If innermost tgid is not the first, process is in a different
89 nsi->in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
99 int nsinfo__init(struct nsinfo *nsi)
101 char oldns[PATH_MAX];
102 char spath[PATH_MAX];
104 struct stat old_stat;
105 struct stat new_stat;
108 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
111 if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
114 if (stat(oldns, &old_stat) < 0)
117 if (stat(newns, &new_stat) < 0)
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.
123 if (old_stat.st_ino != new_stat.st_ino) {
124 nsi->need_setns = true;
125 nsi->mntns_path = newns;
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.
132 if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
135 rv = nsinfo__get_nspid(nsi, spath);
142 struct nsinfo *nsinfo__new(pid_t pid)
149 nsi = calloc(1, sizeof(*nsi));
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.
160 if (nsinfo__init(nsi) == -1)
161 nsi->need_setns = false;
163 refcount_set(&nsi->refcnt, 1);
169 struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
176 nnsi = calloc(1, sizeof(*nnsi));
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) {
190 refcount_set(&nnsi->refcnt, 1);
196 void nsinfo__delete(struct nsinfo *nsi)
198 zfree(&nsi->mntns_path);
202 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
205 refcount_inc(&nsi->refcnt);
209 void nsinfo__put(struct nsinfo *nsi)
211 if (nsi && refcount_dec_and_test(&nsi->refcnt))
215 void nsinfo__mountns_enter(struct nsinfo *nsi,
218 char curpath[PATH_MAX];
229 if (!nsi || !nsi->need_setns)
232 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
235 oldcwd = get_current_dir_name();
239 oldns = open(curpath, O_RDONLY);
243 newns = open(nsi->mntns_path, O_RDONLY);
247 if (setns(newns, CLONE_NEWNS) < 0)
263 void nsinfo__mountns_exit(struct nscookie *nc)
265 if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
268 setns(nc->oldns, CLONE_NEWNS);
271 WARN_ON_ONCE(chdir(nc->oldcwd));
275 if (nc->oldns > -1) {
280 if (nc->newns > -1) {
286 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
291 nsinfo__mountns_enter(nsi, &nsc);
292 rpath = realpath(path, NULL);
293 nsinfo__mountns_exit(&nsc);
298 int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi)
303 nsinfo__mountns_enter(nsi, &nsc);
304 ret = stat(filename, st);
305 nsinfo__mountns_exit(&nsc);
310 bool nsinfo__is_in_root_namespace(void)
314 memset(&nsi, 0x0, sizeof(nsi));
315 nsinfo__get_nspid(&nsi, "/proc/self/status");
316 return !nsi.in_pidns;