1/*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Xing Gu <gux.fnst@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17/*
18 * Description:
19 *   Verify that,
20 *   1) vmsplice() returns -1 and sets errno to EBADF if fd
21 *      is not valid.
22 *   2) vmsplice() returns -1 and sets errno to EBADF if fd
23 *      doesn't refer to a pipe.
24 *   3) vmsplice() returns -1 and sets errno to EINVAL if
25 *      nr_segs is greater than IOV_MAX.
26 */
27
28#define _GNU_SOURCE
29
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <sys/uio.h>
36#include <limits.h>
37
38#include "tst_test.h"
39#include "lapi/syscalls.h"
40#include "lapi/fcntl.h"
41#include "lapi/vmsplice.h"
42
43#include "limits_ltp.h"
44
45#define TESTFILE "testfile"
46
47#define TEST_BLOCK_SIZE 128
48
49static char buffer[TEST_BLOCK_SIZE];
50static int notvalidfd = -1;
51static int filefd;
52static int pipes[2];
53static struct iovec ivc;
54
55static struct tcase {
56	int *fd;
57	const struct iovec *iov;
58	unsigned long nr_segs;
59	int exp_errno;
60} tcases[] = {
61	{ &notvalidfd, &ivc, 1, EBADF },
62	{ &filefd, &ivc, 1, EBADF },
63	{ &pipes[1], &ivc, IOV_MAX + 1, EINVAL },
64};
65
66static void setup(void)
67{
68	if (tst_fs_type(".") == TST_NFS_MAGIC) {
69		tst_brk(TCONF, "Cannot do splice() "
70			"on a file located on an NFS filesystem");
71	}
72
73	filefd = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT, 0644);
74
75	SAFE_PIPE(pipes);
76
77	ivc.iov_base = buffer;
78	ivc.iov_len = TEST_BLOCK_SIZE;
79}
80
81static void vmsplice_verify(unsigned int n)
82{
83	struct tcase *tc = &tcases[n];
84
85	TEST(vmsplice(*(tc->fd), tc->iov, tc->nr_segs, 0));
86
87	if (TEST_RETURN != -1) {
88		tst_res(TFAIL, "vmsplice() returned %ld, "
89			"expected -1, errno:%d", TEST_RETURN,
90			tc->exp_errno);
91		return;
92	}
93
94	if (TEST_ERRNO != tc->exp_errno) {
95		tst_res(TFAIL | TTERRNO,
96			"vmsplice() failed unexpectedly; expected: %d - %s",
97			tc->exp_errno, tst_strerrno(tc->exp_errno));
98		return;
99	}
100
101	tst_res(TPASS | TTERRNO, "vmsplice() failed as expected");
102}
103
104static void cleanup(void)
105{
106	if (filefd > 0)
107		SAFE_CLOSE(filefd);
108
109	if (pipes[0] > 0)
110		SAFE_CLOSE(pipes[0]);
111
112	if (pipes[1] > 0)
113		SAFE_CLOSE(pipes[1]);
114}
115
116static struct tst_test test = {
117	.setup = setup,
118	.cleanup = cleanup,
119	.test = vmsplice_verify,
120	.tcnt = ARRAY_SIZE(tcases),
121	.needs_tmpdir = 1,
122	.min_kver = "2.6.17",
123};
124