Sat Aug 27 18:49:00 2011 UTC ()
static + const + __dead


(joerg)
diff -r1.19 -r1.20 src/sbin/rndctl/rndctl.c

cvs diff -r1.19 -r1.20 src/sbin/rndctl/rndctl.c (expand / switch to unified diff)

--- src/sbin/rndctl/rndctl.c 2009/04/05 12:06:33 1.19
+++ src/sbin/rndctl/rndctl.c 2011/08/27 18:48:59 1.20
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: rndctl.c,v 1.19 2009/04/05 12:06:33 lukem Exp $ */ 1/* $NetBSD: rndctl.c,v 1.20 2011/08/27 18:48:59 joerg Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997 Michael Graff. 4 * Copyright (c) 1997 Michael Graff.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -21,152 +21,152 @@ @@ -21,152 +21,152 @@
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31#include <sys/cdefs.h> 31#include <sys/cdefs.h>
32 32
33#ifndef lint 33#ifndef lint
34__RCSID("$NetBSD: rndctl.c,v 1.19 2009/04/05 12:06:33 lukem Exp $"); 34__RCSID("$NetBSD: rndctl.c,v 1.20 2011/08/27 18:48:59 joerg Exp $");
35#endif 35#endif
36 36
37 37
38#include <sys/types.h> 38#include <sys/types.h>
39#include <sys/ioctl.h> 39#include <sys/ioctl.h>
40#include <sys/rnd.h> 40#include <sys/rnd.h>
41 41
42#include <stdio.h> 42#include <stdio.h>
43#include <stdlib.h> 43#include <stdlib.h>
44#include <unistd.h> 44#include <unistd.h>
45#include <fcntl.h> 45#include <fcntl.h>
46#include <errno.h> 46#include <errno.h>
47#include <err.h> 47#include <err.h>
48#include <string.h> 48#include <string.h>
49 49
50typedef struct { 50typedef struct {
51 const char *a_name; 51 const char *a_name;
52 u_int32_t a_type; 52 u_int32_t a_type;
53} arg_t; 53} arg_t;
54 54
55arg_t source_types[] = { 55static const arg_t source_types[] = {
56 { "???", RND_TYPE_UNKNOWN }, 56 { "???", RND_TYPE_UNKNOWN },
57 { "disk", RND_TYPE_DISK }, 57 { "disk", RND_TYPE_DISK },
58 { "net", RND_TYPE_NET }, 58 { "net", RND_TYPE_NET },
59 { "tape", RND_TYPE_TAPE }, 59 { "tape", RND_TYPE_TAPE },
60 { "tty", RND_TYPE_TTY }, 60 { "tty", RND_TYPE_TTY },
61 { "rng", RND_TYPE_RNG }, 61 { "rng", RND_TYPE_RNG },
62 { NULL, 0 } 62 { NULL, 0 }
63}; 63};
64 64
65static void usage(void); 65__dead static void usage(void);
66u_int32_t find_type(char *name); 66static u_int32_t find_type(const char *name);
67const char *find_name(u_int32_t); 67static const char *find_name(u_int32_t);
68void do_ioctl(rndctl_t *); 68static void do_ioctl(rndctl_t *);
69char * strflags(u_int32_t); 69static char * strflags(u_int32_t);
70void do_list(int, u_int32_t, char *); 70static void do_list(int, u_int32_t, char *);
71void do_stats(void); 71static void do_stats(void);
72 72
73static void 73static void
74usage(void) 74usage(void)
75{ 75{
76 76
77 fprintf(stderr, "usage: %s -CEce [-d devname | -t devtype]\n", 77 fprintf(stderr, "usage: %s -CEce [-d devname | -t devtype]\n",
78 getprogname()); 78 getprogname());
79 fprintf(stderr, " %s -ls [-d devname | -t devtype]\n", 79 fprintf(stderr, " %s -ls [-d devname | -t devtype]\n",
80 getprogname()); 80 getprogname());
81 exit(1); 81 exit(1);
82} 82}
83 83
84u_int32_t 84static u_int32_t
85find_type(char *name) 85find_type(const char *name)
86{ 86{
87 arg_t *a; 87 const arg_t *a;
88 88
89 a = source_types; 89 a = source_types;
90 90
91 while (a->a_name != NULL) { 91 while (a->a_name != NULL) {
92 if (strcmp(a->a_name, name) == 0) 92 if (strcmp(a->a_name, name) == 0)
93 return (a->a_type); 93 return (a->a_type);
94 a++; 94 a++;
95 } 95 }
96 96
97 errx(1, "device name %s unknown", name); 97 errx(1, "device name %s unknown", name);
98 return (0); 98 return (0);
99} 99}
100 100
101const char * 101static const char *
102find_name(u_int32_t type) 102find_name(u_int32_t type)
103{ 103{
104 arg_t *a; 104 const arg_t *a;
105 105
106 a = source_types; 106 a = source_types;
107 107
108 while (a->a_name != NULL) { 108 while (a->a_name != NULL) {
109 if (type == a->a_type) 109 if (type == a->a_type)
110 return (a->a_name); 110 return (a->a_name);
111 a++; 111 a++;
112 } 112 }
113 113
114 warnx("device type %u unknown", type); 114 warnx("device type %u unknown", type);
115 return ("???"); 115 return ("???");
116} 116}
117 117
118void 118static void
119do_ioctl(rndctl_t *rctl) 119do_ioctl(rndctl_t *rctl)
120{ 120{
121 int fd; 121 int fd;
122 int res; 122 int res;
123 123
124 fd = open("/dev/urandom", O_RDONLY, 0644); 124 fd = open("/dev/urandom", O_RDONLY, 0644);
125 if (fd < 0) 125 if (fd < 0)
126 err(1, "open"); 126 err(1, "open");
127 127
128 res = ioctl(fd, RNDCTL, rctl); 128 res = ioctl(fd, RNDCTL, rctl);
129 if (res < 0) 129 if (res < 0)
130 err(1, "ioctl(RNDCTL)"); 130 err(1, "ioctl(RNDCTL)");
131 131
132 close(fd); 132 close(fd);
133} 133}
134 134
135char * 135static char *
136strflags(u_int32_t fl) 136strflags(u_int32_t fl)
137{ 137{
138 static char str[512]; 138 static char str[512];
139 139
140 str[0] = 0; 140 str[0] = 0;
141 if (fl & RND_FLAG_NO_ESTIMATE) 141 if (fl & RND_FLAG_NO_ESTIMATE)
142 ; 142 ;
143 else 143 else
144 strlcat(str, "estimate", sizeof(str)); 144 strlcat(str, "estimate", sizeof(str));
145 145
146 if (fl & RND_FLAG_NO_COLLECT) 146 if (fl & RND_FLAG_NO_COLLECT)
147 ; 147 ;
148 else { 148 else {
149 if (str[0]) 149 if (str[0])
150 strlcat(str, ", ", sizeof(str)); 150 strlcat(str, ", ", sizeof(str));
151 strlcat(str, "collect", sizeof(str)); 151 strlcat(str, "collect", sizeof(str));
152 } 152 }
153 153
154 return (str); 154 return (str);
155} 155}
156 156
157#define HEADER "Source Bits Type Flags\n" 157#define HEADER "Source Bits Type Flags\n"
158 158
159void 159static void
160do_list(int all, u_int32_t type, char *name) 160do_list(int all, u_int32_t type, char *name)
161{ 161{
162 rndstat_t rstat; 162 rndstat_t rstat;
163 rndstat_name_t rstat_name; 163 rndstat_name_t rstat_name;
164 int fd; 164 int fd;
165 int res; 165 int res;
166 uint32_t i; 166 uint32_t i;
167 u_int32_t start; 167 u_int32_t start;
168 168
169 fd = open("/dev/urandom", O_RDONLY, 0644); 169 fd = open("/dev/urandom", O_RDONLY, 0644);
170 if (fd < 0) 170 if (fd < 0)
171 err(1, "open"); 171 err(1, "open");
172 172
@@ -206,28 +206,28 @@ do_list(int all, u_int32_t type, char *n @@ -206,28 +206,28 @@ do_list(int all, u_int32_t type, char *n
206 type == rstat.source[i].type) 206 type == rstat.source[i].type)
207 printf("%-16s %10u %-4s %s\n", 207 printf("%-16s %10u %-4s %s\n",
208 rstat.source[i].name, 208 rstat.source[i].name,
209 rstat.source[i].total, 209 rstat.source[i].total,
210 find_name(rstat.source[i].type), 210 find_name(rstat.source[i].type),
211 strflags(rstat.source[i].flags)); 211 strflags(rstat.source[i].flags));
212 } 212 }
213 start += rstat.count; 213 start += rstat.count;
214 } 214 }
215 215
216 close(fd); 216 close(fd);
217} 217}
218 218
219void 219static void
220do_stats() 220do_stats(void)
221{ 221{
222 rndpoolstat_t rs; 222 rndpoolstat_t rs;
223 int fd; 223 int fd;
224 224
225 fd = open("/dev/urandom", O_RDONLY, 0644); 225 fd = open("/dev/urandom", O_RDONLY, 0644);
226 if (fd < 0) 226 if (fd < 0)
227 err(1, "open"); 227 err(1, "open");
228 228
229 if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0) 229 if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0)
230 err(1, "ioctl(RNDGETPOOLSTAT)"); 230 err(1, "ioctl(RNDGETPOOLSTAT)");
231 231
232 printf("\t%9u bits mixed into pool\n", rs.added); 232 printf("\t%9u bits mixed into pool\n", rs.added);
233 printf("\t%9u bits currently stored in pool (max %u)\n", 233 printf("\t%9u bits currently stored in pool (max %u)\n",