2 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/selinfo.h>
35 #include <sys/systm.h>
37 #include <dev/random/random_adaptors.h>
38 #include <dev/random/randomdev.h>
40 static struct mtx pseudo_random_block_mtx;
42 /* Used to fake out unused random calls in random_adaptor */
44 random_null_func(void)
49 pseudo_random_block_read(void *buf __unused, int c __unused)
52 mtx_lock(&pseudo_random_block_mtx);
54 printf("random(4) device is blocking.\n");
55 msleep(pseudo_random_block_read, &pseudo_random_block_mtx, 0,
58 mtx_unlock(&pseudo_random_block_mtx);
64 pseudo_random_block_init(void)
67 mtx_init(&pseudo_random_block_mtx, "sleep mtx for random_block",
72 pseudo_random_block_deinit(void)
75 mtx_destroy(&pseudo_random_block_mtx);
78 struct random_adaptor pseudo_random_block = {
79 .ident = "pseudo-RNG that always blocks",
80 .init = pseudo_random_block_init,
81 .deinit = pseudo_random_block_deinit,
82 .read = pseudo_random_block_read,
83 .write = (random_write_func_t *)random_null_func,
84 .reseed = (random_reseed_func_t *)random_null_func,
89 pseudo_random_panic_read(void *buf, int c)
92 panic("Insert a witty panic msg in here.");
97 struct random_adaptor pseudo_random_panic = {
98 .ident = "pseudo-RNG that always panics on first read(2)",
99 .init = (random_init_func_t *)random_null_func,
100 .deinit = (random_deinit_func_t *)random_null_func,
101 .read = pseudo_random_panic_read,
102 .write = (random_write_func_t *)random_null_func,
103 .reseed = (random_reseed_func_t *)random_null_func,
108 pseudo_random_modevent(module_t mod, int type, void *unused)
113 random_adaptor_register("block", &pseudo_random_block);
114 EVENTHANDLER_INVOKE(random_adaptor_attach,
115 &pseudo_random_block);
117 random_adaptor_register("panic", &pseudo_random_panic);
125 RANDOM_ADAPTOR_MODULE(pseudo, pseudo_random_modevent, 1);