Home | History | Annotate | Line # | Download | only in procfs
procfs_cmdline.c revision 1.6.2.1
      1  1.6.2.1    bouyer /*	$NetBSD: procfs_cmdline.c,v 1.6.2.1 2000/11/20 18:09:49 bouyer Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 1999 Jaromir Dolecek <dolecek (at) ics.muni.cz>
      5      1.1  christos  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      6      1.1  christos  * All rights reserved.
      7      1.1  christos  *
      8      1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      9      1.1  christos  * by Jaromir Dolecek.
     10      1.1  christos  *
     11      1.1  christos  * Redistribution and use in source and binary forms, with or without
     12      1.1  christos  * modification, are permitted provided that the following conditions
     13      1.1  christos  * are met:
     14      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18      1.1  christos  *    documentation and/or other materials provided with the distribution.
     19      1.1  christos  * 3. All advertising materials mentioning features or use of this software
     20      1.1  christos  *    must display the following acknowledgement:
     21      1.1  christos  *	This product includes software developed by the NetBSD
     22      1.1  christos  *	Foundation, Inc. and its contributors.
     23      1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.1  christos  *    contributors may be used to endorse or promote products derived
     25      1.1  christos  *    from this software without specific prior written permission.
     26      1.1  christos  *
     27      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     38      1.1  christos  */
     39      1.1  christos 
     40      1.1  christos #include <sys/param.h>
     41      1.1  christos #include <sys/types.h>
     42      1.1  christos #include <sys/systm.h>
     43      1.1  christos #include <sys/syslimits.h>
     44      1.1  christos #include <sys/proc.h>
     45      1.1  christos #include <sys/vnode.h>
     46      1.1  christos #include <sys/exec.h>
     47      1.3   thorpej #include <sys/malloc.h>
     48      1.1  christos #include <miscfs/procfs/procfs.h>
     49      1.1  christos 
     50      1.2   thorpej #include <uvm/uvm_extern.h>
     51      1.2   thorpej 
     52      1.1  christos /*
     53      1.1  christos  * code for returning process's command line arguments
     54      1.1  christos  */
     55      1.1  christos int
     56      1.1  christos procfs_docmdline(curp, p, pfs, uio)
     57      1.1  christos 	struct proc *curp;
     58      1.1  christos 	struct proc *p;
     59      1.1  christos 	struct pfsnode *pfs;
     60      1.1  christos 	struct uio *uio;
     61      1.1  christos {
     62      1.1  christos 	struct ps_strings pss;
     63  1.6.2.1    bouyer 	int count, error, i;
     64  1.6.2.1    bouyer 	size_t len, xlen, upper_bound;
     65      1.2   thorpej 	struct uio auio;
     66      1.2   thorpej 	struct iovec aiov;
     67      1.2   thorpej 	vaddr_t argv;
     68      1.3   thorpej 	char *arg;
     69      1.2   thorpej 
     70      1.2   thorpej 	/* Don't allow writing. */
     71      1.2   thorpej 	if (uio->uio_rw != UIO_READ)
     72      1.2   thorpej 		return (EOPNOTSUPP);
     73      1.2   thorpej 
     74      1.2   thorpej 	/*
     75      1.3   thorpej 	 * Allocate a temporary buffer to hold the arguments.
     76      1.3   thorpej 	 */
     77      1.5   thorpej 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
     78      1.3   thorpej 
     79      1.3   thorpej 	/*
     80      1.2   thorpej 	 * Zombies don't have a stack, so we can't read their psstrings.
     81      1.2   thorpej 	 * System processes also don't have a user stack.  This is what
     82      1.2   thorpej 	 * ps(1) would display.
     83      1.2   thorpej 	 */
     84      1.6   thorpej 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0) {
     85      1.5   thorpej 		len = snprintf(arg, PAGE_SIZE, "(%s)", p->p_comm);
     86  1.6.2.1    bouyer 		xlen = len - uio->uio_offset;
     87  1.6.2.1    bouyer 		if (xlen <= 0)
     88  1.6.2.1    bouyer 			error = 0;
     89  1.6.2.1    bouyer 		else
     90  1.6.2.1    bouyer 			error = uiomove(arg, xlen, uio);
     91  1.6.2.1    bouyer 
     92  1.6.2.1    bouyer 		free(arg, M_TEMP);
     93  1.6.2.1    bouyer 		return (error);
     94      1.2   thorpej 	}
     95      1.1  christos 
     96      1.2   thorpej 	/*
     97      1.2   thorpej 	 * NOTE: Don't bother doing a procfs_checkioperm() here
     98      1.2   thorpej 	 * because the psstrings info is available by using ps(1),
     99      1.2   thorpej 	 * so it's not like there's anything to protect here.
    100      1.2   thorpej 	 */
    101      1.2   thorpej 
    102      1.2   thorpej 	/*
    103      1.2   thorpej 	 * Lock the process down in memory.
    104      1.2   thorpej 	 */
    105      1.2   thorpej 	/* XXXCDC: how should locking work here? */
    106  1.6.2.1    bouyer 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1)) {
    107  1.6.2.1    bouyer 		free(arg, M_TEMP);
    108      1.2   thorpej 		return (EFAULT);
    109  1.6.2.1    bouyer 	}
    110      1.2   thorpej 	p->p_vmspace->vm_refcnt++;	/* XXX */
    111      1.2   thorpej 
    112      1.2   thorpej 	/*
    113      1.2   thorpej 	 * Read in the ps_strings structure.
    114      1.2   thorpej 	 */
    115      1.2   thorpej 	aiov.iov_base = &pss;
    116      1.2   thorpej 	aiov.iov_len = sizeof(pss);
    117      1.2   thorpej 	auio.uio_iov = &aiov;
    118      1.2   thorpej 	auio.uio_iovcnt = 1;
    119  1.6.2.1    bouyer 	auio.uio_offset = (vaddr_t)p->p_psstr;
    120      1.2   thorpej 	auio.uio_resid = sizeof(pss);
    121      1.2   thorpej 	auio.uio_segflg = UIO_SYSSPACE;
    122      1.2   thorpej 	auio.uio_rw = UIO_READ;
    123      1.2   thorpej 	auio.uio_procp = NULL;
    124      1.2   thorpej 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
    125      1.2   thorpej 	if (error)
    126      1.2   thorpej 		goto bad;
    127      1.2   thorpej 
    128      1.2   thorpej 	/*
    129      1.2   thorpej 	 * Now read the address of the argument vector.
    130      1.2   thorpej 	 */
    131      1.2   thorpej 	aiov.iov_base = &argv;
    132      1.2   thorpej 	aiov.iov_len = sizeof(argv);
    133      1.2   thorpej 	auio.uio_iov = &aiov;
    134      1.2   thorpej 	auio.uio_iovcnt = 1;
    135      1.2   thorpej 	auio.uio_offset = (vaddr_t)pss.ps_argvstr;
    136      1.2   thorpej 	auio.uio_resid = sizeof(argv);
    137      1.2   thorpej 	auio.uio_segflg = UIO_SYSSPACE;
    138      1.2   thorpej 	auio.uio_rw = UIO_READ;
    139      1.2   thorpej 	auio.uio_procp = NULL;
    140      1.2   thorpej 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
    141      1.2   thorpej 	if (error)
    142      1.2   thorpej 		goto bad;
    143      1.2   thorpej 
    144      1.2   thorpej 	/*
    145      1.5   thorpej 	 * Now copy in the actual argument vector, one page at a time,
    146      1.2   thorpej 	 * since we don't know how long the vector is (though, we do
    147      1.2   thorpej 	 * know how many NUL-terminated strings are in the vector).
    148      1.2   thorpej 	 */
    149      1.1  christos 	len = 0;
    150      1.1  christos 	count = pss.ps_nargvstr;
    151  1.6.2.1    bouyer 	upper_bound = round_page(uio->uio_offset + uio->uio_resid);
    152  1.6.2.1    bouyer 	for (; count && len < upper_bound; len += xlen) {
    153      1.5   thorpej 		aiov.iov_base = arg;
    154      1.5   thorpej 		aiov.iov_len = PAGE_SIZE;
    155      1.2   thorpej 		auio.uio_iov = &aiov;
    156      1.2   thorpej 		auio.uio_iovcnt = 1;
    157      1.2   thorpej 		auio.uio_offset = argv + len;
    158  1.6.2.1    bouyer 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
    159  1.6.2.1    bouyer 		auio.uio_resid = xlen;
    160      1.2   thorpej 		auio.uio_segflg = UIO_SYSSPACE;
    161      1.2   thorpej 		auio.uio_rw = UIO_READ;
    162      1.2   thorpej 		auio.uio_procp = NULL;
    163      1.2   thorpej 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
    164      1.2   thorpej 		if (error)
    165      1.2   thorpej 			goto bad;
    166      1.2   thorpej 
    167  1.6.2.1    bouyer 		for (i = 0; i < xlen && count != 0; i++) {
    168      1.5   thorpej 			if (arg[i] == '\0')
    169      1.5   thorpej 				count--;	/* one full string */
    170      1.5   thorpej 		}
    171      1.5   thorpej 
    172  1.6.2.1    bouyer 		if (count == 0)
    173  1.6.2.1    bouyer 			i--;		/* exclude the final NUL */
    174  1.6.2.1    bouyer 
    175  1.6.2.1    bouyer 		if (len + i > uio->uio_offset) {
    176  1.6.2.1    bouyer 			/* Have data in this page, copy it out */
    177  1.6.2.1    bouyer 			error = uiomove(arg + uio->uio_offset - len,
    178  1.6.2.1    bouyer 			    i + len - uio->uio_offset, uio);
    179  1.6.2.1    bouyer 			if (error || uio->uio_resid <= 0)
    180  1.6.2.1    bouyer 				break;
    181      1.5   thorpej 		}
    182      1.1  christos 	}
    183      1.1  christos 
    184  1.6.2.1    bouyer  bad:
    185      1.2   thorpej 	/*
    186      1.3   thorpej 	 * Release the process.
    187      1.2   thorpej 	 */
    188      1.2   thorpej 	uvmspace_free(p->p_vmspace);
    189      1.2   thorpej 
    190      1.3   thorpej 	free(arg, M_TEMP);
    191      1.2   thorpej 	return (error);
    192      1.1  christos }
    193