1 /**	$MirOS: src/sys/miscfs/procfs/procfs_subr.c,v 1.4 2012/05/20 15:43:54 tg Exp $ */
2 /*	$OpenBSD: procfs_subr.c,v 1.22 2005/04/16 22:19:28 kettenis Exp $	*/
3 /*	$NetBSD: procfs_subr.c,v 1.15 1996/02/12 15:01:42 christos Exp $	*/
4 
5 /*
6  * Copyright (c) 1993 Jan-Simon Pendry
7  * Copyright (c) 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)procfs_subr.c	8.5 (Berkeley) 6/15/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/time.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/malloc.h>
47 #include <sys/stat.h>
48 
49 #include <miscfs/procfs/procfs.h>
50 
51 extern int allowpsa, allowpse;
52 
53 static TAILQ_HEAD(, pfsnode)	pfshead;
54 struct lock pfs_vlock;
55 
56 /*ARGSUSED*/
57 int
procfs_init(vfsp)58 procfs_init(vfsp)
59 	struct vfsconf *vfsp;
60 
61 {
62 	lockinit(&pfs_vlock, PVFS, "procfsl", 0, 0);
63 	TAILQ_INIT(&pfshead);
64 	return (0);
65 }
66 
67 /*
68  * allocate a pfsnode/vnode pair.  the vnode is
69  * referenced, but not locked.
70  *
71  * the pid, pfs_type, and mount point uniquely
72  * identify a pfsnode.  the mount point is needed
73  * because someone might mount this filesystem
74  * twice.
75  *
76  * all pfsnodes are maintained on a singly-linked
77  * list.  new nodes are only allocated when they cannot
78  * be found on this list.  entries on the list are
79  * removed when the vfs reclaim entry is called.
80  *
81  * a single lock is kept for the entire list.  this is
82  * needed because the getnewvnode() function can block
83  * waiting for a vnode to become free, in which case there
84  * may be more than one process trying to get the same
85  * vnode.  this lock is only taken if we are going to
86  * call getnewvnode, since the kernel itself is single-threaded.
87  *
88  * if an entry is found on the list, then call vget() to
89  * take a reference.  this is done because there may be
90  * zero references to it and so it needs to removed from
91  * the vnode free list.
92  */
93 int
procfs_allocvp(mp,vpp,pid,pfs_type)94 procfs_allocvp(mp, vpp, pid, pfs_type)
95 	struct mount *mp;
96 	struct vnode **vpp;
97 	long pid;
98 	pfstype pfs_type;
99 {
100 	struct proc *p = curproc;
101 	struct pfsnode *pfs;
102 	struct vnode *vp;
103 	int error;
104 
105 	/*
106 	 * Lock the vp list, getnewvnode can sleep.
107 	 */
108 	error = lockmgr(&pfs_vlock, LK_EXCLUSIVE, NULL, p);
109 	if (error)
110 		return (error);
111 loop:
112 	for (pfs = pfshead.tqh_first; pfs != NULL; pfs = pfs->list.tqe_next) {
113 		vp = PFSTOV(pfs);
114 		if (pfs->pfs_pid == pid &&
115 		    pfs->pfs_type == pfs_type &&
116 		    vp->v_mount == mp) {
117 			if (vget(vp, 0, p))
118 				goto loop;
119 			*vpp = vp;
120 			goto out;
121 		}
122 	}
123 
124 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) != 0)
125 		goto out;
126 	vp = *vpp;
127 
128 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
129 	vp->v_data = pfs;
130 
131 	pfs->pfs_pid = (pid_t) pid;
132 	pfs->pfs_type = pfs_type;
133 	pfs->pfs_vnode = vp;
134 	pfs->pfs_flags = 0;
135 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
136 
137 	switch (pfs_type) {
138 	case Proot:	/* /proc = dr-xr-xr-x */
139 		/* sysctl kern.allowpsa=0 changes to dr-x------ */
140 		pfs->pfs_mode = S_IRUSR | S_IXUSR |
141 		    (allowpsa ? S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH : 0);
142 		vp->v_type = VDIR;
143 		vp->v_flag = VROOT;
144 		break;
145 
146 	case Pcurproc:	/* /proc/curproc = lr--r--r-- */
147 	case Pself:	/* /proc/self = lr--r--r-- */
148 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
149 		vp->v_type = VLNK;
150 		break;
151 
152 	case Pproc:	/* /proc/N = dr-xr-xr-x */
153 		/* sysctl kern.allowpse=0 changes to dr-x------ */
154 		pfs->pfs_mode = S_IRUSR | S_IXUSR |
155 		    ((allowpsa || allowpse) ? S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH : 0);
156 		vp->v_type = VDIR;
157 		break;
158 
159 	case Pfile:	/* /proc/N/file = -rw------- */
160 	case Pmem:	/* /proc/N/mem = -rw------- */
161 	case Pregs:	/* /proc/N/regs = -rw------- */
162 	case Pfpregs:	/* /proc/N/fpregs = -rw------- */
163 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
164 		vp->v_type = VREG;
165 		break;
166 
167 	case Pctl:	/* /proc/N/ctl = --w------ */
168 	case Pnote:	/* /proc/N/note = --w------ */
169 	case Pnotepg:	/* /proc/N/notepg = --w------ */
170 		pfs->pfs_mode = S_IWUSR;
171 		vp->v_type = VREG;
172 		break;
173 
174 	case Pstatus:	/* /proc/N/status = -r--r--r-- */
175 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
176 	case Pmeminfo:	/* /proc/meminfo = -r--r--r-- */
177 	case Pcpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
178 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
179 		vp->v_type = VREG;
180 		break;
181 
182 	default:
183 		panic("procfs_allocvp");
184 	}
185 
186 	/* add to procfs vnode list */
187 	TAILQ_INSERT_TAIL(&pfshead, pfs, list);
188 	uvm_vnp_setsize(vp, 0);
189 out:
190 	lockmgr(&pfs_vlock, LK_RELEASE, NULL, p);
191 
192 	return (error);
193 }
194 
195 int
procfs_freevp(vp)196 procfs_freevp(vp)
197 	struct vnode *vp;
198 {
199 	struct pfsnode *pfs = VTOPFS(vp);
200 
201 	TAILQ_REMOVE(&pfshead, pfs, list);
202 	FREE(vp->v_data, M_TEMP);
203 	vp->v_data = 0;
204 	return (0);
205 }
206 
207 int
procfs_rw(v)208 procfs_rw(v)
209 	void *v;
210 {
211 	struct vop_read_args *ap = v;
212 	struct vnode *vp = ap->a_vp;
213 	struct uio *uio = ap->a_uio;
214 	struct proc *curp = uio->uio_procp;
215 	struct pfsnode *pfs = VTOPFS(vp);
216 	struct proc *p;
217 
218 	p = pfind(pfs->pfs_pid);
219 	if (p == 0)
220 		return (EINVAL);
221 	/* Do not permit games to be played with init(8) */
222 	if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
223 		return (EPERM);
224 	if (uio->uio_offset < 0)
225 		return (EINVAL);
226 
227 	switch (pfs->pfs_type) {
228 	case Pnote:
229 	case Pnotepg:
230 		return (procfs_donote(curp, p, pfs, uio));
231 
232 	case Pctl:
233 		return (procfs_doctl(curp, p, pfs, uio));
234 
235 	case Pstatus:
236 		return (procfs_dostatus(curp, p, pfs, uio));
237 
238 	case Pmem:
239 		return (procfs_domem(curp, p, pfs, uio));
240 
241 	case Pcmdline:
242 		return (procfs_docmdline(curp, p, pfs, uio));
243 
244 	case Pmeminfo:
245 		return (procfs_domeminfo(curp, p, pfs, uio));
246 
247 	case Pcpuinfo:
248 		return (procfs_docpuinfo(curp, p, pfs, uio));
249 
250 	default:
251 		return (EOPNOTSUPP);
252 	}
253 }
254 
255 /*
256  * Get a string from userland into (buf).  Strip a trailing
257  * nl character (to allow easy access from the shell).
258  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
259  * will automatically add a nul char at the end.
260  *
261  * Returns 0 on success or the following errors
262  *
263  * EINVAL:    file offset is non-zero.
264  * EMSGSIZE:  message is longer than kernel buffer
265  * EFAULT:    user i/o buffer is not addressable
266  */
267 int
vfs_getuserstr(uio,buf,buflenp)268 vfs_getuserstr(uio, buf, buflenp)
269 	struct uio *uio;
270 	char *buf;
271 	int *buflenp;
272 {
273 	int xlen;
274 	int error;
275 
276 	if (uio->uio_offset != 0)
277 		return (EINVAL);
278 
279 	xlen = *buflenp;
280 
281 	/* must be able to read the whole string in one go */
282 	if (xlen < uio->uio_resid)
283 		return (EMSGSIZE);
284 	xlen = uio->uio_resid;
285 
286 	if ((error = uiomove(buf, xlen, uio)) != 0)
287 		return (error);
288 
289 	/* allow multiple writes without seeks */
290 	uio->uio_offset = 0;
291 
292 	/* cleanup string and remove trailing newline */
293 	buf[xlen] = '\0';
294 	xlen = strlen(buf);
295 	if (xlen > 0 && buf[xlen-1] == '\n')
296 		buf[--xlen] = '\0';
297 	*buflenp = xlen;
298 
299 	return (0);
300 }
301 
302 const vfs_namemap_t *
vfs_findname(nm,buf,buflen)303 vfs_findname(nm, buf, buflen)
304 	const vfs_namemap_t *nm;
305 	char *buf;
306 	int buflen;
307 {
308 
309 	for (; nm->nm_name; nm++)
310 		if (bcmp(buf, nm->nm_name, buflen + 1) == 0)
311 			return (nm);
312 
313 	return (0);
314 }
315