1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2011, Fajar A. Nugraha. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/fs/zfs.h>
34 #include <sys/ioctl.h>
35 #include <sys/stat.h>
36
37 #if defined(ZFS_ASAN_ENABLED)
38 /*
39 * zvol_id is invoked by udev with the help of ptrace()
40 * making sanitized binary with leak detection croak
41 * because of tracing mechanisms collision
42 */
43 extern const char *__asan_default_options(void);
44
__asan_default_options(void)45 const char *__asan_default_options(void) {
46 return ("abort_on_error=true:halt_on_error=true:"
47 "allocator_may_return_null=true:disable_coredump=false:"
48 "detect_stack_use_after_return=true:detect_leaks=false");
49 }
50 #endif
51
52 int
main(int argc,const char * const * argv)53 main(int argc, const char *const *argv)
54 {
55 if (argc != 2 || strncmp(argv[1], "/dev/zd", 7) != 0) {
56 fprintf(stderr, "usage: %s /dev/zdX\n", argv[0]);
57 return (1);
58 }
59 const char *dev_name = argv[1];
60 size_t i, len;
61
62 int fd;
63 struct stat sb;
64 if ((fd = open(dev_name, O_RDONLY|O_CLOEXEC)) == -1 ||
65 fstat(fd, &sb) != 0) {
66 fprintf(stderr, "%s: %s\n", dev_name, strerror(errno));
67 return (1);
68 }
69
70 char zvol_name[MAXNAMELEN + strlen("-part") + 10];
71 if (ioctl(fd, BLKZNAME, zvol_name) == -1) {
72 fprintf(stderr, "%s: BLKZNAME: %s\n",
73 dev_name, strerror(errno));
74 return (1);
75 }
76
77 const char *dev_part = strrchr(dev_name, 'p');
78 len = strlen(zvol_name);
79 if (dev_part != NULL) {
80 sprintf(zvol_name + len, "-part%s", dev_part + 1);
81 len = strlen(zvol_name);
82 }
83
84 for (i = 0; i < len; ++i)
85 if (isblank(zvol_name[i]))
86 zvol_name[i] = '+';
87
88 puts(zvol_name);
89
90 return (0);
91 }
92