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