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