1716fd348SMartin Matuska /*
2716fd348SMartin Matuska * CDDL HEADER START
3716fd348SMartin Matuska *
4716fd348SMartin Matuska * The contents of this file are subject to the terms of the
5716fd348SMartin Matuska * Common Development and Distribution License (the "License").
6716fd348SMartin Matuska * You may not use this file except in compliance with the License.
7716fd348SMartin Matuska *
8716fd348SMartin Matuska * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10716fd348SMartin Matuska * See the License for the specific language governing permissions
11716fd348SMartin Matuska * and limitations under the License.
12716fd348SMartin Matuska *
13716fd348SMartin Matuska * When distributing Covered Code, include this CDDL HEADER in each
14716fd348SMartin Matuska * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15716fd348SMartin Matuska * If applicable, add the following below this CDDL HEADER, with the
16716fd348SMartin Matuska * fields enclosed by brackets "[]" replaced with your own identifying
17716fd348SMartin Matuska * information: Portions Copyright [yyyy] [name of copyright owner]
18716fd348SMartin Matuska *
19716fd348SMartin Matuska * CDDL HEADER END
20716fd348SMartin Matuska */
21716fd348SMartin Matuska /*
22716fd348SMartin Matuska * Copyright (c) 2011, Fajar A. Nugraha. All rights reserved.
23716fd348SMartin Matuska * Use is subject to license terms.
24716fd348SMartin Matuska */
25716fd348SMartin Matuska
26716fd348SMartin Matuska #include <ctype.h>
27716fd348SMartin Matuska #include <errno.h>
28716fd348SMartin Matuska #include <fcntl.h>
29716fd348SMartin Matuska #include <stdio.h>
30716fd348SMartin Matuska #include <string.h>
31716fd348SMartin Matuska #include <unistd.h>
32716fd348SMartin Matuska #include <sys/fs/zfs.h>
33716fd348SMartin Matuska #include <sys/ioctl.h>
34716fd348SMartin Matuska #include <sys/stat.h>
35716fd348SMartin Matuska
36716fd348SMartin Matuska #if defined(ZFS_ASAN_ENABLED)
37716fd348SMartin Matuska /*
38716fd348SMartin Matuska * zvol_id is invoked by udev with the help of ptrace()
39716fd348SMartin Matuska * making sanitized binary with leak detection croak
40716fd348SMartin Matuska * because of tracing mechanisms collision
41716fd348SMartin Matuska */
42716fd348SMartin Matuska extern const char *__asan_default_options(void);
43716fd348SMartin Matuska
__asan_default_options(void)44716fd348SMartin Matuska const char *__asan_default_options(void) {
45716fd348SMartin Matuska return ("abort_on_error=true:halt_on_error=true:"
46716fd348SMartin Matuska "allocator_may_return_null=true:disable_coredump=false:"
47716fd348SMartin Matuska "detect_stack_use_after_return=true:detect_leaks=false");
48716fd348SMartin Matuska }
49716fd348SMartin Matuska #endif
50716fd348SMartin Matuska
51716fd348SMartin Matuska int
main(int argc,const char * const * argv)52716fd348SMartin Matuska main(int argc, const char *const *argv)
53716fd348SMartin Matuska {
54783d3ff6SMartin Matuska if (argc != 2 || strncmp(argv[1], "/dev/zd", 7) != 0) {
55716fd348SMartin Matuska fprintf(stderr, "usage: %s /dev/zdX\n", argv[0]);
56716fd348SMartin Matuska return (1);
57716fd348SMartin Matuska }
58716fd348SMartin Matuska const char *dev_name = argv[1];
59*7a7741afSMartin Matuska size_t i, len;
60716fd348SMartin Matuska
61716fd348SMartin Matuska int fd;
62716fd348SMartin Matuska struct stat sb;
63716fd348SMartin Matuska if ((fd = open(dev_name, O_RDONLY|O_CLOEXEC)) == -1 ||
64716fd348SMartin Matuska fstat(fd, &sb) != 0) {
65716fd348SMartin Matuska fprintf(stderr, "%s: %s\n", dev_name, strerror(errno));
66716fd348SMartin Matuska return (1);
67716fd348SMartin Matuska }
68716fd348SMartin Matuska
69716fd348SMartin Matuska char zvol_name[MAXNAMELEN + strlen("-part") + 10];
70716fd348SMartin Matuska if (ioctl(fd, BLKZNAME, zvol_name) == -1) {
71716fd348SMartin Matuska fprintf(stderr, "%s: BLKZNAME: %s\n",
72716fd348SMartin Matuska dev_name, strerror(errno));
73716fd348SMartin Matuska return (1);
74716fd348SMartin Matuska }
75716fd348SMartin Matuska
76783d3ff6SMartin Matuska const char *dev_part = strrchr(dev_name, 'p');
77*7a7741afSMartin Matuska len = strlen(zvol_name);
78783d3ff6SMartin Matuska if (dev_part != NULL) {
79*7a7741afSMartin Matuska sprintf(zvol_name + len, "-part%s", dev_part + 1);
80*7a7741afSMartin Matuska len = strlen(zvol_name);
81783d3ff6SMartin Matuska }
82716fd348SMartin Matuska
83*7a7741afSMartin Matuska for (i = 0; i < len; ++i)
84716fd348SMartin Matuska if (isblank(zvol_name[i]))
85716fd348SMartin Matuska zvol_name[i] = '+';
86716fd348SMartin Matuska
87716fd348SMartin Matuska puts(zvol_name);
88716fd348SMartin Matuska
89716fd348SMartin Matuska return (0);
90716fd348SMartin Matuska }
91