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