]> git.proxmox.com Git - mirror_zfs.git/blob - cmd/zvol_id/zvol_id_main.c
Add `--enable-asan` and `--enable-ubsan` switches
[mirror_zfs.git] / cmd / zvol_id / zvol_id_main.c
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 * Copyright (c) 2011, Fajar A. Nugraha. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <linux/ioctl.h>
33 #include <sys/stat.h>
34 #include <sys/ioctl.h>
35 #include <sys/zfs_znode.h>
36 #include <sys/fs/zfs.h>
37
38 #if defined(ZFS_ASAN_ENABLED)
39 /*
40 * zvol_id is invoked by udev with the help of ptrace()
41 * making sanitized binary with leak detection croak
42 * because of tracing mechanisms collision
43 */
44 extern const char *__asan_default_options(void);
45
46 const char *__asan_default_options(void) {
47 return ("abort_on_error=true:halt_on_error=true:"
48 "allocator_may_return_null=true:disable_coredump=false:"
49 "detect_stack_use_after_return=true:detect_leaks=false");
50 }
51 #endif
52
53 static int
54 ioctl_get_msg(char *var, int fd)
55 {
56 int ret;
57 char msg[ZFS_MAX_DATASET_NAME_LEN];
58
59 ret = ioctl(fd, BLKZNAME, msg);
60 if (ret < 0) {
61 return (ret);
62 }
63
64 snprintf(var, ZFS_MAX_DATASET_NAME_LEN, "%s", msg);
65 return (ret);
66 }
67
68 int
69 main(int argc, char **argv)
70 {
71 int fd = -1, ret = 0, status = EXIT_FAILURE;
72 char zvol_name[ZFS_MAX_DATASET_NAME_LEN];
73 char *zvol_name_part = NULL;
74 char *dev_name;
75 struct stat64 statbuf;
76 int dev_minor, dev_part;
77 int i;
78
79 if (argc < 2) {
80 fprintf(stderr, "Usage: %s /dev/zvol_device_node\n", argv[0]);
81 goto fail;
82 }
83
84 dev_name = argv[1];
85 ret = stat64(dev_name, &statbuf);
86 if (ret != 0) {
87 fprintf(stderr, "Unable to access device file: %s\n", dev_name);
88 goto fail;
89 }
90
91 dev_minor = minor(statbuf.st_rdev);
92 dev_part = dev_minor % ZVOL_MINORS;
93
94 fd = open(dev_name, O_RDONLY);
95 if (fd < 0) {
96 fprintf(stderr, "Unable to open device file: %s\n", dev_name);
97 goto fail;
98 }
99
100 ret = ioctl_get_msg(zvol_name, fd);
101 if (ret < 0) {
102 fprintf(stderr, "ioctl_get_msg failed: %s\n", strerror(errno));
103 goto fail;
104 }
105 if (dev_part > 0)
106 ret = asprintf(&zvol_name_part, "%s-part%d", zvol_name,
107 dev_part);
108 else
109 ret = asprintf(&zvol_name_part, "%s", zvol_name);
110
111 if (ret == -1 || zvol_name_part == NULL)
112 goto fail;
113
114 for (i = 0; i < strlen(zvol_name_part); i++) {
115 if (isblank(zvol_name_part[i]))
116 zvol_name_part[i] = '+';
117 }
118
119 printf("%s\n", zvol_name_part);
120 status = EXIT_SUCCESS;
121
122 fail:
123 if (zvol_name_part)
124 free(zvol_name_part);
125 if (fd >= 0)
126 close(fd);
127
128 return (status);
129 }