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