2020-06-11 16:49:57 +03:00
|
|
|
/* $NetBSD: drvctl.c,v 1.21 2020/06/11 13:49:57 thorpej Exp $ */
|
2004-08-18 16:30:01 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2004
|
|
|
|
* Matthias Drochner. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions, and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-08-07 16:00:11 +04:00
|
|
|
#include <inttypes.h>
|
2009-04-21 01:40:42 +04:00
|
|
|
#include <stdbool.h>
|
2004-08-18 16:30:01 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/drvctlio.h>
|
|
|
|
|
2006-09-22 08:37:36 +04:00
|
|
|
|
|
|
|
#define OPEN_MODE(mode) \
|
|
|
|
(((mode) == 'd' || (mode) == 'r') ? O_RDWR \
|
|
|
|
: O_RDONLY)
|
2004-08-18 16:30:01 +04:00
|
|
|
|
2011-08-29 18:34:58 +04:00
|
|
|
__dead static void usage(void);
|
2012-01-16 23:43:50 +04:00
|
|
|
static void extract_property(prop_dictionary_t, const char *, bool);
|
|
|
|
static void display_object(prop_object_t, bool);
|
2011-08-07 17:00:35 +04:00
|
|
|
static void list_children(int, char *, bool, bool, int);
|
2004-08-18 16:30:01 +04:00
|
|
|
|
|
|
|
static void
|
2005-01-20 18:56:30 +03:00
|
|
|
usage(void)
|
2004-08-18 16:30:01 +04:00
|
|
|
{
|
2018-01-07 18:26:43 +03:00
|
|
|
const char *p = getprogname();
|
2004-08-18 16:30:01 +04:00
|
|
|
|
2004-08-18 17:24:55 +04:00
|
|
|
fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
|
2006-09-22 08:37:36 +04:00
|
|
|
" %s -d device\n"
|
2011-08-07 17:00:35 +04:00
|
|
|
" %s [-nt] -l [device]\n"
|
2015-12-07 06:34:00 +03:00
|
|
|
" %s [-n] -p device [property]\n"
|
2008-01-27 04:38:33 +03:00
|
|
|
" %s -Q device\n"
|
|
|
|
" %s -R device\n"
|
2018-01-07 18:26:43 +03:00
|
|
|
" %s -S device\n", p, p, p, p, p, p, p);
|
|
|
|
exit(EXIT_FAILURE);
|
2004-08-18 16:30:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2011-08-07 17:00:35 +04:00
|
|
|
bool nflag = false, tflag = false;
|
2004-08-18 16:30:01 +04:00
|
|
|
int c, mode;
|
|
|
|
char *attr = 0;
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
int fd, res;
|
2018-01-07 18:26:43 +03:00
|
|
|
struct devpmargs paa = { .devname = "", .flags = 0 };
|
2004-08-18 16:30:01 +04:00
|
|
|
struct devdetachargs daa;
|
|
|
|
struct devrescanargs raa;
|
|
|
|
int *locs, i;
|
2018-01-07 18:26:43 +03:00
|
|
|
prop_dictionary_t command_dict, args_dict, results_dict, data_dict;
|
2008-01-27 04:38:33 +03:00
|
|
|
char *xml;
|
2020-06-11 16:49:57 +03:00
|
|
|
int drvctl_error;
|
2004-08-18 16:30:01 +04:00
|
|
|
|
2005-06-02 04:00:46 +04:00
|
|
|
mode = 0;
|
2018-01-07 18:26:43 +03:00
|
|
|
while ((c = getopt(argc, argv, "QRSa:dlnprt")) != -1) {
|
2004-08-18 16:30:01 +04:00
|
|
|
switch (c) {
|
2008-01-27 04:38:33 +03:00
|
|
|
case 'Q':
|
|
|
|
case 'R':
|
|
|
|
case 'S':
|
2018-01-07 18:26:43 +03:00
|
|
|
case 'd':
|
|
|
|
case 'l':
|
|
|
|
case 'p':
|
|
|
|
case 'r':
|
|
|
|
mode = c;
|
|
|
|
break;
|
2018-02-14 20:43:09 +03:00
|
|
|
case 'a':
|
|
|
|
attr = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = true;
|
|
|
|
break;
|
2011-08-07 17:00:35 +04:00
|
|
|
case 't':
|
|
|
|
tflag = nflag = true;
|
|
|
|
break;
|
2004-08-18 16:30:01 +04:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2009-04-05 02:05:47 +04:00
|
|
|
if ((argc < 1 && mode != 'l') || mode == 0)
|
2004-08-18 16:30:01 +04:00
|
|
|
usage();
|
|
|
|
|
2006-09-22 08:37:36 +04:00
|
|
|
fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
|
2018-01-07 18:26:43 +03:00
|
|
|
if (fd == -1)
|
|
|
|
err(EXIT_FAILURE, "open %s", DRVCTLDEV);
|
2004-08-18 16:30:01 +04:00
|
|
|
|
2008-01-27 04:38:33 +03:00
|
|
|
switch (mode) {
|
|
|
|
case 'Q':
|
|
|
|
paa.flags = DEVPM_F_SUBTREE;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case 'R':
|
|
|
|
strlcpy(paa.devname, argv[0], sizeof(paa.devname));
|
|
|
|
|
|
|
|
if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "DRVRESUMEDEV");
|
2008-01-27 04:38:33 +03:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
strlcpy(paa.devname, argv[0], sizeof(paa.devname));
|
|
|
|
|
|
|
|
if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "DRVSUSPENDDEV");
|
2008-01-27 04:38:33 +03:00
|
|
|
break;
|
|
|
|
case 'd':
|
2004-08-18 16:30:01 +04:00
|
|
|
strlcpy(daa.devname, argv[0], sizeof(daa.devname));
|
|
|
|
|
2008-01-27 04:38:33 +03:00
|
|
|
if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "DRVDETACHDEV");
|
2008-01-27 04:38:33 +03:00
|
|
|
break;
|
|
|
|
case 'l':
|
2011-08-07 17:00:35 +04:00
|
|
|
list_children(fd, argc ? argv[0] : NULL, nflag, tflag, 0);
|
2008-01-27 04:38:33 +03:00
|
|
|
break;
|
|
|
|
case 'r':
|
2004-08-18 16:30:01 +04:00
|
|
|
memset(&raa, 0, sizeof(raa));
|
|
|
|
strlcpy(raa.busname, argv[0], sizeof(raa.busname));
|
|
|
|
if (attr)
|
|
|
|
strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
|
|
|
|
if (argc > 1) {
|
|
|
|
locs = malloc((argc - 1) * sizeof(int));
|
|
|
|
if (!locs)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "malloc int[%d]", argc - 1);
|
2004-08-18 16:30:01 +04:00
|
|
|
for (i = 0; i < argc - 1; i++)
|
|
|
|
locs[i] = atoi(argv[i + 1]);
|
|
|
|
raa.numlocators = argc - 1;
|
|
|
|
raa.locators = locs;
|
|
|
|
}
|
|
|
|
|
2008-01-27 04:38:33 +03:00
|
|
|
if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "DRVRESCANBUS");
|
2008-01-27 04:38:33 +03:00
|
|
|
break;
|
|
|
|
case 'p':
|
2006-09-22 08:37:36 +04:00
|
|
|
command_dict = prop_dictionary_create();
|
|
|
|
args_dict = prop_dictionary_create();
|
|
|
|
|
2020-06-11 16:49:57 +03:00
|
|
|
prop_dictionary_set_string_nocopy(command_dict,
|
|
|
|
"drvctl-command", "get-properties");
|
|
|
|
prop_dictionary_set_string(args_dict, "device-name",
|
|
|
|
argv[0]);
|
|
|
|
prop_dictionary_set_and_rel(command_dict, "drvctl-arguments",
|
2018-01-07 18:26:43 +03:00
|
|
|
args_dict);
|
2006-09-22 08:37:36 +04:00
|
|
|
|
|
|
|
res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
|
2018-01-07 18:26:43 +03:00
|
|
|
DRVCTLCOMMAND, &results_dict);
|
2006-09-22 08:37:36 +04:00
|
|
|
prop_object_release(command_dict);
|
|
|
|
if (res)
|
2018-01-07 18:26:43 +03:00
|
|
|
errc(EXIT_FAILURE, res, "DRVCTLCOMMAND");
|
2006-09-22 08:37:36 +04:00
|
|
|
|
2020-06-11 16:49:57 +03:00
|
|
|
if (prop_dictionary_get_int(results_dict, "drvctl-error",
|
|
|
|
&drvctl_error) &&
|
|
|
|
drvctl_error != 0) {
|
|
|
|
errc(EXIT_FAILURE, drvctl_error, "get-properties");
|
2006-09-22 08:37:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
data_dict = prop_dictionary_get(results_dict,
|
2018-01-07 18:26:43 +03:00
|
|
|
"drvctl-result-data");
|
2006-09-22 08:37:36 +04:00
|
|
|
if (data_dict == NULL) {
|
2018-01-07 18:26:43 +03:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"get-properties: failed to return result data");
|
2006-09-22 08:37:36 +04:00
|
|
|
}
|
|
|
|
|
2011-08-07 16:00:11 +04:00
|
|
|
if (argc == 1) {
|
|
|
|
xml = prop_dictionary_externalize(data_dict);
|
2011-10-20 02:13:46 +04:00
|
|
|
if (!nflag) {
|
|
|
|
printf("Properties for device `%s':\n",
|
|
|
|
argv[0]);
|
|
|
|
}
|
|
|
|
printf("%s", xml);
|
2011-08-07 16:00:11 +04:00
|
|
|
free(xml);
|
|
|
|
} else {
|
|
|
|
for (i = 1; i < argc; i++)
|
2012-01-16 23:43:50 +04:00
|
|
|
extract_property(data_dict, argv[i], nflag);
|
2011-08-07 16:00:11 +04:00
|
|
|
}
|
2006-09-22 08:37:36 +04:00
|
|
|
|
2011-08-07 16:00:11 +04:00
|
|
|
prop_object_release(results_dict);
|
2008-01-27 04:38:33 +03:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-07 18:26:43 +03:00
|
|
|
errx(EXIT_FAILURE, "unknown command `%c'", mode);
|
2008-01-27 04:38:33 +03:00
|
|
|
}
|
2004-08-18 16:30:01 +04:00
|
|
|
|
2018-01-07 18:26:43 +03:00
|
|
|
return EXIT_SUCCESS;
|
2004-08-18 16:30:01 +04:00
|
|
|
}
|
2011-08-07 16:00:11 +04:00
|
|
|
|
|
|
|
static void
|
2012-01-16 23:43:50 +04:00
|
|
|
extract_property(prop_dictionary_t dict, const char *prop, bool nflag)
|
2011-08-07 16:00:11 +04:00
|
|
|
{
|
2012-01-16 23:43:50 +04:00
|
|
|
char *s, *p, *cur, *ep = NULL;
|
2011-08-07 16:00:11 +04:00
|
|
|
prop_object_t obj;
|
2015-02-24 21:15:29 +03:00
|
|
|
unsigned long ind;
|
2011-08-07 16:00:11 +04:00
|
|
|
|
2015-02-24 21:15:29 +03:00
|
|
|
obj = dict;
|
|
|
|
cur = NULL;
|
2011-08-07 16:00:11 +04:00
|
|
|
s = strdup(prop);
|
|
|
|
p = strtok_r(s, "/", &ep);
|
|
|
|
while (p) {
|
|
|
|
cur = p;
|
|
|
|
p = strtok_r(NULL, "/", &ep);
|
2015-02-24 21:15:29 +03:00
|
|
|
|
|
|
|
switch (prop_object_type(obj)) {
|
|
|
|
case PROP_TYPE_DICTIONARY:
|
|
|
|
obj = prop_dictionary_get(obj, cur);
|
|
|
|
if (obj == NULL)
|
2011-08-07 16:00:11 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2015-02-24 21:15:29 +03:00
|
|
|
break;
|
|
|
|
case PROP_TYPE_ARRAY:
|
|
|
|
ind = strtoul(cur, NULL, 0);
|
|
|
|
obj = prop_array_get(obj, ind);
|
|
|
|
if (obj == NULL)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-07 18:26:43 +03:00
|
|
|
errx(EXIT_FAILURE, "Select neither dict nor array with"
|
|
|
|
" `%s'", cur);
|
2011-08-07 16:00:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:15:29 +03:00
|
|
|
if (obj != NULL && cur != NULL)
|
|
|
|
display_object(obj, nflag);
|
|
|
|
|
2011-08-07 16:00:11 +04:00
|
|
|
free(s);
|
|
|
|
}
|
2011-08-07 17:00:35 +04:00
|
|
|
|
2012-01-16 23:43:50 +04:00
|
|
|
static void
|
|
|
|
display_object(prop_object_t obj, bool nflag)
|
|
|
|
{
|
|
|
|
char *xml;
|
|
|
|
prop_object_t next_obj;
|
|
|
|
prop_object_iterator_t iter;
|
|
|
|
|
|
|
|
if (obj == NULL)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
switch (prop_object_type(obj)) {
|
|
|
|
case PROP_TYPE_BOOL:
|
|
|
|
printf("%s\n", prop_bool_true(obj) ? "true" : "false");
|
|
|
|
break;
|
|
|
|
case PROP_TYPE_NUMBER:
|
2020-06-11 16:49:57 +03:00
|
|
|
printf("%" PRId64 "\n", prop_number_signed_value(obj));
|
2012-01-16 23:43:50 +04:00
|
|
|
break;
|
|
|
|
case PROP_TYPE_STRING:
|
2020-06-11 16:49:57 +03:00
|
|
|
printf("%s\n", prop_string_value(obj));
|
2012-01-16 23:43:50 +04:00
|
|
|
break;
|
|
|
|
case PROP_TYPE_DICTIONARY:
|
|
|
|
xml = prop_dictionary_externalize(obj);
|
|
|
|
printf("%s", xml);
|
|
|
|
free(xml);
|
|
|
|
break;
|
|
|
|
case PROP_TYPE_ARRAY:
|
|
|
|
iter = prop_array_iterator(obj);
|
|
|
|
if (!nflag)
|
|
|
|
printf("Array:\n");
|
|
|
|
while ((next_obj = prop_object_iterator_next(iter)) != NULL)
|
|
|
|
display_object(next_obj, nflag);
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-07 18:26:43 +03:00
|
|
|
errx(EXIT_FAILURE, "Unhandled type %d", prop_object_type(obj));
|
2012-01-16 23:43:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-07 17:00:35 +04:00
|
|
|
static void
|
|
|
|
list_children(int fd, char *dvname, bool nflag, bool tflag, int depth)
|
|
|
|
{
|
2018-01-07 18:26:43 +03:00
|
|
|
struct devlistargs laa = {
|
|
|
|
.l_devname = "", .l_childname = NULL, .l_children = 0
|
|
|
|
};
|
2011-08-07 17:00:35 +04:00
|
|
|
size_t children;
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
if (dvname == NULL) {
|
|
|
|
if (depth > 0)
|
|
|
|
return;
|
|
|
|
*laa.l_devname = '\0';
|
|
|
|
} else {
|
|
|
|
strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, DRVLISTDEV, &laa) == -1)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "DRVLISTDEV");
|
2011-08-07 17:00:35 +04:00
|
|
|
|
|
|
|
children = laa.l_children;
|
|
|
|
|
|
|
|
laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
|
|
|
|
if (laa.l_childname == NULL)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "DRVLISTDEV");
|
2011-08-07 17:00:35 +04:00
|
|
|
if (ioctl(fd, DRVLISTDEV, &laa) == -1)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "DRVLISTDEV");
|
2011-08-07 17:00:35 +04:00
|
|
|
if (laa.l_children > children)
|
2018-01-07 18:26:43 +03:00
|
|
|
err(EXIT_FAILURE, "DRVLISTDEV: number of children grew");
|
2011-08-07 17:00:35 +04:00
|
|
|
|
|
|
|
for (i = 0; i < (int)laa.l_children; i++) {
|
|
|
|
for (n = 0; n < depth; n++)
|
|
|
|
printf(" ");
|
|
|
|
if (!nflag) {
|
|
|
|
printf("%s ",
|
|
|
|
(dvname == NULL) ? "root" : laa.l_devname);
|
|
|
|
}
|
|
|
|
printf("%s\n", laa.l_childname[i]);
|
|
|
|
if (tflag) {
|
|
|
|
list_children(fd, laa.l_childname[i], nflag,
|
|
|
|
tflag, depth + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(laa.l_childname);
|
|
|
|
}
|