Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / perf / util / namespaces.c
blob5be021701f34e758aae5eda1d95bb7a9ce49ebad
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.
6 * Copyright (C) 2017 Hari Bathini, IBM Corporation
7 */
9 #include "namespaces.h"
10 #include "util.h"
11 #include "event.h"
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <limits.h>
16 #include <sched.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
22 struct namespaces *namespaces__new(struct namespaces_event *event)
24 struct namespaces *namespaces;
25 u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
26 sizeof(struct perf_ns_link_info));
28 namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
29 if (!namespaces)
30 return NULL;
32 namespaces->end_time = -1;
34 if (event)
35 memcpy(namespaces->link_info, event->link_info, link_info_size);
37 return namespaces;
40 void namespaces__free(struct namespaces *namespaces)
42 free(namespaces);
45 int nsinfo__init(struct nsinfo *nsi)
47 char oldns[PATH_MAX];
48 char spath[PATH_MAX];
49 char *newns = NULL;
50 char *statln = NULL;
51 struct stat old_stat;
52 struct stat new_stat;
53 FILE *f = NULL;
54 size_t linesz = 0;
55 int rv = -1;
57 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
58 return rv;
60 if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
61 return rv;
63 if (stat(oldns, &old_stat) < 0)
64 goto out;
66 if (stat(newns, &new_stat) < 0)
67 goto out;
69 /* Check if the mount namespaces differ, if so then indicate that we
70 * want to switch as part of looking up dso/map data.
72 if (old_stat.st_ino != new_stat.st_ino) {
73 nsi->need_setns = true;
74 nsi->mntns_path = newns;
75 newns = NULL;
78 /* If we're dealing with a process that is in a different PID namespace,
79 * attempt to work out the innermost tgid for the process.
81 if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
82 goto out;
84 f = fopen(spath, "r");
85 if (f == NULL)
86 goto out;
88 while (getline(&statln, &linesz, f) != -1) {
89 /* Use tgid if CONFIG_PID_NS is not defined. */
90 if (strstr(statln, "Tgid:") != NULL) {
91 nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
92 NULL, 10);
93 nsi->nstgid = nsi->tgid;
96 if (strstr(statln, "NStgid:") != NULL) {
97 nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'),
98 NULL, 10);
99 break;
102 rv = 0;
104 out:
105 if (f != NULL)
106 (void) fclose(f);
107 free(statln);
108 free(newns);
109 return rv;
112 struct nsinfo *nsinfo__new(pid_t pid)
114 struct nsinfo *nsi;
116 if (pid == 0)
117 return NULL;
119 nsi = calloc(1, sizeof(*nsi));
120 if (nsi != NULL) {
121 nsi->pid = pid;
122 nsi->tgid = pid;
123 nsi->nstgid = pid;
124 nsi->need_setns = false;
125 /* Init may fail if the process exits while we're trying to look
126 * at its proc information. In that case, save the pid but
127 * don't try to enter the namespace.
129 if (nsinfo__init(nsi) == -1)
130 nsi->need_setns = false;
132 refcount_set(&nsi->refcnt, 1);
135 return nsi;
138 struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
140 struct nsinfo *nnsi;
142 nnsi = calloc(1, sizeof(*nnsi));
143 if (nnsi != NULL) {
144 nnsi->pid = nsi->pid;
145 nnsi->tgid = nsi->tgid;
146 nnsi->nstgid = nsi->nstgid;
147 nnsi->need_setns = nsi->need_setns;
148 if (nsi->mntns_path) {
149 nnsi->mntns_path = strdup(nsi->mntns_path);
150 if (!nnsi->mntns_path) {
151 free(nnsi);
152 return NULL;
155 refcount_set(&nnsi->refcnt, 1);
158 return nnsi;
161 void nsinfo__delete(struct nsinfo *nsi)
163 zfree(&nsi->mntns_path);
164 free(nsi);
167 struct nsinfo *nsinfo__get(struct nsinfo *nsi)
169 if (nsi)
170 refcount_inc(&nsi->refcnt);
171 return nsi;
174 void nsinfo__put(struct nsinfo *nsi)
176 if (nsi && refcount_dec_and_test(&nsi->refcnt))
177 nsinfo__delete(nsi);
180 void nsinfo__mountns_enter(struct nsinfo *nsi,
181 struct nscookie *nc)
183 char curpath[PATH_MAX];
184 int oldns = -1;
185 int newns = -1;
187 if (nc == NULL)
188 return;
190 nc->oldns = -1;
191 nc->newns = -1;
193 if (!nsi || !nsi->need_setns)
194 return;
196 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
197 return;
199 oldns = open(curpath, O_RDONLY);
200 if (oldns < 0)
201 return;
203 newns = open(nsi->mntns_path, O_RDONLY);
204 if (newns < 0)
205 goto errout;
207 if (setns(newns, CLONE_NEWNS) < 0)
208 goto errout;
210 nc->oldns = oldns;
211 nc->newns = newns;
212 return;
214 errout:
215 if (oldns > -1)
216 close(oldns);
217 if (newns > -1)
218 close(newns);
221 void nsinfo__mountns_exit(struct nscookie *nc)
223 if (nc == NULL || nc->oldns == -1 || nc->newns == -1)
224 return;
226 setns(nc->oldns, CLONE_NEWNS);
228 if (nc->oldns > -1) {
229 close(nc->oldns);
230 nc->oldns = -1;
233 if (nc->newns > -1) {
234 close(nc->newns);
235 nc->newns = -1;
239 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
241 char *rpath;
242 struct nscookie nsc;
244 nsinfo__mountns_enter(nsi, &nsc);
245 rpath = realpath(path, NULL);
246 nsinfo__mountns_exit(&nsc);
248 return rpath;