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