Home | History | Annotate | Line # | Download | only in procfs
procfs_cmdline.c revision 1.30
      1  1.30  christos /*	$NetBSD: procfs_cmdline.c,v 1.30 2017/12/31 03:29:18 christos 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.30  christos __KERNEL_RCSID(0, "$NetBSD: procfs_cmdline.c,v 1.30 2017/12/31 03:29:18 christos 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.28     joerg #include <sys/sysctl.h>
     44   1.1  christos #include <miscfs/procfs/procfs.h>
     45   1.4       mrg 
     46   1.2   thorpej #include <uvm/uvm_extern.h>
     47   1.2   thorpej 
     48  1.28     joerg static int
     49  1.30  christos procfs_doprocargs_helper(void *cookie, const void *src, size_t off, size_t len)
     50  1.28     joerg {
     51  1.28     joerg 	struct uio *uio = cookie;
     52  1.28     joerg 	char *buf = __UNCONST(src);
     53  1.28     joerg 
     54  1.28     joerg 	buf += uio->uio_offset - off;
     55  1.28     joerg 	if (off + len <= uio->uio_offset)
     56  1.28     joerg 		return 0;
     57  1.28     joerg 	return uiomove(buf, off + len - uio->uio_offset, cookie);
     58  1.28     joerg }
     59  1.28     joerg 
     60   1.1  christos /*
     61  1.30  christos  * code for returning process's command line arguments/environment
     62   1.1  christos  */
     63   1.1  christos int
     64  1.30  christos procfs_doprocargs(
     65  1.23  christos     struct lwp *curl,
     66  1.22  christos     struct proc *p,
     67  1.23  christos     struct pfsnode *pfs,
     68  1.29  christos     struct uio *uio,
     69  1.29  christos     int oid
     70  1.22  christos )
     71   1.1  christos {
     72  1.28     joerg 	size_t len, start;
     73  1.28     joerg 	int error;
     74   1.2   thorpej 
     75   1.2   thorpej 	/* Don't allow writing. */
     76   1.2   thorpej 	if (uio->uio_rw != UIO_READ)
     77  1.30  christos 		return EOPNOTSUPP;
     78   1.2   thorpej 
     79   1.2   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.26     pavel 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
     85  1.28     joerg 		static char msg[] = "()";
     86  1.28     joerg 		error = 0;
     87  1.28     joerg 		if (0 == uio->uio_offset) {
     88  1.28     joerg 			error = uiomove(msg, 1, uio);
     89  1.28     joerg 			if (error)
     90  1.30  christos 				return error;
     91  1.28     joerg 		}
     92  1.28     joerg 		len = strlen(p->p_comm);
     93  1.28     joerg 		if (len >= uio->uio_offset) {
     94  1.28     joerg 			start = uio->uio_offset - 1;
     95  1.28     joerg 			error = uiomove(p->p_comm + start, len - start, uio);
     96  1.28     joerg 			if (error)
     97  1.30  christos 				return error;
     98   1.5   thorpej 		}
     99  1.28     joerg 		if (len + 2 >= uio->uio_offset) {
    100  1.28     joerg 			start = uio->uio_offset - 1 - len;
    101  1.28     joerg 			error = uiomove(msg + 1 + start, 2 - start, uio);
    102   1.5   thorpej 		}
    103  1.30  christos 		return error;
    104   1.1  christos 	}
    105   1.1  christos 
    106  1.28     joerg 	len = uio->uio_offset + uio->uio_resid;
    107  1.30  christos 	return copy_procargs(p, oid, &len, procfs_doprocargs_helper, uio);
    108   1.1  christos }
    109