xref: /netbsd-src/sys/miscfs/procfs/procfs_cmdline.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: procfs_cmdline.c,v 1.26 2007/02/17 22:31:44 pavel 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/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: procfs_cmdline.c,v 1.26 2007/02/17 22:31:44 pavel Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/syslimits.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/exec.h>
49 #include <sys/malloc.h>
50 #include <miscfs/procfs/procfs.h>
51 
52 #include <uvm/uvm_extern.h>
53 
54 /*
55  * code for returning process's command line arguments
56  */
57 int
58 procfs_docmdline(
59     struct lwp *curl,
60     struct proc *p,
61     struct pfsnode *pfs,
62     struct uio *uio
63 )
64 {
65 	struct ps_strings pss;
66 	int count, error;
67 	size_t i, len, xlen, upper_bound;
68 	struct uio auio;
69 	struct iovec aiov;
70 	struct vmspace *vm;
71 	vaddr_t argv;
72 	char *arg;
73 
74 	/* Don't allow writing. */
75 	if (uio->uio_rw != UIO_READ)
76 		return (EOPNOTSUPP);
77 
78 	/*
79 	 * Allocate a temporary buffer to hold the arguments.
80 	 */
81 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
82 
83 	/*
84 	 * Zombies don't have a stack, so we can't read their psstrings.
85 	 * System processes also don't have a user stack.  This is what
86 	 * ps(1) would display.
87 	 */
88 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
89 		len = snprintf(arg, PAGE_SIZE, "(%s)", p->p_comm) + 1;
90 		error = uiomove_frombuf(arg, len, uio);
91 		free(arg, M_TEMP);
92 		return (error);
93 	}
94 
95 	/*
96 	 * NOTE: Don't bother doing a process_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 ((error = proc_vmspace_getref(p, &vm)) != 0) {
105 		free(arg, M_TEMP);
106 		return (error);
107 	}
108 
109 	/*
110 	 * Read in the ps_strings structure.
111 	 */
112 	aiov.iov_base = &pss;
113 	aiov.iov_len = sizeof(pss);
114 	auio.uio_iov = &aiov;
115 	auio.uio_iovcnt = 1;
116 	auio.uio_offset = (vaddr_t)p->p_psstr;
117 	auio.uio_resid = sizeof(pss);
118 	auio.uio_rw = UIO_READ;
119 	UIO_SETUP_SYSSPACE(&auio);
120 	error = uvm_io(&vm->vm_map, &auio);
121 	if (error)
122 		goto bad;
123 
124 	/*
125 	 * Now read the address of the argument vector.
126 	 */
127 	aiov.iov_base = &argv;
128 	aiov.iov_len = sizeof(argv);
129 	auio.uio_iov = &aiov;
130 	auio.uio_iovcnt = 1;
131 	auio.uio_offset = (vaddr_t)pss.ps_argvstr;
132 	auio.uio_resid = sizeof(argv);
133 	auio.uio_rw = UIO_READ;
134 	UIO_SETUP_SYSSPACE(&auio);
135 	error = uvm_io(&vm->vm_map, &auio);
136 	if (error)
137 		goto bad;
138 
139 	/*
140 	 * Now copy in the actual argument vector, one page at a time,
141 	 * since we don't know how long the vector is (though, we do
142 	 * know how many NUL-terminated strings are in the vector).
143 	 */
144 	len = 0;
145 	count = pss.ps_nargvstr;
146 	upper_bound = round_page(uio->uio_offset + uio->uio_resid);
147 	for (; count && len < upper_bound; len += xlen) {
148 		aiov.iov_base = arg;
149 		aiov.iov_len = PAGE_SIZE;
150 		auio.uio_iov = &aiov;
151 		auio.uio_iovcnt = 1;
152 		auio.uio_offset = argv + len;
153 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
154 		auio.uio_resid = xlen;
155 		auio.uio_rw = UIO_READ;
156 		UIO_SETUP_SYSSPACE(&auio);
157 		error = uvm_io(&vm->vm_map, &auio);
158 		if (error)
159 			goto bad;
160 
161 		for (i = 0; i < xlen && count != 0; i++) {
162 			if (arg[i] == '\0')
163 				count--;	/* one full string */
164 		}
165 
166 		if (len + i > uio->uio_offset) {
167 			/* Have data in this page, copy it out */
168 			error = uiomove(arg + uio->uio_offset - len,
169 			    i + len - uio->uio_offset, uio);
170 			if (error || uio->uio_resid <= 0)
171 				break;
172 		}
173 	}
174 
175  bad:
176 	/*
177 	 * Release the process.
178 	 */
179 	uvmspace_free(vm);
180 
181 	free(arg, M_TEMP);
182 	return (error);
183 }
184