Initial import from FreeBSD RELENG_4:
[games.git] / usr.sbin / rndcontrol / rndcontrol.c
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
27 #ifndef lint
28 static const char rcsid[] =
29   "$FreeBSD: src/usr.sbin/rndcontrol/rndcontrol.c,v 1.11.2.1 2000/05/10 02:04:44 obrien Exp $";
30 #endif /* not lint */
31
32 #include <sys/random.h>
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
39
40 static void
41 usage()
42 {
43         fprintf(stderr, "usage: rndcontrol [-q] [-s irq_no] [-c irq_no]\n");
44         exit(1);
45 }
46
47 int
48 main(int argc, char *argv[])
49 {
50         int verbose, ch, fd, result, i;
51         u_int16_t irq;
52
53         verbose = 1;
54
55         fd = open("/dev/random", O_RDONLY, 0);
56         if (fd == -1) {
57                 warn("/dev/random");
58                 return (1);
59         }
60         else {
61                 while ((ch = getopt(argc, argv, "qs:c:")) != -1)
62                         switch (ch) {
63                         case 'q':
64                                 verbose = 0;
65                                 break;
66                         case 's':
67                                 irq = (u_int16_t)atoi(optarg);
68                                 if (verbose)
69                                         printf("%s: setting irq %d\n", argv[0], irq);
70                                 result = ioctl(fd, MEM_SETIRQ, (char *)&irq);
71                                 if (result == -1) {
72                                         warn("%s", argv[0]);
73                                         return (1);
74                                 }
75                                 break;
76                         case 'c':
77                                 irq = (u_int16_t)atoi(optarg);
78                                 if (verbose)
79                                         printf("%s: clearing irq %d\n", argv[0], irq);
80                                 result = ioctl(fd, MEM_CLEARIRQ, (char *)&irq);
81                                 if (result == -1) {
82                                         warn("%s", argv[0]);
83                                         return (1);
84                                 }
85                                 break;
86                         case '?':
87                         default:
88                                 usage();
89                         }
90                 }
91                 if (verbose) {
92                         result = ioctl(fd, MEM_RETURNIRQ, (char *)&irq);
93                         if (result == -1) {
94                                 warn("%s", argv[0]);
95                                 return (1);
96                         }
97                         printf("%s: interrupts in use:", argv[0]);
98                         for (i = 0; i < 16; i++)
99                                 if (irq & (1 << i))
100                                         printf(" %d", i);
101                         printf("\n");
102                 }
103                 argc -= optind;
104                 argv += optind;
105
106                 if (argc) {
107                         fprintf(stderr, "%s: unknown argument(s): ", argv[-optind]);
108                         for (i = 0; i < argc; i++)
109                                 fprintf(stderr, " %s", argv[i]);
110                         fprintf(stderr, "\n");
111                 }
112
113         return 0;
114 }