1 /* $NetBSD: procfs_cmdline.c,v 1.33 2024/05/18 00:05:50 thorpej 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.33 2024/05/18 00:05:50 thorpej 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/sysctl.h>
43 #include <miscfs/procfs/procfs.h>
44
45 #include <uvm/uvm_extern.h>
46
47 static int
procfs_doprocargs_helper(void * cookie,const void * src,size_t off,size_t len)48 procfs_doprocargs_helper(void *cookie, const void *src, size_t off, size_t len)
49 {
50 struct uio *uio = cookie;
51 char *buf = __UNCONST(src);
52
53 buf += uio->uio_offset - off;
54 if (off + len <= (uintmax_t)uio->uio_offset)
55 return 0;
56 return uiomove(buf, off + len - uio->uio_offset, cookie);
57 }
58
59 /*
60 * code for returning process's command line arguments/environment
61 */
62 int
procfs_doprocargs(struct lwp * curl,struct proc * p,struct pfsnode * pfs,struct uio * uio,int oid)63 procfs_doprocargs(
64 struct lwp *curl,
65 struct proc *p,
66 struct pfsnode *pfs,
67 struct uio *uio,
68 int oid
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 >= (uintmax_t)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 >= (uintmax_t)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 return copy_procargs(p, oid, &len, procfs_doprocargs_helper, uio);
107 }
108