Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 2 | /* |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2017 Hari Bathini, IBM Corporation |
| 5 | */ |
| 6 | |
| 7 | #include "namespaces.h" |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 8 | #include "event.h" |
Arnaldo Carvalho de Melo | e5653eb | 2019-07-05 14:16:15 -0300 | [diff] [blame] | 9 | #include "get_current_dir_name.h" |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
Arnaldo Carvalho de Melo | c23c2a0 | 2017-09-11 10:50:26 -0300 | [diff] [blame] | 12 | #include <fcntl.h> |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 13 | #include <limits.h> |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 14 | #include <sched.h> |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
Arnaldo Carvalho de Melo | 72f7c4d | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 17 | #include <string.h> |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 18 | #include <unistd.h> |
Jiri Olsa | b01c1f69 | 2018-11-01 18:00:01 +0100 | [diff] [blame] | 19 | #include <asm/bug.h> |
Arnaldo Carvalho de Melo | 055c67e | 2019-09-18 16:08:52 -0300 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 21 | #include <linux/zalloc.h> |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | 055c67e | 2019-09-18 16:08:52 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 69d81f0 | 2019-08-26 19:02:31 -0300 | [diff] [blame] | 40 | struct namespaces *namespaces__new(struct perf_record_namespaces *event) |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 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 | } |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 62 | |
Leo Yan | 5d28a17 | 2021-12-12 21:47:20 +0800 | [diff] [blame] | 63 | static int nsinfo__get_nspid(struct nsinfo *nsi, const char *path) |
| 64 | { |
| 65 | FILE *f = NULL; |
| 66 | char *statln = NULL; |
| 67 | size_t linesz = 0; |
| 68 | char *nspid; |
| 69 | |
| 70 | f = fopen(path, "r"); |
| 71 | if (f == NULL) |
| 72 | return -1; |
| 73 | |
| 74 | while (getline(&statln, &linesz, f) != -1) { |
| 75 | /* Use tgid if CONFIG_PID_NS is not defined. */ |
| 76 | if (strstr(statln, "Tgid:") != NULL) { |
| 77 | nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'), |
| 78 | NULL, 10); |
Ian Rogers | bcaf0a9 | 2022-02-11 02:34:06 -0800 | [diff] [blame] | 79 | nsi->nstgid = nsinfo__tgid(nsi); |
Leo Yan | 5d28a17 | 2021-12-12 21:47:20 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (strstr(statln, "NStgid:") != NULL) { |
| 83 | nspid = strrchr(statln, '\t'); |
| 84 | nsi->nstgid = (pid_t)strtol(nspid, NULL, 10); |
| 85 | /* |
| 86 | * If innermost tgid is not the first, process is in a different |
| 87 | * PID namespace. |
| 88 | */ |
| 89 | nsi->in_pidns = (statln + sizeof("NStgid:") - 1) != nspid; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | fclose(f); |
| 95 | free(statln); |
| 96 | return 0; |
| 97 | } |
| 98 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 99 | int nsinfo__init(struct nsinfo *nsi) |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 100 | { |
| 101 | char oldns[PATH_MAX]; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 102 | char spath[PATH_MAX]; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 103 | char *newns = NULL; |
| 104 | struct stat old_stat; |
| 105 | struct stat new_stat; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 106 | int rv = -1; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 107 | |
| 108 | if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 109 | return rv; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 110 | |
Ian Rogers | bcaf0a9 | 2022-02-11 02:34:06 -0800 | [diff] [blame] | 111 | if (asprintf(&newns, "/proc/%d/ns/mnt", nsinfo__pid(nsi)) == -1) |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 112 | return rv; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 113 | |
| 114 | if (stat(oldns, &old_stat) < 0) |
| 115 | goto out; |
| 116 | |
| 117 | if (stat(newns, &new_stat) < 0) |
| 118 | goto out; |
| 119 | |
| 120 | /* Check if the mount namespaces differ, if so then indicate that we |
| 121 | * want to switch as part of looking up dso/map data. |
| 122 | */ |
| 123 | if (old_stat.st_ino != new_stat.st_ino) { |
| 124 | nsi->need_setns = true; |
| 125 | nsi->mntns_path = newns; |
| 126 | newns = NULL; |
| 127 | } |
| 128 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 129 | /* If we're dealing with a process that is in a different PID namespace, |
| 130 | * attempt to work out the innermost tgid for the process. |
| 131 | */ |
Ian Rogers | bcaf0a9 | 2022-02-11 02:34:06 -0800 | [diff] [blame] | 132 | if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsinfo__pid(nsi)) >= PATH_MAX) |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 133 | goto out; |
| 134 | |
Leo Yan | 5d28a17 | 2021-12-12 21:47:20 +0800 | [diff] [blame] | 135 | rv = nsinfo__get_nspid(nsi, spath); |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 136 | |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 137 | out: |
| 138 | free(newns); |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 139 | return rv; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | struct nsinfo *nsinfo__new(pid_t pid) |
| 143 | { |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 144 | struct nsinfo *nsi; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 145 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 146 | if (pid == 0) |
| 147 | return NULL; |
| 148 | |
| 149 | nsi = calloc(1, sizeof(*nsi)); |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 150 | if (nsi != NULL) { |
| 151 | nsi->pid = pid; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 152 | nsi->tgid = pid; |
| 153 | nsi->nstgid = pid; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 154 | nsi->need_setns = false; |
Yonatan Goldschmidt | 2b51c71 | 2020-11-05 03:54:18 +0200 | [diff] [blame] | 155 | nsi->in_pidns = false; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 156 | /* Init may fail if the process exits while we're trying to look |
| 157 | * at its proc information. In that case, save the pid but |
| 158 | * don't try to enter the namespace. |
| 159 | */ |
| 160 | if (nsinfo__init(nsi) == -1) |
| 161 | nsi->need_setns = false; |
| 162 | |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 163 | refcount_set(&nsi->refcnt, 1); |
| 164 | } |
| 165 | |
| 166 | return nsi; |
| 167 | } |
| 168 | |
Ian Rogers | bcaf0a9 | 2022-02-11 02:34:06 -0800 | [diff] [blame] | 169 | struct nsinfo *nsinfo__copy(const struct nsinfo *nsi) |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 170 | { |
| 171 | struct nsinfo *nnsi; |
| 172 | |
Benno Evers | 3f4417d | 2018-08-10 15:36:13 +0200 | [diff] [blame] | 173 | if (nsi == NULL) |
| 174 | return NULL; |
| 175 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 176 | nnsi = calloc(1, sizeof(*nnsi)); |
| 177 | if (nnsi != NULL) { |
Ian Rogers | bcaf0a9 | 2022-02-11 02:34:06 -0800 | [diff] [blame] | 178 | nnsi->pid = nsinfo__pid(nsi); |
| 179 | nnsi->tgid = nsinfo__tgid(nsi); |
| 180 | nnsi->nstgid = nsinfo__nstgid(nsi); |
| 181 | nnsi->need_setns = nsinfo__need_setns(nsi); |
| 182 | nnsi->in_pidns = nsinfo__in_pidns(nsi); |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 183 | if (nsi->mntns_path) { |
| 184 | nnsi->mntns_path = strdup(nsi->mntns_path); |
| 185 | if (!nnsi->mntns_path) { |
| 186 | free(nnsi); |
| 187 | return NULL; |
| 188 | } |
| 189 | } |
| 190 | refcount_set(&nnsi->refcnt, 1); |
| 191 | } |
| 192 | |
| 193 | return nnsi; |
| 194 | } |
| 195 | |
Ian Rogers | bcaf0a9 | 2022-02-11 02:34:06 -0800 | [diff] [blame] | 196 | static void nsinfo__delete(struct nsinfo *nsi) |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 197 | { |
| 198 | zfree(&nsi->mntns_path); |
| 199 | free(nsi); |
| 200 | } |
| 201 | |
| 202 | struct nsinfo *nsinfo__get(struct nsinfo *nsi) |
| 203 | { |
| 204 | if (nsi) |
| 205 | refcount_inc(&nsi->refcnt); |
| 206 | return nsi; |
| 207 | } |
| 208 | |
| 209 | void nsinfo__put(struct nsinfo *nsi) |
| 210 | { |
| 211 | if (nsi && refcount_dec_and_test(&nsi->refcnt)) |
| 212 | nsinfo__delete(nsi); |
| 213 | } |
| 214 | |
Ian Rogers | bcaf0a9 | 2022-02-11 02:34:06 -0800 | [diff] [blame] | 215 | bool nsinfo__need_setns(const struct nsinfo *nsi) |
| 216 | { |
| 217 | return nsi->need_setns; |
| 218 | } |
| 219 | |
| 220 | void nsinfo__clear_need_setns(struct nsinfo *nsi) |
| 221 | { |
| 222 | nsi->need_setns = false; |
| 223 | } |
| 224 | |
| 225 | pid_t nsinfo__tgid(const struct nsinfo *nsi) |
| 226 | { |
| 227 | return nsi->tgid; |
| 228 | } |
| 229 | |
| 230 | pid_t nsinfo__nstgid(const struct nsinfo *nsi) |
| 231 | { |
| 232 | return nsi->nstgid; |
| 233 | } |
| 234 | |
| 235 | pid_t nsinfo__pid(const struct nsinfo *nsi) |
| 236 | { |
| 237 | return nsi->pid; |
| 238 | } |
| 239 | |
| 240 | pid_t nsinfo__in_pidns(const struct nsinfo *nsi) |
| 241 | { |
| 242 | return nsi->in_pidns; |
| 243 | } |
| 244 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 245 | void nsinfo__mountns_enter(struct nsinfo *nsi, |
| 246 | struct nscookie *nc) |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 247 | { |
| 248 | char curpath[PATH_MAX]; |
| 249 | int oldns = -1; |
| 250 | int newns = -1; |
Jiri Olsa | b01c1f69 | 2018-11-01 18:00:01 +0100 | [diff] [blame] | 251 | char *oldcwd = NULL; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 252 | |
| 253 | if (nc == NULL) |
| 254 | return; |
| 255 | |
| 256 | nc->oldns = -1; |
| 257 | nc->newns = -1; |
| 258 | |
| 259 | if (!nsi || !nsi->need_setns) |
| 260 | return; |
| 261 | |
| 262 | if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) |
| 263 | return; |
| 264 | |
Jiri Olsa | b01c1f69 | 2018-11-01 18:00:01 +0100 | [diff] [blame] | 265 | oldcwd = get_current_dir_name(); |
| 266 | if (!oldcwd) |
| 267 | return; |
| 268 | |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 269 | oldns = open(curpath, O_RDONLY); |
| 270 | if (oldns < 0) |
Jiri Olsa | b01c1f69 | 2018-11-01 18:00:01 +0100 | [diff] [blame] | 271 | goto errout; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 272 | |
| 273 | newns = open(nsi->mntns_path, O_RDONLY); |
| 274 | if (newns < 0) |
| 275 | goto errout; |
| 276 | |
| 277 | if (setns(newns, CLONE_NEWNS) < 0) |
| 278 | goto errout; |
| 279 | |
Jiri Olsa | b01c1f69 | 2018-11-01 18:00:01 +0100 | [diff] [blame] | 280 | nc->oldcwd = oldcwd; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 281 | nc->oldns = oldns; |
| 282 | nc->newns = newns; |
| 283 | return; |
| 284 | |
| 285 | errout: |
Jiri Olsa | b01c1f69 | 2018-11-01 18:00:01 +0100 | [diff] [blame] | 286 | free(oldcwd); |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 287 | if (oldns > -1) |
| 288 | close(oldns); |
| 289 | if (newns > -1) |
| 290 | close(newns); |
| 291 | } |
| 292 | |
| 293 | void nsinfo__mountns_exit(struct nscookie *nc) |
| 294 | { |
Jiri Olsa | b01c1f69 | 2018-11-01 18:00:01 +0100 | [diff] [blame] | 295 | if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd) |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 296 | return; |
| 297 | |
| 298 | setns(nc->oldns, CLONE_NEWNS); |
| 299 | |
Jiri Olsa | b01c1f69 | 2018-11-01 18:00:01 +0100 | [diff] [blame] | 300 | if (nc->oldcwd) { |
| 301 | WARN_ON_ONCE(chdir(nc->oldcwd)); |
| 302 | zfree(&nc->oldcwd); |
| 303 | } |
| 304 | |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 305 | if (nc->oldns > -1) { |
| 306 | close(nc->oldns); |
| 307 | nc->oldns = -1; |
| 308 | } |
| 309 | |
| 310 | if (nc->newns > -1) { |
| 311 | close(nc->newns); |
| 312 | nc->newns = -1; |
| 313 | } |
| 314 | } |
Krister Johansen | 544abd4 | 2017-07-05 18:48:10 -0700 | [diff] [blame] | 315 | |
| 316 | char *nsinfo__realpath(const char *path, struct nsinfo *nsi) |
| 317 | { |
| 318 | char *rpath; |
| 319 | struct nscookie nsc; |
| 320 | |
| 321 | nsinfo__mountns_enter(nsi, &nsc); |
| 322 | rpath = realpath(path, NULL); |
| 323 | nsinfo__mountns_exit(&nsc); |
| 324 | |
| 325 | return rpath; |
| 326 | } |
Yonatan Goldschmidt | 67dec92 | 2020-11-05 03:56:04 +0200 | [diff] [blame] | 327 | |
| 328 | int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi) |
| 329 | { |
| 330 | int ret; |
| 331 | struct nscookie nsc; |
| 332 | |
| 333 | nsinfo__mountns_enter(nsi, &nsc); |
| 334 | ret = stat(filename, st); |
| 335 | nsinfo__mountns_exit(&nsc); |
| 336 | |
| 337 | return ret; |
| 338 | } |
Leo Yan | 5d28a17 | 2021-12-12 21:47:20 +0800 | [diff] [blame] | 339 | |
| 340 | bool nsinfo__is_in_root_namespace(void) |
| 341 | { |
| 342 | struct nsinfo nsi; |
| 343 | |
| 344 | memset(&nsi, 0x0, sizeof(nsi)); |
| 345 | nsinfo__get_nspid(&nsi, "/proc/self/status"); |
| 346 | return !nsi.in_pidns; |
| 347 | } |