blob: 023c4efd788dc9c8eea75b8de3567fede672fa29 [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"
8#include "util.h"
9#include "event.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>
Hari Bathinif3b36142017-03-08 02:11:43 +053020
21struct namespaces *namespaces__new(struct namespaces_event *event)
22{
23 struct namespaces *namespaces;
24 u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
25 sizeof(struct perf_ns_link_info));
26
27 namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
28 if (!namespaces)
29 return NULL;
30
31 namespaces->end_time = -1;
32
33 if (event)
34 memcpy(namespaces->link_info, event->link_info, link_info_size);
35
36 return namespaces;
37}
38
39void namespaces__free(struct namespaces *namespaces)
40{
41 free(namespaces);
42}
Krister Johansen843ff372017-07-05 18:48:08 -070043
Krister Johansenbf2e7102017-07-05 18:48:09 -070044int nsinfo__init(struct nsinfo *nsi)
Krister Johansen843ff372017-07-05 18:48:08 -070045{
46 char oldns[PATH_MAX];
Krister Johansenbf2e7102017-07-05 18:48:09 -070047 char spath[PATH_MAX];
Krister Johansen843ff372017-07-05 18:48:08 -070048 char *newns = NULL;
Krister Johansenbf2e7102017-07-05 18:48:09 -070049 char *statln = NULL;
Krister Johansen843ff372017-07-05 18:48:08 -070050 struct stat old_stat;
51 struct stat new_stat;
Krister Johansenbf2e7102017-07-05 18:48:09 -070052 FILE *f = NULL;
53 size_t linesz = 0;
54 int rv = -1;
Krister Johansen843ff372017-07-05 18:48:08 -070055
56 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
Krister Johansenbf2e7102017-07-05 18:48:09 -070057 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -070058
59 if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
Krister Johansenbf2e7102017-07-05 18:48:09 -070060 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -070061
62 if (stat(oldns, &old_stat) < 0)
63 goto out;
64
65 if (stat(newns, &new_stat) < 0)
66 goto out;
67
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.
70 */
71 if (old_stat.st_ino != new_stat.st_ino) {
72 nsi->need_setns = true;
73 nsi->mntns_path = newns;
74 newns = NULL;
75 }
76
Krister Johansenbf2e7102017-07-05 18:48:09 -070077 /* 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.
79 */
80 if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
81 goto out;
82
83 f = fopen(spath, "r");
84 if (f == NULL)
85 goto out;
86
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'),
91 NULL, 10);
92 nsi->nstgid = nsi->tgid;
93 }
94
95 if (strstr(statln, "NStgid:") != NULL) {
96 nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'),
97 NULL, 10);
98 break;
99 }
100 }
101 rv = 0;
102
Krister Johansen843ff372017-07-05 18:48:08 -0700103out:
Krister Johansenbf2e7102017-07-05 18:48:09 -0700104 if (f != NULL)
105 (void) fclose(f);
106 free(statln);
Krister Johansen843ff372017-07-05 18:48:08 -0700107 free(newns);
Krister Johansenbf2e7102017-07-05 18:48:09 -0700108 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -0700109}
110
111struct nsinfo *nsinfo__new(pid_t pid)
112{
Krister Johansenbf2e7102017-07-05 18:48:09 -0700113 struct nsinfo *nsi;
Krister Johansen843ff372017-07-05 18:48:08 -0700114
Krister Johansenbf2e7102017-07-05 18:48:09 -0700115 if (pid == 0)
116 return NULL;
117
118 nsi = calloc(1, sizeof(*nsi));
Krister Johansen843ff372017-07-05 18:48:08 -0700119 if (nsi != NULL) {
120 nsi->pid = pid;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700121 nsi->tgid = pid;
122 nsi->nstgid = pid;
Krister Johansen843ff372017-07-05 18:48:08 -0700123 nsi->need_setns = false;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700124 /* 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.
127 */
128 if (nsinfo__init(nsi) == -1)
129 nsi->need_setns = false;
130
Krister Johansen843ff372017-07-05 18:48:08 -0700131 refcount_set(&nsi->refcnt, 1);
132 }
133
134 return nsi;
135}
136
Krister Johansenbf2e7102017-07-05 18:48:09 -0700137struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
138{
139 struct nsinfo *nnsi;
140
Benno Evers3f4417d2018-08-10 15:36:13 +0200141 if (nsi == NULL)
142 return NULL;
143
Krister Johansenbf2e7102017-07-05 18:48:09 -0700144 nnsi = calloc(1, sizeof(*nnsi));
145 if (nnsi != NULL) {
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) {
153 free(nnsi);
154 return NULL;
155 }
156 }
157 refcount_set(&nnsi->refcnt, 1);
158 }
159
160 return nnsi;
161}
162
Krister Johansen843ff372017-07-05 18:48:08 -0700163void nsinfo__delete(struct nsinfo *nsi)
164{
165 zfree(&nsi->mntns_path);
166 free(nsi);
167}
168
169struct nsinfo *nsinfo__get(struct nsinfo *nsi)
170{
171 if (nsi)
172 refcount_inc(&nsi->refcnt);
173 return nsi;
174}
175
176void nsinfo__put(struct nsinfo *nsi)
177{
178 if (nsi && refcount_dec_and_test(&nsi->refcnt))
179 nsinfo__delete(nsi);
180}
181
Krister Johansenbf2e7102017-07-05 18:48:09 -0700182void nsinfo__mountns_enter(struct nsinfo *nsi,
183 struct nscookie *nc)
Krister Johansen843ff372017-07-05 18:48:08 -0700184{
185 char curpath[PATH_MAX];
186 int oldns = -1;
187 int newns = -1;
Jiri Olsab01c1f692018-11-01 18:00:01 +0100188 char *oldcwd = NULL;
Krister Johansen843ff372017-07-05 18:48:08 -0700189
190 if (nc == NULL)
191 return;
192
193 nc->oldns = -1;
194 nc->newns = -1;
195
196 if (!nsi || !nsi->need_setns)
197 return;
198
199 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
200 return;
201
Jiri Olsab01c1f692018-11-01 18:00:01 +0100202 oldcwd = get_current_dir_name();
203 if (!oldcwd)
204 return;
205
Krister Johansen843ff372017-07-05 18:48:08 -0700206 oldns = open(curpath, O_RDONLY);
207 if (oldns < 0)
Jiri Olsab01c1f692018-11-01 18:00:01 +0100208 goto errout;
Krister Johansen843ff372017-07-05 18:48:08 -0700209
210 newns = open(nsi->mntns_path, O_RDONLY);
211 if (newns < 0)
212 goto errout;
213
214 if (setns(newns, CLONE_NEWNS) < 0)
215 goto errout;
216
Jiri Olsab01c1f692018-11-01 18:00:01 +0100217 nc->oldcwd = oldcwd;
Krister Johansen843ff372017-07-05 18:48:08 -0700218 nc->oldns = oldns;
219 nc->newns = newns;
220 return;
221
222errout:
Jiri Olsab01c1f692018-11-01 18:00:01 +0100223 free(oldcwd);
Krister Johansen843ff372017-07-05 18:48:08 -0700224 if (oldns > -1)
225 close(oldns);
226 if (newns > -1)
227 close(newns);
228}
229
230void nsinfo__mountns_exit(struct nscookie *nc)
231{
Jiri Olsab01c1f692018-11-01 18:00:01 +0100232 if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
Krister Johansen843ff372017-07-05 18:48:08 -0700233 return;
234
235 setns(nc->oldns, CLONE_NEWNS);
236
Jiri Olsab01c1f692018-11-01 18:00:01 +0100237 if (nc->oldcwd) {
238 WARN_ON_ONCE(chdir(nc->oldcwd));
239 zfree(&nc->oldcwd);
240 }
241
Krister Johansen843ff372017-07-05 18:48:08 -0700242 if (nc->oldns > -1) {
243 close(nc->oldns);
244 nc->oldns = -1;
245 }
246
247 if (nc->newns > -1) {
248 close(nc->newns);
249 nc->newns = -1;
250 }
251}
Krister Johansen544abd42017-07-05 18:48:10 -0700252
253char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
254{
255 char *rpath;
256 struct nscookie nsc;
257
258 nsinfo__mountns_enter(nsi, &nsc);
259 rpath = realpath(path, NULL);
260 nsinfo__mountns_exit(&nsc);
261
262 return rpath;
263}