xref: /freebsd/sys/contrib/openzfs/udev/zvol_id.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
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 
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
52716fd348SMartin Matuska main(int argc, const char *const *argv)
53716fd348SMartin Matuska {
54*783d3ff6SMartin 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];
59716fd348SMartin Matuska 
60716fd348SMartin Matuska 	int fd;
61716fd348SMartin Matuska 	struct stat sb;
62716fd348SMartin Matuska 	if ((fd = open(dev_name, O_RDONLY|O_CLOEXEC)) == -1 ||
63716fd348SMartin Matuska 	    fstat(fd, &sb) != 0) {
64716fd348SMartin Matuska 		fprintf(stderr, "%s: %s\n", dev_name, strerror(errno));
65716fd348SMartin Matuska 		return (1);
66716fd348SMartin Matuska 	}
67716fd348SMartin Matuska 
68716fd348SMartin Matuska 	char zvol_name[MAXNAMELEN + strlen("-part") + 10];
69716fd348SMartin Matuska 	if (ioctl(fd, BLKZNAME, zvol_name) == -1) {
70716fd348SMartin Matuska 		fprintf(stderr, "%s: BLKZNAME: %s\n",
71716fd348SMartin Matuska 		    dev_name, strerror(errno));
72716fd348SMartin Matuska 		return (1);
73716fd348SMartin Matuska 	}
74716fd348SMartin Matuska 
75*783d3ff6SMartin Matuska 	const char *dev_part = strrchr(dev_name, 'p');
76*783d3ff6SMartin Matuska 	if (dev_part != NULL) {
77*783d3ff6SMartin Matuska 		sprintf(zvol_name + strlen(zvol_name), "-part%s", dev_part + 1);
78*783d3ff6SMartin Matuska 	}
79716fd348SMartin Matuska 
80716fd348SMartin Matuska 	for (size_t i = 0; i < strlen(zvol_name); ++i)
81716fd348SMartin Matuska 		if (isblank(zvol_name[i]))
82716fd348SMartin Matuska 			zvol_name[i] = '+';
83716fd348SMartin Matuska 
84716fd348SMartin Matuska 	puts(zvol_name);
85716fd348SMartin Matuska 
86716fd348SMartin Matuska 	return (0);
87716fd348SMartin Matuska }
88