Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Dec 2017 18:51:47 +0000 (UTC)
From:      Warner Losh <imp@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r327066 - head/usr.sbin/devinfo
Message-ID:  <201712211851.vBLIpl64084267@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: imp
Date: Thu Dec 21 18:51:47 2017
New Revision: 327066
URL: https://svnweb.freebsd.org/changeset/base/327066

Log:
  Implement "-p dev" to print the path to the given device back to the
  nexus. With redirection, could also be used to test if the device
  exists in the device tree.
  
  Sponsored by: Netflix

Modified:
  head/usr.sbin/devinfo/devinfo.8
  head/usr.sbin/devinfo/devinfo.c

Modified: head/usr.sbin/devinfo/devinfo.8
==============================================================================
--- head/usr.sbin/devinfo/devinfo.8	Thu Dec 21 18:30:11 2017	(r327065)
+++ head/usr.sbin/devinfo/devinfo.8	Thu Dec 21 18:51:47 2017	(r327066)
@@ -38,6 +38,8 @@
 .Op Fl rv
 .Nm
 .Fl u
+.Nm
+.Fl p dev
 .Sh DESCRIPTION
 The
 .Nm
@@ -62,6 +64,8 @@ the IRQ consumers together.
 Display all devices in the driver tree, not just those that are attached or
 busy.
 Without this flag, only those devices that have attached are reported.
+.It Fl p dev
+Display the path of dev back to the root of the device tree.
 .El
 .Sh SEE ALSO
 .Xr systat 1 ,

Modified: head/usr.sbin/devinfo/devinfo.c
==============================================================================
--- head/usr.sbin/devinfo/devinfo.c	Thu Dec 21 18:30:11 2017	(r327065)
+++ head/usr.sbin/devinfo/devinfo.c	Thu Dec 21 18:51:47 2017	(r327066)
@@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
 #include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include "devinfo.h"
 
@@ -196,15 +197,47 @@ print_rman(struct devinfo_rman *rman, void *arg __unus
 	return(0);
 }
 
+static void __dead2
+usage(void)
+{
+	fprintf(stderr, "%s\n%s\n%s\n",
+	    "usage: devinfo [-rv]",
+	    "       devinfo -u",
+	    "       devifno -p dev");
+	exit(1);
+}
+
+
+static int
+print_path(struct devinfo_dev *dev, void *xname)
+{
+	const char *name = xname;
+	int rv;
+
+	if (strcmp(dev->dd_name, name) == 0) {
+		printf("%s", dev->dd_name);
+		return (1);
+	}
+
+	rv = devinfo_foreach_device_child(dev, print_path, xname);
+	if (rv == 1)
+		printf(" %s", dev->dd_name[0] ? dev->dd_name : "unknown");
+	return (rv);
+}
+
 int
 main(int argc, char *argv[]) 
 {
 	struct devinfo_dev	*root;
 	int			c, uflag;
+	char			*path;
 
 	uflag = 0;
-	while ((c = getopt(argc, argv, "ruv")) != -1) {
+	while ((c = getopt(argc, argv, "p:ruv")) != -1) {
 		switch(c) {
+		case 'p':
+			path = optarg;
+			break;
 		case 'r':
 			rflag++;
 			break;
@@ -215,21 +248,25 @@ main(int argc, char *argv[]) 
 			vflag++;
 			break;
 		default:
-			fprintf(stderr, "%s\n%s\n",
-			    "usage: devinfo [-rv]",
-			    "       devinfo -u");
-			exit(1);
+			usage();
 		}
 	}
 
+	if (path && (rflag || uflag))
+		usage();
+
 	if (devinfo_init())
 		err(1, "devinfo_init");
 
 	if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
 		errx(1, "can't find root device");
 
-	/* print resource usage? */
-	if (uflag) {
+	if (path) {
+		if (devinfo_foreach_device_child(root, print_path, (void *)path) == 0)
+			errx(1, "%s: Not found", path);
+		printf("\n");
+	} else if (uflag) {
+		/* print resource usage? */
 		devinfo_foreach_rman(print_rman, NULL);
 	} else {
 		/* print device hierarchy */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201712211851.vBLIpl64084267>