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