Home | History | Annotate | Line # | Download | only in drvctl
      1  1.23    rillig /* $NetBSD: drvctl.c,v 1.23 2024/11/03 10:43:26 rillig 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.5   thorpej 
     41   1.5   thorpej #define	OPEN_MODE(mode)							\
     42   1.5   thorpej 	(((mode) == 'd' || (mode) == 'r') ? O_RDWR			\
     43   1.5   thorpej 					  : O_RDONLY)
     44   1.1  drochner 
     45  1.13     joerg __dead static void usage(void);
     46  1.15  pgoyette static void extract_property(prop_dictionary_t, const char *, bool);
     47  1.15  pgoyette static void display_object(prop_object_t, bool);
     48  1.12  jmcneill static void list_children(int, char *, bool, bool, int);
     49   1.1  drochner 
     50   1.1  drochner static void
     51   1.3   xtraeme usage(void)
     52   1.1  drochner {
     53  1.19  christos 	const char *p = getprogname();
     54   1.1  drochner 
     55   1.2       wiz 	fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
     56   1.5   thorpej 	    "       %s -d device\n"
     57  1.12  jmcneill 	    "       %s [-nt] -l [device]\n"
     58  1.18  pgoyette 	    "       %s [-n] -p device [property]\n"
     59   1.6    dyoung 	    "       %s -Q device\n"
     60   1.6    dyoung 	    "       %s -R device\n"
     61  1.19  christos 	    "       %s -S device\n", p, p, p, p, p, p, p);
     62  1.19  christos 	exit(EXIT_FAILURE);
     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.12  jmcneill 	bool nflag = false, tflag = false;
     69   1.1  drochner 	int c, mode;
     70   1.1  drochner 	char *attr = 0;
     71   1.1  drochner 	int fd, res;
     72  1.19  christos 	struct devpmargs paa = { .devname = "", .flags = 0 };
     73   1.1  drochner 	struct devdetachargs daa;
     74   1.1  drochner 	struct devrescanargs raa;
     75   1.1  drochner 	int *locs, i;
     76  1.19  christos 	prop_dictionary_t command_dict, args_dict, results_dict, data_dict;
     77   1.6    dyoung 	char *xml;
     78  1.21   thorpej 	int drvctl_error;
     79   1.1  drochner 
     80   1.4     lukem 	mode = 0;
     81  1.19  christos 	while ((c = getopt(argc, argv, "QRSa:dlnprt")) != -1) {
     82   1.1  drochner 		switch (c) {
     83   1.6    dyoung 		case 'Q':
     84   1.6    dyoung 		case 'R':
     85   1.6    dyoung 		case 'S':
     86  1.20  jakllsch 		case 'd':
     87  1.20  jakllsch 		case 'l':
     88  1.20  jakllsch 		case 'p':
     89  1.20  jakllsch 		case 'r':
     90  1.20  jakllsch 			mode = c;
     91  1.20  jakllsch 			break;
     92  1.19  christos 		case 'a':
     93  1.19  christos 			attr = optarg;
     94  1.19  christos 			break;
     95  1.19  christos 		case 'n':
     96  1.19  christos 			nflag = true;
     97  1.19  christos 			break;
     98  1.12  jmcneill 		case 't':
     99  1.12  jmcneill 			tflag = nflag = true;
    100  1.12  jmcneill 			break;
    101   1.1  drochner 		case '?':
    102   1.1  drochner 		default:
    103   1.1  drochner 			usage();
    104   1.1  drochner 		}
    105   1.1  drochner 	}
    106   1.1  drochner 
    107   1.1  drochner 	argc -= optind;
    108   1.1  drochner 	argv += optind;
    109   1.1  drochner 
    110   1.8     joerg 	if ((argc < 1 && mode != 'l') || mode == 0)
    111   1.1  drochner 		usage();
    112   1.1  drochner 
    113   1.5   thorpej 	fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
    114  1.19  christos 	if (fd == -1)
    115  1.19  christos 		err(EXIT_FAILURE, "open %s", DRVCTLDEV);
    116   1.1  drochner 
    117   1.6    dyoung 	switch (mode) {
    118   1.6    dyoung 	case 'Q':
    119   1.6    dyoung 		paa.flags = DEVPM_F_SUBTREE;
    120   1.6    dyoung 		/*FALLTHROUGH*/
    121   1.6    dyoung 	case 'R':
    122   1.6    dyoung 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    123   1.6    dyoung 
    124   1.6    dyoung 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
    125  1.19  christos 			err(EXIT_FAILURE, "DRVRESUMEDEV");
    126   1.6    dyoung 		break;
    127   1.6    dyoung 	case 'S':
    128   1.6    dyoung 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    129   1.6    dyoung 
    130   1.6    dyoung 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
    131  1.19  christos 			err(EXIT_FAILURE, "DRVSUSPENDDEV");
    132   1.6    dyoung 		break;
    133   1.6    dyoung 	case 'd':
    134   1.1  drochner 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
    135   1.1  drochner 
    136   1.6    dyoung 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
    137  1.19  christos 			err(EXIT_FAILURE, "DRVDETACHDEV");
    138   1.6    dyoung 		break;
    139   1.6    dyoung 	case 'l':
    140  1.12  jmcneill 		list_children(fd, argc ? argv[0] : NULL, nflag, tflag, 0);
    141   1.6    dyoung 		break;
    142   1.6    dyoung 	case 'r':
    143   1.1  drochner 		memset(&raa, 0, sizeof(raa));
    144   1.1  drochner 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
    145   1.1  drochner 		if (attr)
    146   1.1  drochner 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
    147   1.1  drochner 		if (argc > 1) {
    148   1.1  drochner 			locs = malloc((argc - 1) * sizeof(int));
    149   1.1  drochner 			if (!locs)
    150  1.19  christos 				err(EXIT_FAILURE, "malloc int[%d]", argc - 1);
    151   1.1  drochner 			for (i = 0; i < argc - 1; i++)
    152   1.1  drochner 				locs[i] = atoi(argv[i + 1]);
    153   1.1  drochner 			raa.numlocators = argc - 1;
    154   1.1  drochner 			raa.locators = locs;
    155   1.1  drochner 		}
    156   1.1  drochner 
    157   1.6    dyoung 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
    158  1.19  christos 			err(EXIT_FAILURE, "DRVRESCANBUS");
    159   1.6    dyoung 		break;
    160   1.6    dyoung 	case 'p':
    161   1.5   thorpej 		command_dict = prop_dictionary_create();
    162   1.5   thorpej 		args_dict = prop_dictionary_create();
    163   1.5   thorpej 
    164  1.21   thorpej 		prop_dictionary_set_string_nocopy(command_dict,
    165  1.21   thorpej 		    "drvctl-command", "get-properties");
    166  1.21   thorpej 		prop_dictionary_set_string(args_dict, "device-name",
    167  1.21   thorpej 		    argv[0]);
    168  1.21   thorpej 		prop_dictionary_set_and_rel(command_dict, "drvctl-arguments",
    169  1.19  christos 		    args_dict);
    170   1.5   thorpej 
    171   1.5   thorpej 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    172  1.19  christos 		    DRVCTLCOMMAND, &results_dict);
    173   1.5   thorpej 		prop_object_release(command_dict);
    174   1.5   thorpej 		if (res)
    175  1.19  christos 			errc(EXIT_FAILURE, res, "DRVCTLCOMMAND");
    176   1.5   thorpej 
    177  1.21   thorpej 		if (prop_dictionary_get_int(results_dict, "drvctl-error",
    178  1.21   thorpej 					    &drvctl_error) &&
    179  1.21   thorpej 		    drvctl_error != 0) {
    180  1.21   thorpej 			errc(EXIT_FAILURE, drvctl_error, "get-properties");
    181   1.5   thorpej 		}
    182   1.5   thorpej 
    183   1.5   thorpej 		data_dict = prop_dictionary_get(results_dict,
    184  1.19  christos 		    "drvctl-result-data");
    185   1.5   thorpej 		if (data_dict == NULL) {
    186  1.19  christos 			errx(EXIT_FAILURE,
    187  1.19  christos 			    "get-properties: failed to return result data");
    188   1.5   thorpej 		}
    189   1.5   thorpej 
    190  1.11  jmcneill 		if (argc == 1) {
    191  1.11  jmcneill 			xml = prop_dictionary_externalize(data_dict);
    192  1.14    dyoung 			if (!nflag) {
    193  1.14    dyoung 				printf("Properties for device `%s':\n",
    194  1.14    dyoung 				    argv[0]);
    195  1.14    dyoung 			}
    196  1.14    dyoung 			printf("%s", xml);
    197  1.11  jmcneill 			free(xml);
    198  1.11  jmcneill 		} else {
    199  1.11  jmcneill 			for (i = 1; i < argc; i++)
    200  1.15  pgoyette 				extract_property(data_dict, argv[i], nflag);
    201  1.11  jmcneill 		}
    202  1.11  jmcneill 
    203   1.5   thorpej 		prop_object_release(results_dict);
    204   1.6    dyoung 		break;
    205   1.6    dyoung 	default:
    206  1.19  christos 		errx(EXIT_FAILURE, "unknown command `%c'", mode);
    207   1.6    dyoung 	}
    208   1.1  drochner 
    209  1.19  christos 	return EXIT_SUCCESS;
    210   1.1  drochner }
    211  1.11  jmcneill 
    212  1.11  jmcneill static void
    213  1.15  pgoyette extract_property(prop_dictionary_t dict, const char *prop, bool nflag)
    214  1.11  jmcneill {
    215  1.15  pgoyette 	char *s, *p, *cur, *ep = NULL;
    216  1.11  jmcneill 	prop_object_t obj;
    217  1.17   mlelstv 	unsigned long ind;
    218  1.11  jmcneill 
    219  1.17   mlelstv 	obj = dict;
    220  1.17   mlelstv 	cur = NULL;
    221  1.11  jmcneill 	s = strdup(prop);
    222  1.11  jmcneill 	p = strtok_r(s, "/", &ep);
    223  1.11  jmcneill 	while (p) {
    224  1.11  jmcneill 		cur = p;
    225  1.11  jmcneill 		p = strtok_r(NULL, "/", &ep);
    226  1.17   mlelstv 
    227  1.17   mlelstv 		switch (prop_object_type(obj)) {
    228  1.17   mlelstv 		case PROP_TYPE_DICTIONARY:
    229  1.17   mlelstv 			obj = prop_dictionary_get(obj, cur);
    230  1.17   mlelstv 			if (obj == NULL)
    231  1.22   mlelstv 				p = NULL;
    232  1.17   mlelstv 			break;
    233  1.17   mlelstv 		case PROP_TYPE_ARRAY:
    234  1.17   mlelstv 			ind = strtoul(cur, NULL, 0);
    235  1.17   mlelstv 			obj = prop_array_get(obj, ind);
    236  1.17   mlelstv 			if (obj == NULL)
    237  1.22   mlelstv 				p = NULL;
    238  1.17   mlelstv 			break;
    239  1.17   mlelstv 		default:
    240  1.22   mlelstv 			fprintf(stderr, "Select neither dict nor array with"
    241  1.22   mlelstv 			    " `%s'\n", cur);
    242  1.22   mlelstv 			obj = NULL;
    243  1.22   mlelstv 			p = NULL;
    244  1.22   mlelstv 			break;
    245  1.11  jmcneill 		}
    246  1.11  jmcneill 	}
    247  1.11  jmcneill 
    248  1.17   mlelstv 	if (obj != NULL && cur != NULL)
    249  1.17   mlelstv 		display_object(obj, nflag);
    250  1.17   mlelstv 
    251  1.11  jmcneill 	free(s);
    252  1.11  jmcneill }
    253  1.12  jmcneill 
    254  1.12  jmcneill static void
    255  1.15  pgoyette display_object(prop_object_t obj, bool nflag)
    256  1.15  pgoyette {
    257  1.15  pgoyette 	char *xml;
    258  1.15  pgoyette 	prop_object_t next_obj;
    259  1.15  pgoyette 	prop_object_iterator_t iter;
    260  1.15  pgoyette 
    261  1.15  pgoyette 	if (obj == NULL)
    262  1.15  pgoyette 		exit(EXIT_FAILURE);
    263  1.15  pgoyette 	switch (prop_object_type(obj)) {
    264  1.15  pgoyette 	case PROP_TYPE_BOOL:
    265  1.15  pgoyette 		printf("%s\n", prop_bool_true(obj) ? "true" : "false");
    266  1.15  pgoyette 		break;
    267  1.15  pgoyette 	case PROP_TYPE_NUMBER:
    268  1.21   thorpej 		printf("%" PRId64 "\n", prop_number_signed_value(obj));
    269  1.15  pgoyette 		break;
    270  1.15  pgoyette 	case PROP_TYPE_STRING:
    271  1.21   thorpej 		printf("%s\n", prop_string_value(obj));
    272  1.15  pgoyette 		break;
    273  1.15  pgoyette 	case PROP_TYPE_DICTIONARY:
    274  1.15  pgoyette 		xml = prop_dictionary_externalize(obj);
    275  1.15  pgoyette 		printf("%s", xml);
    276  1.15  pgoyette 		free(xml);
    277  1.15  pgoyette 		break;
    278  1.15  pgoyette 	case PROP_TYPE_ARRAY:
    279  1.15  pgoyette 		iter = prop_array_iterator(obj);
    280  1.15  pgoyette 		if (!nflag)
    281  1.15  pgoyette 			printf("Array:\n");
    282  1.15  pgoyette 		while ((next_obj = prop_object_iterator_next(iter)) != NULL)
    283  1.15  pgoyette 			display_object(next_obj, nflag);
    284  1.15  pgoyette 		break;
    285  1.15  pgoyette 	default:
    286  1.19  christos 		errx(EXIT_FAILURE, "Unhandled type %d", prop_object_type(obj));
    287  1.15  pgoyette 	}
    288  1.15  pgoyette }
    289  1.15  pgoyette 
    290  1.15  pgoyette static void
    291  1.12  jmcneill list_children(int fd, char *dvname, bool nflag, bool tflag, int depth)
    292  1.12  jmcneill {
    293  1.19  christos 	struct devlistargs laa = {
    294  1.19  christos 	    .l_devname = "", .l_childname = NULL, .l_children = 0
    295  1.19  christos 	};
    296  1.12  jmcneill 	size_t children;
    297  1.12  jmcneill 	int i, n;
    298  1.12  jmcneill 
    299  1.12  jmcneill 	if (dvname == NULL) {
    300  1.12  jmcneill 		if (depth > 0)
    301  1.12  jmcneill 			return;
    302  1.12  jmcneill 		*laa.l_devname = '\0';
    303  1.12  jmcneill 	} else {
    304  1.12  jmcneill 		strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname));
    305  1.12  jmcneill 	}
    306  1.12  jmcneill 
    307  1.12  jmcneill 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    308  1.19  christos 		err(EXIT_FAILURE, "DRVLISTDEV");
    309  1.12  jmcneill 
    310  1.12  jmcneill 	children = laa.l_children;
    311  1.12  jmcneill 
    312  1.12  jmcneill 	laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
    313  1.12  jmcneill 	if (laa.l_childname == NULL)
    314  1.19  christos 		err(EXIT_FAILURE, "DRVLISTDEV");
    315  1.12  jmcneill 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    316  1.19  christos 		err(EXIT_FAILURE, "DRVLISTDEV");
    317  1.12  jmcneill 	if (laa.l_children > children)
    318  1.19  christos 		err(EXIT_FAILURE, "DRVLISTDEV: number of children grew");
    319  1.12  jmcneill 
    320  1.12  jmcneill 	for (i = 0; i < (int)laa.l_children; i++) {
    321  1.12  jmcneill 		for (n = 0; n < depth; n++)
    322  1.12  jmcneill 			printf("  ");
    323  1.12  jmcneill 		if (!nflag) {
    324  1.12  jmcneill 			printf("%s ",
    325  1.12  jmcneill 			    (dvname == NULL) ? "root" : laa.l_devname);
    326  1.12  jmcneill 		}
    327  1.12  jmcneill 		printf("%s\n", laa.l_childname[i]);
    328  1.12  jmcneill 		if (tflag) {
    329  1.12  jmcneill 			list_children(fd, laa.l_childname[i], nflag,
    330  1.12  jmcneill 			    tflag, depth + 1);
    331  1.12  jmcneill 		}
    332  1.12  jmcneill 	}
    333  1.12  jmcneill 
    334  1.12  jmcneill 	free(laa.l_childname);
    335  1.12  jmcneill }
    336