manual: Clarify undefined behavior of feenableexcept (BZ 31019)
[glibc.git] / posix / tst-posix_fadvise-common.c
blob2a4e4d6a7ff77b922b08076ec7eb1cc16e026bf4
1 /* Common posix_fadvise tests definitions.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
28 #include <support/support.h>
29 #include <support/check.h>
30 #include <support/temp_file.h>
32 static char *temp_filename;
33 static int temp_fd;
34 static char fifoname[] = "/tmp/tst-posix_fadvise-fifo-XXXXXX";
35 static int fifofd;
37 static void
38 do_prepare (int argc, char **argv)
40 temp_fd = create_temp_file ("tst-posix_fadvise.", &temp_filename);
41 if (temp_fd == -1)
42 FAIL_EXIT1 ("cannot create temporary file: %m");
44 if (mktemp (fifoname) == NULL)
45 FAIL_EXIT1 ("cannot generate temp file name: %m");
46 add_temp_file (fifoname);
48 if (mkfifo (fifoname, S_IWUSR | S_IRUSR) != 0)
49 FAIL_EXIT1 ("cannot create fifo: %m");
51 fifofd = open (fifoname, O_RDONLY | O_NONBLOCK);
52 if (fifofd == -1)
53 FAIL_EXIT1 ("cannot open fifo: %m");
56 /* Effectively testing posix_fadvise is hard because side effects are not
57 observed without checking either performance or any kernel specific
58 supplied information. Also, the syscall is meant to be an advisory,
59 so the kernel is free to use this information in any way it deems fit,
60 including ignoring it.
62 This test check for some invalid returned operation to check argument
63 passing and if implementation follows POSIX error definition. */
64 static int
65 do_test_common (void)
67 /* Add some data to file and ensure it is written to disk. */
68 #define BLK_SIZE 2048
69 char buffer[BLK_SIZE] = { 0xcd };
70 ssize_t ret;
72 if ((ret = write (temp_fd, buffer, BLK_SIZE)) != BLK_SIZE)
73 FAIL_EXIT1 ("write returned %zd different than expected %d",
74 ret, BLK_SIZE);
76 if (fsync (temp_fd) != 0)
77 FAIL_EXIT1 ("fsync failed");
79 /* Test passing an invalid fd. */
80 if (posix_fadvise (-1, 0, 0, POSIX_FADV_NORMAL) != EBADF)
81 FAIL_EXIT1 ("posix_fadvise with invalid fd did not return EBADF");
83 /* Test passing an invalid operation. */
84 if (posix_fadvise (temp_fd, 0, 0, -1) != EINVAL)
85 FAIL_EXIT1 ("posix_fadvise with invalid advise did not return EINVAL");
87 /* Test passing a FIFO fd. */
88 if (posix_fadvise (fifofd, 0, 0, POSIX_FADV_NORMAL) != ESPIPE)
89 FAIL_EXIT1 ("posix_advise with PIPE fd did not return ESPIPE");
91 /* Default fadvise on all file starting at initial position. */
92 if (posix_fadvise (temp_fd, 0, 0, POSIX_FADV_NORMAL) != 0)
93 FAIL_EXIT1 ("default posix_fadvise failed");
95 if (posix_fadvise (temp_fd, 0, 2 * BLK_SIZE, POSIX_FADV_NORMAL) != 0)
96 FAIL_EXIT1 ("posix_fadvise failed (offset = 0, len = %d) failed",
97 BLK_SIZE);
99 if (posix_fadvise (temp_fd, 2 * BLK_SIZE, 0, POSIX_FADV_NORMAL) != 0)
100 FAIL_EXIT1 ("posix_fadvise failed (offset = %d, len = 0) failed",
101 BLK_SIZE);
103 return 0;
106 #define PREPARE do_prepare
108 /* This function is defined by the individual tests. */
109 static int do_test (void);
111 #include <support/test-driver.c>