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