Home | History | Annotate | Line # | Download | only in drvctl
drvctl.c revision 1.11
      1  1.11  jmcneill /* $NetBSD: drvctl.c,v 1.11 2011/08/07 12:00:11 jmcneill 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.11  jmcneill #include <inttypes.h>
     30   1.9    dyoung #include <stdbool.h>
     31   1.1  drochner #include <stdio.h>
     32   1.1  drochner #include <stdlib.h>
     33   1.1  drochner #include <unistd.h>
     34   1.1  drochner #include <err.h>
     35   1.1  drochner #include <fcntl.h>
     36   1.1  drochner #include <string.h>
     37   1.1  drochner #include <sys/ioctl.h>
     38   1.1  drochner #include <sys/drvctlio.h>
     39   1.1  drochner 
     40   1.9    dyoung #define OPTS "QRSa:dlnpr"
     41   1.5   thorpej 
     42   1.5   thorpej #define	OPEN_MODE(mode)							\
     43   1.5   thorpej 	(((mode) == 'd' || (mode) == 'r') ? O_RDWR			\
     44   1.5   thorpej 					  : O_RDONLY)
     45   1.1  drochner 
     46   1.1  drochner static void usage(void);
     47  1.11  jmcneill static void extract_property(prop_dictionary_t, const char *);
     48   1.1  drochner 
     49   1.1  drochner static void
     50   1.3   xtraeme usage(void)
     51   1.1  drochner {
     52   1.1  drochner 
     53   1.2       wiz 	fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
     54   1.5   thorpej 	    "       %s -d device\n"
     55  1.10    dyoung 	    "       %s [-n] -l [device]\n"
     56  1.11  jmcneill 	    "       %s -p device [prop]\n"
     57   1.6    dyoung 	    "       %s -Q device\n"
     58   1.6    dyoung 	    "       %s -R device\n"
     59   1.6    dyoung 	    "       %s -S device\n",
     60   1.6    dyoung 	    getprogname(), getprogname(), getprogname(), getprogname(),
     61   1.5   thorpej 	    getprogname(), getprogname(), getprogname());
     62   1.1  drochner 	exit(1);
     63   1.1  drochner }
     64   1.1  drochner 
     65   1.1  drochner int
     66   1.1  drochner main(int argc, char **argv)
     67   1.1  drochner {
     68   1.9    dyoung 	bool nflag = false;
     69   1.1  drochner 	int c, mode;
     70   1.1  drochner 	char *attr = 0;
     71   1.1  drochner 	extern char *optarg;
     72   1.1  drochner 	extern int optind;
     73   1.1  drochner 	int fd, res;
     74   1.6    dyoung 	size_t children;
     75   1.6    dyoung 	struct devpmargs paa = {.devname = "", .flags = 0};
     76   1.6    dyoung 	struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
     77   1.6    dyoung 				  .l_children = 0};
     78   1.1  drochner 	struct devdetachargs daa;
     79   1.1  drochner 	struct devrescanargs raa;
     80   1.1  drochner 	int *locs, i;
     81   1.6    dyoung 	prop_dictionary_t command_dict, args_dict, results_dict,
     82   1.6    dyoung 			  data_dict;
     83   1.6    dyoung 	prop_string_t string;
     84   1.6    dyoung 	prop_number_t number;
     85   1.6    dyoung 	char *xml;
     86   1.1  drochner 
     87   1.4     lukem 	mode = 0;
     88   1.1  drochner 	while ((c = getopt(argc, argv, OPTS)) != -1) {
     89   1.1  drochner 		switch (c) {
     90   1.6    dyoung 		case 'Q':
     91   1.6    dyoung 		case 'R':
     92   1.6    dyoung 		case 'S':
     93   1.1  drochner 		case 'd':
     94   1.6    dyoung 		case 'l':
     95   1.6    dyoung 		case 'p':
     96   1.1  drochner 		case 'r':
     97   1.1  drochner 			mode = c;
     98   1.1  drochner 			break;
     99   1.1  drochner 		case 'a':
    100   1.1  drochner 			attr = optarg;
    101   1.1  drochner 			break;
    102   1.9    dyoung 		case 'n':
    103   1.9    dyoung 			nflag = true;
    104   1.9    dyoung 			break;
    105   1.1  drochner 		case '?':
    106   1.1  drochner 		default:
    107   1.1  drochner 			usage();
    108   1.1  drochner 		}
    109   1.1  drochner 	}
    110   1.1  drochner 
    111   1.1  drochner 	argc -= optind;
    112   1.1  drochner 	argv += optind;
    113   1.1  drochner 
    114   1.8     joerg 	if ((argc < 1 && mode != 'l') || mode == 0)
    115   1.1  drochner 		usage();
    116   1.1  drochner 
    117   1.5   thorpej 	fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
    118   1.1  drochner 	if (fd < 0)
    119   1.1  drochner 		err(2, "open %s", DRVCTLDEV);
    120   1.1  drochner 
    121   1.6    dyoung 	switch (mode) {
    122   1.6    dyoung 	case 'Q':
    123   1.6    dyoung 		paa.flags = DEVPM_F_SUBTREE;
    124   1.6    dyoung 		/*FALLTHROUGH*/
    125   1.6    dyoung 	case 'R':
    126   1.6    dyoung 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    127   1.6    dyoung 
    128   1.6    dyoung 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
    129   1.6    dyoung 			err(3, "DRVRESUMEDEV");
    130   1.6    dyoung 		break;
    131   1.6    dyoung 	case 'S':
    132   1.6    dyoung 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    133   1.6    dyoung 
    134   1.6    dyoung 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
    135   1.6    dyoung 			err(3, "DRVSUSPENDDEV");
    136   1.6    dyoung 		break;
    137   1.6    dyoung 	case 'd':
    138   1.1  drochner 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
    139   1.1  drochner 
    140   1.6    dyoung 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
    141   1.1  drochner 			err(3, "DRVDETACHDEV");
    142   1.6    dyoung 		break;
    143   1.6    dyoung 	case 'l':
    144   1.8     joerg 		if (argc == 0)
    145   1.8     joerg 			*laa.l_devname = '\0';
    146   1.8     joerg 		else
    147   1.8     joerg 			strlcpy(laa.l_devname, argv[0], sizeof(laa.l_devname));
    148   1.6    dyoung 
    149   1.6    dyoung 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    150   1.6    dyoung 			err(3, "DRVLISTDEV");
    151   1.6    dyoung 
    152   1.6    dyoung 		children = laa.l_children;
    153   1.6    dyoung 
    154   1.6    dyoung 		laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
    155   1.6    dyoung 		if (laa.l_childname == NULL)
    156   1.6    dyoung 			err(5, "DRVLISTDEV");
    157   1.6    dyoung 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    158   1.6    dyoung 			err(3, "DRVLISTDEV");
    159   1.6    dyoung 		if (laa.l_children > children)
    160   1.6    dyoung 			err(6, "DRVLISTDEV: number of children grew");
    161   1.6    dyoung 
    162   1.8     joerg 		for (i = 0; i < (int)laa.l_children; i++) {
    163   1.9    dyoung 			if (!nflag) {
    164   1.9    dyoung 				printf("%s ",
    165   1.9    dyoung 				    (argc == 0) ? "root" : laa.l_devname);
    166   1.9    dyoung 			}
    167   1.9    dyoung 			printf("%s\n", laa.l_childname[i]);
    168   1.8     joerg 		}
    169   1.6    dyoung 		break;
    170   1.6    dyoung 	case 'r':
    171   1.1  drochner 		memset(&raa, 0, sizeof(raa));
    172   1.1  drochner 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
    173   1.1  drochner 		if (attr)
    174   1.1  drochner 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
    175   1.1  drochner 		if (argc > 1) {
    176   1.1  drochner 			locs = malloc((argc - 1) * sizeof(int));
    177   1.1  drochner 			if (!locs)
    178   1.1  drochner 				err(5, "malloc int[%d]", argc - 1);
    179   1.1  drochner 			for (i = 0; i < argc - 1; i++)
    180   1.1  drochner 				locs[i] = atoi(argv[i + 1]);
    181   1.1  drochner 			raa.numlocators = argc - 1;
    182   1.1  drochner 			raa.locators = locs;
    183   1.1  drochner 		}
    184   1.1  drochner 
    185   1.6    dyoung 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
    186   1.1  drochner 			err(3, "DRVRESCANBUS");
    187   1.6    dyoung 		break;
    188   1.6    dyoung 	case 'p':
    189   1.5   thorpej 
    190   1.5   thorpej 		command_dict = prop_dictionary_create();
    191   1.5   thorpej 		args_dict = prop_dictionary_create();
    192   1.5   thorpej 
    193   1.5   thorpej 		string = prop_string_create_cstring_nocopy("get-properties");
    194   1.5   thorpej 		prop_dictionary_set(command_dict, "drvctl-command", string);
    195   1.5   thorpej 		prop_object_release(string);
    196   1.5   thorpej 
    197   1.5   thorpej 		string = prop_string_create_cstring(argv[0]);
    198   1.5   thorpej 		prop_dictionary_set(args_dict, "device-name", string);
    199   1.5   thorpej 		prop_object_release(string);
    200   1.5   thorpej 
    201   1.5   thorpej 		prop_dictionary_set(command_dict, "drvctl-arguments",
    202   1.5   thorpej 				    args_dict);
    203   1.5   thorpej 		prop_object_release(args_dict);
    204   1.5   thorpej 
    205   1.5   thorpej 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    206   1.5   thorpej 						     DRVCTLCOMMAND,
    207   1.5   thorpej 						     &results_dict);
    208   1.5   thorpej 		prop_object_release(command_dict);
    209   1.5   thorpej 		if (res)
    210   1.5   thorpej 			errx(3, "DRVCTLCOMMAND: %s", strerror(res));
    211   1.5   thorpej 
    212   1.5   thorpej 		number = prop_dictionary_get(results_dict, "drvctl-error");
    213   1.5   thorpej 		if (prop_number_integer_value(number) != 0) {
    214   1.5   thorpej 			errx(3, "get-properties: %s",
    215   1.5   thorpej 			    strerror((int)prop_number_integer_value(number)));
    216   1.5   thorpej 		}
    217   1.5   thorpej 
    218   1.5   thorpej 		data_dict = prop_dictionary_get(results_dict,
    219   1.5   thorpej 						"drvctl-result-data");
    220   1.5   thorpej 		if (data_dict == NULL) {
    221   1.5   thorpej 			errx(3, "get-properties: failed to return result data");
    222   1.5   thorpej 		}
    223   1.5   thorpej 
    224  1.11  jmcneill 		if (argc == 1) {
    225  1.11  jmcneill 			xml = prop_dictionary_externalize(data_dict);
    226  1.11  jmcneill 			printf("Properties for device `%s':\n%s",
    227  1.11  jmcneill 			       argv[0], xml);
    228  1.11  jmcneill 			free(xml);
    229  1.11  jmcneill 		} else {
    230  1.11  jmcneill 			for (i = 1; i < argc; i++)
    231  1.11  jmcneill 				extract_property(data_dict, argv[i]);
    232  1.11  jmcneill 		}
    233  1.11  jmcneill 
    234   1.5   thorpej 		prop_object_release(results_dict);
    235   1.6    dyoung 		break;
    236   1.6    dyoung 	default:
    237   1.1  drochner 		errx(4, "unknown command");
    238   1.6    dyoung 	}
    239   1.1  drochner 
    240   1.1  drochner 	return (0);
    241   1.1  drochner }
    242  1.11  jmcneill 
    243  1.11  jmcneill static void
    244  1.11  jmcneill extract_property(prop_dictionary_t dict, const char *prop)
    245  1.11  jmcneill {
    246  1.11  jmcneill 	char *s, *p, *cur, *ep = NULL, *xml;
    247  1.11  jmcneill 	prop_object_t obj;
    248  1.11  jmcneill 
    249  1.11  jmcneill 	s = strdup(prop);
    250  1.11  jmcneill 	p = strtok_r(s, "/", &ep);
    251  1.11  jmcneill 	while (p) {
    252  1.11  jmcneill 		cur = p;
    253  1.11  jmcneill 		p = strtok_r(NULL, "/", &ep);
    254  1.11  jmcneill 		if (p) {
    255  1.11  jmcneill 			if (prop_dictionary_get_dict(dict, cur, &dict) == false)
    256  1.11  jmcneill 				exit(EXIT_FAILURE);
    257  1.11  jmcneill 		} else {
    258  1.11  jmcneill 			obj = prop_dictionary_get(dict, cur);
    259  1.11  jmcneill 			if (obj == NULL)
    260  1.11  jmcneill 				exit(EXIT_FAILURE);
    261  1.11  jmcneill 			switch (prop_object_type(obj)) {
    262  1.11  jmcneill 			case PROP_TYPE_BOOL:
    263  1.11  jmcneill 				printf("%s\n",
    264  1.11  jmcneill 				    prop_bool_true(obj) ? "true" : "false");
    265  1.11  jmcneill 				break;
    266  1.11  jmcneill 			case PROP_TYPE_NUMBER:
    267  1.11  jmcneill 				printf("%" PRId64 "\n",
    268  1.11  jmcneill 				    prop_number_integer_value(obj));
    269  1.11  jmcneill 				break;
    270  1.11  jmcneill 			case PROP_TYPE_STRING:
    271  1.11  jmcneill 				printf("%s\n",
    272  1.11  jmcneill 				    prop_string_cstring_nocopy(obj));
    273  1.11  jmcneill 				break;
    274  1.11  jmcneill 			case PROP_TYPE_DICTIONARY:
    275  1.11  jmcneill 				xml = prop_dictionary_externalize(obj);
    276  1.11  jmcneill 				printf("%s", xml);
    277  1.11  jmcneill 				free(xml);
    278  1.11  jmcneill 				break;
    279  1.11  jmcneill 			default:
    280  1.11  jmcneill 				fprintf(stderr, "unhandled type %d\n",
    281  1.11  jmcneill 				    prop_object_type(obj));
    282  1.11  jmcneill 				exit(EXIT_FAILURE);
    283  1.11  jmcneill 			}
    284  1.11  jmcneill 		}
    285  1.11  jmcneill 	}
    286  1.11  jmcneill 
    287  1.11  jmcneill 	free(s);
    288  1.11  jmcneill }
    289