1/*	$NetBSD: procfs_cmdline.c,v 1.27 2008/04/28 20:24:08 martin Exp $	*/
2
3/*
4 * Copyright (c) 1999 Jaromir Dolecek <dolecek@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.27 2008/04/28 20:24:08 martin 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 <sys/sysctl.h>
44#include <miscfs/procfs/procfs.h>
45
46#include <uvm/uvm_extern.h>
47
48static int
49procfs_docmdline_helper(void *cookie, const void *src, size_t off, size_t len)
50{
51	struct uio *uio = cookie;
52	char *buf = __UNCONST(src);
53
54	buf += uio->uio_offset - off;
55	if (off + len <= uio->uio_offset)
56		return 0;
57	return uiomove(buf, off + len - uio->uio_offset, cookie);
58}
59
60/*
61 * code for returning process's command line arguments
62 */
63int
64procfs_docmdline(
65    struct lwp *curl,
66    struct proc *p,
67    struct pfsnode *pfs,
68    struct uio *uio
69)
70{
71	size_t len, start;
72	int error;
73
74	/* Don't allow writing. */
75	if (uio->uio_rw != UIO_READ)
76		return (EOPNOTSUPP);
77
78	/*
79	 * Zombies don't have a stack, so we can't read their psstrings.
80	 * System processes also don't have a user stack.  This is what
81	 * ps(1) would display.
82	 */
83	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
84		static char msg[] = "()";
85		error = 0;
86		if (0 == uio->uio_offset) {
87			error = uiomove(msg, 1, uio);
88			if (error)
89				return (error);
90		}
91		len = strlen(p->p_comm);
92		if (len >= uio->uio_offset) {
93			start = uio->uio_offset - 1;
94			error = uiomove(p->p_comm + start, len - start, uio);
95			if (error)
96				return (error);
97		}
98		if (len + 2 >= uio->uio_offset) {
99			start = uio->uio_offset - 1 - len;
100			error = uiomove(msg + 1 + start, 2 - start, uio);
101		}
102		return (error);
103	}
104
105	len = uio->uio_offset + uio->uio_resid;
106
107	error = copy_procargs(p, KERN_PROC_ARGV, &len,
108	    procfs_docmdline_helper, uio);
109	return error;
110}
111