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