xref: /dflybsd-src/sys/vfs/procfs/procfs_subr.c (revision bd4539cc23771f3c0b3fae4ecf80e725b613b305)
1 /*
2  * Copyright (c) 1993 Jan-Simon Pendry
3  * Copyright (c) 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. 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.6 (Berkeley) 5/14/95
38  *
39  * $FreeBSD: src/sys/miscfs/procfs/procfs_subr.c,v 1.26.2.3 2002/02/18 21:28:04 des Exp $
40  * $DragonFly: src/sys/vfs/procfs/procfs_subr.c,v 1.6 2004/03/01 06:33:22 dillon Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysctl.h>
46 #include <sys/proc.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 
51 #include <vfs/procfs/procfs.h>
52 
53 static struct pfsnode *pfshead;
54 static int pfsvplock;
55 
56 /*
57  * allocate a pfsnode/vnode pair.  the vnode is
58  * referenced, but not locked.
59  *
60  * the pid, pfs_type, and mount point uniquely
61  * identify a pfsnode.  the mount point is needed
62  * because someone might mount this filesystem
63  * twice.
64  *
65  * all pfsnodes are maintained on a singly-linked
66  * list.  new nodes are only allocated when they cannot
67  * be found on this list.  entries on the list are
68  * removed when the vfs reclaim entry is called.
69  *
70  * a single lock is kept for the entire list.  this is
71  * needed because the getnewvnode() function can block
72  * waiting for a vnode to become free, in which case there
73  * may be more than one process trying to get the same
74  * vnode.  this lock is only taken if we are going to
75  * call getnewvnode, since the kernel itself is single-threaded.
76  *
77  * if an entry is found on the list, then call vget() to
78  * take a reference.  this is done because there may be
79  * zero references to it and so it needs to removed from
80  * the vnode free list.
81  */
82 int
83 procfs_allocvp(mp, vpp, pid, pfs_type)
84 	struct mount *mp;
85 	struct vnode **vpp;
86 	long pid;
87 	pfstype pfs_type;
88 {
89 	struct thread *td = curthread;	/* XXX */
90 	struct pfsnode *pfs;
91 	struct vnode *vp;
92 	struct pfsnode **pp;
93 	int error;
94 
95 loop:
96 	for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
97 		vp = PFSTOV(pfs);
98 		if (pfs->pfs_pid == pid &&
99 		    pfs->pfs_type == pfs_type &&
100 		    vp->v_mount == mp) {
101 			if (vget(vp, NULL, 0, td))
102 				goto loop;
103 			*vpp = vp;
104 			return (0);
105 		}
106 	}
107 
108 	/*
109 	 * otherwise lock the vp list while we call getnewvnode
110 	 * since that can block.
111 	 */
112 	if (pfsvplock & PROCFS_LOCKED) {
113 		pfsvplock |= PROCFS_WANT;
114 		(void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
115 		goto loop;
116 	}
117 	pfsvplock |= PROCFS_LOCKED;
118 
119 	/*
120 	 * Do the MALLOC before the getnewvnode since doing so afterward
121 	 * might cause a bogus v_data pointer to get dereferenced
122 	 * elsewhere if MALLOC should block.
123 	 */
124 	MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
125 
126 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) != 0) {
127 		FREE(pfs, M_TEMP);
128 		goto out;
129 	}
130 	vp = *vpp;
131 
132 	vp->v_data = pfs;
133 
134 	pfs->pfs_next = 0;
135 	pfs->pfs_pid = (pid_t) pid;
136 	pfs->pfs_type = pfs_type;
137 	pfs->pfs_vnode = vp;
138 	pfs->pfs_flags = 0;
139 	pfs->pfs_lockowner = 0;
140 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
141 
142 	switch (pfs_type) {
143 	case Proot:	/* /proc = dr-xr-xr-x */
144 		pfs->pfs_mode = (VREAD|VEXEC) |
145 				(VREAD|VEXEC) >> 3 |
146 				(VREAD|VEXEC) >> 6;
147 		vp->v_type = VDIR;
148 		vp->v_flag = VROOT;
149 		break;
150 
151 	case Pcurproc:	/* /proc/curproc = lr--r--r-- */
152 		pfs->pfs_mode = (VREAD) |
153 				(VREAD >> 3) |
154 				(VREAD >> 6);
155 		vp->v_type = VLNK;
156 		break;
157 
158 	case Pproc:
159 		pfs->pfs_mode = (VREAD|VEXEC) |
160 				(VREAD|VEXEC) >> 3 |
161 				(VREAD|VEXEC) >> 6;
162 		vp->v_type = VDIR;
163 		break;
164 
165 	case Pfile:
166 		pfs->pfs_mode = (VREAD|VEXEC) |
167 				(VREAD|VEXEC) >> 3 |
168 				(VREAD|VEXEC) >> 6;
169 		vp->v_type = VLNK;
170 		break;
171 
172 	case Pmem:
173 		pfs->pfs_mode = (VREAD|VWRITE);
174 		vp->v_type = VREG;
175 		break;
176 
177 	case Pregs:
178 	case Pfpregs:
179 	case Pdbregs:
180 		pfs->pfs_mode = (VREAD|VWRITE);
181 		vp->v_type = VREG;
182 		break;
183 
184 	case Pctl:
185 	case Pnote:
186 	case Pnotepg:
187 		pfs->pfs_mode = (VWRITE);
188 		vp->v_type = VREG;
189 		break;
190 
191 	case Ptype:
192 	case Pmap:
193 	case Pstatus:
194 	case Pcmdline:
195 	case Prlimit:
196 		pfs->pfs_mode = (VREAD) |
197 				(VREAD >> 3) |
198 				(VREAD >> 6);
199 		vp->v_type = VREG;
200 		break;
201 
202 	default:
203 		panic("procfs_allocvp");
204 	}
205 
206 	/* add to procfs vnode list */
207 	for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
208 		continue;
209 	*pp = pfs;
210 
211 out:
212 	pfsvplock &= ~PROCFS_LOCKED;
213 
214 	if (pfsvplock & PROCFS_WANT) {
215 		pfsvplock &= ~PROCFS_WANT;
216 		wakeup((caddr_t) &pfsvplock);
217 	}
218 
219 	return (error);
220 }
221 
222 int
223 procfs_freevp(vp)
224 	struct vnode *vp;
225 {
226 	struct pfsnode **pfspp;
227 	struct pfsnode *pfs = VTOPFS(vp);
228 
229 	for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
230 		if (*pfspp == pfs) {
231 			*pfspp = pfs->pfs_next;
232 			break;
233 		}
234 	}
235 
236 	FREE(vp->v_data, M_TEMP);
237 	vp->v_data = 0;
238 	return (0);
239 }
240 
241 int
242 procfs_rw(struct vop_read_args *ap)
243 {
244 	struct vnode *vp = ap->a_vp;
245 	struct uio *uio = ap->a_uio;
246 	struct thread *curtd = uio->uio_td;
247 	struct proc *curp;
248 	struct pfsnode *pfs = VTOPFS(vp);
249 	struct proc *p;
250 	int rtval;
251 
252 	if (curtd == NULL)
253 		return (EINVAL);
254 	if ((curp = curtd->td_proc) == NULL)	/* XXX */
255 		return (EINVAL);
256 
257 	p = PFIND(pfs->pfs_pid);
258 	if (p == NULL)
259 		return (EINVAL);
260 	if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
261 		return (EACCES);
262 
263 	while (pfs->pfs_lockowner) {
264 		tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
265 	}
266 	pfs->pfs_lockowner = curproc->p_pid;
267 
268 	switch (pfs->pfs_type) {
269 	case Pnote:
270 	case Pnotepg:
271 		rtval = procfs_donote(curp, p, pfs, uio);
272 		break;
273 
274 	case Pregs:
275 		rtval = procfs_doregs(curp, p, pfs, uio);
276 		break;
277 
278 	case Pfpregs:
279 		rtval = procfs_dofpregs(curp, p, pfs, uio);
280 		break;
281 
282         case Pdbregs:
283                 rtval = procfs_dodbregs(curp, p, pfs, uio);
284                 break;
285 
286 	case Pctl:
287 		rtval = procfs_doctl(curp, p, pfs, uio);
288 		break;
289 
290 	case Pstatus:
291 		rtval = procfs_dostatus(curp, p, pfs, uio);
292 		break;
293 
294 	case Pmap:
295 		rtval = procfs_domap(curp, p, pfs, uio);
296 		break;
297 
298 	case Pmem:
299 		rtval = procfs_domem(curp, p, pfs, uio);
300 		break;
301 
302 	case Ptype:
303 		rtval = procfs_dotype(curp, p, pfs, uio);
304 		break;
305 
306 	case Pcmdline:
307 		rtval = procfs_docmdline(curp, p, pfs, uio);
308 		break;
309 
310 	case Prlimit:
311 		rtval = procfs_dorlimit(curp, p, pfs, uio);
312 		break;
313 
314 	default:
315 		rtval = EOPNOTSUPP;
316 		break;
317 	}
318 	pfs->pfs_lockowner = 0;
319 	wakeup(&pfs->pfs_lockowner);
320 	return rtval;
321 }
322 
323 /*
324  * Get a string from userland into (buf).  Strip a trailing
325  * nl character (to allow easy access from the shell).
326  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
327  * will automatically add a nul char at the end.
328  *
329  * Returns 0 on success or the following errors
330  *
331  * EINVAL:    file offset is non-zero.
332  * EMSGSIZE:  message is longer than kernel buffer
333  * EFAULT:    user i/o buffer is not addressable
334  */
335 int
336 vfs_getuserstr(uio, buf, buflenp)
337 	struct uio *uio;
338 	char *buf;
339 	int *buflenp;
340 {
341 	int xlen;
342 	int error;
343 
344 	if (uio->uio_offset != 0)
345 		return (EINVAL);
346 
347 	xlen = *buflenp;
348 
349 	/* must be able to read the whole string in one go */
350 	if (xlen < uio->uio_resid)
351 		return (EMSGSIZE);
352 	xlen = uio->uio_resid;
353 
354 	if ((error = uiomove(buf, xlen, uio)) != 0)
355 		return (error);
356 
357 	/* allow multiple writes without seeks */
358 	uio->uio_offset = 0;
359 
360 	/* cleanup string and remove trailing newline */
361 	buf[xlen] = '\0';
362 	xlen = strlen(buf);
363 	if (xlen > 0 && buf[xlen-1] == '\n')
364 		buf[--xlen] = '\0';
365 	*buflenp = xlen;
366 
367 	return (0);
368 }
369 
370 vfs_namemap_t *
371 vfs_findname(nm, buf, buflen)
372 	vfs_namemap_t *nm;
373 	char *buf;
374 	int buflen;
375 {
376 
377 	for (; nm->nm_name; nm++)
378 		if (bcmp(buf, nm->nm_name, buflen+1) == 0)
379 			return (nm);
380 
381 	return (0);
382 }
383 
384 void
385 procfs_exit(struct thread *td)
386 {
387 	struct pfsnode *pfs;
388 	pid_t pid;
389 
390 	KKASSERT(td->td_proc);
391 	pid = td->td_proc->p_pid;
392 
393 	/*
394 	 * The reason for this loop is not obvious -- basicly,
395 	 * procfs_freevp(), which is called via vgone() (eventually),
396 	 * removes the specified procfs node from the pfshead list.
397 	 * It does this by *pfsp = pfs->pfs_next, meaning that it
398 	 * overwrites the node.  So when we do pfs = pfs->next, we
399 	 * end up skipping the node that replaces the one that was
400 	 * vgone'd.  Since it may have been the last one on the list,
401 	 * it may also have been set to null -- but *our* pfs pointer,
402 	 * here, doesn't see this.  So the loop starts from the beginning
403 	 * again.
404 	 *
405 	 * This is not a for() loop because the final event
406 	 * would be "pfs = pfs->pfs_next"; in the case where
407 	 * pfs is set to pfshead again, that would mean that
408 	 * pfshead is skipped over.
409 	 *
410 	 */
411 	pfs = pfshead;
412 	while (pfs) {
413 		if (pfs->pfs_pid == pid) {
414 			vgone(PFSTOV(pfs));
415 			pfs = pfshead;
416 		} else
417 			pfs = pfs->pfs_next;
418 	}
419 }
420