1 /* $NetBSD: procfs_cmdline.c,v 1.26 2007/02/17 22:31:44 pavel Exp $ */
4 * Copyright (c) 1999 Jaromir Dolecek <dolecek@ics.muni.cz>
5 * Copyright (c) 1999 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: procfs_cmdline.c,v 1.26 2007/02/17 22:31:44 pavel Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/syslimits.h>
40 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 #include <miscfs/procfs/procfs.h>
45 #include <uvm/uvm_extern.h>
48 * code for returning process's command line arguments
58 struct ps_strings pss
;
60 size_t i
, len
, xlen
, upper_bound
;
67 /* Don't allow writing. */
68 if (uio
->uio_rw
!= UIO_READ
)
72 * Allocate a temporary buffer to hold the arguments.
74 arg
= malloc(PAGE_SIZE
, M_TEMP
, M_WAITOK
);
77 * Zombies don't have a stack, so we can't read their psstrings.
78 * System processes also don't have a user stack. This is what
79 * ps(1) would display.
81 if (P_ZOMBIE(p
) || (p
->p_flag
& PK_SYSTEM
) != 0) {
82 len
= snprintf(arg
, PAGE_SIZE
, "(%s)", p
->p_comm
) + 1;
83 error
= uiomove_frombuf(arg
, len
, uio
);
89 * NOTE: Don't bother doing a process_checkioperm() here
90 * because the psstrings info is available by using ps(1),
91 * so it's not like there's anything to protect here.
95 * Lock the process down in memory.
97 if ((error
= proc_vmspace_getref(p
, &vm
)) != 0) {
103 * Read in the ps_strings structure.
105 aiov
.iov_base
= &pss
;
106 aiov
.iov_len
= sizeof(pss
);
107 auio
.uio_iov
= &aiov
;
109 auio
.uio_offset
= (vaddr_t
)p
->p_psstr
;
110 auio
.uio_resid
= sizeof(pss
);
111 auio
.uio_rw
= UIO_READ
;
112 UIO_SETUP_SYSSPACE(&auio
);
113 error
= uvm_io(&vm
->vm_map
, &auio
);
118 * Now read the address of the argument vector.
120 aiov
.iov_base
= &argv
;
121 aiov
.iov_len
= sizeof(argv
);
122 auio
.uio_iov
= &aiov
;
124 auio
.uio_offset
= (vaddr_t
)pss
.ps_argvstr
;
125 auio
.uio_resid
= sizeof(argv
);
126 auio
.uio_rw
= UIO_READ
;
127 UIO_SETUP_SYSSPACE(&auio
);
128 error
= uvm_io(&vm
->vm_map
, &auio
);
133 * Now copy in the actual argument vector, one page at a time,
134 * since we don't know how long the vector is (though, we do
135 * know how many NUL-terminated strings are in the vector).
138 count
= pss
.ps_nargvstr
;
139 upper_bound
= round_page(uio
->uio_offset
+ uio
->uio_resid
);
140 for (; count
&& len
< upper_bound
; len
+= xlen
) {
142 aiov
.iov_len
= PAGE_SIZE
;
143 auio
.uio_iov
= &aiov
;
145 auio
.uio_offset
= argv
+ len
;
146 xlen
= PAGE_SIZE
- ((argv
+ len
) & PAGE_MASK
);
147 auio
.uio_resid
= xlen
;
148 auio
.uio_rw
= UIO_READ
;
149 UIO_SETUP_SYSSPACE(&auio
);
150 error
= uvm_io(&vm
->vm_map
, &auio
);
154 for (i
= 0; i
< xlen
&& count
!= 0; i
++) {
156 count
--; /* one full string */
159 if (len
+ i
> uio
->uio_offset
) {
160 /* Have data in this page, copy it out */
161 error
= uiomove(arg
+ uio
->uio_offset
- len
,
162 i
+ len
- uio
->uio_offset
, uio
);
163 if (error
|| uio
->uio_resid
<= 0)
170 * Release the process.