Home | History | Annotate | Line # | Download | only in drvctl
drvctl.c revision 1.5.10.1
      1  1.5.10.1       mjf /* $NetBSD: drvctl.c,v 1.5.10.1 2008/02/18 21:04:16 mjf 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.10.1       mjf #define OPTS "QRSa:dlpr"
     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.10.1       mjf 	    "       %s -l device\n"
     53  1.5.10.1       mjf 	    "       %s -p device\n"
     54  1.5.10.1       mjf 	    "       %s -Q device\n"
     55  1.5.10.1       mjf 	    "       %s -R device\n"
     56  1.5.10.1       mjf 	    "       %s -S device\n",
     57  1.5.10.1       mjf 	    getprogname(), getprogname(), getprogname(), getprogname(),
     58       1.5   thorpej 	    getprogname(), getprogname(), getprogname());
     59       1.1  drochner 	exit(1);
     60       1.1  drochner }
     61       1.1  drochner 
     62       1.1  drochner int
     63       1.1  drochner main(int argc, char **argv)
     64       1.1  drochner {
     65       1.1  drochner 	int c, mode;
     66       1.1  drochner 	char *attr = 0;
     67       1.1  drochner 	extern char *optarg;
     68       1.1  drochner 	extern int optind;
     69       1.1  drochner 	int fd, res;
     70  1.5.10.1       mjf 	size_t children;
     71  1.5.10.1       mjf 	struct devpmargs paa = {.devname = "", .flags = 0};
     72  1.5.10.1       mjf 	struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
     73  1.5.10.1       mjf 				  .l_children = 0};
     74       1.1  drochner 	struct devdetachargs daa;
     75       1.1  drochner 	struct devrescanargs raa;
     76       1.1  drochner 	int *locs, i;
     77  1.5.10.1       mjf 	prop_dictionary_t command_dict, args_dict, results_dict,
     78  1.5.10.1       mjf 			  data_dict;
     79  1.5.10.1       mjf 	prop_string_t string;
     80  1.5.10.1       mjf 	prop_number_t number;
     81  1.5.10.1       mjf 	char *xml;
     82       1.1  drochner 
     83       1.4     lukem 	mode = 0;
     84       1.1  drochner 	while ((c = getopt(argc, argv, OPTS)) != -1) {
     85       1.1  drochner 		switch (c) {
     86  1.5.10.1       mjf 		case 'Q':
     87  1.5.10.1       mjf 		case 'R':
     88  1.5.10.1       mjf 		case 'S':
     89       1.1  drochner 		case 'd':
     90  1.5.10.1       mjf 		case 'l':
     91       1.5   thorpej 		case 'p':
     92  1.5.10.1       mjf 		case 'r':
     93       1.1  drochner 			mode = c;
     94       1.1  drochner 			break;
     95       1.1  drochner 		case 'a':
     96       1.1  drochner 			attr = optarg;
     97       1.1  drochner 			break;
     98       1.1  drochner 		case '?':
     99       1.1  drochner 		default:
    100       1.1  drochner 			usage();
    101       1.1  drochner 		}
    102       1.1  drochner 	}
    103       1.1  drochner 
    104       1.1  drochner 	argc -= optind;
    105       1.1  drochner 	argv += optind;
    106       1.1  drochner 
    107       1.4     lukem 	if (argc < 1 || mode == 0)
    108       1.1  drochner 		usage();
    109       1.1  drochner 
    110       1.5   thorpej 	fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
    111       1.1  drochner 	if (fd < 0)
    112       1.1  drochner 		err(2, "open %s", DRVCTLDEV);
    113       1.1  drochner 
    114  1.5.10.1       mjf 	switch (mode) {
    115  1.5.10.1       mjf 	case 'Q':
    116  1.5.10.1       mjf 		paa.flags = DEVPM_F_SUBTREE;
    117  1.5.10.1       mjf 		/*FALLTHROUGH*/
    118  1.5.10.1       mjf 	case 'R':
    119  1.5.10.1       mjf 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    120  1.5.10.1       mjf 
    121  1.5.10.1       mjf 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
    122  1.5.10.1       mjf 			err(3, "DRVRESUMEDEV");
    123  1.5.10.1       mjf 		break;
    124  1.5.10.1       mjf 	case 'S':
    125  1.5.10.1       mjf 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    126  1.5.10.1       mjf 
    127  1.5.10.1       mjf 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
    128  1.5.10.1       mjf 			err(3, "DRVSUSPENDDEV");
    129  1.5.10.1       mjf 		break;
    130  1.5.10.1       mjf 	case 'd':
    131       1.1  drochner 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
    132       1.1  drochner 
    133  1.5.10.1       mjf 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
    134       1.1  drochner 			err(3, "DRVDETACHDEV");
    135  1.5.10.1       mjf 		break;
    136  1.5.10.1       mjf 	case 'l':
    137  1.5.10.1       mjf 		strlcpy(laa.l_devname, argv[0], sizeof(laa.l_devname));
    138  1.5.10.1       mjf 
    139  1.5.10.1       mjf 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    140  1.5.10.1       mjf 			err(3, "DRVLISTDEV");
    141  1.5.10.1       mjf 
    142  1.5.10.1       mjf 		children = laa.l_children;
    143  1.5.10.1       mjf 
    144  1.5.10.1       mjf 		laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
    145  1.5.10.1       mjf 		if (laa.l_childname == NULL)
    146  1.5.10.1       mjf 			err(5, "DRVLISTDEV");
    147  1.5.10.1       mjf 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    148  1.5.10.1       mjf 			err(3, "DRVLISTDEV");
    149  1.5.10.1       mjf 		if (laa.l_children > children)
    150  1.5.10.1       mjf 			err(6, "DRVLISTDEV: number of children grew");
    151  1.5.10.1       mjf 
    152  1.5.10.1       mjf 		for (i = 0; i < laa.l_children; i++)
    153  1.5.10.1       mjf 			printf("%s %s\n", laa.l_devname, laa.l_childname[i]);
    154  1.5.10.1       mjf 		break;
    155  1.5.10.1       mjf 	case 'r':
    156       1.1  drochner 		memset(&raa, 0, sizeof(raa));
    157       1.1  drochner 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
    158       1.1  drochner 		if (attr)
    159       1.1  drochner 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
    160       1.1  drochner 		if (argc > 1) {
    161       1.1  drochner 			locs = malloc((argc - 1) * sizeof(int));
    162       1.1  drochner 			if (!locs)
    163       1.1  drochner 				err(5, "malloc int[%d]", argc - 1);
    164       1.1  drochner 			for (i = 0; i < argc - 1; i++)
    165       1.1  drochner 				locs[i] = atoi(argv[i + 1]);
    166       1.1  drochner 			raa.numlocators = argc - 1;
    167       1.1  drochner 			raa.locators = locs;
    168       1.1  drochner 		}
    169       1.1  drochner 
    170  1.5.10.1       mjf 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
    171       1.1  drochner 			err(3, "DRVRESCANBUS");
    172  1.5.10.1       mjf 		break;
    173  1.5.10.1       mjf 	case 'p':
    174       1.5   thorpej 
    175       1.5   thorpej 		command_dict = prop_dictionary_create();
    176       1.5   thorpej 		args_dict = prop_dictionary_create();
    177       1.5   thorpej 
    178       1.5   thorpej 		string = prop_string_create_cstring_nocopy("get-properties");
    179       1.5   thorpej 		prop_dictionary_set(command_dict, "drvctl-command", string);
    180       1.5   thorpej 		prop_object_release(string);
    181       1.5   thorpej 
    182       1.5   thorpej 		string = prop_string_create_cstring(argv[0]);
    183       1.5   thorpej 		prop_dictionary_set(args_dict, "device-name", string);
    184       1.5   thorpej 		prop_object_release(string);
    185       1.5   thorpej 
    186       1.5   thorpej 		prop_dictionary_set(command_dict, "drvctl-arguments",
    187       1.5   thorpej 				    args_dict);
    188       1.5   thorpej 		prop_object_release(args_dict);
    189       1.5   thorpej 
    190       1.5   thorpej 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    191       1.5   thorpej 						     DRVCTLCOMMAND,
    192       1.5   thorpej 						     &results_dict);
    193       1.5   thorpej 		prop_object_release(command_dict);
    194       1.5   thorpej 		if (res)
    195       1.5   thorpej 			errx(3, "DRVCTLCOMMAND: %s", strerror(res));
    196       1.5   thorpej 
    197       1.5   thorpej 		number = prop_dictionary_get(results_dict, "drvctl-error");
    198       1.5   thorpej 		if (prop_number_integer_value(number) != 0) {
    199       1.5   thorpej 			errx(3, "get-properties: %s",
    200       1.5   thorpej 			    strerror((int)prop_number_integer_value(number)));
    201       1.5   thorpej 		}
    202       1.5   thorpej 
    203       1.5   thorpej 		data_dict = prop_dictionary_get(results_dict,
    204       1.5   thorpej 						"drvctl-result-data");
    205       1.5   thorpej 		if (data_dict == NULL) {
    206       1.5   thorpej 			errx(3, "get-properties: failed to return result data");
    207       1.5   thorpej 		}
    208       1.5   thorpej 
    209       1.5   thorpej 		xml = prop_dictionary_externalize(data_dict);
    210       1.5   thorpej 		prop_object_release(results_dict);
    211       1.5   thorpej 
    212       1.5   thorpej 		printf("Properties for device `%s':\n%s",
    213       1.5   thorpej 		       argv[0], xml);
    214       1.5   thorpej 		free(xml);
    215  1.5.10.1       mjf 		break;
    216  1.5.10.1       mjf 	default:
    217       1.1  drochner 		errx(4, "unknown command");
    218  1.5.10.1       mjf 	}
    219       1.1  drochner 
    220       1.1  drochner 	return (0);
    221       1.1  drochner }
    222