1 /* $NetBSD: procfs_map.c,v 1.16 2002/11/07 08:17:28 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1993 Jan-Simon Pendry 5 * Copyright (c) 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jan-Simon Pendry. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)procfs_status.c 8.3 (Berkeley) 2/17/94 40 * 41 * $FreeBSD: procfs_map.c,v 1.18 1998/12/04 22:54:51 archie Exp $ 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: procfs_map.c,v 1.16 2002/11/07 08:17:28 thorpej Exp $"); 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/proc.h> 50 #include <sys/vnode.h> 51 #include <sys/malloc.h> 52 #include <sys/namei.h> 53 #include <miscfs/procfs/procfs.h> 54 55 #include <sys/lock.h> 56 57 #include <uvm/uvm.h> 58 59 #define MEBUFFERSIZE 256 60 61 extern int getcwd_common __P((struct vnode *, struct vnode *, 62 char **, char *, int, int, struct proc *)); 63 64 static int procfs_vnode_to_path(struct vnode *vp, char *path, int len, 65 struct proc *curp, struct proc *p); 66 67 /* 68 * The map entries can *almost* be read with programs like cat. However, 69 * large maps need special programs to read. It is not easy to implement 70 * a program that can sense the required size of the buffer, and then 71 * subsequently do a read with the appropriate size. This operation cannot 72 * be atomic. The best that we can do is to allow the program to do a read 73 * with an arbitrarily large buffer, and return as much as we can. We can 74 * return an error code if the buffer is too small (EFBIG), then the program 75 * can try a bigger buffer. 76 */ 77 int 78 procfs_domap(struct proc *curp, struct proc *p, struct pfsnode *pfs, 79 struct uio *uio, int linuxmode) 80 { 81 size_t len; 82 int error; 83 struct vm_map *map = &p->p_vmspace->vm_map; 84 struct vm_map_entry *entry; 85 char mebuffer[MEBUFFERSIZE]; 86 char *path; 87 struct vnode *vp; 88 struct vattr va; 89 dev_t dev; 90 long fileid; 91 92 if (uio->uio_rw != UIO_READ) 93 return (EOPNOTSUPP); 94 95 if (uio->uio_offset != 0) 96 return (0); 97 98 error = 0; 99 if (map != &curproc->p_vmspace->vm_map) 100 vm_map_lock_read(map); 101 for (entry = map->header.next; 102 ((uio->uio_resid > 0) && (entry != &map->header)); 103 entry = entry->next) { 104 105 if (UVM_ET_ISSUBMAP(entry)) 106 continue; 107 108 if (linuxmode != 0) { 109 path = (char *)malloc(MAXPATHLEN * 4, M_TEMP, M_WAITOK); 110 if (path == NULL) { 111 error = ENOMEM; 112 break; 113 } 114 *path = 0; 115 116 dev = (dev_t)0; 117 fileid = 0; 118 if (UVM_ET_ISOBJ(entry) && 119 UVM_OBJ_IS_VNODE(entry->object.uvm_obj)) { 120 vp = (struct vnode *)entry->object.uvm_obj; 121 error = VOP_GETATTR(vp, &va, curp->p_ucred, 122 curp); 123 if (error == 0 && vp != pfs->pfs_vnode) { 124 fileid = va.va_fileid; 125 dev = va.va_fsid; 126 error = procfs_vnode_to_path(vp, path, 127 MAXPATHLEN * 4, curp, p); 128 } 129 } 130 snprintf(mebuffer, sizeof(mebuffer), 131 "%0*lx-%0*lx %c%c%c%c %0*lx %02x:%02x %ld %s\n", 132 (int)sizeof(void *) * 2,(unsigned long)entry->start, 133 (int)sizeof(void *) * 2,(unsigned long)entry->end, 134 (entry->protection & VM_PROT_READ) ? 'r' : '-', 135 (entry->protection & VM_PROT_WRITE) ? 'w' : '-', 136 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-', 137 (entry->etype & UVM_ET_COPYONWRITE) ? 'p' : 's', 138 (int)sizeof(void *) * 2, 139 (unsigned long)entry->offset, 140 major(dev), minor(dev), fileid, path); 141 free(path, M_TEMP); 142 } else { 143 snprintf(mebuffer, sizeof(mebuffer), 144 "0x%lx 0x%lx %c%c%c %c%c%c %s %s %d %d %d\n", 145 entry->start, entry->end, 146 (entry->protection & VM_PROT_READ) ? 'r' : '-', 147 (entry->protection & VM_PROT_WRITE) ? 'w' : '-', 148 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-', 149 (entry->max_protection & VM_PROT_READ) ? 'r' : '-', 150 (entry->max_protection & VM_PROT_WRITE) ? 'w' : '-', 151 (entry->max_protection & VM_PROT_EXECUTE) ? 152 'x' : '-', 153 (entry->etype & UVM_ET_COPYONWRITE) ? 154 "COW" : "NCOW", 155 (entry->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC", 156 entry->inheritance, entry->wired_count, 157 entry->advice); 158 } 159 160 len = strlen(mebuffer); 161 if (len > uio->uio_resid) { 162 error = EFBIG; 163 break; 164 } 165 error = uiomove(mebuffer, len, uio); 166 if (error) 167 break; 168 } 169 if (map != &curproc->p_vmspace->vm_map) 170 vm_map_unlock_read(map); 171 return error; 172 } 173 174 int 175 procfs_validmap(struct proc *p, struct mount *mp) 176 { 177 return ((p->p_flag & P_SYSTEM) == 0); 178 } 179 180 /* 181 * Try to find a pathname for a vnode. Since there is no mapping 182 * vnode -> parent directory, this needs the NAMECACHE_ENTER_REVERSE 183 * option to work (to make cache_revlookup succeed). 184 */ 185 static int procfs_vnode_to_path(struct vnode *vp, char *path, int len, 186 struct proc *curp, struct proc *p) 187 { 188 int error, lenused, elen; 189 char *bp, *bend; 190 struct vnode *dvp; 191 192 bp = bend = &path[len]; 193 *(--bp) = '\0'; 194 195 error = vget(vp, LK_EXCLUSIVE | LK_RETRY); 196 if (error != 0) 197 return error; 198 error = cache_revlookup(vp, &dvp, &bp, path); 199 vput(vp); 200 if (error != 0) 201 return (error == -1 ? ENOENT : error); 202 203 error = vget(dvp, 0); 204 if (error != 0) 205 return error; 206 *(--bp) = '/'; 207 /* XXX GETCWD_CHECK_ACCESS == 0x0001 */ 208 error = getcwd_common(dvp, NULL, &bp, path, len / 2, 1, curp); 209 210 /* 211 * Strip off emulation path for emulated processes looking at 212 * the maps file of a process of the same emulation. (Won't 213 * work if /emul/xxx is a symlink..) 214 */ 215 if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) { 216 elen = strlen(curp->p_emul->e_path); 217 if (!strncmp(bp, curp->p_emul->e_path, elen)) 218 bp = &bp[elen]; 219 } 220 221 lenused = bend - bp; 222 223 memcpy(path, bp, lenused); 224 path[lenused] = 0; 225 226 return 0; 227 } 228