xref: /openbmc/linux/tools/perf/util/namespaces.c (revision 5a244f48)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * Copyright (C) 2017 Hari Bathini, IBM Corporation
7  */
8 
9 #include "namespaces.h"
10 #include "util.h"
11 #include "event.h"
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <limits.h>
15 #include <sched.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.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 	nnsi = calloc(1, sizeof(*nnsi));
142 	if (nnsi != NULL) {
143 		nnsi->pid = nsi->pid;
144 		nnsi->tgid = nsi->tgid;
145 		nnsi->nstgid = nsi->nstgid;
146 		nnsi->need_setns = nsi->need_setns;
147 		if (nsi->mntns_path) {
148 			nnsi->mntns_path = strdup(nsi->mntns_path);
149 			if (!nnsi->mntns_path) {
150 				free(nnsi);
151 				return NULL;
152 			}
153 		}
154 		refcount_set(&nnsi->refcnt, 1);
155 	}
156 
157 	return nnsi;
158 }
159 
160 void nsinfo__delete(struct nsinfo *nsi)
161 {
162 	zfree(&nsi->mntns_path);
163 	free(nsi);
164 }
165 
166 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
167 {
168 	if (nsi)
169 		refcount_inc(&nsi->refcnt);
170 	return nsi;
171 }
172 
173 void nsinfo__put(struct nsinfo *nsi)
174 {
175 	if (nsi && refcount_dec_and_test(&nsi->refcnt))
176 		nsinfo__delete(nsi);
177 }
178 
179 void nsinfo__mountns_enter(struct nsinfo *nsi,
180 				  struct nscookie *nc)
181 {
182 	char curpath[PATH_MAX];
183 	int oldns = -1;
184 	int newns = -1;
185 
186 	if (nc == NULL)
187 		return;
188 
189 	nc->oldns = -1;
190 	nc->newns = -1;
191 
192 	if (!nsi || !nsi->need_setns)
193 		return;
194 
195 	if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
196 		return;
197 
198 	oldns = open(curpath, O_RDONLY);
199 	if (oldns < 0)
200 		return;
201 
202 	newns = open(nsi->mntns_path, O_RDONLY);
203 	if (newns < 0)
204 		goto errout;
205 
206 	if (setns(newns, CLONE_NEWNS) < 0)
207 		goto errout;
208 
209 	nc->oldns = oldns;
210 	nc->newns = newns;
211 	return;
212 
213 errout:
214 	if (oldns > -1)
215 		close(oldns);
216 	if (newns > -1)
217 		close(newns);
218 }
219 
220 void nsinfo__mountns_exit(struct nscookie *nc)
221 {
222 	if (nc == NULL || nc->oldns == -1 || nc->newns == -1)
223 		return;
224 
225 	setns(nc->oldns, CLONE_NEWNS);
226 
227 	if (nc->oldns > -1) {
228 		close(nc->oldns);
229 		nc->oldns = -1;
230 	}
231 
232 	if (nc->newns > -1) {
233 		close(nc->newns);
234 		nc->newns = -1;
235 	}
236 }
237 
238 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
239 {
240 	char *rpath;
241 	struct nscookie nsc;
242 
243 	nsinfo__mountns_enter(nsi, &nsc);
244 	rpath = realpath(path, NULL);
245 	nsinfo__mountns_exit(&nsc);
246 
247 	return rpath;
248 }
249