Home | History | Annotate | Line # | Download | only in rndctl
rndctl.c revision 1.20.2.2
      1  1.20.2.2      yamt /*	$NetBSD: rndctl.c,v 1.20.2.2 2012/10/30 18:59:32 yamt 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.15       agc #include <sys/cdefs.h>
     32  1.20.2.1      yamt #include <sys/types.h>
     33  1.20.2.1      yamt #include <sha1.h>
     34      1.15       agc 
     35      1.15       agc #ifndef lint
     36  1.20.2.2      yamt __RCSID("$NetBSD: rndctl.c,v 1.20.2.2 2012/10/30 18:59:32 yamt Exp $");
     37      1.15       agc #endif
     38      1.15       agc 
     39       1.1  explorer 
     40      1.11     enami #include <sys/types.h>
     41      1.11     enami #include <sys/ioctl.h>
     42  1.20.2.1      yamt #include <sys/param.h>
     43      1.11     enami #include <sys/rnd.h>
     44      1.11     enami 
     45       1.1  explorer #include <stdio.h>
     46       1.1  explorer #include <stdlib.h>
     47       1.1  explorer #include <unistd.h>
     48       1.1  explorer #include <fcntl.h>
     49       1.1  explorer #include <errno.h>
     50       1.1  explorer #include <err.h>
     51  1.20.2.2      yamt #include <paths.h>
     52       1.2  explorer #include <string.h>
     53       1.1  explorer 
     54       1.1  explorer typedef struct {
     55      1.17  christos 	const char *a_name;
     56       1.9     enami 	u_int32_t a_type;
     57       1.1  explorer } arg_t;
     58       1.1  explorer 
     59      1.20     joerg static const arg_t source_types[] = {
     60       1.6  sommerfe 	{ "???",     RND_TYPE_UNKNOWN },
     61       1.1  explorer 	{ "disk",    RND_TYPE_DISK },
     62       1.1  explorer 	{ "net",     RND_TYPE_NET },
     63       1.1  explorer 	{ "tape",    RND_TYPE_TAPE },
     64       1.1  explorer 	{ "tty",     RND_TYPE_TTY },
     65      1.11     enami 	{ "rng",     RND_TYPE_RNG },
     66  1.20.2.1      yamt 	{ "skew",    RND_TYPE_SKEW },
     67  1.20.2.1      yamt 	{ "env",     RND_TYPE_ENV },
     68  1.20.2.1      yamt 	{ "vm",      RND_TYPE_VM },
     69  1.20.2.1      yamt 	{ "power",   RND_TYPE_POWER },
     70       1.1  explorer 	{ NULL,      0 }
     71       1.1  explorer };
     72       1.1  explorer 
     73      1.20     joerg __dead static void usage(void);
     74      1.20     joerg static u_int32_t find_type(const char *name);
     75      1.20     joerg static const char *find_name(u_int32_t);
     76      1.20     joerg static void do_ioctl(rndctl_t *);
     77      1.20     joerg static char * strflags(u_int32_t);
     78      1.20     joerg static void do_list(int, u_int32_t, char *);
     79      1.20     joerg static void do_stats(void);
     80       1.2  explorer 
     81       1.2  explorer static void
     82       1.1  explorer usage(void)
     83       1.1  explorer {
     84       1.9     enami 
     85      1.18       apb 	fprintf(stderr, "usage: %s -CEce [-d devname | -t devtype]\n",
     86      1.11     enami 	    getprogname());
     87      1.18       apb 	fprintf(stderr, "       %s -ls [-d devname | -t devtype]\n",
     88      1.11     enami 	    getprogname());
     89  1.20.2.1      yamt 	fprintf(stderr, "	%s -[L|S] save-file\n", getprogname());
     90       1.5   mycroft 	exit(1);
     91       1.1  explorer }
     92       1.1  explorer 
     93      1.20     joerg static u_int32_t
     94      1.20     joerg find_type(const char *name)
     95       1.1  explorer {
     96      1.20     joerg 	const arg_t *a;
     97       1.1  explorer 
     98       1.1  explorer 	a = source_types;
     99       1.9     enami 
    100       1.9     enami 	while (a->a_name != NULL) {
    101       1.9     enami 		if (strcmp(a->a_name, name) == 0)
    102       1.9     enami 			return (a->a_type);
    103       1.1  explorer 		a++;
    104       1.1  explorer 	}
    105       1.1  explorer 
    106      1.10     enami 	errx(1, "device name %s unknown", name);
    107       1.9     enami 	return (0);
    108       1.1  explorer }
    109       1.1  explorer 
    110      1.20     joerg static const char *
    111       1.1  explorer find_name(u_int32_t type)
    112       1.1  explorer {
    113      1.20     joerg 	const arg_t *a;
    114       1.1  explorer 
    115       1.1  explorer 	a = source_types;
    116       1.9     enami 
    117       1.9     enami 	while (a->a_name != NULL) {
    118       1.9     enami 		if (type == a->a_type)
    119       1.9     enami 			return (a->a_name);
    120       1.1  explorer 		a++;
    121       1.1  explorer 	}
    122       1.1  explorer 
    123      1.10     enami 	warnx("device type %u unknown", type);
    124      1.10     enami 	return ("???");
    125       1.1  explorer }
    126       1.1  explorer 
    127      1.20     joerg static void
    128  1.20.2.1      yamt do_save(const char *const filename)
    129  1.20.2.1      yamt {
    130  1.20.2.1      yamt 	int est1, est2;
    131  1.20.2.1      yamt 	rndpoolstat_t rp;
    132  1.20.2.1      yamt 	rndsave_t rs;
    133  1.20.2.1      yamt 	SHA1_CTX s;
    134  1.20.2.1      yamt 
    135  1.20.2.1      yamt 	int fd;
    136  1.20.2.1      yamt 
    137  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDONLY, 0644);
    138  1.20.2.1      yamt 	if (fd < 0) {
    139  1.20.2.1      yamt 		err(1, "device open");
    140  1.20.2.1      yamt 	}
    141  1.20.2.2      yamt 
    142  1.20.2.1      yamt 	if (ioctl(fd, RNDGETPOOLSTAT, &rp) < 0) {
    143  1.20.2.1      yamt 		err(1, "ioctl(RNDGETPOOLSTAT)");
    144  1.20.2.1      yamt 	}
    145  1.20.2.1      yamt 
    146  1.20.2.1      yamt 	est1 = rp.curentropy;
    147  1.20.2.1      yamt 
    148  1.20.2.1      yamt 	if (read(fd, rs.data, sizeof(rs.data)) != sizeof(rs.data)) {
    149  1.20.2.1      yamt 		err(1, "entropy read");
    150  1.20.2.1      yamt 	}
    151  1.20.2.1      yamt 
    152  1.20.2.1      yamt 	if (ioctl(fd, RNDGETPOOLSTAT, &rp) < 0) {
    153  1.20.2.1      yamt 		err(1, "ioctl(RNDGETPOOLSTAT)");
    154  1.20.2.1      yamt 	}
    155  1.20.2.1      yamt 
    156  1.20.2.1      yamt 	est2 = rp.curentropy;
    157  1.20.2.1      yamt 
    158  1.20.2.1      yamt 	if (est1 - est2 < 0) {
    159  1.20.2.1      yamt 		rs.entropy = 0;
    160  1.20.2.1      yamt 	} else {
    161  1.20.2.1      yamt 		rs.entropy = est1 - est2;
    162  1.20.2.1      yamt 	}
    163  1.20.2.1      yamt 
    164  1.20.2.1      yamt 	SHA1Init(&s);
    165  1.20.2.1      yamt 	SHA1Update(&s, (uint8_t *)&rs.entropy, sizeof(rs.entropy));
    166  1.20.2.1      yamt 	SHA1Update(&s, rs.data, sizeof(rs.data));
    167  1.20.2.1      yamt 	SHA1Final(rs.digest, &s);
    168  1.20.2.1      yamt 
    169  1.20.2.1      yamt 	close(fd);
    170  1.20.2.1      yamt 	unlink(filename);
    171  1.20.2.1      yamt 	fd = open(filename, O_CREAT|O_EXCL|O_WRONLY, 0600);
    172  1.20.2.1      yamt 	if (fd < 0) {
    173  1.20.2.1      yamt 		err(1, "output open");
    174  1.20.2.1      yamt 	}
    175  1.20.2.2      yamt 
    176  1.20.2.1      yamt 	if (write(fd, &rs, sizeof(rs)) != sizeof(rs)) {
    177  1.20.2.1      yamt 		unlink(filename);
    178  1.20.2.1      yamt 		fsync_range(fd, FDATASYNC|FDISKSYNC, (off_t)0, (off_t)0);
    179  1.20.2.1      yamt 		err(1, "write");
    180  1.20.2.1      yamt 	}
    181  1.20.2.1      yamt 	fsync_range(fd, FDATASYNC|FDISKSYNC, (off_t)0, (off_t)0);
    182  1.20.2.1      yamt 	close(fd);
    183  1.20.2.1      yamt }
    184  1.20.2.1      yamt 
    185  1.20.2.1      yamt static void
    186  1.20.2.1      yamt do_load(const char *const filename)
    187  1.20.2.1      yamt {
    188  1.20.2.1      yamt 	int fd;
    189  1.20.2.1      yamt 	rndsave_t rs, rszero;
    190  1.20.2.1      yamt 	rnddata_t rd;
    191  1.20.2.1      yamt 	SHA1_CTX s;
    192  1.20.2.1      yamt 	uint8_t digest[SHA1_DIGEST_LENGTH];
    193  1.20.2.1      yamt 
    194  1.20.2.1      yamt 	fd = open(filename, O_RDWR, 0600);
    195  1.20.2.1      yamt 	if (fd < 0) {
    196  1.20.2.1      yamt 		err(1, "input open");
    197  1.20.2.1      yamt 	}
    198  1.20.2.1      yamt 
    199  1.20.2.1      yamt 	unlink(filename);
    200  1.20.2.1      yamt 
    201  1.20.2.1      yamt 	if (read(fd, &rs, sizeof(rs)) != sizeof(rs)) {
    202  1.20.2.1      yamt 		err(1, "read");
    203  1.20.2.1      yamt 	}
    204  1.20.2.1      yamt 
    205  1.20.2.1      yamt 	memset(&rszero, 0, sizeof(rszero));
    206  1.20.2.1      yamt 	if (write(fd, &rszero, sizeof(rszero) != sizeof(rszero))) {
    207  1.20.2.1      yamt 		err(1, "overwrite");
    208  1.20.2.1      yamt 	}
    209  1.20.2.1      yamt 	fsync_range(fd, FDATASYNC|FDISKSYNC, (off_t)0, (off_t)0);
    210  1.20.2.1      yamt 	close(fd);
    211  1.20.2.1      yamt 
    212  1.20.2.1      yamt 	SHA1Init(&s);
    213  1.20.2.1      yamt 	SHA1Update(&s, (uint8_t *)&rs.entropy, sizeof(rs.entropy));
    214  1.20.2.1      yamt 	SHA1Update(&s, rs.data, sizeof(rs.data));
    215  1.20.2.1      yamt 	SHA1Final(digest, &s);
    216  1.20.2.1      yamt 
    217  1.20.2.1      yamt 	if (memcmp(digest, rs.digest, sizeof(digest))) {
    218  1.20.2.1      yamt 		errx(1, "bad digest");
    219  1.20.2.1      yamt 	}
    220  1.20.2.1      yamt 
    221  1.20.2.1      yamt 	rd.len = MIN(sizeof(rd.data), sizeof(rs.data));
    222  1.20.2.1      yamt 	rd.entropy = rs.entropy;
    223  1.20.2.1      yamt 	memcpy(rd.data, rs.data, MIN(sizeof(rd.data), sizeof(rs.data)));
    224  1.20.2.1      yamt 
    225  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDWR, 0644);
    226  1.20.2.1      yamt 	if (fd < 0) {
    227  1.20.2.1      yamt 		err(1, "device open");
    228  1.20.2.1      yamt 	}
    229  1.20.2.1      yamt 
    230  1.20.2.1      yamt 	if (ioctl(fd, RNDADDDATA, &rd) < 0) {
    231  1.20.2.1      yamt 		err(1, "ioctl");
    232  1.20.2.1      yamt 	}
    233  1.20.2.1      yamt 	close(fd);
    234  1.20.2.1      yamt }
    235  1.20.2.1      yamt 
    236  1.20.2.1      yamt static void
    237       1.1  explorer do_ioctl(rndctl_t *rctl)
    238       1.1  explorer {
    239       1.1  explorer 	int fd;
    240       1.1  explorer 	int res;
    241       1.1  explorer 
    242  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDONLY, 0644);
    243       1.1  explorer 	if (fd < 0)
    244       1.1  explorer 		err(1, "open");
    245       1.1  explorer 
    246       1.1  explorer 	res = ioctl(fd, RNDCTL, rctl);
    247       1.1  explorer 	if (res < 0)
    248       1.1  explorer 		err(1, "ioctl(RNDCTL)");
    249       1.1  explorer 
    250       1.1  explorer 	close(fd);
    251       1.1  explorer }
    252       1.1  explorer 
    253      1.20     joerg static char *
    254       1.1  explorer strflags(u_int32_t fl)
    255       1.1  explorer {
    256       1.1  explorer 	static char str[512];
    257       1.1  explorer 
    258       1.1  explorer 	str[0] = 0;
    259       1.1  explorer 	if (fl & RND_FLAG_NO_ESTIMATE)
    260       1.6  sommerfe 		;
    261       1.9     enami 	else
    262      1.16    itojun 		strlcat(str, "estimate", sizeof(str));
    263       1.9     enami 
    264       1.1  explorer 	if (fl & RND_FLAG_NO_COLLECT)
    265       1.6  sommerfe 		;
    266       1.6  sommerfe 	else {
    267       1.6  sommerfe 		if (str[0])
    268      1.16    itojun 			strlcat(str, ", ", sizeof(str));
    269      1.16    itojun 		strlcat(str, "collect", sizeof(str));
    270       1.6  sommerfe 	}
    271       1.9     enami 
    272       1.9     enami 	return (str);
    273       1.1  explorer }
    274       1.1  explorer 
    275       1.6  sommerfe #define HEADER "Source                 Bits Type      Flags\n"
    276       1.1  explorer 
    277      1.20     joerg static void
    278       1.1  explorer do_list(int all, u_int32_t type, char *name)
    279       1.1  explorer {
    280       1.9     enami 	rndstat_t rstat;
    281       1.9     enami 	rndstat_name_t rstat_name;
    282       1.9     enami 	int fd;
    283       1.9     enami 	int res;
    284      1.19     lukem 	uint32_t i;
    285       1.9     enami 	u_int32_t start;
    286       1.1  explorer 
    287  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDONLY, 0644);
    288       1.1  explorer 	if (fd < 0)
    289       1.1  explorer 		err(1, "open");
    290       1.1  explorer 
    291       1.1  explorer 	if (all == 0 && type == 0xff) {
    292      1.14    itojun 		strncpy(rstat_name.name, name, sizeof(rstat_name.name));
    293       1.1  explorer 		res = ioctl(fd, RNDGETSRCNAME, &rstat_name);
    294       1.1  explorer 		if (res < 0)
    295       1.1  explorer 			err(1, "ioctl(RNDGETSRCNAME)");
    296       1.1  explorer 		printf(HEADER);
    297       1.6  sommerfe 		printf("%-16s %10u %-4s %s\n",
    298       1.9     enami 		    rstat_name.source.name,
    299       1.9     enami 		    rstat_name.source.total,
    300       1.9     enami 		    find_name(rstat_name.source.type),
    301       1.9     enami 		    strflags(rstat_name.source.flags));
    302       1.1  explorer 		close(fd);
    303       1.1  explorer 		return;
    304       1.1  explorer 	}
    305       1.1  explorer 
    306       1.1  explorer 	/*
    307       1.9     enami 	 * Run through all the devices present in the system, and either
    308       1.1  explorer 	 * print out ones that match, or print out all of them.
    309       1.1  explorer 	 */
    310       1.1  explorer 	printf(HEADER);
    311       1.1  explorer 	start = 0;
    312       1.1  explorer 	for (;;) {
    313       1.1  explorer 		rstat.count = RND_MAXSTATCOUNT;
    314       1.1  explorer 		rstat.start = start;
    315       1.1  explorer 		res = ioctl(fd, RNDGETSRCNUM, &rstat);
    316       1.1  explorer 		if (res < 0)
    317       1.1  explorer 			err(1, "ioctl(RNDGETSRCNUM)");
    318       1.9     enami 
    319       1.1  explorer 		if (rstat.count == 0)
    320       1.1  explorer 			break;
    321       1.9     enami 
    322      1.19     lukem 		for (i = 0; i < rstat.count; i++) {
    323       1.9     enami 			if (all != 0 ||
    324      1.19     lukem 			    type == rstat.source[i].type)
    325       1.6  sommerfe 				printf("%-16s %10u %-4s %s\n",
    326      1.19     lukem 				    rstat.source[i].name,
    327      1.19     lukem 				    rstat.source[i].total,
    328      1.19     lukem 				    find_name(rstat.source[i].type),
    329      1.19     lukem 				    strflags(rstat.source[i].flags));
    330       1.1  explorer 		}
    331       1.1  explorer 		start += rstat.count;
    332       1.1  explorer 	}
    333       1.1  explorer 
    334       1.1  explorer 	close(fd);
    335       1.1  explorer }
    336       1.1  explorer 
    337      1.20     joerg static void
    338      1.20     joerg do_stats(void)
    339       1.6  sommerfe {
    340       1.6  sommerfe 	rndpoolstat_t rs;
    341       1.6  sommerfe 	int fd;
    342       1.9     enami 
    343  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDONLY, 0644);
    344       1.6  sommerfe 	if (fd < 0)
    345       1.6  sommerfe 		err(1, "open");
    346       1.9     enami 
    347       1.6  sommerfe 	if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0)
    348       1.6  sommerfe 		err(1, "ioctl(RNDGETPOOLSTAT)");
    349       1.6  sommerfe 
    350      1.12     enami 	printf("\t%9u bits mixed into pool\n", rs.added);
    351      1.12     enami 	printf("\t%9u bits currently stored in pool (max %u)\n",
    352       1.6  sommerfe 	    rs.curentropy, rs.maxentropy);
    353      1.12     enami 	printf("\t%9u bits of entropy discarded due to full pool\n",
    354       1.6  sommerfe 	    rs.discarded);
    355      1.12     enami 	printf("\t%9u hard-random bits generated\n", rs.removed);
    356      1.12     enami 	printf("\t%9u pseudo-random bits generated\n", rs.generated);
    357       1.6  sommerfe 
    358       1.6  sommerfe 	close(fd);
    359       1.6  sommerfe }
    360       1.6  sommerfe 
    361       1.1  explorer int
    362       1.1  explorer main(int argc, char **argv)
    363       1.1  explorer {
    364       1.9     enami 	rndctl_t rctl;
    365       1.9     enami 	int ch, cmd, lflag, mflag, sflag;
    366       1.1  explorer 	u_int32_t type;
    367       1.9     enami 	char name[16];
    368  1.20.2.1      yamt 	const char *filename = NULL;
    369       1.1  explorer 
    370       1.1  explorer 	rctl.mask = 0;
    371       1.1  explorer 	rctl.flags = 0;
    372       1.1  explorer 
    373       1.1  explorer 	cmd = 0;
    374       1.1  explorer 	lflag = 0;
    375       1.1  explorer 	mflag = 0;
    376       1.7      joda 	sflag = 0;
    377       1.2  explorer 	type = 0xff;
    378       1.1  explorer 
    379  1.20.2.1      yamt 	while ((ch = getopt(argc, argv, "CES:L:celt:d:s")) != -1) {
    380       1.9     enami 		switch (ch) {
    381       1.1  explorer 		case 'C':
    382       1.1  explorer 			rctl.flags |= RND_FLAG_NO_COLLECT;
    383       1.1  explorer 			rctl.mask |= RND_FLAG_NO_COLLECT;
    384       1.1  explorer 			mflag++;
    385       1.1  explorer 			break;
    386       1.1  explorer 		case 'E':
    387       1.1  explorer 			rctl.flags |= RND_FLAG_NO_ESTIMATE;
    388       1.1  explorer 			rctl.mask |= RND_FLAG_NO_ESTIMATE;
    389       1.1  explorer 			mflag++;
    390       1.1  explorer 			break;
    391  1.20.2.1      yamt 		case 'L':
    392  1.20.2.1      yamt 			if (cmd != 0)
    393  1.20.2.1      yamt 				usage();
    394  1.20.2.1      yamt 			cmd = 'L';
    395  1.20.2.1      yamt 			filename = optarg;
    396  1.20.2.1      yamt 			break;
    397  1.20.2.1      yamt 		case 'S':
    398  1.20.2.1      yamt 			if (cmd != 0)
    399  1.20.2.1      yamt 				usage();
    400  1.20.2.1      yamt 			cmd = 'S';
    401  1.20.2.1      yamt 			filename = optarg;
    402  1.20.2.1      yamt 			break;
    403       1.1  explorer 		case 'c':
    404       1.1  explorer 			rctl.flags &= ~RND_FLAG_NO_COLLECT;
    405       1.1  explorer 			rctl.mask |= RND_FLAG_NO_COLLECT;
    406       1.1  explorer 			mflag++;
    407       1.1  explorer 			break;
    408       1.1  explorer 		case 'e':
    409       1.1  explorer 			rctl.flags &= ~RND_FLAG_NO_ESTIMATE;
    410       1.1  explorer 			rctl.mask |= RND_FLAG_NO_ESTIMATE;
    411       1.1  explorer 			mflag++;
    412       1.1  explorer 			break;
    413       1.1  explorer 		case 'l':
    414       1.1  explorer 			lflag++;
    415       1.1  explorer 			break;
    416       1.1  explorer 		case 't':
    417       1.1  explorer 			if (cmd != 0)
    418       1.1  explorer 				usage();
    419       1.1  explorer 			cmd = 't';
    420       1.1  explorer 
    421       1.1  explorer 			type = find_type(optarg);
    422       1.1  explorer 			break;
    423       1.1  explorer 		case 'd':
    424       1.1  explorer 			if (cmd != 0)
    425       1.1  explorer 				usage();
    426       1.1  explorer 			cmd = 'd';
    427       1.1  explorer 
    428       1.1  explorer 			type = 0xff;
    429      1.14    itojun 			strlcpy(name, optarg, sizeof(name));
    430       1.1  explorer 			break;
    431       1.6  sommerfe 		case 's':
    432       1.6  sommerfe 			sflag++;
    433       1.6  sommerfe 			break;
    434       1.1  explorer 		case '?':
    435       1.1  explorer 		default:
    436       1.1  explorer 			usage();
    437       1.1  explorer 		}
    438      1.18       apb 	}
    439      1.18       apb 	argc -= optind;
    440      1.18       apb 	argv += optind;
    441      1.18       apb 
    442      1.18       apb 	/*
    443      1.18       apb 	 * No leftover non-option arguments.
    444      1.18       apb 	 */
    445      1.18       apb 	if (argc > 0)
    446      1.18       apb 		usage();
    447       1.1  explorer 
    448       1.1  explorer 	/*
    449  1.20.2.1      yamt 	 * Save.
    450  1.20.2.1      yamt 	 */
    451  1.20.2.1      yamt 	if (cmd == 'S') {
    452  1.20.2.1      yamt 		do_save(filename);
    453  1.20.2.1      yamt 		exit(0);
    454  1.20.2.1      yamt 	}
    455  1.20.2.1      yamt 
    456  1.20.2.1      yamt 	/*
    457  1.20.2.1      yamt 	 * Load.
    458  1.20.2.1      yamt 	 */
    459  1.20.2.1      yamt 	if (cmd == 'L') {
    460  1.20.2.1      yamt 		do_load(filename);
    461  1.20.2.1      yamt 		exit(0);
    462  1.20.2.1      yamt 	}
    463  1.20.2.1      yamt 
    464  1.20.2.1      yamt 	/*
    465       1.9     enami 	 * Cannot list and modify at the same time.
    466       1.1  explorer 	 */
    467       1.6  sommerfe 	if ((lflag != 0 || sflag != 0) && mflag != 0)
    468       1.1  explorer 		usage();
    469       1.1  explorer 
    470       1.1  explorer 	/*
    471       1.9     enami 	 * Bomb out on no-ops.
    472       1.1  explorer 	 */
    473       1.6  sommerfe 	if (lflag == 0 && mflag == 0 && sflag == 0)
    474       1.1  explorer 		usage();
    475       1.1  explorer 
    476       1.1  explorer 	/*
    477       1.9     enami 	 * If not listing, we need a device name or a type.
    478       1.1  explorer 	 */
    479       1.6  sommerfe 	if (lflag == 0 && cmd == 0 && sflag == 0)
    480       1.1  explorer 		usage();
    481       1.1  explorer 
    482       1.1  explorer 	/*
    483       1.9     enami 	 * Modify request.
    484       1.1  explorer 	 */
    485       1.1  explorer 	if (mflag != 0) {
    486       1.1  explorer 		rctl.type = type;
    487      1.14    itojun 		strncpy(rctl.name, name, sizeof(rctl.name));
    488       1.1  explorer 		do_ioctl(&rctl);
    489       1.1  explorer 
    490       1.1  explorer 		exit(0);
    491       1.1  explorer 	}
    492       1.1  explorer 
    493       1.1  explorer 	/*
    494       1.9     enami 	 * List sources.
    495       1.1  explorer 	 */
    496       1.1  explorer 	if (lflag != 0)
    497       1.1  explorer 		do_list(cmd == 0, type, name);
    498       1.1  explorer 
    499       1.6  sommerfe 	if (sflag != 0)
    500       1.6  sommerfe 		do_stats();
    501       1.9     enami 
    502       1.9     enami 	exit(0);
    503       1.1  explorer }
    504