procfs_cmdline.c revision 1.28 1 1.28 joerg /* $NetBSD: procfs_cmdline.c,v 1.28 2011/03/04 22:25:32 joerg 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.28 joerg __KERNEL_RCSID(0, "$NetBSD: procfs_cmdline.c,v 1.28 2011/03/04 22:25:32 joerg 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.28 joerg procfs_docmdline_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.1 christos * code for returning process's command line arguments
62 1.1 christos */
63 1.1 christos int
64 1.22 christos procfs_docmdline(
65 1.23 christos struct lwp *curl,
66 1.22 christos struct proc *p,
67 1.23 christos struct pfsnode *pfs,
68 1.22 christos struct uio *uio
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.2 thorpej 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.28 joerg return (error);
90 1.28 joerg }
91 1.28 joerg len = strlen(p->p_comm);
92 1.28 joerg if (len >= 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.28 joerg return (error);
97 1.5 thorpej }
98 1.28 joerg if (len + 2 >= 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.28 joerg return (error);
103 1.1 christos }
104 1.1 christos
105 1.28 joerg len = uio->uio_offset + uio->uio_resid;
106 1.2 thorpej
107 1.28 joerg error = copy_procargs(p, KERN_PROC_ARGV, &len,
108 1.28 joerg procfs_docmdline_helper, uio);
109 1.28 joerg return error;
110 1.1 christos }
111