blob: fc5f398779a45389e1fdab3f1272208ae6ca5b09 [file] [log] [blame]
Hari Bathinif3b36142017-03-08 02:11:43 +05301/*
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"
Krister Johansen843ff372017-07-05 18:48:08 -070012#include <sys/types.h>
13#include <sys/stat.h>
14#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>
Hari Bathinif3b36142017-03-08 02:11:43 +053019
20struct namespaces *namespaces__new(struct namespaces_event *event)
21{
22 struct namespaces *namespaces;
23 u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) *
24 sizeof(struct perf_ns_link_info));
25
26 namespaces = zalloc(sizeof(struct namespaces) + link_info_size);
27 if (!namespaces)
28 return NULL;
29
30 namespaces->end_time = -1;
31
32 if (event)
33 memcpy(namespaces->link_info, event->link_info, link_info_size);
34
35 return namespaces;
36}
37
38void namespaces__free(struct namespaces *namespaces)
39{
40 free(namespaces);
41}
Krister Johansen843ff372017-07-05 18:48:08 -070042
Krister Johansenbf2e7102017-07-05 18:48:09 -070043int nsinfo__init(struct nsinfo *nsi)
Krister Johansen843ff372017-07-05 18:48:08 -070044{
45 char oldns[PATH_MAX];
Krister Johansenbf2e7102017-07-05 18:48:09 -070046 char spath[PATH_MAX];
Krister Johansen843ff372017-07-05 18:48:08 -070047 char *newns = NULL;
Krister Johansenbf2e7102017-07-05 18:48:09 -070048 char *statln = NULL;
Krister Johansen843ff372017-07-05 18:48:08 -070049 struct stat old_stat;
50 struct stat new_stat;
Krister Johansenbf2e7102017-07-05 18:48:09 -070051 FILE *f = NULL;
52 size_t linesz = 0;
53 int rv = -1;
Krister Johansen843ff372017-07-05 18:48:08 -070054
55 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
Krister Johansenbf2e7102017-07-05 18:48:09 -070056 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -070057
58 if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
Krister Johansenbf2e7102017-07-05 18:48:09 -070059 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -070060
61 if (stat(oldns, &old_stat) < 0)
62 goto out;
63
64 if (stat(newns, &new_stat) < 0)
65 goto out;
66
67 /* Check if the mount namespaces differ, if so then indicate that we
68 * want to switch as part of looking up dso/map data.
69 */
70 if (old_stat.st_ino != new_stat.st_ino) {
71 nsi->need_setns = true;
72 nsi->mntns_path = newns;
73 newns = NULL;
74 }
75
Krister Johansenbf2e7102017-07-05 18:48:09 -070076 /* If we're dealing with a process that is in a different PID namespace,
77 * attempt to work out the innermost tgid for the process.
78 */
79 if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX)
80 goto out;
81
82 f = fopen(spath, "r");
83 if (f == NULL)
84 goto out;
85
86 while (getline(&statln, &linesz, f) != -1) {
87 /* Use tgid if CONFIG_PID_NS is not defined. */
88 if (strstr(statln, "Tgid:") != NULL) {
89 nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'),
90 NULL, 10);
91 nsi->nstgid = nsi->tgid;
92 }
93
94 if (strstr(statln, "NStgid:") != NULL) {
95 nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'),
96 NULL, 10);
97 break;
98 }
99 }
100 rv = 0;
101
Krister Johansen843ff372017-07-05 18:48:08 -0700102out:
Krister Johansenbf2e7102017-07-05 18:48:09 -0700103 if (f != NULL)
104 (void) fclose(f);
105 free(statln);
Krister Johansen843ff372017-07-05 18:48:08 -0700106 free(newns);
Krister Johansenbf2e7102017-07-05 18:48:09 -0700107 return rv;
Krister Johansen843ff372017-07-05 18:48:08 -0700108}
109
110struct nsinfo *nsinfo__new(pid_t pid)
111{
Krister Johansenbf2e7102017-07-05 18:48:09 -0700112 struct nsinfo *nsi;
Krister Johansen843ff372017-07-05 18:48:08 -0700113
Krister Johansenbf2e7102017-07-05 18:48:09 -0700114 if (pid == 0)
115 return NULL;
116
117 nsi = calloc(1, sizeof(*nsi));
Krister Johansen843ff372017-07-05 18:48:08 -0700118 if (nsi != NULL) {
119 nsi->pid = pid;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700120 nsi->tgid = pid;
121 nsi->nstgid = pid;
Krister Johansen843ff372017-07-05 18:48:08 -0700122 nsi->need_setns = false;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700123 /* Init may fail if the process exits while we're trying to look
124 * at its proc information. In that case, save the pid but
125 * don't try to enter the namespace.
126 */
127 if (nsinfo__init(nsi) == -1)
128 nsi->need_setns = false;
129
Krister Johansen843ff372017-07-05 18:48:08 -0700130 refcount_set(&nsi->refcnt, 1);
131 }
132
133 return nsi;
134}
135
Krister Johansenbf2e7102017-07-05 18:48:09 -0700136struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
137{
138 struct nsinfo *nnsi;
139
140 nnsi = calloc(1, sizeof(*nnsi));
141 if (nnsi != NULL) {
142 nnsi->pid = nsi->pid;
143 nnsi->tgid = nsi->tgid;
144 nnsi->nstgid = nsi->nstgid;
145 nnsi->need_setns = nsi->need_setns;
146 if (nsi->mntns_path) {
147 nnsi->mntns_path = strdup(nsi->mntns_path);
148 if (!nnsi->mntns_path) {
149 free(nnsi);
150 return NULL;
151 }
152 }
153 refcount_set(&nnsi->refcnt, 1);
154 }
155
156 return nnsi;
157}
158
Krister Johansen843ff372017-07-05 18:48:08 -0700159void nsinfo__delete(struct nsinfo *nsi)
160{
161 zfree(&nsi->mntns_path);
162 free(nsi);
163}
164
165struct nsinfo *nsinfo__get(struct nsinfo *nsi)
166{
167 if (nsi)
168 refcount_inc(&nsi->refcnt);
169 return nsi;
170}
171
172void nsinfo__put(struct nsinfo *nsi)
173{
174 if (nsi && refcount_dec_and_test(&nsi->refcnt))
175 nsinfo__delete(nsi);
176}
177
Krister Johansenbf2e7102017-07-05 18:48:09 -0700178void nsinfo__mountns_enter(struct nsinfo *nsi,
179 struct nscookie *nc)
Krister Johansen843ff372017-07-05 18:48:08 -0700180{
181 char curpath[PATH_MAX];
182 int oldns = -1;
183 int newns = -1;
184
185 if (nc == NULL)
186 return;
187
188 nc->oldns = -1;
189 nc->newns = -1;
190
191 if (!nsi || !nsi->need_setns)
192 return;
193
194 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
195 return;
196
197 oldns = open(curpath, O_RDONLY);
198 if (oldns < 0)
199 return;
200
201 newns = open(nsi->mntns_path, O_RDONLY);
202 if (newns < 0)
203 goto errout;
204
205 if (setns(newns, CLONE_NEWNS) < 0)
206 goto errout;
207
208 nc->oldns = oldns;
209 nc->newns = newns;
210 return;
211
212errout:
213 if (oldns > -1)
214 close(oldns);
215 if (newns > -1)
216 close(newns);
217}
218
219void nsinfo__mountns_exit(struct nscookie *nc)
220{
221 if (nc == NULL || nc->oldns == -1 || nc->newns == -1)
222 return;
223
224 setns(nc->oldns, CLONE_NEWNS);
225
226 if (nc->oldns > -1) {
227 close(nc->oldns);
228 nc->oldns = -1;
229 }
230
231 if (nc->newns > -1) {
232 close(nc->newns);
233 nc->newns = -1;
234 }
235}