Home | History | Annotate | Line # | Download | only in procfs
procfs_cmdline.c revision 1.26.40.1
      1  1.26.40.1      yamt /*	$NetBSD: procfs_cmdline.c,v 1.26.40.1 2008/05/18 12:35:25 yamt 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  *
     20        1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21        1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22        1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23        1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24        1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25        1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26        1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27        1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28        1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29        1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30        1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1  christos  */
     32       1.12     lukem 
     33       1.12     lukem #include <sys/cdefs.h>
     34  1.26.40.1      yamt __KERNEL_RCSID(0, "$NetBSD: procfs_cmdline.c,v 1.26.40.1 2008/05/18 12:35:25 yamt Exp $");
     35        1.1  christos 
     36        1.1  christos #include <sys/param.h>
     37        1.1  christos #include <sys/systm.h>
     38        1.1  christos #include <sys/syslimits.h>
     39        1.1  christos #include <sys/proc.h>
     40        1.1  christos #include <sys/vnode.h>
     41        1.1  christos #include <sys/exec.h>
     42        1.3   thorpej #include <sys/malloc.h>
     43        1.1  christos #include <miscfs/procfs/procfs.h>
     44        1.4       mrg 
     45        1.2   thorpej #include <uvm/uvm_extern.h>
     46        1.2   thorpej 
     47        1.1  christos /*
     48        1.1  christos  * code for returning process's command line arguments
     49        1.1  christos  */
     50        1.1  christos int
     51       1.22  christos procfs_docmdline(
     52       1.23  christos     struct lwp *curl,
     53       1.22  christos     struct proc *p,
     54       1.23  christos     struct pfsnode *pfs,
     55       1.22  christos     struct uio *uio
     56       1.22  christos )
     57        1.1  christos {
     58        1.1  christos 	struct ps_strings pss;
     59       1.15   thorpej 	int count, error;
     60       1.15   thorpej 	size_t i, len, xlen, upper_bound;
     61        1.2   thorpej 	struct uio auio;
     62        1.2   thorpej 	struct iovec aiov;
     63       1.25        ad 	struct vmspace *vm;
     64        1.2   thorpej 	vaddr_t argv;
     65        1.3   thorpej 	char *arg;
     66        1.2   thorpej 
     67        1.2   thorpej 	/* Don't allow writing. */
     68        1.2   thorpej 	if (uio->uio_rw != UIO_READ)
     69        1.2   thorpej 		return (EOPNOTSUPP);
     70        1.2   thorpej 
     71        1.2   thorpej 	/*
     72        1.3   thorpej 	 * Allocate a temporary buffer to hold the arguments.
     73        1.3   thorpej 	 */
     74        1.5   thorpej 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
     75        1.3   thorpej 
     76        1.3   thorpej 	/*
     77        1.2   thorpej 	 * Zombies don't have a stack, so we can't read their psstrings.
     78        1.2   thorpej 	 * System processes also don't have a user stack.  This is what
     79        1.2   thorpej 	 * ps(1) would display.
     80        1.2   thorpej 	 */
     81       1.26     pavel 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
     82       1.24      elad 		len = snprintf(arg, PAGE_SIZE, "(%s)", p->p_comm) + 1;
     83       1.18    itojun 		error = uiomove_frombuf(arg, len, uio);
     84        1.7    simonb 		free(arg, M_TEMP);
     85        1.7    simonb 		return (error);
     86        1.2   thorpej 	}
     87        1.1  christos 
     88        1.2   thorpej 	/*
     89       1.14   thorpej 	 * NOTE: Don't bother doing a process_checkioperm() here
     90        1.2   thorpej 	 * because the psstrings info is available by using ps(1),
     91        1.2   thorpej 	 * so it's not like there's anything to protect here.
     92        1.2   thorpej 	 */
     93        1.2   thorpej 
     94        1.2   thorpej 	/*
     95        1.2   thorpej 	 * Lock the process down in memory.
     96        1.2   thorpej 	 */
     97       1.25        ad 	if ((error = proc_vmspace_getref(p, &vm)) != 0) {
     98        1.8    simonb 		free(arg, M_TEMP);
     99       1.25        ad 		return (error);
    100        1.8    simonb 	}
    101        1.2   thorpej 
    102        1.2   thorpej 	/*
    103        1.2   thorpej 	 * Read in the ps_strings structure.
    104        1.2   thorpej 	 */
    105        1.2   thorpej 	aiov.iov_base = &pss;
    106        1.2   thorpej 	aiov.iov_len = sizeof(pss);
    107        1.2   thorpej 	auio.uio_iov = &aiov;
    108        1.2   thorpej 	auio.uio_iovcnt = 1;
    109       1.11       eeh 	auio.uio_offset = (vaddr_t)p->p_psstr;
    110        1.2   thorpej 	auio.uio_resid = sizeof(pss);
    111        1.2   thorpej 	auio.uio_rw = UIO_READ;
    112       1.21      yamt 	UIO_SETUP_SYSSPACE(&auio);
    113       1.25        ad 	error = uvm_io(&vm->vm_map, &auio);
    114        1.2   thorpej 	if (error)
    115        1.2   thorpej 		goto bad;
    116        1.2   thorpej 
    117        1.2   thorpej 	/*
    118        1.2   thorpej 	 * Now read the address of the argument vector.
    119        1.2   thorpej 	 */
    120        1.2   thorpej 	aiov.iov_base = &argv;
    121        1.2   thorpej 	aiov.iov_len = sizeof(argv);
    122        1.2   thorpej 	auio.uio_iov = &aiov;
    123        1.2   thorpej 	auio.uio_iovcnt = 1;
    124        1.2   thorpej 	auio.uio_offset = (vaddr_t)pss.ps_argvstr;
    125        1.2   thorpej 	auio.uio_resid = sizeof(argv);
    126       1.19     perry 	auio.uio_rw = UIO_READ;
    127       1.21      yamt 	UIO_SETUP_SYSSPACE(&auio);
    128       1.25        ad 	error = uvm_io(&vm->vm_map, &auio);
    129        1.2   thorpej 	if (error)
    130        1.2   thorpej 		goto bad;
    131        1.2   thorpej 
    132        1.2   thorpej 	/*
    133        1.5   thorpej 	 * Now copy in the actual argument vector, one page at a time,
    134        1.2   thorpej 	 * since we don't know how long the vector is (though, we do
    135        1.2   thorpej 	 * know how many NUL-terminated strings are in the vector).
    136        1.2   thorpej 	 */
    137        1.1  christos 	len = 0;
    138        1.1  christos 	count = pss.ps_nargvstr;
    139        1.7    simonb 	upper_bound = round_page(uio->uio_offset + uio->uio_resid);
    140        1.7    simonb 	for (; count && len < upper_bound; len += xlen) {
    141        1.5   thorpej 		aiov.iov_base = arg;
    142        1.5   thorpej 		aiov.iov_len = PAGE_SIZE;
    143        1.2   thorpej 		auio.uio_iov = &aiov;
    144        1.2   thorpej 		auio.uio_iovcnt = 1;
    145        1.2   thorpej 		auio.uio_offset = argv + len;
    146        1.7    simonb 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
    147        1.7    simonb 		auio.uio_resid = xlen;
    148        1.2   thorpej 		auio.uio_rw = UIO_READ;
    149       1.21      yamt 		UIO_SETUP_SYSSPACE(&auio);
    150       1.25        ad 		error = uvm_io(&vm->vm_map, &auio);
    151        1.2   thorpej 		if (error)
    152        1.2   thorpej 			goto bad;
    153        1.2   thorpej 
    154        1.7    simonb 		for (i = 0; i < xlen && count != 0; i++) {
    155        1.5   thorpej 			if (arg[i] == '\0')
    156        1.5   thorpej 				count--;	/* one full string */
    157        1.5   thorpej 		}
    158        1.5   thorpej 
    159        1.7    simonb 		if (len + i > uio->uio_offset) {
    160        1.7    simonb 			/* Have data in this page, copy it out */
    161        1.7    simonb 			error = uiomove(arg + uio->uio_offset - len,
    162        1.7    simonb 			    i + len - uio->uio_offset, uio);
    163        1.7    simonb 			if (error || uio->uio_resid <= 0)
    164        1.7    simonb 				break;
    165        1.5   thorpej 		}
    166        1.1  christos 	}
    167        1.1  christos 
    168        1.7    simonb  bad:
    169        1.2   thorpej 	/*
    170        1.3   thorpej 	 * Release the process.
    171        1.2   thorpej 	 */
    172       1.25        ad 	uvmspace_free(vm);
    173        1.2   thorpej 
    174        1.3   thorpej 	free(arg, M_TEMP);
    175        1.2   thorpej 	return (error);
    176        1.1  christos }
    177