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