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