Home | History | Annotate | Line # | Download | only in putter
putter.c revision 1.4.2.2
      1  1.4.2.2  joerg /*	$NetBSD: putter.c,v 1.4.2.2 2007/11/14 19:04:33 joerg 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.2  joerg __KERNEL_RCSID(0, "$NetBSD: putter.c,v 1.4.2.2 2007/11/14 19:04:33 joerg 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.2  joerg  * putter instance structures.  these are always allocated and freed
     52  1.4.2.2  joerg  * from the context of the transport user.
     53  1.4.2.2  joerg  */
     54  1.4.2.2  joerg struct putter_instance {
     55  1.4.2.2  joerg 	pid_t			pi_pid;
     56  1.4.2.2  joerg 	int			pi_idx;
     57  1.4.2.2  joerg 	int			pi_fd;
     58  1.4.2.2  joerg 	struct selinfo		pi_sel;
     59  1.4.2.2  joerg 
     60  1.4.2.2  joerg 	void			*pi_private;
     61  1.4.2.2  joerg 	struct putter_ops	*pi_pop;
     62  1.4.2.2  joerg 
     63  1.4.2.2  joerg 	uint8_t			*pi_curput;
     64  1.4.2.2  joerg 	size_t			pi_curres;
     65  1.4.2.2  joerg 	void			*pi_curopaq;
     66  1.4.2.2  joerg 
     67  1.4.2.2  joerg 	TAILQ_ENTRY(putter_instance) pi_entries;
     68  1.4.2.2  joerg };
     69  1.4.2.2  joerg #define PUTTER_EMBRYO ((void *)-1)	/* before attach	*/
     70  1.4.2.2  joerg #define PUTTER_DEAD ((void *)-2)	/* after detach		*/
     71  1.4.2.2  joerg 
     72  1.4.2.2  joerg static TAILQ_HEAD(, putter_instance) putter_ilist
     73  1.4.2.2  joerg     = TAILQ_HEAD_INITIALIZER(putter_ilist);
     74  1.4.2.2  joerg 
     75  1.4.2.2  joerg static int get_pi_idx(struct putter_instance *);
     76  1.4.2.2  joerg 
     77  1.4.2.2  joerg #ifdef DEBUG
     78  1.4.2.2  joerg #ifndef PUTTERDEBUG
     79  1.4.2.2  joerg #define PUTTERDEBUG
     80  1.4.2.2  joerg #endif
     81  1.4.2.2  joerg #endif
     82  1.4.2.2  joerg 
     83  1.4.2.2  joerg #ifdef PUTTERDEBUG
     84  1.4.2.2  joerg static int putterdebug = 0;
     85  1.4.2.2  joerg #define DPRINTF(x) if (putterdebug > 0) printf x
     86  1.4.2.2  joerg #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
     87  1.4.2.2  joerg #else
     88  1.4.2.2  joerg #define DPRINTF(x)
     89  1.4.2.2  joerg #define DPRINTF_VERBOSE(x)
     90  1.4.2.2  joerg #endif
     91  1.4.2.2  joerg 
     92  1.4.2.2  joerg /*
     93  1.4.2.2  joerg  * public init / deinit
     94  1.4.2.2  joerg  */
     95  1.4.2.2  joerg 
     96  1.4.2.2  joerg /* protects both the list and the contents of the list elements */
     97  1.4.2.2  joerg static kmutex_t pi_mtx;
     98  1.4.2.2  joerg 
     99  1.4.2.2  joerg void putterattach(void);
    100  1.4.2.2  joerg 
    101  1.4.2.2  joerg void
    102  1.4.2.2  joerg putterattach()
    103  1.4.2.2  joerg {
    104  1.4.2.2  joerg 
    105  1.4.2.2  joerg 	mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE);
    106  1.4.2.2  joerg }
    107  1.4.2.2  joerg 
    108  1.4.2.2  joerg #if 0
    109  1.4.2.2  joerg void
    110  1.4.2.2  joerg putter_destroy()
    111  1.4.2.2  joerg {
    112  1.4.2.2  joerg 
    113  1.4.2.2  joerg 	mutex_destroy(&pi_mtx);
    114  1.4.2.2  joerg }
    115  1.4.2.2  joerg #endif
    116  1.4.2.2  joerg 
    117  1.4.2.2  joerg /*
    118  1.4.2.2  joerg  * fd routines, for cloner
    119  1.4.2.2  joerg  */
    120  1.4.2.2  joerg static int putter_fop_read(struct file *, off_t *, struct uio *,
    121  1.4.2.2  joerg 			   kauth_cred_t, int);
    122  1.4.2.2  joerg static int putter_fop_write(struct file *, off_t *, struct uio *,
    123  1.4.2.2  joerg 			    kauth_cred_t, int);
    124  1.4.2.2  joerg static int putter_fop_ioctl(struct file*, u_long, void *, struct lwp *);
    125  1.4.2.2  joerg static int putter_fop_poll(struct file *, int, struct lwp *);
    126  1.4.2.2  joerg static int putter_fop_close(struct file *, struct lwp *);
    127  1.4.2.2  joerg static int putter_fop_kqfilter(struct file *, struct knote *);
    128  1.4.2.2  joerg 
    129  1.4.2.2  joerg 
    130  1.4.2.2  joerg static const struct fileops putter_fileops = {
    131  1.4.2.2  joerg 	putter_fop_read,
    132  1.4.2.2  joerg 	putter_fop_write,
    133  1.4.2.2  joerg 	putter_fop_ioctl,
    134  1.4.2.2  joerg 	fnullop_fcntl,
    135  1.4.2.2  joerg 	putter_fop_poll,
    136  1.4.2.2  joerg 	fbadop_stat,
    137  1.4.2.2  joerg 	putter_fop_close,
    138  1.4.2.2  joerg 	putter_fop_kqfilter
    139  1.4.2.2  joerg };
    140  1.4.2.2  joerg 
    141  1.4.2.2  joerg static int
    142  1.4.2.2  joerg putter_fop_read(struct file *fp, off_t *off, struct uio *uio,
    143  1.4.2.2  joerg 	kauth_cred_t cred, int flags)
    144  1.4.2.2  joerg {
    145  1.4.2.2  joerg 	struct putter_instance *pi = fp->f_data;
    146  1.4.2.2  joerg 	size_t origres, moved;
    147  1.4.2.2  joerg 	int error;
    148  1.4.2.2  joerg 
    149  1.4.2.2  joerg 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    150  1.4.2.2  joerg 		printf("putter_fop_read: private %d not inited\n", pi->pi_idx);
    151  1.4.2.2  joerg 		return ENOENT;
    152  1.4.2.2  joerg 	}
    153  1.4.2.2  joerg 
    154  1.4.2.2  joerg 	if (pi->pi_curput == NULL) {
    155  1.4.2.2  joerg 		error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
    156  1.4.2.2  joerg 		    fp->f_flag & O_NONBLOCK, &pi->pi_curput,
    157  1.4.2.2  joerg 		    &pi->pi_curres, &pi->pi_curopaq);
    158  1.4.2.2  joerg 		if (error)
    159  1.4.2.2  joerg 			return error;
    160  1.4.2.2  joerg 	}
    161  1.4.2.2  joerg 
    162  1.4.2.2  joerg 	origres = uio->uio_resid;
    163  1.4.2.2  joerg 	error = uiomove(pi->pi_curput, pi->pi_curres, uio);
    164  1.4.2.2  joerg 	moved = origres - uio->uio_resid;
    165  1.4.2.2  joerg 	DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
    166  1.4.2.2  joerg 	    pi, moved, pi->pi_curput, error));
    167  1.4.2.2  joerg 
    168  1.4.2.2  joerg 	KASSERT(pi->pi_curres >= moved);
    169  1.4.2.2  joerg 	pi->pi_curres -= moved;
    170  1.4.2.2  joerg 	pi->pi_curput += moved;
    171  1.4.2.2  joerg 
    172  1.4.2.2  joerg 	if (pi->pi_curres == 0) {
    173  1.4.2.2  joerg 		pi->pi_pop->pop_releaseout(pi->pi_private,
    174  1.4.2.2  joerg 		    pi->pi_curopaq, error);
    175  1.4.2.2  joerg 		pi->pi_curput = NULL;
    176  1.4.2.2  joerg 	}
    177  1.4.2.2  joerg 
    178  1.4.2.2  joerg 	return error;
    179  1.4.2.2  joerg }
    180  1.4.2.2  joerg 
    181  1.4.2.2  joerg static int
    182  1.4.2.2  joerg putter_fop_write(struct file *fp, off_t *off, struct uio *uio,
    183  1.4.2.2  joerg 	kauth_cred_t cred, int flags)
    184  1.4.2.2  joerg {
    185  1.4.2.2  joerg 	struct putter_instance *pi = fp->f_data;
    186  1.4.2.2  joerg 	struct putter_hdr pth;
    187  1.4.2.2  joerg 	uint8_t *buf;
    188  1.4.2.2  joerg 	size_t frsize;
    189  1.4.2.2  joerg 	int error;
    190  1.4.2.2  joerg 
    191  1.4.2.2  joerg 	DPRINTF(("putter_fop_write (%p): writing response, resid %zu\n",
    192  1.4.2.2  joerg 	    pi->pi_private, uio->uio_resid));
    193  1.4.2.2  joerg 
    194  1.4.2.2  joerg 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    195  1.4.2.2  joerg 		printf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
    196  1.4.2.2  joerg 		return ENOENT;
    197  1.4.2.2  joerg 	}
    198  1.4.2.2  joerg 
    199  1.4.2.2  joerg 	error = uiomove(&pth, sizeof(struct putter_hdr), uio);
    200  1.4.2.2  joerg 	if (error)
    201  1.4.2.2  joerg 		return error;
    202  1.4.2.2  joerg 
    203  1.4.2.2  joerg 	/* Sorry mate, the kernel doesn't buffer. */
    204  1.4.2.2  joerg 	frsize = pth.pth_framelen - sizeof(struct putter_hdr);
    205  1.4.2.2  joerg 	if (uio->uio_resid < frsize)
    206  1.4.2.2  joerg 		return EINVAL;
    207  1.4.2.2  joerg 
    208  1.4.2.2  joerg 	buf = kmem_alloc(frsize + sizeof(struct putter_hdr), KM_SLEEP);
    209  1.4.2.2  joerg 	memcpy(buf, &pth, sizeof(pth));
    210  1.4.2.2  joerg 	error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio);
    211  1.4.2.2  joerg 	if (error == 0) {
    212  1.4.2.2  joerg 		pi->pi_pop->pop_dispatch(pi->pi_private,
    213  1.4.2.2  joerg 		    (struct putter_hdr *)buf);
    214  1.4.2.2  joerg 	}
    215  1.4.2.2  joerg 	kmem_free(buf, frsize + sizeof(struct putter_hdr));
    216  1.4.2.2  joerg 
    217  1.4.2.2  joerg 	return error;
    218  1.4.2.2  joerg }
    219  1.4.2.2  joerg 
    220  1.4.2.2  joerg /*
    221  1.4.2.2  joerg  * Poll query interface.  The question is only if an event
    222  1.4.2.2  joerg  * can be read from us.
    223  1.4.2.2  joerg  */
    224  1.4.2.2  joerg #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
    225  1.4.2.2  joerg static int
    226  1.4.2.2  joerg putter_fop_poll(struct file *fp, int events, struct lwp *l)
    227  1.4.2.2  joerg {
    228  1.4.2.2  joerg 	struct putter_instance *pi = fp->f_data;
    229  1.4.2.2  joerg 	int revents;
    230  1.4.2.2  joerg 
    231  1.4.2.2  joerg 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    232  1.4.2.2  joerg 		printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
    233  1.4.2.2  joerg 		return ENOENT;
    234  1.4.2.2  joerg 	}
    235  1.4.2.2  joerg 
    236  1.4.2.2  joerg 	revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
    237  1.4.2.2  joerg 	if ((events & PUTTERPOLL_EVSET) == 0)
    238  1.4.2.2  joerg 		return revents;
    239  1.4.2.2  joerg 
    240  1.4.2.2  joerg 	/* check queue */
    241  1.4.2.2  joerg 	if (pi->pi_pop->pop_waitcount(pi->pi_private))
    242  1.4.2.2  joerg 		revents |= PUTTERPOLL_EVSET;
    243  1.4.2.2  joerg 	else
    244  1.4.2.2  joerg 		selrecord(l, &pi->pi_sel);
    245  1.4.2.2  joerg 
    246  1.4.2.2  joerg 	return revents;
    247  1.4.2.2  joerg }
    248  1.4.2.2  joerg 
    249  1.4.2.2  joerg /*
    250  1.4.2.2  joerg  * device close = forced unmount.
    251  1.4.2.2  joerg  *
    252  1.4.2.2  joerg  * unmounting is a frightfully complex operation to avoid races
    253  1.4.2.2  joerg  */
    254  1.4.2.2  joerg static int
    255  1.4.2.2  joerg putter_fop_close(struct file *fp, struct lwp *l)
    256  1.4.2.2  joerg {
    257  1.4.2.2  joerg 	struct putter_instance *pi = fp->f_data;
    258  1.4.2.2  joerg 	int rv;
    259  1.4.2.2  joerg 
    260  1.4.2.2  joerg 	DPRINTF(("putter_fop_close: device closed\n"));
    261  1.4.2.2  joerg 
    262  1.4.2.2  joerg  restart:
    263  1.4.2.2  joerg 	mutex_enter(&pi_mtx);
    264  1.4.2.2  joerg 	/*
    265  1.4.2.2  joerg 	 * First check if the fs was never mounted.  In that case
    266  1.4.2.2  joerg 	 * remove the instance from the list.  If mount is attempted later,
    267  1.4.2.2  joerg 	 * it will simply fail.
    268  1.4.2.2  joerg 	 */
    269  1.4.2.2  joerg 	if (pi->pi_private == PUTTER_EMBRYO) {
    270  1.4.2.2  joerg 		TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
    271  1.4.2.2  joerg 		mutex_exit(&pi_mtx);
    272  1.4.2.2  joerg 
    273  1.4.2.2  joerg 		DPRINTF(("putter_fop_close: data associated with fp %p was "
    274  1.4.2.2  joerg 		    "embryonic\n", fp));
    275  1.4.2.2  joerg 
    276  1.4.2.2  joerg 		goto out;
    277  1.4.2.2  joerg 	}
    278  1.4.2.2  joerg 
    279  1.4.2.2  joerg 	/*
    280  1.4.2.2  joerg 	 * Next, analyze if unmount was called and the instance is dead.
    281  1.4.2.2  joerg 	 * In this case we can just free the structure and go home, it
    282  1.4.2.2  joerg 	 * was removed from the list by putter_rmprivate().
    283  1.4.2.2  joerg 	 */
    284  1.4.2.2  joerg 	if (pi->pi_private == PUTTER_DEAD) {
    285  1.4.2.2  joerg 		mutex_exit(&pi_mtx);
    286  1.4.2.2  joerg 
    287  1.4.2.2  joerg 		DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
    288  1.4.2.2  joerg 		    "dead, freeing\n", fp, pi->pi_idx));
    289  1.4.2.2  joerg 
    290  1.4.2.2  joerg 		goto out;
    291  1.4.2.2  joerg 	}
    292  1.4.2.2  joerg 
    293  1.4.2.2  joerg 	/*
    294  1.4.2.2  joerg 	 * So we have a reference.  Proceed to unwrap the file system.
    295  1.4.2.2  joerg 	 */
    296  1.4.2.2  joerg 	mutex_exit(&pi_mtx);
    297  1.4.2.2  joerg 
    298  1.4.2.2  joerg 	/* hmm?  suspicious locking? */
    299  1.4.2.2  joerg 	while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
    300  1.4.2.2  joerg 		goto restart;
    301  1.4.2.2  joerg 
    302  1.4.2.2  joerg  out:
    303  1.4.2.2  joerg 	/*
    304  1.4.2.2  joerg 	 * Finally, release the instance information.  It was already
    305  1.4.2.2  joerg 	 * removed from the list by putter_rmprivate() and we know it's
    306  1.4.2.2  joerg 	 * dead, so no need to lock.
    307  1.4.2.2  joerg 	 */
    308  1.4.2.2  joerg 	kmem_free(pi, sizeof(struct putter_instance));
    309  1.4.2.2  joerg 
    310  1.4.2.2  joerg 	return 0;
    311  1.4.2.2  joerg }
    312  1.4.2.2  joerg 
    313  1.4.2.2  joerg static int
    314  1.4.2.2  joerg putter_fop_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
    315  1.4.2.2  joerg {
    316  1.4.2.2  joerg 
    317  1.4.2.2  joerg 	/*
    318  1.4.2.2  joerg 	 * work already done in sys_ioctl().  skip sanity checks to enable
    319  1.4.2.2  joerg 	 * setting non-blocking fd without yet having mounted the fs
    320  1.4.2.2  joerg 	 */
    321  1.4.2.2  joerg 	if (cmd == FIONBIO)
    322  1.4.2.2  joerg 		return 0;
    323  1.4.2.2  joerg 
    324  1.4.2.2  joerg 	return EINVAL;
    325  1.4.2.2  joerg }
    326  1.4.2.2  joerg 
    327  1.4.2.2  joerg /* kqueue stuff */
    328  1.4.2.2  joerg 
    329  1.4.2.2  joerg static void
    330  1.4.2.2  joerg filt_putterdetach(struct knote *kn)
    331  1.4.2.2  joerg {
    332  1.4.2.2  joerg 	struct putter_instance *pi = kn->kn_hook;
    333  1.4.2.2  joerg 
    334  1.4.2.2  joerg 	mutex_enter(&pi_mtx);
    335  1.4.2.2  joerg 	SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext);
    336  1.4.2.2  joerg 	mutex_exit(&pi_mtx);
    337  1.4.2.2  joerg }
    338  1.4.2.2  joerg 
    339  1.4.2.2  joerg static int
    340  1.4.2.2  joerg filt_putterioctl(struct knote *kn, long hint)
    341  1.4.2.2  joerg {
    342  1.4.2.2  joerg 	struct putter_instance *pi = kn->kn_hook;
    343  1.4.2.2  joerg 	int error;
    344  1.4.2.2  joerg 
    345  1.4.2.2  joerg 	error = 0;
    346  1.4.2.2  joerg 	mutex_enter(&pi_mtx);
    347  1.4.2.2  joerg 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
    348  1.4.2.2  joerg 		error = 1;
    349  1.4.2.2  joerg 	mutex_exit(&pi_mtx);
    350  1.4.2.2  joerg 	if (error)
    351  1.4.2.2  joerg 		return 0;
    352  1.4.2.2  joerg 
    353  1.4.2.2  joerg 	kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
    354  1.4.2.2  joerg 
    355  1.4.2.2  joerg 	return kn->kn_data != 0;
    356  1.4.2.2  joerg }
    357  1.4.2.2  joerg 
    358  1.4.2.2  joerg static const struct filterops putterioctl_filtops =
    359  1.4.2.2  joerg 	{ 1, NULL, filt_putterdetach, filt_putterioctl };
    360  1.4.2.2  joerg 
    361  1.4.2.2  joerg static int
    362  1.4.2.2  joerg putter_fop_kqfilter(struct file *fp, struct knote *kn)
    363  1.4.2.2  joerg {
    364  1.4.2.2  joerg 	struct putter_instance *pi = fp->f_data;
    365  1.4.2.2  joerg 	struct klist *klist;
    366  1.4.2.2  joerg 
    367  1.4.2.2  joerg 	if (kn->kn_filter != EVFILT_READ)
    368  1.4.2.2  joerg 		return 1;
    369  1.4.2.2  joerg 
    370  1.4.2.2  joerg 	klist = &pi->pi_sel.sel_klist;
    371  1.4.2.2  joerg 	kn->kn_fop = &putterioctl_filtops;
    372  1.4.2.2  joerg 	kn->kn_hook = pi;
    373  1.4.2.2  joerg 
    374  1.4.2.2  joerg 	mutex_enter(&pi_mtx);
    375  1.4.2.2  joerg 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    376  1.4.2.2  joerg 	mutex_exit(&pi_mtx);
    377  1.4.2.2  joerg 
    378  1.4.2.2  joerg 	return 0;
    379  1.4.2.2  joerg }
    380  1.4.2.2  joerg 
    381  1.4.2.2  joerg 
    382  1.4.2.2  joerg /*
    383  1.4.2.2  joerg  * Device routines.  These are for when /dev/puffs is initially
    384  1.4.2.2  joerg  * opened before it has been cloned.
    385  1.4.2.2  joerg  */
    386  1.4.2.2  joerg 
    387  1.4.2.2  joerg dev_type_open(puttercdopen);
    388  1.4.2.2  joerg dev_type_close(puttercdclose);
    389  1.4.2.2  joerg dev_type_ioctl(puttercdioctl);
    390  1.4.2.2  joerg 
    391  1.4.2.2  joerg /* dev */
    392  1.4.2.2  joerg const struct cdevsw putter_cdevsw = {
    393  1.4.2.2  joerg 	puttercdopen,	puttercdclose,	noread,		nowrite,
    394  1.4.2.2  joerg 	noioctl,	nostop,		notty,		nopoll,
    395  1.4.2.2  joerg 	nommap,		nokqfilter,	D_OTHER
    396  1.4.2.2  joerg };
    397  1.4.2.2  joerg int
    398  1.4.2.2  joerg puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
    399  1.4.2.2  joerg {
    400  1.4.2.2  joerg 	struct putter_instance *pi;
    401  1.4.2.2  joerg 	struct file *fp;
    402  1.4.2.2  joerg 	int error, fd, idx;
    403  1.4.2.2  joerg 
    404  1.4.2.2  joerg 	if ((error = falloc(l, &fp, &fd)) != 0)
    405  1.4.2.2  joerg 		return error;
    406  1.4.2.2  joerg 
    407  1.4.2.2  joerg 	pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
    408  1.4.2.2  joerg 
    409  1.4.2.2  joerg 	mutex_enter(&pi_mtx);
    410  1.4.2.2  joerg 	idx = get_pi_idx(pi);
    411  1.4.2.2  joerg 
    412  1.4.2.2  joerg 	pi->pi_pid = l->l_proc->p_pid;
    413  1.4.2.2  joerg 	pi->pi_idx = idx;
    414  1.4.2.2  joerg 	pi->pi_curput = NULL;
    415  1.4.2.2  joerg 	pi->pi_curres = 0;
    416  1.4.2.2  joerg 	pi->pi_curopaq = NULL;
    417  1.4.2.2  joerg 	selinit(&pi->pi_sel);
    418  1.4.2.2  joerg 	mutex_exit(&pi_mtx);
    419  1.4.2.2  joerg 
    420  1.4.2.2  joerg 	DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
    421  1.4.2.2  joerg 	    pi->pi_pid));
    422  1.4.2.2  joerg 
    423  1.4.2.2  joerg 	return fdclone(l, fp, fd, FREAD|FWRITE, &putter_fileops, pi);
    424  1.4.2.2  joerg }
    425  1.4.2.2  joerg 
    426  1.4.2.2  joerg int
    427  1.4.2.2  joerg puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
    428  1.4.2.2  joerg {
    429  1.4.2.2  joerg 
    430  1.4.2.2  joerg 	panic("puttercdclose impossible\n");
    431  1.4.2.2  joerg 
    432  1.4.2.2  joerg 	return 0;
    433  1.4.2.2  joerg }
    434  1.4.2.2  joerg 
    435  1.4.2.2  joerg 
    436  1.4.2.2  joerg /*
    437  1.4.2.2  joerg  * Set the private structure for the file descriptor.  This is
    438  1.4.2.2  joerg  * typically done immediately when the counterpart has knowledge
    439  1.4.2.2  joerg  * about the private structure's address and the file descriptor
    440  1.4.2.2  joerg  * (e.g. vfs mount routine).
    441  1.4.2.2  joerg  *
    442  1.4.2.2  joerg  * We only want to make sure that the caller had the right to open the
    443  1.4.2.2  joerg  * device, we don't so much care about which context it gets in case
    444  1.4.2.2  joerg  * the same process opened multiple (since they are equal at this point).
    445  1.4.2.2  joerg  */
    446  1.4.2.2  joerg struct putter_instance *
    447  1.4.2.2  joerg putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
    448  1.4.2.2  joerg {
    449  1.4.2.2  joerg 	struct putter_instance *pi = NULL;
    450  1.4.2.2  joerg 
    451  1.4.2.2  joerg 	mutex_enter(&pi_mtx);
    452  1.4.2.2  joerg 	TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
    453  1.4.2.2  joerg 		if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
    454  1.4.2.2  joerg 			pi->pi_private = ppriv;
    455  1.4.2.2  joerg 			pi->pi_fd = fd;
    456  1.4.2.2  joerg 			pi->pi_pop = pop;
    457  1.4.2.2  joerg 			break;
    458  1.4.2.2  joerg 		    }
    459  1.4.2.2  joerg 	}
    460  1.4.2.2  joerg 	mutex_exit(&pi_mtx);
    461  1.4.2.2  joerg 
    462  1.4.2.2  joerg 	DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
    463  1.4.2.2  joerg 	    pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
    464  1.4.2.2  joerg 
    465  1.4.2.2  joerg 	return pi;
    466  1.4.2.2  joerg }
    467  1.4.2.2  joerg 
    468  1.4.2.2  joerg /*
    469  1.4.2.2  joerg  * Remove fp <-> private mapping.
    470  1.4.2.2  joerg  */
    471  1.4.2.2  joerg void
    472  1.4.2.2  joerg putter_detach(struct putter_instance *pi)
    473  1.4.2.2  joerg {
    474  1.4.2.2  joerg 
    475  1.4.2.2  joerg 	mutex_enter(&pi_mtx);
    476  1.4.2.2  joerg 	TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
    477  1.4.2.2  joerg 	pi->pi_private = PUTTER_DEAD;
    478  1.4.2.2  joerg 	mutex_exit(&pi_mtx);
    479  1.4.2.2  joerg 
    480  1.4.2.2  joerg 	DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
    481  1.4.2.2  joerg }
    482  1.4.2.2  joerg 
    483  1.4.2.2  joerg void
    484  1.4.2.2  joerg putter_notify(struct putter_instance *pi)
    485  1.4.2.2  joerg {
    486  1.4.2.2  joerg 
    487  1.4.2.2  joerg 	selnotify(&pi->pi_sel, 0);
    488  1.4.2.2  joerg }
    489  1.4.2.2  joerg 
    490  1.4.2.2  joerg /* search sorted list of instances for free minor, sorted insert arg */
    491  1.4.2.2  joerg static int
    492  1.4.2.2  joerg get_pi_idx(struct putter_instance *pi_i)
    493  1.4.2.2  joerg {
    494  1.4.2.2  joerg 	struct putter_instance *pi;
    495  1.4.2.2  joerg 	int i;
    496  1.4.2.2  joerg 
    497  1.4.2.2  joerg 	KASSERT(mutex_owned(&pi_mtx));
    498  1.4.2.2  joerg 
    499  1.4.2.2  joerg 	i = 0;
    500  1.4.2.2  joerg 	TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
    501  1.4.2.2  joerg 		if (i != pi->pi_idx)
    502  1.4.2.2  joerg 			break;
    503  1.4.2.2  joerg 		i++;
    504  1.4.2.2  joerg 	}
    505  1.4.2.2  joerg 
    506  1.4.2.2  joerg 	pi_i->pi_private = PUTTER_EMBRYO;
    507  1.4.2.2  joerg 
    508  1.4.2.2  joerg 	if (pi == NULL)
    509  1.4.2.2  joerg 		TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
    510  1.4.2.2  joerg 	else
    511  1.4.2.2  joerg 		TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
    512  1.4.2.2  joerg 
    513  1.4.2.2  joerg 	return i;
    514  1.4.2.2  joerg }
    515