xref: /netbsd-src/sbin/drvctl/drvctl.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: drvctl.c,v 1.2 2004/08/18 13:24:55 wiz Exp $ */
2 
3 /*
4  * Copyright (c) 2004
5  * 	Matthias Drochner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <err.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36 #include <sys/drvctlio.h>
37 
38 #define OPTS "dra:"
39 
40 static void usage(void);
41 
42 static void
43 usage()
44 {
45 
46 	fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
47 	    "       %s -d device\n", getprogname(), getprogname());
48 	exit(1);
49 }
50 
51 int
52 main(int argc, char **argv)
53 {
54 	int c, mode;
55 	char *attr = 0;
56 	extern char *optarg;
57 	extern int optind;
58 	int fd, res;
59 	struct devdetachargs daa;
60 	struct devrescanargs raa;
61 	int *locs, i;
62 
63 	while ((c = getopt(argc, argv, OPTS)) != -1) {
64 		switch (c) {
65 		case 'd':
66 		case 'r':
67 			mode = c;
68 			break;
69 		case 'a':
70 			attr = optarg;
71 			break;
72 		case '?':
73 		default:
74 			usage();
75 		}
76 	}
77 
78 	argc -= optind;
79 	argv += optind;
80 
81 	if (argc < 1)
82 		usage();
83 
84 	fd = open(DRVCTLDEV, O_RDWR, 0);
85 	if (fd < 0)
86 		err(2, "open %s", DRVCTLDEV);
87 
88 	if (mode == 'd') {
89 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
90 
91 		res = ioctl(fd, DRVDETACHDEV, &daa);
92 		if (res)
93 			err(3, "DRVDETACHDEV");
94 	} else if (mode == 'r') {
95 		memset(&raa, 0, sizeof(raa));
96 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
97 		if (attr)
98 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
99 		if (argc > 1) {
100 			locs = malloc((argc - 1) * sizeof(int));
101 			if (!locs)
102 				err(5, "malloc int[%d]", argc - 1);
103 			for (i = 0; i < argc - 1; i++)
104 				locs[i] = atoi(argv[i + 1]);
105 			raa.numlocators = argc - 1;
106 			raa.locators = locs;
107 		}
108 
109 		res = ioctl(fd, DRVRESCANBUS, &raa);
110 		if (res)
111 			err(3, "DRVRESCANBUS");
112 	} else
113 		errx(4, "unknown command");
114 
115 	return (0);
116 }
117