1/* Common posix_fadvise tests definitions.
2 Copyright (C) 2016-2022 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 <https://www.gnu.org/licenses/>. */
18
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>
27
28#include <support/support.h>
29#include <support/check.h>
30#include <support/temp_file.h>
31
32static char *temp_filename;
33static int temp_fd;
34static char fifoname[] = "/tmp/tst-posix_fadvise-fifo-XXXXXX";
35static int fifofd;
36
37static void
38do_prepare (int argc, char **argv)
39{
40 temp_fd = create_temp_file (base: "tst-posix_fadvise.", filename: &temp_filename);
41 if (temp_fd == -1)
42 FAIL_EXIT1 ("cannot create temporary file: %m");
43
44 if (mktemp (template: fifoname) == NULL)
45 FAIL_EXIT1 ("cannot generate temp file name: %m");
46 add_temp_file (name: fifoname);
47
48 if (mkfifo (path: fifoname, S_IWUSR | S_IRUSR) != 0)
49 FAIL_EXIT1 ("cannot create fifo: %m");
50
51 fifofd = open (file: fifoname, O_RDONLY | O_NONBLOCK);
52 if (fifofd == -1)
53 FAIL_EXIT1 ("cannot open fifo: %m");
54}
55
56/* Effectivelly 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.
61
62 This test check for some invalid returned operation to check argument
63 passing and if implementation follows POSIX error definition. */
64static int
65do_test_common (void)
66{
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;
71
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);
75
76 if (fsync (temp_fd) != 0)
77 FAIL_EXIT1 ("fsync failed");
78
79 /* Test passing an invalid fd. */
80 if (posix_fadvise (fd: -1, offset: 0, len: 0, POSIX_FADV_NORMAL) != EBADF)
81 FAIL_EXIT1 ("posix_fadvise with invalid fd did not return EBADF");
82
83 /* Test passing an invalid operation. */
84 if (posix_fadvise (fd: temp_fd, offset: 0, len: 0, advise: -1) != EINVAL)
85 FAIL_EXIT1 ("posix_fadvise with invalid advise did not return EINVAL");
86
87 /* Test passing a FIFO fd. */
88 if (posix_fadvise (fd: fifofd, offset: 0, len: 0, POSIX_FADV_NORMAL) != ESPIPE)
89 FAIL_EXIT1 ("posix_advise with PIPE fd did not return ESPIPE");
90
91 /* Default fadvise on all file starting at initial position. */
92 if (posix_fadvise (fd: temp_fd, offset: 0, len: 0, POSIX_FADV_NORMAL) != 0)
93 FAIL_EXIT1 ("default posix_fadvise failed");
94
95 if (posix_fadvise (fd: temp_fd, offset: 0, len: 2 * BLK_SIZE, POSIX_FADV_NORMAL) != 0)
96 FAIL_EXIT1 ("posix_fadvise failed (offset = 0, len = %d) failed",
97 BLK_SIZE);
98
99 if (posix_fadvise (fd: temp_fd, offset: 2 * BLK_SIZE, len: 0, POSIX_FADV_NORMAL) != 0)
100 FAIL_EXIT1 ("posix_fadvise failed (offset = %d, len = 0) failed",
101 BLK_SIZE);
102
103 return 0;
104}
105
106#define PREPARE do_prepare
107
108/* This function is defined by the individual tests. */
109static int do_test (void);
110
111#include <support/test-driver.c>
112

source code of glibc/posix/tst-posix_fadvise-common.c