xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/channel_program/synctask_core/change_key.c (revision b0de25cb23668fa4535078d18a0618eee442c000)
1 /*
2  * CDDL HEADER START
3  *
4  * This file and its contents are supplied under the terms of the
5  * Common Development and Distribution License ("CDDL"), version 1.0.
6  * You may only use this file in accordance with the terms of version
7  * 1.0 of the CDDL.
8  *
9  * A full copy of the text of the CDDL should have accompanied this
10  * source.  A copy of the CDDL is also available via the Internet at
11  * http://www.illumos.org/license/CDDL.
12  *
13  * CDDL HEADER END
14  */
15 
16 /*
17  * Copyright 2020 Joyent, Inc.
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/debug.h>
24 #include <sys/fs/zfs.h>
25 #include <libzfs_core.h>
26 #include <libnvpair.h>
27 
28 const char prog[] =
29 	"arg = ... \n"
30 	"fs = arg[\"dataset\"]\n"
31 	"hexkey = arg[\"" ZPOOL_HIDDEN_ARGS "\"][\"key\"]\n"
32 	"err = zfs.sync.change_key(fs, hexkey, 'hex')\n"
33 	"msg = \"changing key on \" .. fs .. \" err=\" .. err\n"
34 	"return msg";
35 
36 /*
37  * Get the pool name from a dataset. This is crude but good enough
38  * for a test.
39  */
40 static char *
41 get_pool(const char *dataset)
42 {
43 	char *res = strdup(dataset);
44 
45 	if (res == NULL)
46 		abort();
47 
48 	char *p = strchr(res, '/');
49 
50 	if (p != NULL)
51 		*p = '\0';
52 
53 	return (res);
54 }
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	const char *dataset = argv[1];
60 	const char *key = argv[2];
61 	char *pool = NULL;
62 	nvlist_t *args = fnvlist_alloc();
63 	nvlist_t *hidden_args = fnvlist_alloc();
64 	nvlist_t *result = NULL;
65 	int ret = 0;
66 
67 	if (argc != 3) {
68 		(void) fprintf(stderr, "Usage: %s dataset key\n", argv[0]);
69 		exit(2);
70 	}
71 
72 	VERIFY0(libzfs_core_init());
73 
74 	pool = get_pool(dataset);
75 
76 	fnvlist_add_string(args, "dataset", dataset);
77 	fnvlist_add_string(hidden_args, "key", key);
78 	fnvlist_add_nvlist(args, ZPOOL_HIDDEN_ARGS, hidden_args);
79 
80 	ret = lzc_channel_program(pool, prog, ZCP_DEFAULT_INSTRLIMIT,
81 	    ZCP_DEFAULT_MEMLIMIT, args, &result);
82 
83 	(void) printf("lzc_channel_program returned %d", ret);
84 	if (ret != 0)
85 		(void) printf(" (%s)", strerror(ret));
86 	(void) fputc('\n', stdout);
87 
88 	dump_nvlist(result, 5);
89 
90 	nvlist_free(args);
91 	nvlist_free(hidden_args);
92 	nvlist_free(result);
93 	free(pool);
94 
95 	libzfs_core_fini();
96 
97 	return (ret);
98 }
99