xref: /netbsd-src/sys/miscfs/procfs/procfs_cmdline.c (revision 4865d6ff88e74736c85bdbe96dc6c1104f18db44)
1 /*	$NetBSD: procfs_cmdline.c,v 1.2 1999/03/13 01:01:30 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  * 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 <miscfs/procfs/procfs.h>
48 
49 #include <vm/vm.h>
50 #if defined(UVM)
51 #include <uvm/uvm_extern.h>
52 #endif
53 
54 /*
55  * code for returning process's command line arguments
56  */
57 int
58 procfs_docmdline(curp, p, pfs, uio)
59 	struct proc *curp;
60 	struct proc *p;
61 	struct pfsnode *pfs;
62 	struct uio *uio;
63 {
64 	struct ps_strings pss;
65 	char arg[ARG_MAX];
66 	int xlen, len, count, error;
67 	struct uio auio;
68 	struct iovec aiov;
69 	vaddr_t argv;
70 
71 	/* Don't allow writing. */
72 	if (uio->uio_rw != UIO_READ)
73 		return (EOPNOTSUPP);
74 
75 	/*
76 	 * Zombies don't have a stack, so we can't read their psstrings.
77 	 * System processes also don't have a user stack.  This is what
78 	 * ps(1) would display.
79 	 */
80 	if (p->p_stat == SZOMB || (p->p_flag & P_SYSTEM) != 0) {
81 		snprintf(arg, sizeof(arg), "(%s)", p->p_comm);
82 		len = strlen(arg);	/* exclude last NUL */
83 		goto doio;
84 	}
85 
86 	/*
87 	 * NOTE: Don't bother doing a procfs_checkioperm() here
88 	 * because the psstrings info is available by using ps(1),
89 	 * so it's not like there's anything to protect here.
90 	 */
91 
92 	/*
93 	 * Lock the process down in memory.
94 	 */
95 #if defined(UVM)
96 	/* XXXCDC: how should locking work here? */
97 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
98 		return (EFAULT);
99 	PHOLD(p);
100 	p->p_vmspace->vm_refcnt++;	/* XXX */
101 #else
102 	PHOLD(p);
103 #endif
104 
105 	/*
106 	 * Read in the ps_strings structure.
107 	 */
108 	aiov.iov_base = &pss;
109 	aiov.iov_len = sizeof(pss);
110 	auio.uio_iov = &aiov;
111 	auio.uio_iovcnt = 1;
112 	auio.uio_offset = (vaddr_t)PS_STRINGS;
113 	auio.uio_resid = sizeof(pss);
114 	auio.uio_segflg = UIO_SYSSPACE;
115 	auio.uio_rw = UIO_READ;
116 	auio.uio_procp = NULL;
117 #if defined(UVM)
118 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
119 #else
120 	error = procfs_rwmem(p, &auio);
121 #endif
122 	if (error)
123 		goto bad;
124 
125 	/*
126 	 * Now read the address of the argument vector.
127 	 */
128 	aiov.iov_base = &argv;
129 	aiov.iov_len = sizeof(argv);
130 	auio.uio_iov = &aiov;
131 	auio.uio_iovcnt = 1;
132 	auio.uio_offset = (vaddr_t)pss.ps_argvstr;
133 	auio.uio_resid = sizeof(argv);
134 	auio.uio_segflg = UIO_SYSSPACE;
135 	auio.uio_rw = UIO_READ;
136 	auio.uio_procp = NULL;
137 #if defined(UVM)
138 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
139 #else
140 	error = procfs_rwmem(p, &auio);
141 #endif
142 	if (error)
143 		goto bad;
144 
145 	/*
146 	 * Now copy in the actual argument vector, one byte at a time,
147 	 * since we don't know how long the vector is (though, we do
148 	 * know how many NUL-terminated strings are in the vector).
149 	 */
150 	len = 0;
151 	count = pss.ps_nargvstr;
152 	while (count && len < ARG_MAX) {
153 		aiov.iov_base = &arg[len];
154 		aiov.iov_len = 1;
155 		auio.uio_iov = &aiov;
156 		auio.uio_iovcnt = 1;
157 		auio.uio_offset = argv + len;
158 		auio.uio_resid = 1;
159 		auio.uio_segflg = UIO_SYSSPACE;
160 		auio.uio_rw = UIO_READ;
161 		auio.uio_procp = NULL;
162 #if defined(UVM)
163 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
164 #else
165 		error = procfs_rwmem(p, &auio);
166 #endif
167 		if (error)
168 			goto bad;
169 
170 		if (len > 0 && arg[len] == '\0')
171 			count--;	/* one full string */
172 		len++;
173 	}
174 	if (len > 0)
175 		len--;			/* exclude last NUL */
176 
177 	/*
178 	 * No longer need the process to be locked down.
179 	 */
180 #if defined(UVM)
181 	PRELE(p);
182 	uvmspace_free(p->p_vmspace);
183 #else
184 	PRELE(p);
185 #endif
186 
187  doio:
188 	xlen = len - uio->uio_offset;
189 	xlen = imin(xlen, uio->uio_resid);
190 	if (xlen <= 0)
191 		return 0;
192 	else
193 		return (uiomove(arg + uio->uio_offset, xlen, uio));
194 
195  bad:
196 #if defined(UVM)
197 	PRELE(p);
198 	uvmspace_free(p->p_vmspace);
199 #else
200 	PRELE(p);
201 #endif
202 	return (error);
203 }
204