Home | History | Annotate | Line # | Download | only in drvctl
drvctl.c revision 1.8
      1 /* $NetBSD: drvctl.c,v 1.8 2009/04/04 22:05:47 joerg Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004
      5  * 	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions, and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <unistd.h>
     32 #include <err.h>
     33 #include <fcntl.h>
     34 #include <string.h>
     35 #include <sys/ioctl.h>
     36 #include <sys/drvctlio.h>
     37 
     38 #define OPTS "QRSa:dlpr"
     39 
     40 #define	OPEN_MODE(mode)							\
     41 	(((mode) == 'd' || (mode) == 'r') ? O_RDWR			\
     42 					  : O_RDONLY)
     43 
     44 static void usage(void);
     45 
     46 static void
     47 usage(void)
     48 {
     49 
     50 	fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
     51 	    "       %s -d device\n"
     52 	    "       %s -l [device]\n"
     53 	    "       %s -p device\n"
     54 	    "       %s -Q device\n"
     55 	    "       %s -R device\n"
     56 	    "       %s -S device\n",
     57 	    getprogname(), getprogname(), getprogname(), getprogname(),
     58 	    getprogname(), getprogname(), getprogname());
     59 	exit(1);
     60 }
     61 
     62 int
     63 main(int argc, char **argv)
     64 {
     65 	int c, mode;
     66 	char *attr = 0;
     67 	extern char *optarg;
     68 	extern int optind;
     69 	int fd, res;
     70 	size_t children;
     71 	struct devpmargs paa = {.devname = "", .flags = 0};
     72 	struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
     73 				  .l_children = 0};
     74 	struct devdetachargs daa;
     75 	struct devrescanargs raa;
     76 	int *locs, i;
     77 	prop_dictionary_t command_dict, args_dict, results_dict,
     78 			  data_dict;
     79 	prop_string_t string;
     80 	prop_number_t number;
     81 	char *xml;
     82 
     83 	mode = 0;
     84 	while ((c = getopt(argc, argv, OPTS)) != -1) {
     85 		switch (c) {
     86 		case 'Q':
     87 		case 'R':
     88 		case 'S':
     89 		case 'd':
     90 		case 'l':
     91 		case 'p':
     92 		case 'r':
     93 			mode = c;
     94 			break;
     95 		case 'a':
     96 			attr = optarg;
     97 			break;
     98 		case '?':
     99 		default:
    100 			usage();
    101 		}
    102 	}
    103 
    104 	argc -= optind;
    105 	argv += optind;
    106 
    107 	if ((argc < 1 && mode != 'l') || mode == 0)
    108 		usage();
    109 
    110 	fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
    111 	if (fd < 0)
    112 		err(2, "open %s", DRVCTLDEV);
    113 
    114 	switch (mode) {
    115 	case 'Q':
    116 		paa.flags = DEVPM_F_SUBTREE;
    117 		/*FALLTHROUGH*/
    118 	case 'R':
    119 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    120 
    121 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
    122 			err(3, "DRVRESUMEDEV");
    123 		break;
    124 	case 'S':
    125 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    126 
    127 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
    128 			err(3, "DRVSUSPENDDEV");
    129 		break;
    130 	case 'd':
    131 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
    132 
    133 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
    134 			err(3, "DRVDETACHDEV");
    135 		break;
    136 	case 'l':
    137 		if (argc == 0)
    138 			*laa.l_devname = '\0';
    139 		else
    140 			strlcpy(laa.l_devname, argv[0], sizeof(laa.l_devname));
    141 
    142 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    143 			err(3, "DRVLISTDEV");
    144 
    145 		children = laa.l_children;
    146 
    147 		laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
    148 		if (laa.l_childname == NULL)
    149 			err(5, "DRVLISTDEV");
    150 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    151 			err(3, "DRVLISTDEV");
    152 		if (laa.l_children > children)
    153 			err(6, "DRVLISTDEV: number of children grew");
    154 
    155 		for (i = 0; i < (int)laa.l_children; i++) {
    156 			printf("%s%s%s\n", laa.l_devname, (argc ? " " : ""),
    157 			    laa.l_childname[i]);
    158 		}
    159 		break;
    160 	case 'r':
    161 		memset(&raa, 0, sizeof(raa));
    162 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
    163 		if (attr)
    164 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
    165 		if (argc > 1) {
    166 			locs = malloc((argc - 1) * sizeof(int));
    167 			if (!locs)
    168 				err(5, "malloc int[%d]", argc - 1);
    169 			for (i = 0; i < argc - 1; i++)
    170 				locs[i] = atoi(argv[i + 1]);
    171 			raa.numlocators = argc - 1;
    172 			raa.locators = locs;
    173 		}
    174 
    175 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
    176 			err(3, "DRVRESCANBUS");
    177 		break;
    178 	case 'p':
    179 
    180 		command_dict = prop_dictionary_create();
    181 		args_dict = prop_dictionary_create();
    182 
    183 		string = prop_string_create_cstring_nocopy("get-properties");
    184 		prop_dictionary_set(command_dict, "drvctl-command", string);
    185 		prop_object_release(string);
    186 
    187 		string = prop_string_create_cstring(argv[0]);
    188 		prop_dictionary_set(args_dict, "device-name", string);
    189 		prop_object_release(string);
    190 
    191 		prop_dictionary_set(command_dict, "drvctl-arguments",
    192 				    args_dict);
    193 		prop_object_release(args_dict);
    194 
    195 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    196 						     DRVCTLCOMMAND,
    197 						     &results_dict);
    198 		prop_object_release(command_dict);
    199 		if (res)
    200 			errx(3, "DRVCTLCOMMAND: %s", strerror(res));
    201 
    202 		number = prop_dictionary_get(results_dict, "drvctl-error");
    203 		if (prop_number_integer_value(number) != 0) {
    204 			errx(3, "get-properties: %s",
    205 			    strerror((int)prop_number_integer_value(number)));
    206 		}
    207 
    208 		data_dict = prop_dictionary_get(results_dict,
    209 						"drvctl-result-data");
    210 		if (data_dict == NULL) {
    211 			errx(3, "get-properties: failed to return result data");
    212 		}
    213 
    214 		xml = prop_dictionary_externalize(data_dict);
    215 		prop_object_release(results_dict);
    216 
    217 		printf("Properties for device `%s':\n%s",
    218 		       argv[0], xml);
    219 		free(xml);
    220 		break;
    221 	default:
    222 		errx(4, "unknown command");
    223 	}
    224 
    225 	return (0);
    226 }
    227