putter.c revision 1.4.2.4 1 1.4.2.4 jmcneill /* $NetBSD: putter.c,v 1.4.2.4 2007/12/09 19:37:56 jmcneill Exp $ */
2 1.4.2.2 joerg
3 1.4.2.2 joerg /*
4 1.4.2.2 joerg * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
5 1.4.2.2 joerg *
6 1.4.2.2 joerg * Development of this software was supported by the
7 1.4.2.2 joerg * Ulla Tuominen Foundation and the Finnish Cultural Foundation and the
8 1.4.2.2 joerg * Research Foundation of Helsinki University of Technology
9 1.4.2.2 joerg *
10 1.4.2.2 joerg * Redistribution and use in source and binary forms, with or without
11 1.4.2.2 joerg * modification, are permitted provided that the following conditions
12 1.4.2.2 joerg * are met:
13 1.4.2.2 joerg * 1. Redistributions of source code must retain the above copyright
14 1.4.2.2 joerg * notice, this list of conditions and the following disclaimer.
15 1.4.2.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
16 1.4.2.2 joerg * notice, this list of conditions and the following disclaimer in the
17 1.4.2.2 joerg * documentation and/or other materials provided with the distribution.
18 1.4.2.2 joerg *
19 1.4.2.2 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.4.2.2 joerg * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.4.2.2 joerg * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 1.4.2.2 joerg * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 1.4.2.2 joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.4.2.2 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.4.2.2 joerg * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.4.2.2 joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.4.2.2 joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.4.2.2 joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.4.2.2 joerg * SUCH DAMAGE.
30 1.4.2.2 joerg */
31 1.4.2.2 joerg
32 1.4.2.2 joerg /*
33 1.4.2.2 joerg * Pass-to-Userspace TransporTER: generic kernel-user request-response
34 1.4.2.2 joerg * transport interface.
35 1.4.2.2 joerg */
36 1.4.2.2 joerg
37 1.4.2.2 joerg #include <sys/cdefs.h>
38 1.4.2.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: putter.c,v 1.4.2.4 2007/12/09 19:37:56 jmcneill Exp $");
39 1.4.2.2 joerg
40 1.4.2.2 joerg #include <sys/param.h>
41 1.4.2.2 joerg #include <sys/conf.h>
42 1.4.2.2 joerg #include <sys/file.h>
43 1.4.2.2 joerg #include <sys/filedesc.h>
44 1.4.2.2 joerg #include <sys/kmem.h>
45 1.4.2.2 joerg #include <sys/poll.h>
46 1.4.2.2 joerg #include <sys/socketvar.h>
47 1.4.2.2 joerg
48 1.4.2.2 joerg #include <dev/putter/putter_sys.h>
49 1.4.2.2 joerg
50 1.4.2.2 joerg /*
51 1.4.2.3 joerg * Configuration data.
52 1.4.2.3 joerg *
53 1.4.2.3 joerg * This is static-size for now. Will be redone for devfs.
54 1.4.2.3 joerg */
55 1.4.2.3 joerg
56 1.4.2.3 joerg #define PUTTER_CONFSIZE 16
57 1.4.2.3 joerg
58 1.4.2.3 joerg static struct putter_config {
59 1.4.2.3 joerg int pc_minor;
60 1.4.2.3 joerg int (*pc_config)(int, int, int);
61 1.4.2.3 joerg } putterconf[PUTTER_CONFSIZE];
62 1.4.2.3 joerg
63 1.4.2.3 joerg static int
64 1.4.2.3 joerg putter_configure(dev_t dev, int flags, int fmt, int fd)
65 1.4.2.3 joerg {
66 1.4.2.3 joerg struct putter_config *pc;
67 1.4.2.3 joerg
68 1.4.2.3 joerg /* are we the catch-all node? */
69 1.4.2.3 joerg if (minor(dev) == PUTTER_MINOR_WILDCARD
70 1.4.2.3 joerg || minor(dev) == PUTTER_MINOR_COMPAT)
71 1.4.2.3 joerg return 0;
72 1.4.2.3 joerg
73 1.4.2.3 joerg /* nopes? try to configure us */
74 1.4.2.3 joerg for (pc = putterconf; pc->pc_config; pc++)
75 1.4.2.3 joerg if (minor(dev) == pc->pc_minor)
76 1.4.2.3 joerg return pc->pc_config(fd, flags, fmt);
77 1.4.2.3 joerg return ENXIO;
78 1.4.2.3 joerg }
79 1.4.2.3 joerg
80 1.4.2.3 joerg int
81 1.4.2.3 joerg putter_register(putter_config_fn pcfn, int minor)
82 1.4.2.3 joerg {
83 1.4.2.3 joerg int i;
84 1.4.2.3 joerg
85 1.4.2.3 joerg for (i = 0; i < PUTTER_CONFSIZE; i++)
86 1.4.2.3 joerg if (putterconf[i].pc_config == NULL)
87 1.4.2.3 joerg break;
88 1.4.2.3 joerg if (i == PUTTER_CONFSIZE)
89 1.4.2.3 joerg return EBUSY;
90 1.4.2.3 joerg
91 1.4.2.3 joerg putterconf[i].pc_minor = minor;
92 1.4.2.3 joerg putterconf[i].pc_config = pcfn;
93 1.4.2.3 joerg return 0;
94 1.4.2.3 joerg }
95 1.4.2.3 joerg
96 1.4.2.3 joerg /*
97 1.4.2.2 joerg * putter instance structures. these are always allocated and freed
98 1.4.2.2 joerg * from the context of the transport user.
99 1.4.2.2 joerg */
100 1.4.2.2 joerg struct putter_instance {
101 1.4.2.2 joerg pid_t pi_pid;
102 1.4.2.2 joerg int pi_idx;
103 1.4.2.2 joerg int pi_fd;
104 1.4.2.2 joerg struct selinfo pi_sel;
105 1.4.2.2 joerg
106 1.4.2.2 joerg void *pi_private;
107 1.4.2.2 joerg struct putter_ops *pi_pop;
108 1.4.2.2 joerg
109 1.4.2.2 joerg uint8_t *pi_curput;
110 1.4.2.2 joerg size_t pi_curres;
111 1.4.2.2 joerg void *pi_curopaq;
112 1.4.2.2 joerg
113 1.4.2.2 joerg TAILQ_ENTRY(putter_instance) pi_entries;
114 1.4.2.2 joerg };
115 1.4.2.2 joerg #define PUTTER_EMBRYO ((void *)-1) /* before attach */
116 1.4.2.2 joerg #define PUTTER_DEAD ((void *)-2) /* after detach */
117 1.4.2.2 joerg
118 1.4.2.2 joerg static TAILQ_HEAD(, putter_instance) putter_ilist
119 1.4.2.2 joerg = TAILQ_HEAD_INITIALIZER(putter_ilist);
120 1.4.2.2 joerg
121 1.4.2.2 joerg static int get_pi_idx(struct putter_instance *);
122 1.4.2.2 joerg
123 1.4.2.2 joerg #ifdef DEBUG
124 1.4.2.2 joerg #ifndef PUTTERDEBUG
125 1.4.2.2 joerg #define PUTTERDEBUG
126 1.4.2.2 joerg #endif
127 1.4.2.2 joerg #endif
128 1.4.2.2 joerg
129 1.4.2.2 joerg #ifdef PUTTERDEBUG
130 1.4.2.2 joerg static int putterdebug = 0;
131 1.4.2.2 joerg #define DPRINTF(x) if (putterdebug > 0) printf x
132 1.4.2.2 joerg #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
133 1.4.2.2 joerg #else
134 1.4.2.2 joerg #define DPRINTF(x)
135 1.4.2.2 joerg #define DPRINTF_VERBOSE(x)
136 1.4.2.2 joerg #endif
137 1.4.2.2 joerg
138 1.4.2.2 joerg /*
139 1.4.2.2 joerg * public init / deinit
140 1.4.2.2 joerg */
141 1.4.2.2 joerg
142 1.4.2.2 joerg /* protects both the list and the contents of the list elements */
143 1.4.2.2 joerg static kmutex_t pi_mtx;
144 1.4.2.2 joerg
145 1.4.2.2 joerg void putterattach(void);
146 1.4.2.2 joerg
147 1.4.2.2 joerg void
148 1.4.2.2 joerg putterattach()
149 1.4.2.2 joerg {
150 1.4.2.2 joerg
151 1.4.2.2 joerg mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE);
152 1.4.2.2 joerg }
153 1.4.2.2 joerg
154 1.4.2.2 joerg #if 0
155 1.4.2.2 joerg void
156 1.4.2.2 joerg putter_destroy()
157 1.4.2.2 joerg {
158 1.4.2.2 joerg
159 1.4.2.2 joerg mutex_destroy(&pi_mtx);
160 1.4.2.2 joerg }
161 1.4.2.2 joerg #endif
162 1.4.2.2 joerg
163 1.4.2.2 joerg /*
164 1.4.2.2 joerg * fd routines, for cloner
165 1.4.2.2 joerg */
166 1.4.2.2 joerg static int putter_fop_read(struct file *, off_t *, struct uio *,
167 1.4.2.2 joerg kauth_cred_t, int);
168 1.4.2.2 joerg static int putter_fop_write(struct file *, off_t *, struct uio *,
169 1.4.2.2 joerg kauth_cred_t, int);
170 1.4.2.2 joerg static int putter_fop_ioctl(struct file*, u_long, void *, struct lwp *);
171 1.4.2.2 joerg static int putter_fop_poll(struct file *, int, struct lwp *);
172 1.4.2.2 joerg static int putter_fop_close(struct file *, struct lwp *);
173 1.4.2.2 joerg static int putter_fop_kqfilter(struct file *, struct knote *);
174 1.4.2.2 joerg
175 1.4.2.2 joerg
176 1.4.2.2 joerg static const struct fileops putter_fileops = {
177 1.4.2.2 joerg putter_fop_read,
178 1.4.2.2 joerg putter_fop_write,
179 1.4.2.2 joerg putter_fop_ioctl,
180 1.4.2.2 joerg fnullop_fcntl,
181 1.4.2.2 joerg putter_fop_poll,
182 1.4.2.2 joerg fbadop_stat,
183 1.4.2.2 joerg putter_fop_close,
184 1.4.2.2 joerg putter_fop_kqfilter
185 1.4.2.2 joerg };
186 1.4.2.2 joerg
187 1.4.2.2 joerg static int
188 1.4.2.2 joerg putter_fop_read(struct file *fp, off_t *off, struct uio *uio,
189 1.4.2.2 joerg kauth_cred_t cred, int flags)
190 1.4.2.2 joerg {
191 1.4.2.2 joerg struct putter_instance *pi = fp->f_data;
192 1.4.2.2 joerg size_t origres, moved;
193 1.4.2.2 joerg int error;
194 1.4.2.2 joerg
195 1.4.2.2 joerg if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
196 1.4.2.2 joerg printf("putter_fop_read: private %d not inited\n", pi->pi_idx);
197 1.4.2.2 joerg return ENOENT;
198 1.4.2.2 joerg }
199 1.4.2.2 joerg
200 1.4.2.2 joerg if (pi->pi_curput == NULL) {
201 1.4.2.2 joerg error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
202 1.4.2.2 joerg fp->f_flag & O_NONBLOCK, &pi->pi_curput,
203 1.4.2.2 joerg &pi->pi_curres, &pi->pi_curopaq);
204 1.4.2.2 joerg if (error)
205 1.4.2.2 joerg return error;
206 1.4.2.2 joerg }
207 1.4.2.2 joerg
208 1.4.2.2 joerg origres = uio->uio_resid;
209 1.4.2.2 joerg error = uiomove(pi->pi_curput, pi->pi_curres, uio);
210 1.4.2.2 joerg moved = origres - uio->uio_resid;
211 1.4.2.2 joerg DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
212 1.4.2.2 joerg pi, moved, pi->pi_curput, error));
213 1.4.2.2 joerg
214 1.4.2.2 joerg KASSERT(pi->pi_curres >= moved);
215 1.4.2.2 joerg pi->pi_curres -= moved;
216 1.4.2.2 joerg pi->pi_curput += moved;
217 1.4.2.2 joerg
218 1.4.2.2 joerg if (pi->pi_curres == 0) {
219 1.4.2.2 joerg pi->pi_pop->pop_releaseout(pi->pi_private,
220 1.4.2.2 joerg pi->pi_curopaq, error);
221 1.4.2.2 joerg pi->pi_curput = NULL;
222 1.4.2.2 joerg }
223 1.4.2.2 joerg
224 1.4.2.2 joerg return error;
225 1.4.2.2 joerg }
226 1.4.2.2 joerg
227 1.4.2.2 joerg static int
228 1.4.2.2 joerg putter_fop_write(struct file *fp, off_t *off, struct uio *uio,
229 1.4.2.2 joerg kauth_cred_t cred, int flags)
230 1.4.2.2 joerg {
231 1.4.2.2 joerg struct putter_instance *pi = fp->f_data;
232 1.4.2.2 joerg struct putter_hdr pth;
233 1.4.2.2 joerg uint8_t *buf;
234 1.4.2.2 joerg size_t frsize;
235 1.4.2.2 joerg int error;
236 1.4.2.2 joerg
237 1.4.2.2 joerg DPRINTF(("putter_fop_write (%p): writing response, resid %zu\n",
238 1.4.2.2 joerg pi->pi_private, uio->uio_resid));
239 1.4.2.2 joerg
240 1.4.2.2 joerg if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
241 1.4.2.2 joerg printf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
242 1.4.2.2 joerg return ENOENT;
243 1.4.2.2 joerg }
244 1.4.2.2 joerg
245 1.4.2.2 joerg error = uiomove(&pth, sizeof(struct putter_hdr), uio);
246 1.4.2.2 joerg if (error)
247 1.4.2.2 joerg return error;
248 1.4.2.2 joerg
249 1.4.2.2 joerg /* Sorry mate, the kernel doesn't buffer. */
250 1.4.2.2 joerg frsize = pth.pth_framelen - sizeof(struct putter_hdr);
251 1.4.2.2 joerg if (uio->uio_resid < frsize)
252 1.4.2.2 joerg return EINVAL;
253 1.4.2.2 joerg
254 1.4.2.2 joerg buf = kmem_alloc(frsize + sizeof(struct putter_hdr), KM_SLEEP);
255 1.4.2.2 joerg memcpy(buf, &pth, sizeof(pth));
256 1.4.2.2 joerg error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio);
257 1.4.2.2 joerg if (error == 0) {
258 1.4.2.2 joerg pi->pi_pop->pop_dispatch(pi->pi_private,
259 1.4.2.2 joerg (struct putter_hdr *)buf);
260 1.4.2.2 joerg }
261 1.4.2.2 joerg kmem_free(buf, frsize + sizeof(struct putter_hdr));
262 1.4.2.2 joerg
263 1.4.2.2 joerg return error;
264 1.4.2.2 joerg }
265 1.4.2.2 joerg
266 1.4.2.2 joerg /*
267 1.4.2.2 joerg * Poll query interface. The question is only if an event
268 1.4.2.2 joerg * can be read from us.
269 1.4.2.2 joerg */
270 1.4.2.2 joerg #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
271 1.4.2.2 joerg static int
272 1.4.2.2 joerg putter_fop_poll(struct file *fp, int events, struct lwp *l)
273 1.4.2.2 joerg {
274 1.4.2.2 joerg struct putter_instance *pi = fp->f_data;
275 1.4.2.2 joerg int revents;
276 1.4.2.2 joerg
277 1.4.2.2 joerg if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
278 1.4.2.2 joerg printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
279 1.4.2.2 joerg return ENOENT;
280 1.4.2.2 joerg }
281 1.4.2.2 joerg
282 1.4.2.2 joerg revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
283 1.4.2.2 joerg if ((events & PUTTERPOLL_EVSET) == 0)
284 1.4.2.2 joerg return revents;
285 1.4.2.2 joerg
286 1.4.2.2 joerg /* check queue */
287 1.4.2.2 joerg if (pi->pi_pop->pop_waitcount(pi->pi_private))
288 1.4.2.2 joerg revents |= PUTTERPOLL_EVSET;
289 1.4.2.2 joerg else
290 1.4.2.2 joerg selrecord(l, &pi->pi_sel);
291 1.4.2.2 joerg
292 1.4.2.2 joerg return revents;
293 1.4.2.2 joerg }
294 1.4.2.2 joerg
295 1.4.2.2 joerg /*
296 1.4.2.2 joerg * device close = forced unmount.
297 1.4.2.2 joerg *
298 1.4.2.2 joerg * unmounting is a frightfully complex operation to avoid races
299 1.4.2.2 joerg */
300 1.4.2.2 joerg static int
301 1.4.2.2 joerg putter_fop_close(struct file *fp, struct lwp *l)
302 1.4.2.2 joerg {
303 1.4.2.2 joerg struct putter_instance *pi = fp->f_data;
304 1.4.2.2 joerg int rv;
305 1.4.2.2 joerg
306 1.4.2.2 joerg DPRINTF(("putter_fop_close: device closed\n"));
307 1.4.2.2 joerg
308 1.4.2.2 joerg restart:
309 1.4.2.2 joerg mutex_enter(&pi_mtx);
310 1.4.2.2 joerg /*
311 1.4.2.2 joerg * First check if the fs was never mounted. In that case
312 1.4.2.2 joerg * remove the instance from the list. If mount is attempted later,
313 1.4.2.2 joerg * it will simply fail.
314 1.4.2.2 joerg */
315 1.4.2.2 joerg if (pi->pi_private == PUTTER_EMBRYO) {
316 1.4.2.2 joerg TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
317 1.4.2.2 joerg mutex_exit(&pi_mtx);
318 1.4.2.2 joerg
319 1.4.2.2 joerg DPRINTF(("putter_fop_close: data associated with fp %p was "
320 1.4.2.2 joerg "embryonic\n", fp));
321 1.4.2.2 joerg
322 1.4.2.2 joerg goto out;
323 1.4.2.2 joerg }
324 1.4.2.2 joerg
325 1.4.2.2 joerg /*
326 1.4.2.2 joerg * Next, analyze if unmount was called and the instance is dead.
327 1.4.2.2 joerg * In this case we can just free the structure and go home, it
328 1.4.2.2 joerg * was removed from the list by putter_rmprivate().
329 1.4.2.2 joerg */
330 1.4.2.2 joerg if (pi->pi_private == PUTTER_DEAD) {
331 1.4.2.2 joerg mutex_exit(&pi_mtx);
332 1.4.2.2 joerg
333 1.4.2.2 joerg DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
334 1.4.2.2 joerg "dead, freeing\n", fp, pi->pi_idx));
335 1.4.2.2 joerg
336 1.4.2.2 joerg goto out;
337 1.4.2.2 joerg }
338 1.4.2.2 joerg
339 1.4.2.2 joerg /*
340 1.4.2.2 joerg * So we have a reference. Proceed to unwrap the file system.
341 1.4.2.2 joerg */
342 1.4.2.2 joerg mutex_exit(&pi_mtx);
343 1.4.2.2 joerg
344 1.4.2.2 joerg /* hmm? suspicious locking? */
345 1.4.2.2 joerg while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
346 1.4.2.2 joerg goto restart;
347 1.4.2.2 joerg
348 1.4.2.2 joerg out:
349 1.4.2.2 joerg /*
350 1.4.2.2 joerg * Finally, release the instance information. It was already
351 1.4.2.2 joerg * removed from the list by putter_rmprivate() and we know it's
352 1.4.2.2 joerg * dead, so no need to lock.
353 1.4.2.2 joerg */
354 1.4.2.2 joerg kmem_free(pi, sizeof(struct putter_instance));
355 1.4.2.2 joerg
356 1.4.2.2 joerg return 0;
357 1.4.2.2 joerg }
358 1.4.2.2 joerg
359 1.4.2.2 joerg static int
360 1.4.2.2 joerg putter_fop_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
361 1.4.2.2 joerg {
362 1.4.2.2 joerg
363 1.4.2.2 joerg /*
364 1.4.2.2 joerg * work already done in sys_ioctl(). skip sanity checks to enable
365 1.4.2.2 joerg * setting non-blocking fd without yet having mounted the fs
366 1.4.2.2 joerg */
367 1.4.2.2 joerg if (cmd == FIONBIO)
368 1.4.2.2 joerg return 0;
369 1.4.2.2 joerg
370 1.4.2.2 joerg return EINVAL;
371 1.4.2.2 joerg }
372 1.4.2.2 joerg
373 1.4.2.2 joerg /* kqueue stuff */
374 1.4.2.2 joerg
375 1.4.2.2 joerg static void
376 1.4.2.2 joerg filt_putterdetach(struct knote *kn)
377 1.4.2.2 joerg {
378 1.4.2.2 joerg struct putter_instance *pi = kn->kn_hook;
379 1.4.2.2 joerg
380 1.4.2.2 joerg mutex_enter(&pi_mtx);
381 1.4.2.2 joerg SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext);
382 1.4.2.2 joerg mutex_exit(&pi_mtx);
383 1.4.2.2 joerg }
384 1.4.2.2 joerg
385 1.4.2.2 joerg static int
386 1.4.2.4 jmcneill filt_putter(struct knote *kn, long hint)
387 1.4.2.2 joerg {
388 1.4.2.2 joerg struct putter_instance *pi = kn->kn_hook;
389 1.4.2.2 joerg int error;
390 1.4.2.2 joerg
391 1.4.2.2 joerg error = 0;
392 1.4.2.2 joerg mutex_enter(&pi_mtx);
393 1.4.2.2 joerg if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
394 1.4.2.2 joerg error = 1;
395 1.4.2.2 joerg mutex_exit(&pi_mtx);
396 1.4.2.2 joerg if (error)
397 1.4.2.2 joerg return 0;
398 1.4.2.2 joerg
399 1.4.2.2 joerg kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
400 1.4.2.2 joerg
401 1.4.2.2 joerg return kn->kn_data != 0;
402 1.4.2.2 joerg }
403 1.4.2.2 joerg
404 1.4.2.4 jmcneill static const struct filterops putter_filtops =
405 1.4.2.4 jmcneill { 1, NULL, filt_putterdetach, filt_putter };
406 1.4.2.2 joerg
407 1.4.2.2 joerg static int
408 1.4.2.2 joerg putter_fop_kqfilter(struct file *fp, struct knote *kn)
409 1.4.2.2 joerg {
410 1.4.2.2 joerg struct putter_instance *pi = fp->f_data;
411 1.4.2.2 joerg struct klist *klist;
412 1.4.2.2 joerg
413 1.4.2.4 jmcneill switch (kn->kn_filter) {
414 1.4.2.4 jmcneill case EVFILT_READ:
415 1.4.2.4 jmcneill klist = &pi->pi_sel.sel_klist;
416 1.4.2.4 jmcneill kn->kn_fop = &putter_filtops;
417 1.4.2.4 jmcneill kn->kn_hook = pi;
418 1.4.2.2 joerg
419 1.4.2.4 jmcneill mutex_enter(&pi_mtx);
420 1.4.2.4 jmcneill SLIST_INSERT_HEAD(klist, kn, kn_selnext);
421 1.4.2.4 jmcneill mutex_exit(&pi_mtx);
422 1.4.2.2 joerg
423 1.4.2.4 jmcneill break;
424 1.4.2.4 jmcneill case EVFILT_WRITE:
425 1.4.2.4 jmcneill kn->kn_fop = &seltrue_filtops;
426 1.4.2.4 jmcneill break;
427 1.4.2.4 jmcneill default:
428 1.4.2.4 jmcneill return EINVAL;
429 1.4.2.4 jmcneill }
430 1.4.2.2 joerg
431 1.4.2.2 joerg return 0;
432 1.4.2.2 joerg }
433 1.4.2.2 joerg
434 1.4.2.2 joerg /*
435 1.4.2.2 joerg * Device routines. These are for when /dev/puffs is initially
436 1.4.2.2 joerg * opened before it has been cloned.
437 1.4.2.2 joerg */
438 1.4.2.2 joerg
439 1.4.2.2 joerg dev_type_open(puttercdopen);
440 1.4.2.2 joerg dev_type_close(puttercdclose);
441 1.4.2.2 joerg dev_type_ioctl(puttercdioctl);
442 1.4.2.2 joerg
443 1.4.2.2 joerg /* dev */
444 1.4.2.2 joerg const struct cdevsw putter_cdevsw = {
445 1.4.2.2 joerg puttercdopen, puttercdclose, noread, nowrite,
446 1.4.2.2 joerg noioctl, nostop, notty, nopoll,
447 1.4.2.2 joerg nommap, nokqfilter, D_OTHER
448 1.4.2.2 joerg };
449 1.4.2.2 joerg int
450 1.4.2.2 joerg puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
451 1.4.2.2 joerg {
452 1.4.2.2 joerg struct putter_instance *pi;
453 1.4.2.2 joerg struct file *fp;
454 1.4.2.2 joerg int error, fd, idx;
455 1.4.2.2 joerg
456 1.4.2.2 joerg pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
457 1.4.2.2 joerg mutex_enter(&pi_mtx);
458 1.4.2.2 joerg idx = get_pi_idx(pi);
459 1.4.2.2 joerg
460 1.4.2.2 joerg pi->pi_pid = l->l_proc->p_pid;
461 1.4.2.2 joerg pi->pi_idx = idx;
462 1.4.2.2 joerg pi->pi_curput = NULL;
463 1.4.2.2 joerg pi->pi_curres = 0;
464 1.4.2.2 joerg pi->pi_curopaq = NULL;
465 1.4.2.2 joerg selinit(&pi->pi_sel);
466 1.4.2.2 joerg mutex_exit(&pi_mtx);
467 1.4.2.2 joerg
468 1.4.2.3 joerg if ((error = falloc(l, &fp, &fd)) != 0)
469 1.4.2.3 joerg goto bad1;
470 1.4.2.3 joerg
471 1.4.2.3 joerg if ((error = putter_configure(dev, flags, fmt, fd)) != 0)
472 1.4.2.3 joerg goto bad2;
473 1.4.2.3 joerg
474 1.4.2.2 joerg DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
475 1.4.2.2 joerg pi->pi_pid));
476 1.4.2.2 joerg
477 1.4.2.3 joerg error = fdclone(l, fp, fd, FREAD|FWRITE, &putter_fileops, pi);
478 1.4.2.3 joerg KASSERT(error = EMOVEFD);
479 1.4.2.3 joerg return error;
480 1.4.2.3 joerg
481 1.4.2.3 joerg bad2:
482 1.4.2.3 joerg FILE_UNUSE(fp, l);
483 1.4.2.3 joerg fdremove(l->l_proc->p_fd, fd);
484 1.4.2.3 joerg ffree(fp);
485 1.4.2.3 joerg bad1:
486 1.4.2.3 joerg putter_detach(pi);
487 1.4.2.3 joerg kmem_free(pi, sizeof(struct putter_instance));
488 1.4.2.3 joerg return error;
489 1.4.2.2 joerg }
490 1.4.2.2 joerg
491 1.4.2.2 joerg int
492 1.4.2.2 joerg puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
493 1.4.2.2 joerg {
494 1.4.2.2 joerg
495 1.4.2.2 joerg panic("puttercdclose impossible\n");
496 1.4.2.2 joerg
497 1.4.2.2 joerg return 0;
498 1.4.2.2 joerg }
499 1.4.2.2 joerg
500 1.4.2.2 joerg
501 1.4.2.2 joerg /*
502 1.4.2.2 joerg * Set the private structure for the file descriptor. This is
503 1.4.2.2 joerg * typically done immediately when the counterpart has knowledge
504 1.4.2.2 joerg * about the private structure's address and the file descriptor
505 1.4.2.2 joerg * (e.g. vfs mount routine).
506 1.4.2.2 joerg *
507 1.4.2.2 joerg * We only want to make sure that the caller had the right to open the
508 1.4.2.2 joerg * device, we don't so much care about which context it gets in case
509 1.4.2.2 joerg * the same process opened multiple (since they are equal at this point).
510 1.4.2.2 joerg */
511 1.4.2.2 joerg struct putter_instance *
512 1.4.2.2 joerg putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
513 1.4.2.2 joerg {
514 1.4.2.2 joerg struct putter_instance *pi = NULL;
515 1.4.2.2 joerg
516 1.4.2.2 joerg mutex_enter(&pi_mtx);
517 1.4.2.2 joerg TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
518 1.4.2.2 joerg if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
519 1.4.2.2 joerg pi->pi_private = ppriv;
520 1.4.2.2 joerg pi->pi_fd = fd;
521 1.4.2.2 joerg pi->pi_pop = pop;
522 1.4.2.2 joerg break;
523 1.4.2.2 joerg }
524 1.4.2.2 joerg }
525 1.4.2.2 joerg mutex_exit(&pi_mtx);
526 1.4.2.2 joerg
527 1.4.2.2 joerg DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
528 1.4.2.2 joerg pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
529 1.4.2.2 joerg
530 1.4.2.2 joerg return pi;
531 1.4.2.2 joerg }
532 1.4.2.2 joerg
533 1.4.2.2 joerg /*
534 1.4.2.2 joerg * Remove fp <-> private mapping.
535 1.4.2.2 joerg */
536 1.4.2.2 joerg void
537 1.4.2.2 joerg putter_detach(struct putter_instance *pi)
538 1.4.2.2 joerg {
539 1.4.2.2 joerg
540 1.4.2.2 joerg mutex_enter(&pi_mtx);
541 1.4.2.2 joerg TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
542 1.4.2.2 joerg pi->pi_private = PUTTER_DEAD;
543 1.4.2.2 joerg mutex_exit(&pi_mtx);
544 1.4.2.2 joerg
545 1.4.2.2 joerg DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
546 1.4.2.2 joerg }
547 1.4.2.2 joerg
548 1.4.2.2 joerg void
549 1.4.2.2 joerg putter_notify(struct putter_instance *pi)
550 1.4.2.2 joerg {
551 1.4.2.2 joerg
552 1.4.2.2 joerg selnotify(&pi->pi_sel, 0);
553 1.4.2.2 joerg }
554 1.4.2.2 joerg
555 1.4.2.2 joerg /* search sorted list of instances for free minor, sorted insert arg */
556 1.4.2.2 joerg static int
557 1.4.2.2 joerg get_pi_idx(struct putter_instance *pi_i)
558 1.4.2.2 joerg {
559 1.4.2.2 joerg struct putter_instance *pi;
560 1.4.2.2 joerg int i;
561 1.4.2.2 joerg
562 1.4.2.2 joerg KASSERT(mutex_owned(&pi_mtx));
563 1.4.2.2 joerg
564 1.4.2.2 joerg i = 0;
565 1.4.2.2 joerg TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
566 1.4.2.2 joerg if (i != pi->pi_idx)
567 1.4.2.2 joerg break;
568 1.4.2.2 joerg i++;
569 1.4.2.2 joerg }
570 1.4.2.2 joerg
571 1.4.2.2 joerg pi_i->pi_private = PUTTER_EMBRYO;
572 1.4.2.2 joerg
573 1.4.2.2 joerg if (pi == NULL)
574 1.4.2.2 joerg TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
575 1.4.2.2 joerg else
576 1.4.2.2 joerg TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
577 1.4.2.2 joerg
578 1.4.2.2 joerg return i;
579 1.4.2.2 joerg }
580