4 # Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # Test scenario by kib@freebsd.org
35 [ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
41 [ ! -d $dir ] && mkdir -p $dir
48 .include <bsd.kmod.mk>
51 sed '1,/^EOF2/d' < $odir/$0 > fpclone.c
53 kldload $dir/fpclone.ko
57 dd if=/dev/fpclone bs=1m count=10 > /dev/null 2>&1 &
61 cd /home/pho/stress2; ./run.sh pty.cfg
67 dd if=/dev/fpclone bs=1m count=1k > /dev/null 2>&1 &
68 kldunload $dir/fpclone.ko
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/module.h>
81 #include <sys/malloc.h>
83 typedef void (*cdevpriv_dtr_t)(void *data);
84 int devfs_get_cdevpriv(void **datap);
85 int devfs_set_cdevpriv(void *priv, cdevpriv_dtr_t dtr);
86 void devfs_clear_cdevpriv(void);
87 void devfs_fpdrop(struct file *fp); /* XXX This is not public KPI */
90 static d_open_t fpclone_open;
91 static d_close_t fpclone_close;
92 static d_read_t fpclone_read;
94 static struct cdevsw fpclone_cdevsw = {
95 .d_open = fpclone_open,
96 .d_close = fpclone_close,
97 .d_read = fpclone_read,
99 .d_version = D_VERSION,
100 .d_flags = D_TRACKCLOSE
103 MALLOC_DEFINE(M_FPCLONESC, "fpclone memory", "fpclone memory");
110 static struct cdev *fpclone_dev;
111 static struct mtx me;
114 fpclone_cdevpriv_dtr(void *data)
116 free(data, M_FPCLONESC);
120 fpclone_open(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
122 struct fpclone_sc *sc;
125 sc = malloc(sizeof(struct fpclone_sc), M_FPCLONESC,
127 error = devfs_set_cdevpriv(sc, fpclone_cdevpriv_dtr);
129 fpclone_cdevpriv_dtr(sc);
134 fpclone_close(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
137 devfs_clear_cdevpriv();
141 static char rdata[] = "fpclone sample data string\n";
144 fpclone_read(struct cdev *dev, struct uio *uio, int ioflag)
146 struct fpclone_sc *sc;
147 int rv, amnt, svpos, error;
149 error = devfs_get_cdevpriv((void **)&sc);
154 while (uio->uio_resid > 0) {
156 amnt = MIN(uio->uio_resid, sizeof(rdata) - svpos);
157 rv = uiomove(rdata + svpos, amnt, uio);
162 sc->pos %= sizeof(rdata);
169 fpclone_modevent(module_t mod, int what, void *arg)
173 mtx_init(&me, "fp_ref", NULL, MTX_DEF);
174 fpclone_dev = make_dev(&fpclone_cdevsw, 0, 0, 0, 0666,
179 destroy_dev(fpclone_dev);
190 moduledata_t fpclone_mdata = {
196 DECLARE_MODULE(fpclone, fpclone_mdata, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
197 MODULE_VERSION(fpclone, 1);