blob: cc87ebeb00a588073df4fa2ec437e633bbf46b57 [file] [log] [blame]
Xing Gu8dc8c1c2014-06-23 19:13:37 +08001/*
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
37#include "test.h"
Xing Gu8dc8c1c2014-06-23 19:13:37 +080038#include "safe_macros.h"
39#include "linux_syscall_numbers.h"
40#include "tst_fs_type.h"
41#include "lapi/fcntl.h"
42
43#define TESTFILE "testfile"
44
45#define TEST_BLOCK_SIZE 128
46
47static char buffer[TEST_BLOCK_SIZE];
48static int notvalidfd = -1;
49static int filefd;
50static int pipes[2];
51static struct iovec ivc;
52
53static struct test_case_t {
54 int *fd;
55 const struct iovec *iov;
56 unsigned long nr_segs;
57 int exp_errno;
58} test_cases[] = {
59 { &notvalidfd, &ivc, 1, EBADF },
60 { &filefd, &ivc, 1, EBADF },
61 { &pipes[1], &ivc, IOV_MAX + 1, EINVAL },
62};
63
64static void setup(void);
65static void cleanup(void);
66static void vmsplice_verify(const struct test_case_t *);
67
68char *TCID = "vmsplice02";
69int TST_TOTAL = ARRAY_SIZE(test_cases);
Xing Gu8dc8c1c2014-06-23 19:13:37 +080070
71int main(int ac, char **av)
72{
73 int i, lc;
74 const char *msg;
75
76 msg = parse_opts(ac, av, NULL, NULL);
77 if (msg != NULL)
78 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
79
80 setup();
81
Xing Gu8dc8c1c2014-06-23 19:13:37 +080082 for (lc = 0; TEST_LOOPING(lc); lc++) {
83 tst_count = 0;
84
85 for (i = 0; i < TST_TOTAL; i++)
86 vmsplice_verify(&test_cases[i]);
87 }
88
89 cleanup();
90 tst_exit();
91}
92
93static void setup(void)
94{
95 if ((tst_kvercmp(2, 6, 17)) < 0) {
96 tst_brkm(TCONF, cleanup, "This test can only run on "
97 "kernels that are 2.6.17 or higher");
98 }
99
100 tst_sig(NOFORK, DEF_HANDLER, cleanup);
101
102 TEST_PAUSE;
103
104 tst_tmpdir();
105
106 if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) {
107 tst_brkm(TCONF, cleanup, "Cannot do splice() "
108 "on a file located on an NFS filesystem");
109 }
110
111 filefd = SAFE_OPEN(cleanup, TESTFILE,
112 O_WRONLY | O_CREAT, 0644);
113
114 SAFE_PIPE(cleanup, pipes);
115
116 ivc.iov_base = buffer;
117 ivc.iov_len = TEST_BLOCK_SIZE;
118}
119
120static void vmsplice_verify(const struct test_case_t *tc)
121{
122 TEST(vmsplice(*(tc->fd), tc->iov, tc->nr_segs, 0));
123
124 if (TEST_RETURN != -1) {
125 tst_resm(TFAIL, "vmsplice() returned %ld, "
126 "expected -1, errno:%d", TEST_RETURN,
127 tc->exp_errno);
128 return;
129 }
130
Xing Gu8dc8c1c2014-06-23 19:13:37 +0800131 if (TEST_ERRNO == tc->exp_errno) {
132 tst_resm(TPASS | TTERRNO, "vmsplice() failed as expected");
133 } else {
134 tst_resm(TFAIL | TTERRNO,
135 "vmsplice() failed unexpectedly; expected: %d - %s",
136 tc->exp_errno, strerror(tc->exp_errno));
137 }
138}
139
140static void cleanup(void)
141{
Xing Gu8dc8c1c2014-06-23 19:13:37 +0800142 if (filefd && close(filefd) < 0)
143 tst_resm(TWARN | TERRNO, "close filefd failed");
144
145 if (pipes[0] && close(pipes[0]) < 0)
146 tst_resm(TWARN | TERRNO, "close pipes[0] failed");
147
148 if (pipes[1] && close(pipes[1]) < 0)
149 tst_resm(TWARN | TERRNO, "close pipes[1] failed");
150
151 tst_rmdir();
152}