1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Description:
8f08c3bdfSopenharmony_ci *   Verify that,
9f08c3bdfSopenharmony_ci *   1) vmsplice() returns -1 and sets errno to EBADF if fd
10f08c3bdfSopenharmony_ci *      is not valid.
11f08c3bdfSopenharmony_ci *   2) vmsplice() returns -1 and sets errno to EBADF if fd
12f08c3bdfSopenharmony_ci *      doesn't refer to a pipe.
13f08c3bdfSopenharmony_ci *   3) vmsplice() returns -1 and sets errno to EINVAL if
14f08c3bdfSopenharmony_ci *      nr_segs is greater than IOV_MAX.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#define _GNU_SOURCE
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include <sys/types.h>
20f08c3bdfSopenharmony_ci#include <sys/stat.h>
21f08c3bdfSopenharmony_ci#include <unistd.h>
22f08c3bdfSopenharmony_ci#include <sys/uio.h>
23f08c3bdfSopenharmony_ci#include <limits.h>
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#include "tst_test.h"
26f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
27f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
28f08c3bdfSopenharmony_ci#include "lapi/vmsplice.h"
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci#define TESTFILE "testfile"
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci#define TEST_BLOCK_SIZE 128
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic char buffer[TEST_BLOCK_SIZE];
35f08c3bdfSopenharmony_cistatic int notvalidfd = -1;
36f08c3bdfSopenharmony_cistatic int filefd;
37f08c3bdfSopenharmony_cistatic int pipes[2];
38f08c3bdfSopenharmony_cistatic struct iovec ivc;
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic struct tcase {
41f08c3bdfSopenharmony_ci	int *fd;
42f08c3bdfSopenharmony_ci	const struct iovec *iov;
43f08c3bdfSopenharmony_ci	unsigned long nr_segs;
44f08c3bdfSopenharmony_ci	int exp_errno;
45f08c3bdfSopenharmony_ci} tcases[] = {
46f08c3bdfSopenharmony_ci	{ &notvalidfd, &ivc, 1, EBADF },
47f08c3bdfSopenharmony_ci	{ &filefd, &ivc, 1, EBADF },
48f08c3bdfSopenharmony_ci	{ &pipes[1], &ivc, IOV_MAX + 1, EINVAL },
49f08c3bdfSopenharmony_ci};
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	filefd = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT, 0644);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	SAFE_PIPE(pipes);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	ivc.iov_base = buffer;
58f08c3bdfSopenharmony_ci	ivc.iov_len = TEST_BLOCK_SIZE;
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic void vmsplice_verify(unsigned int n)
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	TEST(vmsplice(*(tc->fd), tc->iov, tc->nr_segs, 0));
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
68f08c3bdfSopenharmony_ci		tst_res(TFAIL, "vmsplice() returned %ld, "
69f08c3bdfSopenharmony_ci			"expected -1, errno:%d", TST_RET,
70f08c3bdfSopenharmony_ci			tc->exp_errno);
71f08c3bdfSopenharmony_ci		return;
72f08c3bdfSopenharmony_ci	}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	if (TST_ERR != tc->exp_errno) {
75f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
76f08c3bdfSopenharmony_ci			"vmsplice() failed unexpectedly; expected: %d - %s",
77f08c3bdfSopenharmony_ci			tc->exp_errno, tst_strerrno(tc->exp_errno));
78f08c3bdfSopenharmony_ci		return;
79f08c3bdfSopenharmony_ci	}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	tst_res(TPASS | TTERRNO, "vmsplice() failed as expected");
82f08c3bdfSopenharmony_ci}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_cistatic void cleanup(void)
85f08c3bdfSopenharmony_ci{
86f08c3bdfSopenharmony_ci	if (filefd > 0)
87f08c3bdfSopenharmony_ci		SAFE_CLOSE(filefd);
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	if (pipes[0] > 0)
90f08c3bdfSopenharmony_ci		SAFE_CLOSE(pipes[0]);
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	if (pipes[1] > 0)
93f08c3bdfSopenharmony_ci		SAFE_CLOSE(pipes[1]);
94f08c3bdfSopenharmony_ci}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_cistatic struct tst_test test = {
97f08c3bdfSopenharmony_ci	.setup = setup,
98f08c3bdfSopenharmony_ci	.cleanup = cleanup,
99f08c3bdfSopenharmony_ci	.test = vmsplice_verify,
100f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
101f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
102f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const []) {
103f08c3bdfSopenharmony_ci		"nfs",
104f08c3bdfSopenharmony_ci		NULL
105f08c3bdfSopenharmony_ci	},
106f08c3bdfSopenharmony_ci};
107