xref: /freebsd/tests/sys/cddl/zfs/bin/devname2devid.c (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  * $FreeBSD$
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"@(#)devname2devid.c	1.3	07/05/25 SMI"
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <devid.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 
38 /*
39  * Usage: devname2devid <devicepath>
40  *
41  * Examples:
42  *	# ./devname2devid /dev/c1t4d0s0
43  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/a
44  *	# ./devname2devid /dev/c1t4d0
45  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/wd
46  *	# ./devname2devid /dev/c1t4d0s1
47  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/b
48  *	#
49  *
50  * This program accepts a disk or disk slice path and prints a
51  * device id.
52  *
53  * Exit values:
54  *	0 - means success
55  *	1 - means failure
56  *
57  */
58 int
59 main(int argc, char *argv[])
60 {
61 	int		fd;
62 	ddi_devid_t	devid;
63 	char		*minor_name, *devidstr, *device;
64 #ifdef DEBUG
65 	devid_nmlist_t  *list = NULL;
66 	char		*search_path;
67 	int		i;
68 #endif
69 
70 	if (argc == 1) {
71 		(void) printf("%s <devicepath> [search path]\n",
72 		    argv[0]);
73 		exit(1);
74 	}
75 	device = argv[1];
76 
77 	if ((fd = open(device, O_RDONLY|O_NDELAY)) < 0) {
78 		perror(device);
79 		exit(1);
80 	}
81 	if (devid_get(fd, &devid) != 0) {
82 		perror("devid_get");
83 		exit(1);
84 	}
85 	if (devid_get_minor_name(fd, &minor_name) != 0) {
86 		perror("devid_get_minor_name");
87 		exit(1);
88 	}
89 	if ((devidstr = devid_str_encode(devid, minor_name)) == 0) {
90 		perror("devid_str_encode");
91 		exit(1);
92 	}
93 
94 	(void) printf("devid %s\n", devidstr);
95 
96 	devid_str_free(devidstr);
97 
98 #ifdef DEBUG
99 	if (argc == 3) {
100 		search_path = argv[2];
101 	} else {
102 		search_path = "/dev/";
103 	}
104 
105 	if (devid_deviceid_to_nmlist(search_path, devid, DEVID_MINOR_NAME_ALL,
106 	    &list)) {
107 		perror("devid_deviceid_to_nmlist");
108 		exit(1);
109 	}
110 
111 	/* loop through list and process device names and numbers */
112 	for (i = 0; list[i].devname != NULL; i++) {
113 		(void) printf("devname: %s %p\n", list[i].devname, list[i].dev);
114 	}
115 	devid_free_nmlist(list);
116 
117 #endif /* DEBUG */
118 
119 	devid_str_free(minor_name);
120 	devid_free(devid);
121 
122 	return (0);
123 }
124