xref: /freebsd/tests/sys/cddl/zfs/bin/devname2devid.c (revision 63f537551380d2dab29fa402ad1269feae17e594)
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  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"@(#)devname2devid.c	1.3	07/05/25 SMI"
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <devid.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <fcntl.h>
36 
37 /*
38  * Usage: devname2devid <devicepath>
39  *
40  * Examples:
41  *	# ./devname2devid /dev/c1t4d0s0
42  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/a
43  *	# ./devname2devid /dev/c1t4d0
44  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/wd
45  *	# ./devname2devid /dev/c1t4d0s1
46  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/b
47  *	#
48  *
49  * This program accepts a disk or disk slice path and prints a
50  * device id.
51  *
52  * Exit values:
53  *	0 - means success
54  *	1 - means failure
55  *
56  */
57 int
58 main(int argc, char *argv[])
59 {
60 	int		fd;
61 	ddi_devid_t	devid;
62 	char		*minor_name, *devidstr, *device;
63 #ifdef DEBUG
64 	devid_nmlist_t  *list = NULL;
65 	char		*search_path;
66 	int		i;
67 #endif
68 
69 	if (argc == 1) {
70 		(void) printf("%s <devicepath> [search path]\n",
71 		    argv[0]);
72 		exit(1);
73 	}
74 	device = argv[1];
75 
76 	if ((fd = open(device, O_RDONLY|O_NDELAY)) < 0) {
77 		perror(device);
78 		exit(1);
79 	}
80 	if (devid_get(fd, &devid) != 0) {
81 		perror("devid_get");
82 		exit(1);
83 	}
84 	if (devid_get_minor_name(fd, &minor_name) != 0) {
85 		perror("devid_get_minor_name");
86 		exit(1);
87 	}
88 	if ((devidstr = devid_str_encode(devid, minor_name)) == 0) {
89 		perror("devid_str_encode");
90 		exit(1);
91 	}
92 
93 	(void) printf("devid %s\n", devidstr);
94 
95 	devid_str_free(devidstr);
96 
97 #ifdef DEBUG
98 	if (argc == 3) {
99 		search_path = argv[2];
100 	} else {
101 		search_path = "/dev/";
102 	}
103 
104 	if (devid_deviceid_to_nmlist(search_path, devid, DEVID_MINOR_NAME_ALL,
105 	    &list)) {
106 		perror("devid_deviceid_to_nmlist");
107 		exit(1);
108 	}
109 
110 	/* loop through list and process device names and numbers */
111 	for (i = 0; list[i].devname != NULL; i++) {
112 		(void) printf("devname: %s %p\n", list[i].devname, list[i].dev);
113 	}
114 	devid_free_nmlist(list);
115 
116 #endif /* DEBUG */
117 
118 	devid_str_free(minor_name);
119 	devid_free(devid);
120 
121 	return (0);
122 }
123