dummy_rng.c revision 256381
1/*-
2 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
14 *
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.
25 *
26 */
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/dev/random/dummy_rng.c 256381 2013-10-12 15:31:36Z markm $");
29
30#include <sys/param.h>
31#include <sys/fcntl.h>
32#include <sys/kernel.h>
33#include <sys/malloc.h>
34#include <sys/module.h>
35#include <sys/random.h>
36#include <sys/selinfo.h>
37#include <sys/systm.h>
38#include <sys/time.h>
39
40#include <dev/random/random_adaptors.h>
41#include <dev/random/randomdev.h>
42
43static struct mtx	dummy_random_mtx;
44
45/* Used to fake out unused random calls in random_adaptor */
46static void
47random_null_func(void)
48{
49}
50
51static int
52dummy_random_poll(int events __unused, struct thread *td __unused)
53{
54
55	return (0);
56}
57
58static int
59dummy_random_block(int flag)
60{
61	int error = 0;
62
63	mtx_lock(&dummy_random_mtx);
64
65	/* Blocking logic */
66	while (!error) {
67		if (flag & O_NONBLOCK)
68			error = EWOULDBLOCK;
69		else {
70			printf("random: dummy device blocking on read.\n");
71			error = msleep(&dummy_random_block,
72			    &dummy_random_mtx,
73			    PUSER | PCATCH, "block", 0);
74		}
75	}
76	mtx_unlock(&dummy_random_mtx);
77
78	return (error);
79}
80
81static void
82dummy_random_init(void)
83{
84
85	mtx_init(&dummy_random_mtx, "sleep mtx for dummy_random",
86	    NULL, MTX_DEF);
87}
88
89static void
90dummy_random_deinit(void)
91{
92
93	mtx_destroy(&dummy_random_mtx);
94}
95
96struct random_adaptor dummy_random = {
97	.ident = "Dummy entropy device that always blocks",
98	.init = dummy_random_init,
99	.deinit = dummy_random_deinit,
100	.block = dummy_random_block,
101	.poll = dummy_random_poll,
102	.read = (random_read_func_t *)random_null_func,
103	.reseed = (random_reseed_func_t *)random_null_func,
104	.seeded = 0, /* This device can never be seeded */
105};
106
107static int
108dummy_random_modevent(module_t mod __unused, int type, void *unused __unused)
109{
110
111	switch (type) {
112	case MOD_LOAD:
113		random_adaptor_register("dummy", &dummy_random);
114		EVENTHANDLER_INVOKE(random_adaptor_attach,
115		    &dummy_random);
116
117		return (0);
118	}
119
120	return (EINVAL);
121}
122
123RANDOM_ADAPTOR_MODULE(dummy, dummy_random_modevent, 1);
124