drvctl.c revision 1.5 1 /* $NetBSD: drvctl.c,v 1.5 2006/09/22 04:37:36 thorpej 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:p"
39
40 #define OPEN_MODE(mode) \
41 (((mode) == 'd' || (mode) == 'r') ? O_RDWR \
42 : O_RDONLY)
43
44 static void usage(void);
45
46 static void
47 usage(void)
48 {
49
50 fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
51 " %s -d device\n"
52 " %s -p device\n",
53 getprogname(), getprogname(), getprogname());
54 exit(1);
55 }
56
57 int
58 main(int argc, char **argv)
59 {
60 int c, mode;
61 char *attr = 0;
62 extern char *optarg;
63 extern int optind;
64 int fd, res;
65 struct devdetachargs daa;
66 struct devrescanargs raa;
67 int *locs, i;
68
69 mode = 0;
70 while ((c = getopt(argc, argv, OPTS)) != -1) {
71 switch (c) {
72 case 'd':
73 case 'r':
74 case 'p':
75 mode = c;
76 break;
77 case 'a':
78 attr = optarg;
79 break;
80 case '?':
81 default:
82 usage();
83 }
84 }
85
86 argc -= optind;
87 argv += optind;
88
89 if (argc < 1 || mode == 0)
90 usage();
91
92 fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
93 if (fd < 0)
94 err(2, "open %s", DRVCTLDEV);
95
96 if (mode == 'd') {
97 strlcpy(daa.devname, argv[0], sizeof(daa.devname));
98
99 res = ioctl(fd, DRVDETACHDEV, &daa);
100 if (res)
101 err(3, "DRVDETACHDEV");
102 } else if (mode == 'r') {
103 memset(&raa, 0, sizeof(raa));
104 strlcpy(raa.busname, argv[0], sizeof(raa.busname));
105 if (attr)
106 strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
107 if (argc > 1) {
108 locs = malloc((argc - 1) * sizeof(int));
109 if (!locs)
110 err(5, "malloc int[%d]", argc - 1);
111 for (i = 0; i < argc - 1; i++)
112 locs[i] = atoi(argv[i + 1]);
113 raa.numlocators = argc - 1;
114 raa.locators = locs;
115 }
116
117 res = ioctl(fd, DRVRESCANBUS, &raa);
118 if (res)
119 err(3, "DRVRESCANBUS");
120 } else if (mode == 'p') {
121 prop_dictionary_t command_dict, args_dict, results_dict,
122 data_dict;
123 prop_string_t string;
124 prop_number_t number;
125 char *xml;
126
127 command_dict = prop_dictionary_create();
128 args_dict = prop_dictionary_create();
129
130 string = prop_string_create_cstring_nocopy("get-properties");
131 prop_dictionary_set(command_dict, "drvctl-command", string);
132 prop_object_release(string);
133
134 string = prop_string_create_cstring(argv[0]);
135 prop_dictionary_set(args_dict, "device-name", string);
136 prop_object_release(string);
137
138 prop_dictionary_set(command_dict, "drvctl-arguments",
139 args_dict);
140 prop_object_release(args_dict);
141
142 res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
143 DRVCTLCOMMAND,
144 &results_dict);
145 prop_object_release(command_dict);
146 if (res)
147 errx(3, "DRVCTLCOMMAND: %s", strerror(res));
148
149 number = prop_dictionary_get(results_dict, "drvctl-error");
150 if (prop_number_integer_value(number) != 0) {
151 errx(3, "get-properties: %s",
152 strerror((int)prop_number_integer_value(number)));
153 }
154
155 data_dict = prop_dictionary_get(results_dict,
156 "drvctl-result-data");
157 if (data_dict == NULL) {
158 errx(3, "get-properties: failed to return result data");
159 }
160
161 xml = prop_dictionary_externalize(data_dict);
162 prop_object_release(results_dict);
163
164 printf("Properties for device `%s':\n%s",
165 argv[0], xml);
166 free(xml);
167 } else
168 errx(4, "unknown command");
169
170 return (0);
171 }
172