1 /* Common posix_fadvise tests definitions.
2 Copyright (C) 2016-2017 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 <http://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
27 #include <support/support.h>
28 #include <support/check.h>
29 #include <support/temp_file.h>
31 static char *temp_filename
;
33 static char fifoname
[] = "/tmp/tst-posix_fadvise-fifo-XXXXXX";
37 do_prepare (int argc
, char **argv
)
39 temp_fd
= create_temp_file ("tst-posix_fadvise.", &temp_filename
);
41 FAIL_EXIT1 ("cannot create temporary file: %m");
43 if (mktemp (fifoname
) == NULL
)
44 FAIL_EXIT1 ("cannot generate temp file name: %m");
45 add_temp_file (fifoname
);
47 if (mkfifo (fifoname
, S_IWUSR
| S_IRUSR
) != 0)
48 FAIL_EXIT1 ("cannot create fifo: %m");
50 fifofd
= open (fifoname
, O_RDONLY
| O_NONBLOCK
);
52 FAIL_EXIT1 ("cannot open fifo: %m");
55 /* Effectivelly testing posix_fadvise is hard because side effects are not
56 observed without checking either performance or any kernel specific
57 supplied information. Also, the syscall is meant to be an advisory,
58 so the kernel is free to use this information in any way it deems fit,
59 including ignoring it.
61 This test check for some invalid returned operation to check argument
62 passing and if implementation follows POSIX error definition. */
66 /* Add some data to file and ensure it is written to disk. */
68 char buffer
[BLK_SIZE
] = { 0xcd };
71 if ((ret
= write (temp_fd
, buffer
, BLK_SIZE
)) != BLK_SIZE
)
72 FAIL_EXIT1 ("write returned %zd different than expected %d",
75 if (fsync (temp_fd
) != 0)
76 FAIL_EXIT1 ("fsync failed");
78 /* Test passing an invalid fd. */
79 if (posix_fadvise (-1, 0, 0, POSIX_FADV_NORMAL
) != EBADF
)
80 FAIL_EXIT1 ("posix_fadvise with invalid fd did not return EBADF");
82 /* Test passing an invalid operation. */
83 if (posix_fadvise (temp_fd
, 0, 0, -1) != EINVAL
)
84 FAIL_EXIT1 ("posix_fadvise with invalid advise did not return EINVAL");
86 /* Test passing a FIFO fd. */
87 if (posix_fadvise (fifofd
, 0, 0, POSIX_FADV_NORMAL
) != ESPIPE
)
88 FAIL_EXIT1 ("posix_advise with PIPE fd did not return ESPIPE");
90 /* Default fadvise on all file starting at initial position. */
91 if (posix_fadvise (temp_fd
, 0, 0, POSIX_FADV_NORMAL
) != 0)
92 FAIL_EXIT1 ("default posix_fadvise failed");
94 if (posix_fadvise (temp_fd
, 0, 2 * BLK_SIZE
, POSIX_FADV_NORMAL
) != 0)
95 FAIL_EXIT1 ("posix_fadvise failed (offset = 0, len = %d) failed",
98 if (posix_fadvise (temp_fd
, 2 * BLK_SIZE
, 0, POSIX_FADV_NORMAL
) != 0)
99 FAIL_EXIT1 ("posix_fadvise failed (offset = %d, len = 0) failed",
105 #define PREPARE do_prepare
107 /* This function is defined by the individual tests. */
108 static int do_test (void);
110 #include <support/test-driver.c>