Home | History | Annotate | Line # | Download | only in drvctl
drvctl.c revision 1.6.10.1
      1  1.6.10.1       snj /* $NetBSD: drvctl.c,v 1.6.10.1 2009/05/03 22:49:51 snj 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.6    dyoung #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.6.10.1       snj 	    "       %s -l [device]\n"
     53       1.6    dyoung 	    "       %s -p device\n"
     54       1.6    dyoung 	    "       %s -Q device\n"
     55       1.6    dyoung 	    "       %s -R device\n"
     56       1.6    dyoung 	    "       %s -S device\n",
     57       1.6    dyoung 	    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.6    dyoung 	size_t children;
     71       1.6    dyoung 	struct devpmargs paa = {.devname = "", .flags = 0};
     72       1.6    dyoung 	struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
     73       1.6    dyoung 				  .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.6    dyoung 	prop_dictionary_t command_dict, args_dict, results_dict,
     78       1.6    dyoung 			  data_dict;
     79       1.6    dyoung 	prop_string_t string;
     80       1.6    dyoung 	prop_number_t number;
     81       1.6    dyoung 	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.6    dyoung 		case 'Q':
     87       1.6    dyoung 		case 'R':
     88       1.6    dyoung 		case 'S':
     89       1.1  drochner 		case 'd':
     90       1.6    dyoung 		case 'l':
     91       1.6    dyoung 		case 'p':
     92       1.1  drochner 		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.6.10.1       snj 	if ((argc < 1 && mode != 'l') || 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.6    dyoung 	switch (mode) {
    115       1.6    dyoung 	case 'Q':
    116       1.6    dyoung 		paa.flags = DEVPM_F_SUBTREE;
    117       1.6    dyoung 		/*FALLTHROUGH*/
    118       1.6    dyoung 	case 'R':
    119       1.6    dyoung 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    120       1.6    dyoung 
    121       1.6    dyoung 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
    122       1.6    dyoung 			err(3, "DRVRESUMEDEV");
    123       1.6    dyoung 		break;
    124       1.6    dyoung 	case 'S':
    125       1.6    dyoung 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    126       1.6    dyoung 
    127       1.6    dyoung 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
    128       1.6    dyoung 			err(3, "DRVSUSPENDDEV");
    129       1.6    dyoung 		break;
    130       1.6    dyoung 	case 'd':
    131       1.1  drochner 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
    132       1.1  drochner 
    133       1.6    dyoung 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
    134       1.1  drochner 			err(3, "DRVDETACHDEV");
    135       1.6    dyoung 		break;
    136       1.6    dyoung 	case 'l':
    137  1.6.10.1       snj 		if (argc == 0)
    138  1.6.10.1       snj 			*laa.l_devname = '\0';
    139  1.6.10.1       snj 		else
    140  1.6.10.1       snj 			strlcpy(laa.l_devname, argv[0], sizeof(laa.l_devname));
    141       1.6    dyoung 
    142       1.6    dyoung 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    143       1.6    dyoung 			err(3, "DRVLISTDEV");
    144       1.6    dyoung 
    145       1.6    dyoung 		children = laa.l_children;
    146       1.6    dyoung 
    147       1.6    dyoung 		laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
    148       1.6    dyoung 		if (laa.l_childname == NULL)
    149       1.6    dyoung 			err(5, "DRVLISTDEV");
    150       1.6    dyoung 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    151       1.6    dyoung 			err(3, "DRVLISTDEV");
    152       1.6    dyoung 		if (laa.l_children > children)
    153       1.6    dyoung 			err(6, "DRVLISTDEV: number of children grew");
    154       1.6    dyoung 
    155  1.6.10.1       snj 		for (i = 0; i < laa.l_children; i++) {
    156  1.6.10.1       snj 			printf("%s%s%s\n", laa.l_devname, (argc ? " " : ""),
    157  1.6.10.1       snj 			    laa.l_childname[i]);
    158  1.6.10.1       snj 		}
    159       1.6    dyoung 		break;
    160       1.6    dyoung 	case 'r':
    161       1.1  drochner 		memset(&raa, 0, sizeof(raa));
    162       1.1  drochner 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
    163       1.1  drochner 		if (attr)
    164       1.1  drochner 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
    165       1.1  drochner 		if (argc > 1) {
    166       1.1  drochner 			locs = malloc((argc - 1) * sizeof(int));
    167       1.1  drochner 			if (!locs)
    168       1.1  drochner 				err(5, "malloc int[%d]", argc - 1);
    169       1.1  drochner 			for (i = 0; i < argc - 1; i++)
    170       1.1  drochner 				locs[i] = atoi(argv[i + 1]);
    171       1.1  drochner 			raa.numlocators = argc - 1;
    172       1.1  drochner 			raa.locators = locs;
    173       1.1  drochner 		}
    174       1.1  drochner 
    175       1.6    dyoung 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
    176       1.1  drochner 			err(3, "DRVRESCANBUS");
    177       1.6    dyoung 		break;
    178       1.6    dyoung 	case 'p':
    179       1.5   thorpej 
    180       1.5   thorpej 		command_dict = prop_dictionary_create();
    181       1.5   thorpej 		args_dict = prop_dictionary_create();
    182       1.5   thorpej 
    183       1.5   thorpej 		string = prop_string_create_cstring_nocopy("get-properties");
    184       1.5   thorpej 		prop_dictionary_set(command_dict, "drvctl-command", string);
    185       1.5   thorpej 		prop_object_release(string);
    186       1.5   thorpej 
    187       1.5   thorpej 		string = prop_string_create_cstring(argv[0]);
    188       1.5   thorpej 		prop_dictionary_set(args_dict, "device-name", string);
    189       1.5   thorpej 		prop_object_release(string);
    190       1.5   thorpej 
    191       1.5   thorpej 		prop_dictionary_set(command_dict, "drvctl-arguments",
    192       1.5   thorpej 				    args_dict);
    193       1.5   thorpej 		prop_object_release(args_dict);
    194       1.5   thorpej 
    195       1.5   thorpej 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    196       1.5   thorpej 						     DRVCTLCOMMAND,
    197       1.5   thorpej 						     &results_dict);
    198       1.5   thorpej 		prop_object_release(command_dict);
    199       1.5   thorpej 		if (res)
    200       1.5   thorpej 			errx(3, "DRVCTLCOMMAND: %s", strerror(res));
    201       1.5   thorpej 
    202       1.5   thorpej 		number = prop_dictionary_get(results_dict, "drvctl-error");
    203       1.5   thorpej 		if (prop_number_integer_value(number) != 0) {
    204       1.5   thorpej 			errx(3, "get-properties: %s",
    205       1.5   thorpej 			    strerror((int)prop_number_integer_value(number)));
    206       1.5   thorpej 		}
    207       1.5   thorpej 
    208       1.5   thorpej 		data_dict = prop_dictionary_get(results_dict,
    209       1.5   thorpej 						"drvctl-result-data");
    210       1.5   thorpej 		if (data_dict == NULL) {
    211       1.5   thorpej 			errx(3, "get-properties: failed to return result data");
    212       1.5   thorpej 		}
    213       1.5   thorpej 
    214       1.5   thorpej 		xml = prop_dictionary_externalize(data_dict);
    215       1.5   thorpej 		prop_object_release(results_dict);
    216       1.5   thorpej 
    217       1.5   thorpej 		printf("Properties for device `%s':\n%s",
    218       1.5   thorpej 		       argv[0], xml);
    219       1.5   thorpej 		free(xml);
    220       1.6    dyoung 		break;
    221       1.6    dyoung 	default:
    222       1.1  drochner 		errx(4, "unknown command");
    223       1.6    dyoung 	}
    224       1.1  drochner 
    225       1.1  drochner 	return (0);
    226       1.1  drochner }
    227