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