Linux: Fix detection of register_sysctl_sz
[zfs.git] / tests / zfs-tests / cmd / badsend.c
blob7a54532fb28d401467cdd3d6c99d740660c6ac52
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Portions Copyright 2020 iXsystems, Inc.
27 * Test some invalid send operations with libzfs/libzfs_core.
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.
35 #include <libzfs.h>
36 #include <libzfs_core.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sysexits.h>
43 #include <err.h>
45 static void
46 usage(const char *name)
48 fprintf(stderr, "usage: %s snap0 snap1\n", name);
49 exit(EX_USAGE);
52 int
53 main(int argc, char const * const argv[])
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;
62 if (argc != 3)
63 usage(argv[0]);
65 fromfull = argv[1];
66 tofull = argv[2];
68 p = strchr(fromfull, '@');
69 if (p == NULL)
70 usage(argv[0]);
71 fromsnap = p + 1;
73 p = strchr(tofull, '@');
74 if (p == NULL)
75 usage(argv[0]);
76 tosnap = p + 1;
78 fsname = strndup(tofull, p - tofull);
79 if (fsname == NULL) {
80 perror("strndup");
81 exit(EXIT_FAILURE);
83 if (strncmp(fsname, fromfull, p - tofull) != 0)
84 usage(argv[0]);
86 fd = open("/dev/null", O_WRONLY);
87 if (fd == -1)
88 err(EX_OSERR, "open(\"/dev/null\", O_WRONLY)");
90 zhdl = libzfs_init();
91 if (zhdl == NULL)
92 errx(EX_OSERR, "libzfs_init(): %s", libzfs_error_init(errno));
94 zhp = zfs_open(zhdl, fsname, ZFS_TYPE_FILESYSTEM);
95 if (zhp == NULL)
96 err(EX_OSERR, "zfs_open(\"%s\")", fsname);
99 * Exercise EXDEV in dmu_send_obj. The error gets translated to
100 * EZFS_CROSSTARGET in libzfs.
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));
110 zfs_close(zhp);
113 * Exercise EXDEV in dmu_send.
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));
124 * Exercise EXDEV in dmu_send_estimate_fast.
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));
135 close(fd);
136 libzfs_fini(zhdl);
137 free((void *)fsname);
139 return (EXIT_SUCCESS);