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 https://opensource.org/licenses/CDDL-1.0.
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 /*
23  * Portions Copyright 2020 iXsystems, Inc.
24  */
25 
26 /*
27  * Test some invalid send operations with libzfs/libzfs_core.
28  *
29  * Specifying the to and from snaps in the wrong order should return EXDEV.
30  * We are checking that the early return doesn't accidentally leave any
31  * references held, so this test is designed to trigger a panic when asserts
32  * are verified with the bug present.
33  */
34 
35 #include <libzfs.h>
36 #include <libzfs_core.h>
37 
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sysexits.h>
43 #include <err.h>
44 
45 static void
46 usage(const char *name)
47 {
48 	fprintf(stderr, "usage: %s snap0 snap1\n", name);
49 	exit(EX_USAGE);
50 }
51 
52 int
53 main(int argc, char const * const argv[])
54 {
55 	sendflags_t flags = { 0 };
56 	libzfs_handle_t *zhdl;
57 	zfs_handle_t *zhp;
58 	const char *fromfull, *tofull, *fsname, *fromsnap, *tosnap, *p;
59 	uint64_t size;
60 	int fd, error;
61 
62 	if (argc != 3)
63 		usage(argv[0]);
64 
65 	fromfull = argv[1];
66 	tofull = argv[2];
67 
68 	p = strchr(fromfull, '@');
69 	if (p == NULL)
70 		usage(argv[0]);
71 	fromsnap = p + 1;
72 
73 	p = strchr(tofull, '@');
74 	if (p == NULL)
75 		usage(argv[0]);
76 	tosnap = p + 1;
77 
78 	fsname = strndup(tofull, p - tofull);
79 	if (fsname == NULL) {
80 		perror("strndup");
81 		exit(EXIT_FAILURE);
82 	}
83 	if (strncmp(fsname, fromfull, p - tofull) != 0)
84 		usage(argv[0]);
85 
86 	fd = open("/dev/null", O_WRONLY);
87 	if (fd == -1)
88 		err(EX_OSERR, "open(\"/dev/null\", O_WRONLY)");
89 
90 	zhdl = libzfs_init();
91 	if (zhdl == NULL)
92 		errx(EX_OSERR, "libzfs_init(): %s", libzfs_error_init(errno));
93 
94 	zhp = zfs_open(zhdl, fsname, ZFS_TYPE_FILESYSTEM);
95 	if (zhp == NULL)
96 		err(EX_OSERR, "zfs_open(\"%s\")", fsname);
97 
98 	/*
99 	 * Exercise EXDEV in dmu_send_obj.  The error gets translated to
100 	 * EZFS_CROSSTARGET in libzfs.
101 	 */
102 	error = zfs_send(zhp, tosnap, fromsnap, &flags, fd, NULL, NULL, NULL);
103 	if (error == 0 || libzfs_errno(zhdl) != EZFS_CROSSTARGET)
104 		errx(EX_OSERR, "zfs_send(\"%s\", \"%s\") should have failed "
105 		    "with EZFS_CROSSTARGET, not %d",
106 		    tofull, fromfull, libzfs_errno(zhdl));
107 	printf("zfs_send(\"%s\", \"%s\"): %s\n",
108 	    tofull, fromfull, libzfs_error_description(zhdl));
109 
110 	zfs_close(zhp);
111 
112 	/*
113 	 * Exercise EXDEV in dmu_send.
114 	 */
115 	error = lzc_send_resume_redacted(fromfull, tofull, fd, 0, 0, 0, NULL);
116 	if (error != EXDEV)
117 		errx(EX_OSERR, "lzc_send_resume_redacted(\"%s\", \"%s\")"
118 		    " should have failed with EXDEV, not %d",
119 		    fromfull, tofull, error);
120 	printf("lzc_send_resume_redacted(\"%s\", \"%s\"): %s\n",
121 	    fromfull, tofull, strerror(error));
122 
123 	/*
124 	 * Exercise EXDEV in dmu_send_estimate_fast.
125 	 */
126 	error = lzc_send_space_resume_redacted(fromfull, tofull, 0, 0, 0, 0,
127 	    NULL, fd, &size);
128 	if (error != EXDEV)
129 		errx(EX_OSERR, "lzc_send_space_resume_redacted(\"%s\", \"%s\")"
130 		    " should have failed with EXDEV, not %d",
131 		    fromfull, tofull, error);
132 	printf("lzc_send_space_resume_redacted(\"%s\", \"%s\"): %s\n",
133 	    fromfull, tofull, strerror(error));
134 
135 	close(fd);
136 	libzfs_fini(zhdl);
137 	free((void *)fsname);
138 
139 	return (EXIT_SUCCESS);
140 }
141