1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2017 Hari Bathini, IBM Corporation
7 #include "namespaces.h"
10 #include <sys/types.h>
21 struct namespaces *namespaces__new(struct namespaces_event *event)
23 struct namespaces *namespaces;
24 u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
25 sizeof(struct perf_ns_link_info));
27 namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
31 namespaces->end_time = -1;
34 memcpy(namespaces->link_info, event->link_info, link_info_size);
39 void namespaces__free(struct namespaces *namespaces)
44 int nsinfo__init(struct nsinfo *nsi)
56 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
59 if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
62 if (stat(oldns, &old_stat) < 0)
65 if (stat(newns, &new_stat) < 0)
68 /* Check if the mount namespaces differ, if so then indicate that we
69 * want to switch as part of looking up dso/map data.
71 if (old_stat.st_ino != new_stat.st_ino) {
72 nsi->need_setns = true;
73 nsi->mntns_path = newns;
77 /* If we're dealing with a process that is in a different PID namespace,
78 * attempt to work out the innermost tgid for the process.
80 if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
83 f = fopen(spath, "r");
87 while (getline(&statln, &linesz, f) != -1) {
88 /* Use tgid if CONFIG_PID_NS is not defined. */
89 if (strstr(statln, "Tgid:") != NULL) {
90 nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
92 nsi->nstgid = nsi->tgid;
95 if (strstr(statln, "NStgid:") != NULL) {
96 nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'),
111 struct nsinfo *nsinfo__new(pid_t pid)
118 nsi = calloc(1, sizeof(*nsi));
123 nsi->need_setns = false;
124 /* Init may fail if the process exits while we're trying to look
125 * at its proc information. In that case, save the pid but
126 * don't try to enter the namespace.
128 if (nsinfo__init(nsi) == -1)
129 nsi->need_setns = false;
131 refcount_set(&nsi->refcnt, 1);
137 struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
144 nnsi = calloc(1, sizeof(*nnsi));
146 nnsi->pid = nsi->pid;
147 nnsi->tgid = nsi->tgid;
148 nnsi->nstgid = nsi->nstgid;
149 nnsi->need_setns = nsi->need_setns;
150 if (nsi->mntns_path) {
151 nnsi->mntns_path = strdup(nsi->mntns_path);
152 if (!nnsi->mntns_path) {
157 refcount_set(&nnsi->refcnt, 1);
163 void nsinfo__delete(struct nsinfo *nsi)
165 zfree(&nsi->mntns_path);
169 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
172 refcount_inc(&nsi->refcnt);
176 void nsinfo__put(struct nsinfo *nsi)
178 if (nsi && refcount_dec_and_test(&nsi->refcnt))
182 void nsinfo__mountns_enter(struct nsinfo *nsi,
185 char curpath[PATH_MAX];
196 if (!nsi || !nsi->need_setns)
199 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
202 oldcwd = get_current_dir_name();
206 oldns = open(curpath, O_RDONLY);
210 newns = open(nsi->mntns_path, O_RDONLY);
214 if (setns(newns, CLONE_NEWNS) < 0)
230 void nsinfo__mountns_exit(struct nscookie *nc)
232 if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
235 setns(nc->oldns, CLONE_NEWNS);
238 WARN_ON_ONCE(chdir(nc->oldcwd));
242 if (nc->oldns > -1) {
247 if (nc->newns > -1) {
253 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
258 nsinfo__mountns_enter(nsi, &nsc);
259 rpath = realpath(path, NULL);
260 nsinfo__mountns_exit(&nsc);