1 /*
2  * Copyright (c) 1993 The Regents of the University of California.
3  * Copyright (c) 1993 Jan-Simon Pendry
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)procfs_subr.c	8.1 (Berkeley) 01/05/94
12  *
13  * From:
14  *	$Id: procfs_subr.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
15  */
16 
17 #include <sys/param.h>
18 #include <sys/systm.h>
19 #include <sys/time.h>
20 #include <sys/kernel.h>
21 #include <sys/proc.h>
22 #include <sys/vnode.h>
23 #include <sys/malloc.h>
24 #include <miscfs/procfs/procfs.h>
25 
26 static struct pfsnode *pfshead;
27 static int pfsvplock;
28 
29 /*
30  * allocate a pfsnode/vnode pair.  the vnode is
31  * referenced, but not locked.
32  *
33  * the pid, pfs_type, and mount point uniquely
34  * identify a pfsnode.  the mount point is needed
35  * because someone might mount this filesystem
36  * twice.
37  *
38  * all pfsnodes are maintained on a singly-linked
39  * list.  new nodes are only allocated when they cannot
40  * be found on this list.  entries on the list are
41  * removed when the vfs reclaim entry is called.
42  *
43  * a single lock is kept for the entire list.  this is
44  * needed because the getnewvnode() function can block
45  * waiting for a vnode to become free, in which case there
46  * may be more than one process trying to get the same
47  * vnode.  this lock is only taken if we are going to
48  * call getnewvnode, since the kernel itself is single-threaded.
49  *
50  * if an entry is found on the list, then call vget() to
51  * take a reference.  this is done because there may be
52  * zero references to it and so it needs to removed from
53  * the vnode free list.
54  */
55 int
56 procfs_allocvp(mp, vpp, pid, pfs_type)
57 	struct mount *mp;
58 	struct vnode **vpp;
59 	long pid;
60 	pfstype pfs_type;
61 {
62 	int error;
63 	struct pfsnode *pfs;
64 	struct pfsnode **pp;
65 
66 loop:
67 	for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
68 		if (pfs->pfs_pid == pid &&
69 		    pfs->pfs_type == pfs_type &&
70 		    PFSTOV(pfs)->v_mount == mp) {
71 			if (vget(pfs->pfs_vnode, 1))
72 				goto loop;
73 			VOP_UNLOCK(pfs->pfs_vnode);
74 			*vpp = pfs->pfs_vnode;
75 			return (0);
76 		}
77 	}
78 
79 	/*
80 	 * otherwise lock the vp list while we call getnewvnode
81 	 * since that can block.
82 	 */
83 	if (pfsvplock & PROCFS_LOCKED) {
84 		pfsvplock |= PROCFS_WANT;
85 		sleep((caddr_t) &pfsvplock, PINOD);
86 		goto loop;
87 	}
88 	pfsvplock |= PROCFS_LOCKED;
89 
90 	error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp);
91 	if (error)
92 		goto out;
93 
94 	MALLOC((*vpp)->v_data, void *, sizeof(struct pfsnode),
95 		M_TEMP, M_WAITOK);
96 
97 	pfs = VTOPFS(*vpp);
98 	pfs->pfs_next = 0;
99 	pfs->pfs_pid = (pid_t) pid;
100 	pfs->pfs_type = pfs_type;
101 	pfs->pfs_vnode = *vpp;
102 	pfs->pfs_flags = 0;
103 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
104 
105 	switch (pfs_type) {
106 	case Proot:	/* /proc = dr-xr-xr-x */
107 		pfs->pfs_mode = (VREAD|VEXEC) |
108 				(VREAD|VEXEC) >> 3 |
109 				(VREAD|VEXEC) >> 6;
110 		break;
111 
112 	case Pproc:
113 		pfs->pfs_mode = (VREAD|VEXEC) |
114 				(VREAD|VEXEC) >> 3 |
115 				(VREAD|VEXEC) >> 6;
116 		break;
117 
118 	case Pfile:
119 		pfs->pfs_mode = (VREAD|VWRITE);
120 		break;
121 
122 	case Pmem:
123 		pfs->pfs_mode = (VREAD|VWRITE);
124 		break;
125 
126 	case Pregs:
127 		pfs->pfs_mode = (VREAD|VWRITE);
128 		break;
129 
130 	case Pctl:
131 		pfs->pfs_mode = (VWRITE);
132 		break;
133 
134 	case Pstatus:
135 		pfs->pfs_mode = (VREAD) |
136 				(VREAD >> 3) |
137 				(VREAD >> 6);
138 		break;
139 
140 	case Pnote:
141 		pfs->pfs_mode = (VWRITE);
142 		break;
143 
144 	case Pnotepg:
145 		pfs->pfs_mode = (VWRITE);
146 		break;
147 
148 	default:
149 		panic("procfs_allocvp");
150 	}
151 
152 	/* add to procfs vnode list */
153 	for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
154 		continue;
155 	*pp = pfs;
156 
157 out:
158 	pfsvplock &= ~PROCFS_LOCKED;
159 
160 	if (pfsvplock & PROCFS_WANT) {
161 		pfsvplock &= ~PROCFS_WANT;
162 		wakeup((caddr_t) &pfsvplock);
163 	}
164 
165 	return (error);
166 }
167 
168 int
169 procfs_freevp(vp)
170 	struct vnode *vp;
171 {
172 	struct pfsnode **pfspp;
173 	struct pfsnode *pfs = VTOPFS(vp);
174 
175 	for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
176 		if (*pfspp == pfs) {
177 			*pfspp = pfs->pfs_next;
178 			break;
179 		}
180 	}
181 
182 	FREE(vp->v_data, M_TEMP);
183 	vp->v_data = 0;
184 	return (0);
185 }
186 
187 int
188 procfs_rw(ap)
189 	struct vop_read_args *ap;
190 {
191 	struct vnode *vp = ap->a_vp;
192 	struct uio *uio = ap->a_uio;
193 	struct proc *curp = uio->uio_procp;
194 	struct pfsnode *pfs = VTOPFS(vp);
195 	struct proc *p;
196 
197 	p = PFIND(pfs->pfs_pid);
198 	if (p == 0)
199 		return (EINVAL);
200 
201 	switch (pfs->pfs_type) {
202 	case Pnote:
203 	case Pnotepg:
204 		return (procfs_donote(curp, p, pfs, uio));
205 
206 	case Pregs:
207 		return (procfs_doregs(curp, p, pfs, uio));
208 
209 	case Pctl:
210 		return (procfs_doctl(curp, p, pfs, uio));
211 
212 	case Pstatus:
213 		return (procfs_dostatus(curp, p, pfs, uio));
214 
215 	case Pmem:
216 		return (procfs_domem(curp, p, pfs, uio));
217 
218 	default:
219 		return (EOPNOTSUPP);
220 	}
221 }
222 
223 /*
224  * Get a string from userland into (buf).  Strip a trailing
225  * nl character (to allow easy access from the shell).
226  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
227  * will automatically add a nul char at the end.
228  *
229  * Returns 0 on success or the following errors
230  *
231  * EINVAL:    file offset is non-zero.
232  * EMSGSIZE:  message is longer than kernel buffer
233  * EFAULT:    user i/o buffer is not addressable
234  */
235 int
236 vfs_getuserstr(uio, buf, buflenp)
237 	struct uio *uio;
238 	char *buf;
239 	int *buflenp;
240 {
241 	int xlen;
242 	int error;
243 
244 	if (uio->uio_offset != 0)
245 		return (EINVAL);
246 
247 	xlen = *buflenp;
248 
249 	/* must be able to read the whole string in one go */
250 	if (xlen < uio->uio_resid)
251 		return (EMSGSIZE);
252 	xlen = uio->uio_resid;
253 
254 	error = uiomove(buf, xlen, uio);
255 	if (error)
256 		return (error);
257 
258 	/* allow multiple writes without seeks */
259 	uio->uio_offset = 0;
260 
261 	/* cleanup string and remove trailing newline */
262 	buf[xlen] = '\0';
263 	xlen = strlen(buf);
264 	if (xlen > 0 && buf[xlen-1] == '\n')
265 		buf[--xlen] = '\0';
266 	*buflenp = xlen;
267 
268 	return (0);
269 }
270 
271 vfs_namemap_t *
272 vfs_findname(nm, buf, buflen)
273 	vfs_namemap_t *nm;
274 	char *buf;
275 	int buflen;
276 {
277 	for (; nm->nm_name; nm++)
278 		if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0)
279 			return (nm);
280 
281 	return (0);
282 }
283