1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) Red Hat Inc., 2007
5 * 11/2007 Copyed from sendfile02.c by Masatake YAMATO
6 */
7
8 /*\
9 * [Description]
10 *
11 * Testcase to test that sendfile(2) system call returns EINVAL when passing
12 * negative offset.
13 *
14 * [Algorithm]
15 *
16 * Call sendfile with offset = -1.
17 */
18
19 #include <stdio.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <sys/sendfile.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/mman.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29
30 #include "tst_test.h"
31
32 static int in_fd;
33 static int out_fd;
34
setup(void)35 static void setup(void)
36 {
37 in_fd = SAFE_OPEN("in_file", O_CREAT | O_RDWR, 0600);
38 out_fd = SAFE_CREAT("out_file", 0600);
39 }
40
cleanup(void)41 static void cleanup(void)
42 {
43 SAFE_CLOSE(in_fd);
44 SAFE_CLOSE(out_fd);
45 }
46
run(void)47 static void run(void)
48 {
49 off_t offset = -1;
50
51 TST_EXP_FAIL(sendfile(out_fd, in_fd, &offset, 1), EINVAL,
52 "sendfile(out, in, &offset, ..) with offset=%ld", offset);
53 }
54
55 static struct tst_test test = {
56 .needs_tmpdir = 1,
57 .cleanup = cleanup,
58 .setup = setup,
59 .test_all = run,
60 };
61