1 /*
2  * Copyright (c) 1995
3  *	Mark Murray.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY MARK MURRAY AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.sbin/rndcontrol/rndcontrol.c,v 1.11.2.1 2000/05/10 02:04:44 obrien Exp $
27  * $DragonFly: src/usr.sbin/rndcontrol/rndcontrol.c,v 1.2 2003/06/17 04:30:02 dillon Exp $
28  */
29 
30 #include <sys/random.h>
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <sys/ioctl.h>
37 
38 static void
39 usage()
40 {
41 	fprintf(stderr, "usage: rndcontrol [-q] [-s irq_no] [-c irq_no]\n");
42 	exit(1);
43 }
44 
45 int
46 main(int argc, char *argv[])
47 {
48 	int verbose, ch, fd, result, i;
49 	u_int16_t irq;
50 
51 	verbose = 1;
52 
53 	fd = open("/dev/random", O_RDONLY, 0);
54 	if (fd == -1) {
55 		warn("/dev/random");
56 		return (1);
57 	}
58 	else {
59 		while ((ch = getopt(argc, argv, "qs:c:")) != -1)
60 			switch (ch) {
61 			case 'q':
62 				verbose = 0;
63 				break;
64 			case 's':
65 				irq = (u_int16_t)atoi(optarg);
66 				if (verbose)
67 					printf("%s: setting irq %d\n", argv[0], irq);
68 				result = ioctl(fd, MEM_SETIRQ, (char *)&irq);
69 				if (result == -1) {
70 					warn("%s", argv[0]);
71 					return (1);
72 				}
73 				break;
74 			case 'c':
75 				irq = (u_int16_t)atoi(optarg);
76 				if (verbose)
77 					printf("%s: clearing irq %d\n", argv[0], irq);
78 				result = ioctl(fd, MEM_CLEARIRQ, (char *)&irq);
79 				if (result == -1) {
80 					warn("%s", argv[0]);
81 					return (1);
82 				}
83 				break;
84 			case '?':
85 			default:
86 				usage();
87 			}
88 		}
89 		if (verbose) {
90 			result = ioctl(fd, MEM_RETURNIRQ, (char *)&irq);
91 			if (result == -1) {
92 				warn("%s", argv[0]);
93 				return (1);
94 			}
95 			printf("%s: interrupts in use:", argv[0]);
96 			for (i = 0; i < 16; i++)
97 				if (irq & (1 << i))
98 					printf(" %d", i);
99 			printf("\n");
100 		}
101 		argc -= optind;
102 		argv += optind;
103 
104 		if (argc) {
105 			fprintf(stderr, "%s: unknown argument(s): ", argv[-optind]);
106 			for (i = 0; i < argc; i++)
107 				fprintf(stderr, " %s", argv[i]);
108 			fprintf(stderr, "\n");
109 		}
110 
111 	return 0;
112 }
113