Home | History | Annotate | Line # | Download | only in procfs
procfs_map.c revision 1.10.2.5
      1 /*	$NetBSD: procfs_map.c,v 1.10.2.5 2002/06/24 22:11:15 nathanw 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.10.2.5 2002/06/24 22:11:15 nathanw Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/lwp.h>
     50 #include <sys/proc.h>
     51 #include <sys/vnode.h>
     52 #include <sys/malloc.h>
     53 #include <sys/namei.h>
     54 #include <miscfs/procfs/procfs.h>
     55 
     56 #include <sys/lock.h>
     57 
     58 #include <uvm/uvm.h>
     59 
     60 #define MEBUFFERSIZE 256
     61 
     62 extern int getcwd_common __P((struct vnode *, struct vnode *,
     63 			      char **, char *, int, int, struct proc *));
     64 
     65 static int procfs_vnode_to_path(struct vnode *vp, char *path, int len,
     66 				struct proc *curp, struct proc *p);
     67 
     68 /*
     69  * The map entries can *almost* be read with programs like cat.  However,
     70  * large maps need special programs to read.  It is not easy to implement
     71  * a program that can sense the required size of the buffer, and then
     72  * subsequently do a read with the appropriate size.  This operation cannot
     73  * be atomic.  The best that we can do is to allow the program to do a read
     74  * with an arbitrarily large buffer, and return as much as we can.  We can
     75  * return an error code if the buffer is too small (EFBIG), then the program
     76  * can try a bigger buffer.
     77  */
     78 int
     79 procfs_domap(struct proc *curp, struct proc *p, struct pfsnode *pfs,
     80 	     struct uio *uio, int linuxmode)
     81 {
     82 	int len;
     83 	int error;
     84 	struct vm_map *map = &p->p_vmspace->vm_map;
     85 	struct vm_map_entry *entry;
     86 	char mebuffer[MEBUFFERSIZE];
     87 	char *path;
     88 	struct vnode *vp;
     89 	struct vattr va;
     90 	dev_t dev;
     91 	long fileid;
     92 
     93 	if (uio->uio_rw != UIO_READ)
     94 		return (EOPNOTSUPP);
     95 
     96 	if (uio->uio_offset != 0)
     97 		return (0);
     98 
     99 	error = 0;
    100 	if (map != &curproc->p_vmspace->vm_map)
    101 		vm_map_lock_read(map);
    102 	for (entry = map->header.next;
    103 		((uio->uio_resid > 0) && (entry != &map->header));
    104 		entry = entry->next) {
    105 
    106 		if (UVM_ET_ISSUBMAP(entry))
    107 			continue;
    108 
    109 		if (linuxmode != 0) {
    110 			path = (char *)malloc(MAXPATHLEN * 4, M_TEMP, M_WAITOK);
    111 			if (path == NULL) {
    112 				error = ENOMEM;
    113 				break;
    114 			}
    115 			*path = 0;
    116 
    117 			dev = (dev_t)0;
    118 			fileid = 0;
    119 			if (UVM_ET_ISOBJ(entry) &&
    120 			    UVM_OBJ_IS_VNODE(entry->object.uvm_obj)) {
    121 				vp = (struct vnode *)entry->object.uvm_obj;
    122 				error = VOP_GETATTR(vp, &va, curp->p_ucred,
    123 				    curp);
    124 				if (error == 0 && vp != pfs->pfs_vnode) {
    125 					fileid = va.va_fileid;
    126 					dev = va.va_fsid;
    127 					error = procfs_vnode_to_path(vp, path,
    128 					    MAXPATHLEN * 4, curp, p);
    129 				}
    130 			}
    131 			snprintf(mebuffer, sizeof(mebuffer),
    132 			    "%0*lx-%0*lx %c%c%c%c %0*lx %02x:%02x %ld     %s\n",
    133 			    (int)sizeof(void *) * 2,(unsigned long)entry->start,
    134 			    (int)sizeof(void *) * 2,(unsigned long)entry->end,
    135 			    (entry->protection & VM_PROT_READ) ? 'r' : '-',
    136 			    (entry->protection & VM_PROT_WRITE) ? 'w' : '-',
    137 			    (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-',
    138 			    (entry->etype & UVM_ET_COPYONWRITE) ? 'p' : 's',
    139 			    (int)sizeof(void *) * 2,
    140 			    (unsigned long)entry->offset,
    141 			    major(dev), minor(dev), fileid, path);
    142 			free(path, M_TEMP);
    143 		} else {
    144 			snprintf(mebuffer, sizeof(mebuffer),
    145 			    "0x%lx 0x%lx %c%c%c %c%c%c %s %s %d %d %d\n",
    146 			    entry->start, entry->end,
    147 			    (entry->protection & VM_PROT_READ) ? 'r' : '-',
    148 			    (entry->protection & VM_PROT_WRITE) ? 'w' : '-',
    149 			    (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-',
    150 			    (entry->max_protection & VM_PROT_READ) ? 'r' : '-',
    151 			    (entry->max_protection & VM_PROT_WRITE) ? 'w' : '-',
    152 			    (entry->max_protection & VM_PROT_EXECUTE) ?
    153 				'x' : '-',
    154 			    (entry->etype & UVM_ET_COPYONWRITE) ?
    155 				"COW" : "NCOW",
    156 			    (entry->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC",
    157 			    entry->inheritance, entry->wired_count,
    158 			    entry->advice);
    159 		}
    160 
    161 		len = strlen(mebuffer);
    162 		if (len > uio->uio_resid) {
    163 			error = EFBIG;
    164 			break;
    165 		}
    166 		error = uiomove(mebuffer, len, uio);
    167 		if (error)
    168 			break;
    169 	}
    170 	if (map != &curproc->p_vmspace->vm_map)
    171 		vm_map_unlock_read(map);
    172 	return error;
    173 }
    174 
    175 int
    176 procfs_validmap(struct lwp *l, struct mount *mp)
    177 {
    178 	return ((l->l_proc->p_flag & P_SYSTEM) == 0);
    179 }
    180 
    181 /*
    182  * Try to find a pathname for a vnode. Since there is no mapping
    183  * vnode -> parent directory, this needs the NAMECACHE_ENTER_REVERSE
    184  * option to work (to make cache_revlookup succeed).
    185  */
    186 static int procfs_vnode_to_path(struct vnode *vp, char *path, int len,
    187 				struct proc *curp, struct proc *p)
    188 {
    189 	int error, lenused, elen;
    190 	char *bp, *bend;
    191 	struct vnode *dvp;
    192 
    193 	bp = bend = &path[len];
    194 	*(--bp) = '\0';
    195 
    196 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
    197 	if (error != 0)
    198 		return error;
    199 	error = cache_revlookup(vp, &dvp, &bp, path);
    200 	vput(vp);
    201 	if (error != 0)
    202 		return (error == -1 ? ENOENT : error);
    203 
    204 	error = vget(dvp, 0);
    205 	if (error != 0)
    206 		return error;
    207 	*(--bp) = '/';
    208 	/* XXX GETCWD_CHECK_ACCESS == 0x0001 */
    209 	error = getcwd_common(dvp, NULL, &bp, path, len / 2, 1, curp);
    210 
    211 	/*
    212 	 * Strip off emulation path for emulated processes looking at
    213 	 * the maps file of a process of the same emulation. (Won't
    214 	 * work if /emul/xxx is a symlink..)
    215 	 */
    216 	if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
    217 		elen = strlen(curp->p_emul->e_path);
    218 		if (!strncmp(bp, curp->p_emul->e_path, elen))
    219 			bp = &bp[elen];
    220 	}
    221 
    222 	lenused = bend - bp;
    223 
    224 	memcpy(path, bp, lenused);
    225 	path[lenused] = 0;
    226 
    227 	return 0;
    228 }
    229