Home | History | Annotate | Line # | Download | only in rndctl
rndctl.c revision 1.20.2.3
      1  1.20.2.3      yamt /*	$NetBSD: rndctl.c,v 1.20.2.3 2014/05/22 11:37:31 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.3      yamt __RCSID("$NetBSD: rndctl.c,v 1.20.2.3 2014/05/22 11:37:31 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.3      yamt 	if (pwrite(fd, &rszero, sizeof(rszero), (off_t)0) != sizeof(rszero))
    207  1.20.2.1      yamt 		err(1, "overwrite");
    208  1.20.2.1      yamt 	fsync_range(fd, FDATASYNC|FDISKSYNC, (off_t)0, (off_t)0);
    209  1.20.2.1      yamt 	close(fd);
    210  1.20.2.1      yamt 
    211  1.20.2.1      yamt 	SHA1Init(&s);
    212  1.20.2.1      yamt 	SHA1Update(&s, (uint8_t *)&rs.entropy, sizeof(rs.entropy));
    213  1.20.2.1      yamt 	SHA1Update(&s, rs.data, sizeof(rs.data));
    214  1.20.2.1      yamt 	SHA1Final(digest, &s);
    215  1.20.2.1      yamt 
    216  1.20.2.1      yamt 	if (memcmp(digest, rs.digest, sizeof(digest))) {
    217  1.20.2.1      yamt 		errx(1, "bad digest");
    218  1.20.2.1      yamt 	}
    219  1.20.2.1      yamt 
    220  1.20.2.1      yamt 	rd.len = MIN(sizeof(rd.data), sizeof(rs.data));
    221  1.20.2.1      yamt 	rd.entropy = rs.entropy;
    222  1.20.2.1      yamt 	memcpy(rd.data, rs.data, MIN(sizeof(rd.data), sizeof(rs.data)));
    223  1.20.2.1      yamt 
    224  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDWR, 0644);
    225  1.20.2.1      yamt 	if (fd < 0) {
    226  1.20.2.1      yamt 		err(1, "device open");
    227  1.20.2.1      yamt 	}
    228  1.20.2.1      yamt 
    229  1.20.2.1      yamt 	if (ioctl(fd, RNDADDDATA, &rd) < 0) {
    230  1.20.2.1      yamt 		err(1, "ioctl");
    231  1.20.2.1      yamt 	}
    232  1.20.2.1      yamt 	close(fd);
    233  1.20.2.1      yamt }
    234  1.20.2.1      yamt 
    235  1.20.2.1      yamt static void
    236       1.1  explorer do_ioctl(rndctl_t *rctl)
    237       1.1  explorer {
    238       1.1  explorer 	int fd;
    239       1.1  explorer 	int res;
    240       1.1  explorer 
    241  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDONLY, 0644);
    242       1.1  explorer 	if (fd < 0)
    243       1.1  explorer 		err(1, "open");
    244       1.1  explorer 
    245       1.1  explorer 	res = ioctl(fd, RNDCTL, rctl);
    246       1.1  explorer 	if (res < 0)
    247       1.1  explorer 		err(1, "ioctl(RNDCTL)");
    248       1.1  explorer 
    249       1.1  explorer 	close(fd);
    250       1.1  explorer }
    251       1.1  explorer 
    252      1.20     joerg static char *
    253       1.1  explorer strflags(u_int32_t fl)
    254       1.1  explorer {
    255       1.1  explorer 	static char str[512];
    256       1.1  explorer 
    257       1.1  explorer 	str[0] = 0;
    258       1.1  explorer 	if (fl & RND_FLAG_NO_ESTIMATE)
    259       1.6  sommerfe 		;
    260       1.9     enami 	else
    261      1.16    itojun 		strlcat(str, "estimate", sizeof(str));
    262       1.9     enami 
    263       1.1  explorer 	if (fl & RND_FLAG_NO_COLLECT)
    264       1.6  sommerfe 		;
    265       1.6  sommerfe 	else {
    266       1.6  sommerfe 		if (str[0])
    267      1.16    itojun 			strlcat(str, ", ", sizeof(str));
    268      1.16    itojun 		strlcat(str, "collect", sizeof(str));
    269       1.6  sommerfe 	}
    270       1.9     enami 
    271       1.9     enami 	return (str);
    272       1.1  explorer }
    273       1.1  explorer 
    274       1.6  sommerfe #define HEADER "Source                 Bits Type      Flags\n"
    275       1.1  explorer 
    276      1.20     joerg static void
    277       1.1  explorer do_list(int all, u_int32_t type, char *name)
    278       1.1  explorer {
    279       1.9     enami 	rndstat_t rstat;
    280       1.9     enami 	rndstat_name_t rstat_name;
    281       1.9     enami 	int fd;
    282       1.9     enami 	int res;
    283      1.19     lukem 	uint32_t i;
    284       1.9     enami 	u_int32_t start;
    285       1.1  explorer 
    286  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDONLY, 0644);
    287       1.1  explorer 	if (fd < 0)
    288       1.1  explorer 		err(1, "open");
    289       1.1  explorer 
    290       1.1  explorer 	if (all == 0 && type == 0xff) {
    291      1.14    itojun 		strncpy(rstat_name.name, name, sizeof(rstat_name.name));
    292       1.1  explorer 		res = ioctl(fd, RNDGETSRCNAME, &rstat_name);
    293       1.1  explorer 		if (res < 0)
    294       1.1  explorer 			err(1, "ioctl(RNDGETSRCNAME)");
    295       1.1  explorer 		printf(HEADER);
    296       1.6  sommerfe 		printf("%-16s %10u %-4s %s\n",
    297       1.9     enami 		    rstat_name.source.name,
    298       1.9     enami 		    rstat_name.source.total,
    299       1.9     enami 		    find_name(rstat_name.source.type),
    300       1.9     enami 		    strflags(rstat_name.source.flags));
    301       1.1  explorer 		close(fd);
    302       1.1  explorer 		return;
    303       1.1  explorer 	}
    304       1.1  explorer 
    305       1.1  explorer 	/*
    306       1.9     enami 	 * Run through all the devices present in the system, and either
    307       1.1  explorer 	 * print out ones that match, or print out all of them.
    308       1.1  explorer 	 */
    309       1.1  explorer 	printf(HEADER);
    310       1.1  explorer 	start = 0;
    311       1.1  explorer 	for (;;) {
    312       1.1  explorer 		rstat.count = RND_MAXSTATCOUNT;
    313       1.1  explorer 		rstat.start = start;
    314       1.1  explorer 		res = ioctl(fd, RNDGETSRCNUM, &rstat);
    315       1.1  explorer 		if (res < 0)
    316       1.1  explorer 			err(1, "ioctl(RNDGETSRCNUM)");
    317       1.9     enami 
    318       1.1  explorer 		if (rstat.count == 0)
    319       1.1  explorer 			break;
    320       1.9     enami 
    321      1.19     lukem 		for (i = 0; i < rstat.count; i++) {
    322       1.9     enami 			if (all != 0 ||
    323      1.19     lukem 			    type == rstat.source[i].type)
    324       1.6  sommerfe 				printf("%-16s %10u %-4s %s\n",
    325      1.19     lukem 				    rstat.source[i].name,
    326      1.19     lukem 				    rstat.source[i].total,
    327      1.19     lukem 				    find_name(rstat.source[i].type),
    328      1.19     lukem 				    strflags(rstat.source[i].flags));
    329       1.1  explorer 		}
    330       1.1  explorer 		start += rstat.count;
    331       1.1  explorer 	}
    332       1.1  explorer 
    333       1.1  explorer 	close(fd);
    334       1.1  explorer }
    335       1.1  explorer 
    336      1.20     joerg static void
    337      1.20     joerg do_stats(void)
    338       1.6  sommerfe {
    339       1.6  sommerfe 	rndpoolstat_t rs;
    340       1.6  sommerfe 	int fd;
    341       1.9     enami 
    342  1.20.2.2      yamt 	fd = open(_PATH_URANDOM, O_RDONLY, 0644);
    343       1.6  sommerfe 	if (fd < 0)
    344       1.6  sommerfe 		err(1, "open");
    345       1.9     enami 
    346       1.6  sommerfe 	if (ioctl(fd, RNDGETPOOLSTAT, &rs) < 0)
    347       1.6  sommerfe 		err(1, "ioctl(RNDGETPOOLSTAT)");
    348       1.6  sommerfe 
    349      1.12     enami 	printf("\t%9u bits mixed into pool\n", rs.added);
    350      1.12     enami 	printf("\t%9u bits currently stored in pool (max %u)\n",
    351       1.6  sommerfe 	    rs.curentropy, rs.maxentropy);
    352      1.12     enami 	printf("\t%9u bits of entropy discarded due to full pool\n",
    353       1.6  sommerfe 	    rs.discarded);
    354      1.12     enami 	printf("\t%9u hard-random bits generated\n", rs.removed);
    355      1.12     enami 	printf("\t%9u pseudo-random bits generated\n", rs.generated);
    356       1.6  sommerfe 
    357       1.6  sommerfe 	close(fd);
    358       1.6  sommerfe }
    359       1.6  sommerfe 
    360       1.1  explorer int
    361       1.1  explorer main(int argc, char **argv)
    362       1.1  explorer {
    363       1.9     enami 	rndctl_t rctl;
    364       1.9     enami 	int ch, cmd, lflag, mflag, sflag;
    365       1.1  explorer 	u_int32_t type;
    366       1.9     enami 	char name[16];
    367  1.20.2.1      yamt 	const char *filename = NULL;
    368       1.1  explorer 
    369       1.1  explorer 	rctl.mask = 0;
    370       1.1  explorer 	rctl.flags = 0;
    371       1.1  explorer 
    372       1.1  explorer 	cmd = 0;
    373       1.1  explorer 	lflag = 0;
    374       1.1  explorer 	mflag = 0;
    375       1.7      joda 	sflag = 0;
    376       1.2  explorer 	type = 0xff;
    377       1.1  explorer 
    378  1.20.2.1      yamt 	while ((ch = getopt(argc, argv, "CES:L:celt:d:s")) != -1) {
    379       1.9     enami 		switch (ch) {
    380       1.1  explorer 		case 'C':
    381       1.1  explorer 			rctl.flags |= RND_FLAG_NO_COLLECT;
    382       1.1  explorer 			rctl.mask |= RND_FLAG_NO_COLLECT;
    383       1.1  explorer 			mflag++;
    384       1.1  explorer 			break;
    385       1.1  explorer 		case 'E':
    386       1.1  explorer 			rctl.flags |= RND_FLAG_NO_ESTIMATE;
    387       1.1  explorer 			rctl.mask |= RND_FLAG_NO_ESTIMATE;
    388       1.1  explorer 			mflag++;
    389       1.1  explorer 			break;
    390  1.20.2.1      yamt 		case 'L':
    391  1.20.2.1      yamt 			if (cmd != 0)
    392  1.20.2.1      yamt 				usage();
    393  1.20.2.1      yamt 			cmd = 'L';
    394  1.20.2.1      yamt 			filename = optarg;
    395  1.20.2.1      yamt 			break;
    396  1.20.2.1      yamt 		case 'S':
    397  1.20.2.1      yamt 			if (cmd != 0)
    398  1.20.2.1      yamt 				usage();
    399  1.20.2.1      yamt 			cmd = 'S';
    400  1.20.2.1      yamt 			filename = optarg;
    401  1.20.2.1      yamt 			break;
    402       1.1  explorer 		case 'c':
    403       1.1  explorer 			rctl.flags &= ~RND_FLAG_NO_COLLECT;
    404       1.1  explorer 			rctl.mask |= RND_FLAG_NO_COLLECT;
    405       1.1  explorer 			mflag++;
    406       1.1  explorer 			break;
    407       1.1  explorer 		case 'e':
    408       1.1  explorer 			rctl.flags &= ~RND_FLAG_NO_ESTIMATE;
    409       1.1  explorer 			rctl.mask |= RND_FLAG_NO_ESTIMATE;
    410       1.1  explorer 			mflag++;
    411       1.1  explorer 			break;
    412       1.1  explorer 		case 'l':
    413       1.1  explorer 			lflag++;
    414       1.1  explorer 			break;
    415       1.1  explorer 		case 't':
    416       1.1  explorer 			if (cmd != 0)
    417       1.1  explorer 				usage();
    418       1.1  explorer 			cmd = 't';
    419       1.1  explorer 
    420       1.1  explorer 			type = find_type(optarg);
    421       1.1  explorer 			break;
    422       1.1  explorer 		case 'd':
    423       1.1  explorer 			if (cmd != 0)
    424       1.1  explorer 				usage();
    425       1.1  explorer 			cmd = 'd';
    426       1.1  explorer 
    427       1.1  explorer 			type = 0xff;
    428      1.14    itojun 			strlcpy(name, optarg, sizeof(name));
    429       1.1  explorer 			break;
    430       1.6  sommerfe 		case 's':
    431       1.6  sommerfe 			sflag++;
    432       1.6  sommerfe 			break;
    433       1.1  explorer 		case '?':
    434       1.1  explorer 		default:
    435       1.1  explorer 			usage();
    436       1.1  explorer 		}
    437      1.18       apb 	}
    438      1.18       apb 	argc -= optind;
    439      1.18       apb 	argv += optind;
    440      1.18       apb 
    441      1.18       apb 	/*
    442      1.18       apb 	 * No leftover non-option arguments.
    443      1.18       apb 	 */
    444      1.18       apb 	if (argc > 0)
    445      1.18       apb 		usage();
    446       1.1  explorer 
    447       1.1  explorer 	/*
    448  1.20.2.1      yamt 	 * Save.
    449  1.20.2.1      yamt 	 */
    450  1.20.2.1      yamt 	if (cmd == 'S') {
    451  1.20.2.1      yamt 		do_save(filename);
    452  1.20.2.1      yamt 		exit(0);
    453  1.20.2.1      yamt 	}
    454  1.20.2.1      yamt 
    455  1.20.2.1      yamt 	/*
    456  1.20.2.1      yamt 	 * Load.
    457  1.20.2.1      yamt 	 */
    458  1.20.2.1      yamt 	if (cmd == 'L') {
    459  1.20.2.1      yamt 		do_load(filename);
    460  1.20.2.1      yamt 		exit(0);
    461  1.20.2.1      yamt 	}
    462  1.20.2.1      yamt 
    463  1.20.2.1      yamt 	/*
    464       1.9     enami 	 * Cannot list and modify at the same time.
    465       1.1  explorer 	 */
    466       1.6  sommerfe 	if ((lflag != 0 || sflag != 0) && mflag != 0)
    467       1.1  explorer 		usage();
    468       1.1  explorer 
    469       1.1  explorer 	/*
    470       1.9     enami 	 * Bomb out on no-ops.
    471       1.1  explorer 	 */
    472       1.6  sommerfe 	if (lflag == 0 && mflag == 0 && sflag == 0)
    473       1.1  explorer 		usage();
    474       1.1  explorer 
    475       1.1  explorer 	/*
    476       1.9     enami 	 * If not listing, we need a device name or a type.
    477       1.1  explorer 	 */
    478       1.6  sommerfe 	if (lflag == 0 && cmd == 0 && sflag == 0)
    479       1.1  explorer 		usage();
    480       1.1  explorer 
    481       1.1  explorer 	/*
    482       1.9     enami 	 * Modify request.
    483       1.1  explorer 	 */
    484       1.1  explorer 	if (mflag != 0) {
    485       1.1  explorer 		rctl.type = type;
    486      1.14    itojun 		strncpy(rctl.name, name, sizeof(rctl.name));
    487       1.1  explorer 		do_ioctl(&rctl);
    488       1.1  explorer 
    489       1.1  explorer 		exit(0);
    490       1.1  explorer 	}
    491       1.1  explorer 
    492       1.1  explorer 	/*
    493       1.9     enami 	 * List sources.
    494       1.1  explorer 	 */
    495       1.1  explorer 	if (lflag != 0)
    496       1.1  explorer 		do_list(cmd == 0, type, name);
    497       1.1  explorer 
    498       1.6  sommerfe 	if (sflag != 0)
    499       1.6  sommerfe 		do_stats();
    500       1.9     enami 
    501       1.9     enami 	exit(0);
    502       1.1  explorer }
    503