1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (C) 2017 Hari Bathini, IBM Corporation 5 */ 6 7 #include "namespaces.h" 8 #include "event.h" 9 #include "get_current_dir_name.h" 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 #include <fcntl.h> 13 #include <limits.h> 14 #include <sched.h> 15 #include <stdlib.h> 16 #include <stdio.h> 17 #include <string.h> 18 #include <unistd.h> 19 #include <asm/bug.h> 20 #include <linux/zalloc.h> 21 22 struct namespaces *namespaces__new(struct namespaces_event *event) 23 { 24 struct namespaces *namespaces; 25 u64 link_info_size = ((event ? event->nr_namespaces : NR_NAMESPACES) * 26 sizeof(struct perf_ns_link_info)); 27 28 namespaces = zalloc(sizeof(struct namespaces) + link_info_size); 29 if (!namespaces) 30 return NULL; 31 32 namespaces->end_time = -1; 33 34 if (event) 35 memcpy(namespaces->link_info, event->link_info, link_info_size); 36 37 return namespaces; 38 } 39 40 void namespaces__free(struct namespaces *namespaces) 41 { 42 free(namespaces); 43 } 44 45 int nsinfo__init(struct nsinfo *nsi) 46 { 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; 56 57 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) 58 return rv; 59 60 if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1) 61 return rv; 62 63 if (stat(oldns, &old_stat) < 0) 64 goto out; 65 66 if (stat(newns, &new_stat) < 0) 67 goto out; 68 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. 71 */ 72 if (old_stat.st_ino != new_stat.st_ino) { 73 nsi->need_setns = true; 74 nsi->mntns_path = newns; 75 newns = NULL; 76 } 77 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. 80 */ 81 if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX) 82 goto out; 83 84 f = fopen(spath, "r"); 85 if (f == NULL) 86 goto out; 87 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; 94 } 95 96 if (strstr(statln, "NStgid:") != NULL) { 97 nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'), 98 NULL, 10); 99 break; 100 } 101 } 102 rv = 0; 103 104 out: 105 if (f != NULL) 106 (void) fclose(f); 107 free(statln); 108 free(newns); 109 return rv; 110 } 111 112 struct nsinfo *nsinfo__new(pid_t pid) 113 { 114 struct nsinfo *nsi; 115 116 if (pid == 0) 117 return NULL; 118 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. 128 */ 129 if (nsinfo__init(nsi) == -1) 130 nsi->need_setns = false; 131 132 refcount_set(&nsi->refcnt, 1); 133 } 134 135 return nsi; 136 } 137 138 struct nsinfo *nsinfo__copy(struct nsinfo *nsi) 139 { 140 struct nsinfo *nnsi; 141 142 if (nsi == NULL) 143 return NULL; 144 145 nnsi = calloc(1, sizeof(*nnsi)); 146 if (nnsi != NULL) { 147 nnsi->pid = nsi->pid; 148 nnsi->tgid = nsi->tgid; 149 nnsi->nstgid = nsi->nstgid; 150 nnsi->need_setns = nsi->need_setns; 151 if (nsi->mntns_path) { 152 nnsi->mntns_path = strdup(nsi->mntns_path); 153 if (!nnsi->mntns_path) { 154 free(nnsi); 155 return NULL; 156 } 157 } 158 refcount_set(&nnsi->refcnt, 1); 159 } 160 161 return nnsi; 162 } 163 164 void nsinfo__delete(struct nsinfo *nsi) 165 { 166 zfree(&nsi->mntns_path); 167 free(nsi); 168 } 169 170 struct nsinfo *nsinfo__get(struct nsinfo *nsi) 171 { 172 if (nsi) 173 refcount_inc(&nsi->refcnt); 174 return nsi; 175 } 176 177 void nsinfo__put(struct nsinfo *nsi) 178 { 179 if (nsi && refcount_dec_and_test(&nsi->refcnt)) 180 nsinfo__delete(nsi); 181 } 182 183 void nsinfo__mountns_enter(struct nsinfo *nsi, 184 struct nscookie *nc) 185 { 186 char curpath[PATH_MAX]; 187 int oldns = -1; 188 int newns = -1; 189 char *oldcwd = NULL; 190 191 if (nc == NULL) 192 return; 193 194 nc->oldns = -1; 195 nc->newns = -1; 196 197 if (!nsi || !nsi->need_setns) 198 return; 199 200 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) 201 return; 202 203 oldcwd = get_current_dir_name(); 204 if (!oldcwd) 205 return; 206 207 oldns = open(curpath, O_RDONLY); 208 if (oldns < 0) 209 goto errout; 210 211 newns = open(nsi->mntns_path, O_RDONLY); 212 if (newns < 0) 213 goto errout; 214 215 if (setns(newns, CLONE_NEWNS) < 0) 216 goto errout; 217 218 nc->oldcwd = oldcwd; 219 nc->oldns = oldns; 220 nc->newns = newns; 221 return; 222 223 errout: 224 free(oldcwd); 225 if (oldns > -1) 226 close(oldns); 227 if (newns > -1) 228 close(newns); 229 } 230 231 void nsinfo__mountns_exit(struct nscookie *nc) 232 { 233 if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd) 234 return; 235 236 setns(nc->oldns, CLONE_NEWNS); 237 238 if (nc->oldcwd) { 239 WARN_ON_ONCE(chdir(nc->oldcwd)); 240 zfree(&nc->oldcwd); 241 } 242 243 if (nc->oldns > -1) { 244 close(nc->oldns); 245 nc->oldns = -1; 246 } 247 248 if (nc->newns > -1) { 249 close(nc->newns); 250 nc->newns = -1; 251 } 252 } 253 254 char *nsinfo__realpath(const char *path, struct nsinfo *nsi) 255 { 256 char *rpath; 257 struct nscookie nsc; 258 259 nsinfo__mountns_enter(nsi, &nsc); 260 rpath = realpath(path, NULL); 261 nsinfo__mountns_exit(&nsc); 262 263 return rpath; 264 } 265