procfs_cmdline.c revision 1.3 1 /* $NetBSD: procfs_cmdline.c,v 1.3 1999/03/13 22:26:48 thorpej 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/syslimits.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/exec.h>
47 #include <sys/malloc.h>
48 #include <miscfs/procfs/procfs.h>
49
50 #include <vm/vm.h>
51 #if defined(UVM)
52 #include <uvm/uvm_extern.h>
53 #endif
54
55 /*
56 * code for returning process's command line arguments
57 */
58 int
59 procfs_docmdline(curp, p, pfs, uio)
60 struct proc *curp;
61 struct proc *p;
62 struct pfsnode *pfs;
63 struct uio *uio;
64 {
65 struct ps_strings pss;
66 int xlen, len, count, error;
67 struct uio auio;
68 struct iovec aiov;
69 vaddr_t argv;
70 char *arg;
71
72 /* Don't allow writing. */
73 if (uio->uio_rw != UIO_READ)
74 return (EOPNOTSUPP);
75
76 /*
77 * Allocate a temporary buffer to hold the arguments.
78 *
79 * XXX THIS COULD BE HANDLED MUCH MORE INTELLIGENTLY,
80 * XXX WITHOUT REQUIRING A 256k TEMPORARY BUFFER!
81 */
82 arg = malloc(ARG_MAX, M_TEMP, M_WAITOK);
83
84 /*
85 * Zombies don't have a stack, so we can't read their psstrings.
86 * System processes also don't have a user stack. This is what
87 * ps(1) would display.
88 */
89 if (p->p_stat == SZOMB || (p->p_flag & P_SYSTEM) != 0) {
90 snprintf(arg, sizeof(arg), "(%s)", p->p_comm);
91 len = strlen(arg); /* exclude last NUL */
92 goto doio;
93 }
94
95 /*
96 * NOTE: Don't bother doing a procfs_checkioperm() here
97 * because the psstrings info is available by using ps(1),
98 * so it's not like there's anything to protect here.
99 */
100
101 /*
102 * Lock the process down in memory.
103 */
104 #if defined(UVM)
105 /* XXXCDC: how should locking work here? */
106 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
107 return (EFAULT);
108 PHOLD(p);
109 p->p_vmspace->vm_refcnt++; /* XXX */
110 #else
111 PHOLD(p);
112 #endif
113
114 /*
115 * Read in the ps_strings structure.
116 */
117 aiov.iov_base = &pss;
118 aiov.iov_len = sizeof(pss);
119 auio.uio_iov = &aiov;
120 auio.uio_iovcnt = 1;
121 auio.uio_offset = (vaddr_t)PS_STRINGS;
122 auio.uio_resid = sizeof(pss);
123 auio.uio_segflg = UIO_SYSSPACE;
124 auio.uio_rw = UIO_READ;
125 auio.uio_procp = NULL;
126 #if defined(UVM)
127 error = uvm_io(&p->p_vmspace->vm_map, &auio);
128 #else
129 error = procfs_rwmem(p, &auio);
130 #endif
131 if (error)
132 goto bad;
133
134 /*
135 * Now read the address of the argument vector.
136 */
137 aiov.iov_base = &argv;
138 aiov.iov_len = sizeof(argv);
139 auio.uio_iov = &aiov;
140 auio.uio_iovcnt = 1;
141 auio.uio_offset = (vaddr_t)pss.ps_argvstr;
142 auio.uio_resid = sizeof(argv);
143 auio.uio_segflg = UIO_SYSSPACE;
144 auio.uio_rw = UIO_READ;
145 auio.uio_procp = NULL;
146 #if defined(UVM)
147 error = uvm_io(&p->p_vmspace->vm_map, &auio);
148 #else
149 error = procfs_rwmem(p, &auio);
150 #endif
151 if (error)
152 goto bad;
153
154 /*
155 * Now copy in the actual argument vector, one byte at a time,
156 * since we don't know how long the vector is (though, we do
157 * know how many NUL-terminated strings are in the vector).
158 */
159 len = 0;
160 count = pss.ps_nargvstr;
161 while (count && len < ARG_MAX) {
162 aiov.iov_base = &arg[len];
163 aiov.iov_len = 1;
164 auio.uio_iov = &aiov;
165 auio.uio_iovcnt = 1;
166 auio.uio_offset = argv + len;
167 auio.uio_resid = 1;
168 auio.uio_segflg = UIO_SYSSPACE;
169 auio.uio_rw = UIO_READ;
170 auio.uio_procp = NULL;
171 #if defined(UVM)
172 error = uvm_io(&p->p_vmspace->vm_map, &auio);
173 #else
174 error = procfs_rwmem(p, &auio);
175 #endif
176 if (error)
177 goto bad;
178
179 if (len > 0 && arg[len] == '\0')
180 count--; /* one full string */
181 len++;
182 }
183 if (len > 0)
184 len--; /* exclude last NUL */
185
186 /*
187 * Release the process.
188 */
189 #if defined(UVM)
190 PRELE(p);
191 uvmspace_free(p->p_vmspace);
192 #else
193 PRELE(p);
194 #endif
195
196 doio:
197 xlen = len - uio->uio_offset;
198 xlen = imin(xlen, uio->uio_resid);
199 if (xlen <= 0)
200 error = 0;
201 else
202 error = uiomove(arg + uio->uio_offset, xlen, uio);
203
204 free(arg, M_TEMP);
205 return (error);
206
207 bad:
208 #if defined(UVM)
209 PRELE(p);
210 uvmspace_free(p->p_vmspace);
211 #else
212 PRELE(p);
213 #endif
214 free(arg, M_TEMP);
215 return (error);
216 }
217