]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/random/dummy_rng.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / random / dummy_rng.c
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$");
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
43 static struct mtx       dummy_random_mtx;
44
45 /* Used to fake out unused random calls in random_adaptor */
46 static void
47 random_null_func(void)
48 {
49 }
50
51 static int
52 dummy_random_poll(int events __unused, struct thread *td __unused)
53 {
54
55         return (0);
56 }
57
58 static int
59 dummy_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
81 static void
82 dummy_random_init(void)
83 {
84
85         mtx_init(&dummy_random_mtx, "sleep mtx for dummy_random",
86             NULL, MTX_DEF);
87 }
88
89 static void
90 dummy_random_deinit(void)
91 {
92
93         mtx_destroy(&dummy_random_mtx);
94 }
95
96 struct 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         .priority = 1, /* Bottom priority, so goes to last position */
106 };
107
108 static int
109 dummy_random_modevent(module_t mod __unused, int type, void *unused __unused)
110 {
111
112         switch (type) {
113         case MOD_LOAD:
114                 random_adaptor_register("dummy", &dummy_random);
115                 EVENTHANDLER_INVOKE(random_adaptor_attach,
116                     &dummy_random);
117
118                 return (0);
119         }
120
121         return (EINVAL);
122 }
123
124 RANDOM_ADAPTOR_MODULE(dummy, dummy_random_modevent, 1);