xref: /netbsd-src/sbin/drvctl/drvctl.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /* $NetBSD: drvctl.c,v 1.4 2005/06/02 00:00:46 lukem 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(void)
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 	mode = 0;
64 	while ((c = getopt(argc, argv, OPTS)) != -1) {
65 		switch (c) {
66 		case 'd':
67 		case 'r':
68 			mode = c;
69 			break;
70 		case 'a':
71 			attr = optarg;
72 			break;
73 		case '?':
74 		default:
75 			usage();
76 		}
77 	}
78 
79 	argc -= optind;
80 	argv += optind;
81 
82 	if (argc < 1 || mode == 0)
83 		usage();
84 
85 	fd = open(DRVCTLDEV, O_RDWR, 0);
86 	if (fd < 0)
87 		err(2, "open %s", DRVCTLDEV);
88 
89 	if (mode == 'd') {
90 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
91 
92 		res = ioctl(fd, DRVDETACHDEV, &daa);
93 		if (res)
94 			err(3, "DRVDETACHDEV");
95 	} else if (mode == 'r') {
96 		memset(&raa, 0, sizeof(raa));
97 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
98 		if (attr)
99 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
100 		if (argc > 1) {
101 			locs = malloc((argc - 1) * sizeof(int));
102 			if (!locs)
103 				err(5, "malloc int[%d]", argc - 1);
104 			for (i = 0; i < argc - 1; i++)
105 				locs[i] = atoi(argv[i + 1]);
106 			raa.numlocators = argc - 1;
107 			raa.locators = locs;
108 		}
109 
110 		res = ioctl(fd, DRVRESCANBUS, &raa);
111 		if (res)
112 			err(3, "DRVRESCANBUS");
113 	} else
114 		errx(4, "unknown command");
115 
116 	return (0);
117 }
118