drvctl.c revision 1.1 1 1.1 drochner /* $NetBSD: drvctl.c,v 1.1 2004/08/18 12:30:01 drochner 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.1 drochner #define OPTS "dra:"
39 1.1 drochner
40 1.1 drochner static void usage(void);
41 1.1 drochner
42 1.1 drochner static void
43 1.1 drochner usage()
44 1.1 drochner {
45 1.1 drochner
46 1.1 drochner fprintf(stderr, "%s {-d,-r} [-a attr] dev [loc...]\n", getprogname());
47 1.1 drochner exit(1);
48 1.1 drochner }
49 1.1 drochner
50 1.1 drochner int
51 1.1 drochner main(int argc, char **argv)
52 1.1 drochner {
53 1.1 drochner int c, mode;
54 1.1 drochner char *attr = 0;
55 1.1 drochner extern char *optarg;
56 1.1 drochner extern int optind;
57 1.1 drochner int fd, res;
58 1.1 drochner struct devdetachargs daa;
59 1.1 drochner struct devrescanargs raa;
60 1.1 drochner int *locs, i;
61 1.1 drochner
62 1.1 drochner while ((c = getopt(argc, argv, OPTS)) != -1) {
63 1.1 drochner switch (c) {
64 1.1 drochner case 'd':
65 1.1 drochner case 'r':
66 1.1 drochner mode = c;
67 1.1 drochner break;
68 1.1 drochner case 'a':
69 1.1 drochner attr = optarg;
70 1.1 drochner break;
71 1.1 drochner case '?':
72 1.1 drochner default:
73 1.1 drochner usage();
74 1.1 drochner }
75 1.1 drochner }
76 1.1 drochner
77 1.1 drochner argc -= optind;
78 1.1 drochner argv += optind;
79 1.1 drochner
80 1.1 drochner if (argc < 1)
81 1.1 drochner usage();
82 1.1 drochner
83 1.1 drochner fd = open(DRVCTLDEV, O_RDWR, 0);
84 1.1 drochner if (fd < 0)
85 1.1 drochner err(2, "open %s", DRVCTLDEV);
86 1.1 drochner
87 1.1 drochner if (mode == 'd') {
88 1.1 drochner strlcpy(daa.devname, argv[0], sizeof(daa.devname));
89 1.1 drochner
90 1.1 drochner res = ioctl(fd, DRVDETACHDEV, &daa);
91 1.1 drochner if (res)
92 1.1 drochner err(3, "DRVDETACHDEV");
93 1.1 drochner } else if (mode == 'r') {
94 1.1 drochner memset(&raa, 0, sizeof(raa));
95 1.1 drochner strlcpy(raa.busname, argv[0], sizeof(raa.busname));
96 1.1 drochner if (attr)
97 1.1 drochner strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
98 1.1 drochner if (argc > 1) {
99 1.1 drochner locs = malloc((argc - 1) * sizeof(int));
100 1.1 drochner if (!locs)
101 1.1 drochner err(5, "malloc int[%d]", argc - 1);
102 1.1 drochner for (i = 0; i < argc - 1; i++)
103 1.1 drochner locs[i] = atoi(argv[i + 1]);
104 1.1 drochner raa.numlocators = argc - 1;
105 1.1 drochner raa.locators = locs;
106 1.1 drochner }
107 1.1 drochner
108 1.1 drochner res = ioctl(fd, DRVRESCANBUS, &raa);
109 1.1 drochner if (res)
110 1.1 drochner err(3, "DRVRESCANBUS");
111 1.1 drochner } else
112 1.1 drochner errx(4, "unknown command");
113 1.1 drochner
114 1.1 drochner return (0);
115 1.1 drochner }
116