Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 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" |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
| 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> |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 19 | |
| 20 | struct 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 | |
| 38 | void namespaces__free(struct namespaces *namespaces) |
| 39 | { |
| 40 | free(namespaces); |
| 41 | } |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 42 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 43 | int nsinfo__init(struct nsinfo *nsi) |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 44 | { |
| 45 | char oldns[PATH_MAX]; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 46 | char spath[PATH_MAX]; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 47 | char *newns = NULL; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 48 | char *statln = NULL; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 49 | struct stat old_stat; |
| 50 | struct stat new_stat; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 51 | FILE *f = NULL; |
| 52 | size_t linesz = 0; |
| 53 | int rv = -1; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 54 | |
| 55 | if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 56 | return rv; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 57 | |
| 58 | if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1) |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 59 | return rv; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 60 | |
| 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 Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 76 | /* 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 Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 102 | out: |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 103 | if (f != NULL) |
| 104 | (void) fclose(f); |
| 105 | free(statln); |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 106 | free(newns); |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 107 | return rv; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | struct nsinfo *nsinfo__new(pid_t pid) |
| 111 | { |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 112 | struct nsinfo *nsi; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 113 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 114 | if (pid == 0) |
| 115 | return NULL; |
| 116 | |
| 117 | nsi = calloc(1, sizeof(*nsi)); |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 118 | if (nsi != NULL) { |
| 119 | nsi->pid = pid; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 120 | nsi->tgid = pid; |
| 121 | nsi->nstgid = pid; |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 122 | nsi->need_setns = false; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 123 | /* 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 Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 130 | refcount_set(&nsi->refcnt, 1); |
| 131 | } |
| 132 | |
| 133 | return nsi; |
| 134 | } |
| 135 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 136 | struct 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 Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 159 | void nsinfo__delete(struct nsinfo *nsi) |
| 160 | { |
| 161 | zfree(&nsi->mntns_path); |
| 162 | free(nsi); |
| 163 | } |
| 164 | |
| 165 | struct nsinfo *nsinfo__get(struct nsinfo *nsi) |
| 166 | { |
| 167 | if (nsi) |
| 168 | refcount_inc(&nsi->refcnt); |
| 169 | return nsi; |
| 170 | } |
| 171 | |
| 172 | void nsinfo__put(struct nsinfo *nsi) |
| 173 | { |
| 174 | if (nsi && refcount_dec_and_test(&nsi->refcnt)) |
| 175 | nsinfo__delete(nsi); |
| 176 | } |
| 177 | |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame^] | 178 | void nsinfo__mountns_enter(struct nsinfo *nsi, |
| 179 | struct nscookie *nc) |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 180 | { |
| 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 | |
| 212 | errout: |
| 213 | if (oldns > -1) |
| 214 | close(oldns); |
| 215 | if (newns > -1) |
| 216 | close(newns); |
| 217 | } |
| 218 | |
| 219 | void 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 | } |