xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/zcp_support.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /* Test for zfs channel program support */
14 
15 #include <err.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/ioctl.h>
20 #include <sys/nvpair.h>
21 #include <sys/zfs_ioctl.h>
22 #include <sys/fs/zfs.h>
23 
24 
25 int
main(int argc,const char * const * argv)26 main(int argc, const char *const *argv)
27 {
28 	if (argc != 2)
29 		errx(EXIT_FAILURE, "usage: %s pool", argv[0]);
30 
31 	zfs_cmd_t zc = {};
32 	(void) strlcpy(zc.zc_name, argv[1], sizeof (zc.zc_name));
33 
34 	char prog[8192] = {};
35 	snprintf(prog, 8192, "zfs.exists(\"%s\")", zc.zc_name);
36 
37 	int fd = open(ZFS_DEV, O_RDWR);
38 	if (fd == -1)
39 		err(EXIT_FAILURE, "%s", ZFS_DEV);
40 
41 	nvlist_t *zc_args = fnvlist_alloc();
42 	fnvlist_add_string(zc_args, ZCP_ARG_PROGRAM, prog);
43 
44 	size_t sz = 0;
45 	char *packed_nvl = fnvlist_pack(zc_args, &sz);
46 	zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed_nvl;
47 	zc.zc_nvlist_src_size = sz;
48 
49 	int rc = EXIT_SUCCESS;
50 
51 	if (ioctl(fd, ZFS_IOC_CHANNEL_PROGRAM, &zc) == ZFS_ERR_IOC_CMD_UNAVAIL)
52 		rc = EXIT_FAILURE;
53 
54 	close(fd);
55 	fnvlist_free(zc_args);
56 	fnvlist_pack_free(packed_nvl, sz);
57 	return (rc);
58 }
59