rndctl.c revision 1.4 1 /* $NetBSD: rndctl.c,v 1.4 1999/02/28 17:42:37 explorer Exp $ */
2
3 /*-
4 * Copyright (c) 1997 Michael Graff.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 * 3. Neither the name of the author nor the names of other contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <err.h>
38 #include <string.h>
39
40 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 #include <sys/rnd.h>
43
44 typedef struct {
45 char *name;
46 u_int32_t type;
47 } arg_t;
48
49 arg_t source_types[] = {
50 { "unknown", RND_TYPE_UNKNOWN },
51 { "disk", RND_TYPE_DISK },
52 { "network", RND_TYPE_NET },
53 { "net", RND_TYPE_NET },
54 { "tape", RND_TYPE_TAPE },
55 { "tty", RND_TYPE_TTY },
56 { NULL, 0 }
57 };
58
59 static void usage(void);
60 u_int32_t find_type(char *name);
61 char *find_name(u_int32_t);
62 void do_ioctl(rndctl_t *);
63 char * strflags(u_int32_t);
64 void do_list(int, u_int32_t, char *);
65
66 static void
67 usage(void)
68 {
69 errx(1, "Usage: rndctl [-CEce | -l] [-d name | -t type]");
70 }
71
72 u_int32_t
73 find_type(char *name)
74 {
75 arg_t *a;
76
77 a = source_types;
78
79 while (a->name != NULL) {
80 if (strcmp(a->name, name) == 0)
81 return a->type;
82 a++;
83 }
84
85 errx(1, "Error: Device type %s unknown", name);
86 return 0;
87 }
88
89 char *
90 find_name(u_int32_t type)
91 {
92 arg_t *a;
93
94 a = source_types;
95
96 while (a->name != NULL) {
97 if (type == a->type)
98 return a->name;
99 a++;
100 }
101
102 errx(1, "Error: Device type %u unknown", type);
103 return 0;
104 }
105
106 void
107 do_ioctl(rndctl_t *rctl)
108 {
109 int fd;
110 int res;
111
112 fd = open("/dev/urandom", O_RDONLY, 0644);
113 if (fd < 0)
114 err(1, "open");
115
116 res = ioctl(fd, RNDCTL, rctl);
117 if (res < 0)
118 err(1, "ioctl(RNDCTL)");
119
120 close(fd);
121 }
122
123 char *
124 strflags(u_int32_t fl)
125 {
126 static char str[512];
127
128 str[0] = 0;
129 strcat(str, "<");
130
131 if (fl & RND_FLAG_NO_ESTIMATE)
132 strcat(str, "no");
133 strcat(str, "estimate, ");
134 if (fl & RND_FLAG_NO_COLLECT)
135 strcat(str, "no");
136 strcat(str, "collect>");
137
138 return str;
139 }
140
141 #define HEADER "Device Name Type Bits Flags\n" \
142 "---------------- -------- ---------- -----\n"
143
144 void
145 do_list(int all, u_int32_t type, char *name)
146 {
147 rndstat_t rstat;
148 rndstat_name_t rstat_name;
149 int fd;
150 int res;
151 u_int32_t start;
152
153 fd = open("/dev/urandom", O_RDONLY, 0644);
154 if (fd < 0)
155 err(1, "open");
156
157 if (all == 0 && type == 0xff) {
158 strncpy(rstat_name.name, name, 16);
159 res = ioctl(fd, RNDGETSRCNAME, &rstat_name);
160 if (res < 0)
161 err(1, "ioctl(RNDGETSRCNAME)");
162 printf(HEADER);
163 printf("%-16s %-8s %10u %s\n",
164 rstat_name.source.name,
165 find_name(rstat_name.source.type),
166 rstat_name.source.total,
167 strflags(rstat_name.source.flags));
168 close(fd);
169 return;
170 }
171
172 /*
173 * run through all the devices present in the system, and either
174 * print out ones that match, or print out all of them.
175 */
176 printf(HEADER);
177 start = 0;
178 for (;;) {
179 rstat.count = RND_MAXSTATCOUNT;
180 rstat.start = start;
181 res = ioctl(fd, RNDGETSRCNUM, &rstat);
182 if (res < 0)
183 err(1, "ioctl(RNDGETSRCNUM)");
184
185 if (rstat.count == 0)
186 break;
187
188 for (res = 0 ; res < rstat.count ; res++) {
189 if ((all != 0)
190 || (type == rstat.source[res].type))
191 printf("%-16s %-8s %10u %s\n",
192 rstat.source[res].name,
193 find_name(rstat.source[res].type),
194 rstat.source[res].total,
195 strflags(rstat.source[res].flags));
196 }
197 start += rstat.count;
198 }
199
200 close(fd);
201 }
202
203 int
204 main(int argc, char **argv)
205 {
206 rndctl_t rctl;
207 int ch;
208 int cmd;
209 int lflag;
210 int mflag;
211 u_int32_t type;
212 char name[16];
213
214 rctl.mask = 0;
215 rctl.flags = 0;
216
217 cmd = 0;
218 lflag = 0;
219 mflag = 0;
220 type = 0xff;
221
222 while ((ch = getopt(argc, argv, "CEcelt:d:")) != -1)
223 switch(ch) {
224 case 'C':
225 rctl.flags |= RND_FLAG_NO_COLLECT;
226 rctl.mask |= RND_FLAG_NO_COLLECT;
227 mflag++;
228 break;
229 case 'E':
230 rctl.flags |= RND_FLAG_NO_ESTIMATE;
231 rctl.mask |= RND_FLAG_NO_ESTIMATE;
232 mflag++;
233 break;
234 case 'c':
235 rctl.flags &= ~RND_FLAG_NO_COLLECT;
236 rctl.mask |= RND_FLAG_NO_COLLECT;
237 mflag++;
238 break;
239 case 'e':
240 rctl.flags &= ~RND_FLAG_NO_ESTIMATE;
241 rctl.mask |= RND_FLAG_NO_ESTIMATE;
242 mflag++;
243 break;
244 case 'l':
245 lflag++;
246 break;
247 case 't':
248 if (cmd != 0)
249 usage();
250 cmd = 't';
251
252 type = find_type(optarg);
253 break;
254 case 'd':
255 if (cmd != 0)
256 usage();
257 cmd = 'd';
258
259 type = 0xff;
260 strncpy(name, optarg, 16);
261 break;
262 case '?':
263 default:
264 usage();
265 }
266
267 /*
268 * cannot list and modify at the same time
269 */
270 if (lflag != 0 && mflag != 0)
271 usage();
272
273 /*
274 * bomb out on no-ops
275 */
276 if (lflag == 0 && mflag == 0)
277 usage();
278
279 /*
280 * if not listing, we need a device name or a type
281 */
282 if (lflag == 0 && cmd == 0)
283 usage();
284
285 /*
286 * modify request
287 */
288 if (mflag != 0) {
289 rctl.type = type;
290 strncpy(rctl.name, name, 16);
291 do_ioctl(&rctl);
292
293 exit(0);
294 }
295
296 /*
297 * list sources
298 */
299 if (lflag != 0)
300 do_list(cmd == 0, type, name);
301
302 return 0;
303 }
304