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