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