]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/tst-sync_file_range.c
powerpc: Remove unintended __longjmp symbol from ABI
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / tst-sync_file_range.c
CommitLineData
5da2c626
AZ
1/* Basic sync_file_range (not specific flag is checked).
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19/* sync_file_range is only define for LFS. */
20#define _FILE_OFFSET_BITS 64
21#include <fcntl.h>
22#include <errno.h>
23
24static void do_prepare (void);
25#define PREPARE(argc, argv) do_prepare ()
26static int do_test (void);
27#define TEST_FUNCTION do_test ()
28
29#define TIMEOUT 20 /* sec. */
30
31#define XSTR(s) STR(S)
32#define STR(s) #s
33
34#include <test-skeleton.c>
35
36static char *temp_filename;
37static int temp_fd;
38
39static char fifoname[] = "/tmp/tst-posix_fadvise-fifo-XXXXXX";
40static int fifofd;
41
42void
43do_prepare (void)
44{
45 temp_fd = create_temp_file ("tst-file_sync_range.", &temp_filename);
46 if (temp_fd == -1)
47 FAIL_EXIT1 ("cannot create temporary file: %m");
48
49 if (mktemp (fifoname) == NULL)
50 FAIL_EXIT1 ("cannot generate temp file name: %m");
51 add_temp_file (fifoname);
52
53 if (mkfifo (fifoname, S_IWUSR | S_IRUSR) != 0)
54 FAIL_EXIT1 ("cannot create fifo: %m");
55
56 fifofd = open (fifoname, O_RDONLY | O_NONBLOCK);
57 if (fifofd == -1)
58 FAIL_EXIT1 ("cannot open fifo: %m");
59}
60
61static int
62do_test (void)
63{
64 int ret;
65
66 /* This tests first check for some invalid usage and then check for
67 a simple usage. It does not cover for all possible issue since for
68 EIO/ENOMEM/ENOSPC would require to create very specific scenarios that
69 are outside the current test coverage (basically correct kernel argument
70 passing. */
71
72 /* Check for invalid file descriptor. */
73 if ((ret = sync_file_range (-1, 0, 0, 0)) != -1)
74 FAIL_EXIT1 ("sync_file_range did not fail on an invalid descriptor "
75 "(returned %d, expected -1)", ret);
76 if (errno != EBADF)
77 FAIL_EXIT1 ("sync_file_range on an invalid descriptor did not set errno to "
78 "EBADF (%d)", errno);
79
80 if ((ret = sync_file_range (fifofd, 0, 0, 0)) != -1)
81 FAIL_EXIT1 ("sync_file_range did not fail on an invalid descriptor "
82 "(returned %d, expected -1)", ret);
83 if (errno != ESPIPE)
84 FAIL_EXIT1 ("sync_file_range on an invalid descriptor did not set errno to "
85 "EBADF (%d)", errno);
86
87 /* Check for invalid flags (it must be
88 SYNC_FILE_RANGE_{WAIT_BEFORE,WRITE,WAIT_AFTER) or a 'or' combination of
89 them. */
90 if ((ret = sync_file_range (temp_fd, 0, 0, -1)) != -1)
91 FAIL_EXIT1 ("sync_file_range did not failed with invalid flags "
92 "(returned %d, " "expected -1)", ret);
93 if (errno != EINVAL)
94 FAIL_EXIT1 ("sync_file_range with invalid flag did not set errno to "
95 "EINVAL (%d)", errno);
96
97 /* Check for negative offset. */
98 if ((ret = sync_file_range (temp_fd, -1, 1, 0)) != -1)
99 FAIL_EXIT1 ("sync_file_range did not failed with invalid offset "
100 "(returned %d, expected -1)", ret);
101 if (errno != EINVAL)
102 FAIL_EXIT1 ("sync_file_range with invalid offset did not set errno to "
103 "EINVAL (%d)", errno);
104
105 /* offset + nbytes must be a positive value. */
106 if ((ret = sync_file_range (temp_fd, 1024, -2048, 0)) != -1)
107 FAIL_EXIT1 ("sync_file_range did not failed with invalid nbytes (returned %d, "
108 "expected -1)", ret);
109 if (errno != EINVAL)
110 FAIL_EXIT1 ("sync_file_range with invalid offset did not set errno to "
111 "EINVAL (%d)", errno);
112
113 /* offset + nbytes must be larger or equal than offset */
114 if ((ret = sync_file_range (temp_fd, -1024, 1024, 0)) != -1)
115 FAIL_EXIT1 ("sync_file_range did not failed with invalid offset "
116 "(returned %d, expected -1)", ret);
117 if (errno != EINVAL)
118 FAIL_EXIT1 ("sync_file_range with invalid offset did not set errno to "
119 "EINVAL (%d)", errno);
120
121 /* Check simple successful case. */
122 if ((ret = sync_file_range (temp_fd, 0, 1024, 0)) == -1)
123 FAIL_EXIT1 ("sync_file_range failed (errno = %d)", errno);
124
125 /* Finally check also a successful case with a 64-bit offset. */
126 off_t large_offset = UINT32_MAX + 2048LL;
127 if ((ret = sync_file_range (temp_fd, large_offset, 1024, 0)) == -1)
128 FAIL_EXIT1 ("sync_file_range failed (errno = %d)", errno);
129
130 return 0;
131}