procfs_cmdline.c revision 1.30 1 /* $NetBSD: procfs_cmdline.c,v 1.30 2017/12/31 03:29:18 christos 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.30 2017/12/31 03:29:18 christos 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
48 static int
49 procfs_doprocargs_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/environment
62 */
63 int
64 procfs_doprocargs(
65 struct lwp *curl,
66 struct proc *p,
67 struct pfsnode *pfs,
68 struct uio *uio,
69 int oid
70 )
71 {
72 size_t len, start;
73 int error;
74
75 /* Don't allow writing. */
76 if (uio->uio_rw != UIO_READ)
77 return EOPNOTSUPP;
78
79 /*
80 * Zombies don't have a stack, so we can't read their psstrings.
81 * System processes also don't have a user stack. This is what
82 * ps(1) would display.
83 */
84 if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
85 static char msg[] = "()";
86 error = 0;
87 if (0 == uio->uio_offset) {
88 error = uiomove(msg, 1, uio);
89 if (error)
90 return error;
91 }
92 len = strlen(p->p_comm);
93 if (len >= uio->uio_offset) {
94 start = uio->uio_offset - 1;
95 error = uiomove(p->p_comm + start, len - start, uio);
96 if (error)
97 return error;
98 }
99 if (len + 2 >= uio->uio_offset) {
100 start = uio->uio_offset - 1 - len;
101 error = uiomove(msg + 1 + start, 2 - start, uio);
102 }
103 return error;
104 }
105
106 len = uio->uio_offset + uio->uio_resid;
107 return copy_procargs(p, oid, &len, procfs_doprocargs_helper, uio);
108 }
109