treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[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 "util.h"
9 #include "event.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
21 struct 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
39 void namespaces__free(struct namespaces *namespaces)
40 {
41         free(namespaces);
42 }
43
44 int nsinfo__init(struct nsinfo *nsi)
45 {
46         char oldns[PATH_MAX];
47         char spath[PATH_MAX];
48         char *newns = NULL;
49         char *statln = NULL;
50         struct stat old_stat;
51         struct stat new_stat;
52         FILE *f = NULL;
53         size_t linesz = 0;
54         int rv = -1;
55
56         if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
57                 return rv;
58
59         if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
60                 return rv;
61
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
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.
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
103 out:
104         if (f != NULL)
105                 (void) fclose(f);
106         free(statln);
107         free(newns);
108         return rv;
109 }
110
111 struct nsinfo *nsinfo__new(pid_t pid)
112 {
113         struct nsinfo *nsi;
114
115         if (pid == 0)
116                 return NULL;
117
118         nsi = calloc(1, sizeof(*nsi));
119         if (nsi != NULL) {
120                 nsi->pid = pid;
121                 nsi->tgid = pid;
122                 nsi->nstgid = pid;
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.
127                  */
128                 if (nsinfo__init(nsi) == -1)
129                         nsi->need_setns = false;
130
131                 refcount_set(&nsi->refcnt, 1);
132         }
133
134         return nsi;
135 }
136
137 struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
138 {
139         struct nsinfo *nnsi;
140
141         if (nsi == NULL)
142                 return NULL;
143
144         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
163 void nsinfo__delete(struct nsinfo *nsi)
164 {
165         zfree(&nsi->mntns_path);
166         free(nsi);
167 }
168
169 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
170 {
171         if (nsi)
172                 refcount_inc(&nsi->refcnt);
173         return nsi;
174 }
175
176 void nsinfo__put(struct nsinfo *nsi)
177 {
178         if (nsi && refcount_dec_and_test(&nsi->refcnt))
179                 nsinfo__delete(nsi);
180 }
181
182 void nsinfo__mountns_enter(struct nsinfo *nsi,
183                                   struct nscookie *nc)
184 {
185         char curpath[PATH_MAX];
186         int oldns = -1;
187         int newns = -1;
188         char *oldcwd = NULL;
189
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
202         oldcwd = get_current_dir_name();
203         if (!oldcwd)
204                 return;
205
206         oldns = open(curpath, O_RDONLY);
207         if (oldns < 0)
208                 goto errout;
209
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
217         nc->oldcwd = oldcwd;
218         nc->oldns = oldns;
219         nc->newns = newns;
220         return;
221
222 errout:
223         free(oldcwd);
224         if (oldns > -1)
225                 close(oldns);
226         if (newns > -1)
227                 close(newns);
228 }
229
230 void nsinfo__mountns_exit(struct nscookie *nc)
231 {
232         if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
233                 return;
234
235         setns(nc->oldns, CLONE_NEWNS);
236
237         if (nc->oldcwd) {
238                 WARN_ON_ONCE(chdir(nc->oldcwd));
239                 zfree(&nc->oldcwd);
240         }
241
242         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 }
252
253 char *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 }