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