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