procfs_cmdline.c revision 1.33 1 1.33 thorpej /* $NetBSD: procfs_cmdline.c,v 1.33 2024/05/18 00:05:50 thorpej 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.33 thorpej __KERNEL_RCSID(0, "$NetBSD: procfs_cmdline.c,v 1.33 2024/05/18 00:05:50 thorpej 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.28 joerg #include <sys/sysctl.h>
43 1.1 christos #include <miscfs/procfs/procfs.h>
44 1.4 mrg
45 1.2 thorpej #include <uvm/uvm_extern.h>
46 1.2 thorpej
47 1.28 joerg static int
48 1.30 christos procfs_doprocargs_helper(void *cookie, const void *src, size_t off, size_t len)
49 1.28 joerg {
50 1.28 joerg struct uio *uio = cookie;
51 1.28 joerg char *buf = __UNCONST(src);
52 1.28 joerg
53 1.28 joerg buf += uio->uio_offset - off;
54 1.32 christos if (off + len <= (uintmax_t)uio->uio_offset)
55 1.28 joerg return 0;
56 1.28 joerg return uiomove(buf, off + len - uio->uio_offset, cookie);
57 1.28 joerg }
58 1.28 joerg
59 1.1 christos /*
60 1.30 christos * code for returning process's command line arguments/environment
61 1.1 christos */
62 1.1 christos int
63 1.30 christos procfs_doprocargs(
64 1.23 christos struct lwp *curl,
65 1.22 christos struct proc *p,
66 1.23 christos struct pfsnode *pfs,
67 1.29 christos struct uio *uio,
68 1.29 christos int oid
69 1.22 christos )
70 1.1 christos {
71 1.28 joerg size_t len, start;
72 1.28 joerg int error;
73 1.2 thorpej
74 1.2 thorpej /* Don't allow writing. */
75 1.2 thorpej if (uio->uio_rw != UIO_READ)
76 1.30 christos return EOPNOTSUPP;
77 1.2 thorpej
78 1.2 thorpej /*
79 1.2 thorpej * Zombies don't have a stack, so we can't read their psstrings.
80 1.2 thorpej * System processes also don't have a user stack. This is what
81 1.2 thorpej * ps(1) would display.
82 1.2 thorpej */
83 1.26 pavel if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
84 1.28 joerg static char msg[] = "()";
85 1.28 joerg error = 0;
86 1.28 joerg if (0 == uio->uio_offset) {
87 1.28 joerg error = uiomove(msg, 1, uio);
88 1.28 joerg if (error)
89 1.30 christos return error;
90 1.28 joerg }
91 1.28 joerg len = strlen(p->p_comm);
92 1.32 christos if (len >= (uintmax_t)uio->uio_offset) {
93 1.28 joerg start = uio->uio_offset - 1;
94 1.28 joerg error = uiomove(p->p_comm + start, len - start, uio);
95 1.28 joerg if (error)
96 1.30 christos return error;
97 1.5 thorpej }
98 1.32 christos if (len + 2 >= (uintmax_t)uio->uio_offset) {
99 1.28 joerg start = uio->uio_offset - 1 - len;
100 1.28 joerg error = uiomove(msg + 1 + start, 2 - start, uio);
101 1.5 thorpej }
102 1.30 christos return error;
103 1.1 christos }
104 1.1 christos
105 1.28 joerg len = uio->uio_offset + uio->uio_resid;
106 1.30 christos return copy_procargs(p, oid, &len, procfs_doprocargs_helper, uio);
107 1.1 christos }
108