drvctl.c revision 1.5 1 1.5 thorpej /* $NetBSD: drvctl.c,v 1.5 2006/09/22 04:37:36 thorpej Exp $ */
2 1.1 drochner
3 1.1 drochner /*
4 1.1 drochner * Copyright (c) 2004
5 1.1 drochner * Matthias Drochner. All rights reserved.
6 1.1 drochner *
7 1.1 drochner * Redistribution and use in source and binary forms, with or without
8 1.1 drochner * modification, are permitted provided that the following conditions
9 1.1 drochner * are met:
10 1.1 drochner * 1. Redistributions of source code must retain the above copyright
11 1.1 drochner * notice, this list of conditions, and the following disclaimer.
12 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 drochner * notice, this list of conditions and the following disclaimer in the
14 1.1 drochner * documentation and/or other materials provided with the distribution.
15 1.1 drochner *
16 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 drochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 drochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 drochner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 drochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 drochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 drochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 drochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 drochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 drochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 drochner * SUCH DAMAGE.
27 1.1 drochner */
28 1.1 drochner
29 1.1 drochner #include <stdio.h>
30 1.1 drochner #include <stdlib.h>
31 1.1 drochner #include <unistd.h>
32 1.1 drochner #include <err.h>
33 1.1 drochner #include <fcntl.h>
34 1.1 drochner #include <string.h>
35 1.1 drochner #include <sys/ioctl.h>
36 1.1 drochner #include <sys/drvctlio.h>
37 1.1 drochner
38 1.5 thorpej #define OPTS "dra:p"
39 1.5 thorpej
40 1.5 thorpej #define OPEN_MODE(mode) \
41 1.5 thorpej (((mode) == 'd' || (mode) == 'r') ? O_RDWR \
42 1.5 thorpej : O_RDONLY)
43 1.1 drochner
44 1.1 drochner static void usage(void);
45 1.1 drochner
46 1.1 drochner static void
47 1.3 xtraeme usage(void)
48 1.1 drochner {
49 1.1 drochner
50 1.2 wiz fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
51 1.5 thorpej " %s -d device\n"
52 1.5 thorpej " %s -p device\n",
53 1.5 thorpej getprogname(), getprogname(), getprogname());
54 1.1 drochner exit(1);
55 1.1 drochner }
56 1.1 drochner
57 1.1 drochner int
58 1.1 drochner main(int argc, char **argv)
59 1.1 drochner {
60 1.1 drochner int c, mode;
61 1.1 drochner char *attr = 0;
62 1.1 drochner extern char *optarg;
63 1.1 drochner extern int optind;
64 1.1 drochner int fd, res;
65 1.1 drochner struct devdetachargs daa;
66 1.1 drochner struct devrescanargs raa;
67 1.1 drochner int *locs, i;
68 1.1 drochner
69 1.4 lukem mode = 0;
70 1.1 drochner while ((c = getopt(argc, argv, OPTS)) != -1) {
71 1.1 drochner switch (c) {
72 1.1 drochner case 'd':
73 1.1 drochner case 'r':
74 1.5 thorpej case 'p':
75 1.1 drochner mode = c;
76 1.1 drochner break;
77 1.1 drochner case 'a':
78 1.1 drochner attr = optarg;
79 1.1 drochner break;
80 1.1 drochner case '?':
81 1.1 drochner default:
82 1.1 drochner usage();
83 1.1 drochner }
84 1.1 drochner }
85 1.1 drochner
86 1.1 drochner argc -= optind;
87 1.1 drochner argv += optind;
88 1.1 drochner
89 1.4 lukem if (argc < 1 || mode == 0)
90 1.1 drochner usage();
91 1.1 drochner
92 1.5 thorpej fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
93 1.1 drochner if (fd < 0)
94 1.1 drochner err(2, "open %s", DRVCTLDEV);
95 1.1 drochner
96 1.1 drochner if (mode == 'd') {
97 1.1 drochner strlcpy(daa.devname, argv[0], sizeof(daa.devname));
98 1.1 drochner
99 1.1 drochner res = ioctl(fd, DRVDETACHDEV, &daa);
100 1.1 drochner if (res)
101 1.1 drochner err(3, "DRVDETACHDEV");
102 1.1 drochner } else if (mode == 'r') {
103 1.1 drochner memset(&raa, 0, sizeof(raa));
104 1.1 drochner strlcpy(raa.busname, argv[0], sizeof(raa.busname));
105 1.1 drochner if (attr)
106 1.1 drochner strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
107 1.1 drochner if (argc > 1) {
108 1.1 drochner locs = malloc((argc - 1) * sizeof(int));
109 1.1 drochner if (!locs)
110 1.1 drochner err(5, "malloc int[%d]", argc - 1);
111 1.1 drochner for (i = 0; i < argc - 1; i++)
112 1.1 drochner locs[i] = atoi(argv[i + 1]);
113 1.1 drochner raa.numlocators = argc - 1;
114 1.1 drochner raa.locators = locs;
115 1.1 drochner }
116 1.1 drochner
117 1.1 drochner res = ioctl(fd, DRVRESCANBUS, &raa);
118 1.1 drochner if (res)
119 1.1 drochner err(3, "DRVRESCANBUS");
120 1.5 thorpej } else if (mode == 'p') {
121 1.5 thorpej prop_dictionary_t command_dict, args_dict, results_dict,
122 1.5 thorpej data_dict;
123 1.5 thorpej prop_string_t string;
124 1.5 thorpej prop_number_t number;
125 1.5 thorpej char *xml;
126 1.5 thorpej
127 1.5 thorpej command_dict = prop_dictionary_create();
128 1.5 thorpej args_dict = prop_dictionary_create();
129 1.5 thorpej
130 1.5 thorpej string = prop_string_create_cstring_nocopy("get-properties");
131 1.5 thorpej prop_dictionary_set(command_dict, "drvctl-command", string);
132 1.5 thorpej prop_object_release(string);
133 1.5 thorpej
134 1.5 thorpej string = prop_string_create_cstring(argv[0]);
135 1.5 thorpej prop_dictionary_set(args_dict, "device-name", string);
136 1.5 thorpej prop_object_release(string);
137 1.5 thorpej
138 1.5 thorpej prop_dictionary_set(command_dict, "drvctl-arguments",
139 1.5 thorpej args_dict);
140 1.5 thorpej prop_object_release(args_dict);
141 1.5 thorpej
142 1.5 thorpej res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
143 1.5 thorpej DRVCTLCOMMAND,
144 1.5 thorpej &results_dict);
145 1.5 thorpej prop_object_release(command_dict);
146 1.5 thorpej if (res)
147 1.5 thorpej errx(3, "DRVCTLCOMMAND: %s", strerror(res));
148 1.5 thorpej
149 1.5 thorpej number = prop_dictionary_get(results_dict, "drvctl-error");
150 1.5 thorpej if (prop_number_integer_value(number) != 0) {
151 1.5 thorpej errx(3, "get-properties: %s",
152 1.5 thorpej strerror((int)prop_number_integer_value(number)));
153 1.5 thorpej }
154 1.5 thorpej
155 1.5 thorpej data_dict = prop_dictionary_get(results_dict,
156 1.5 thorpej "drvctl-result-data");
157 1.5 thorpej if (data_dict == NULL) {
158 1.5 thorpej errx(3, "get-properties: failed to return result data");
159 1.5 thorpej }
160 1.5 thorpej
161 1.5 thorpej xml = prop_dictionary_externalize(data_dict);
162 1.5 thorpej prop_object_release(results_dict);
163 1.5 thorpej
164 1.5 thorpej printf("Properties for device `%s':\n%s",
165 1.5 thorpej argv[0], xml);
166 1.5 thorpej free(xml);
167 1.1 drochner } else
168 1.1 drochner errx(4, "unknown command");
169 1.1 drochner
170 1.1 drochner return (0);
171 1.1 drochner }
172