Path: blob/main/sys/contrib/openzfs/udev/zvol_id.c
48262 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2011, Fajar A. Nugraha. All rights reserved.23* Use is subject to license terms.24*/2526#include <ctype.h>27#include <errno.h>28#include <fcntl.h>29#include <stdio.h>30#include <string.h>31#include <unistd.h>32#include <sys/fs/zfs.h>33#include <sys/ioctl.h>34#include <sys/stat.h>3536#if defined(ZFS_ASAN_ENABLED)37/*38* zvol_id is invoked by udev with the help of ptrace()39* making sanitized binary with leak detection croak40* because of tracing mechanisms collision41*/42extern const char *__asan_default_options(void);4344const char *__asan_default_options(void) {45return ("abort_on_error=true:halt_on_error=true:"46"allocator_may_return_null=true:disable_coredump=false:"47"detect_stack_use_after_return=true:detect_leaks=false");48}49#endif5051int52main(int argc, const char *const *argv)53{54if (argc != 2 || strncmp(argv[1], "/dev/zd", 7) != 0) {55fprintf(stderr, "usage: %s /dev/zdX\n", argv[0]);56return (1);57}58const char *dev_name = argv[1];59size_t i, len;6061int fd;62struct stat sb;63if ((fd = open(dev_name, O_RDONLY|O_CLOEXEC)) == -1 ||64fstat(fd, &sb) != 0) {65fprintf(stderr, "%s: %s\n", dev_name, strerror(errno));66return (1);67}6869char zvol_name[MAXNAMELEN + strlen("-part") + 10];70if (ioctl(fd, BLKZNAME, zvol_name) == -1) {71fprintf(stderr, "%s: BLKZNAME: %s\n",72dev_name, strerror(errno));73return (1);74}7576const char *dev_part = strrchr(dev_name, 'p');77len = strlen(zvol_name);78if (dev_part != NULL) {79sprintf(zvol_name + len, "-part%s", dev_part + 1);80len = strlen(zvol_name);81}8283for (i = 0; i < len; ++i)84if (isblank(zvol_name[i]))85zvol_name[i] = '+';8687puts(zvol_name);8889return (0);90}919293