1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Red Hat Inc., 2007
4 */
5
6 /*
7 * NAME
8 * posix_fadvise02.c
9 *
10 * DESCRIPTION
11 * Check the value that posix_fadvise returns for wrong file descriptor.
12 *
13 * USAGE
14 * posix_fadvise02
15 *
16 * HISTORY
17 * 11/2007 Initial version by Masatake YAMATO <yamato@redhat.com>
18 *
19 * RESTRICTIONS
20 * None
21 */
22
23 #define _XOPEN_SOURCE 600
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <string.h>
29
30 #include "tst_test.h"
31 #include "lapi/syscalls.h"
32
33 #define WRONG_FD 42 /* The number has no meaning.
34 Just used as something wrong fd */
35
36 int defined_advise[] = {
37 POSIX_FADV_NORMAL,
38 POSIX_FADV_SEQUENTIAL,
39 POSIX_FADV_RANDOM,
40 POSIX_FADV_NOREUSE,
41 POSIX_FADV_WILLNEED,
42 POSIX_FADV_DONTNEED,
43 };
44
verify_fadvise(unsigned int n)45 static void verify_fadvise(unsigned int n)
46 {
47 TEST(posix_fadvise
48 (WRONG_FD, 0, 0, defined_advise[n]));
49
50 if (TST_RET == 0) {
51 tst_res(TFAIL, "call succeeded unexpectedly");
52 return;
53 }
54
55 /* Man page says:
56 "On error, an error number is returned." */
57 if (TST_RET == EBADF) {
58 tst_res(TPASS, "expected failure - "
59 "returned value = %ld : %s",
60 TST_RET, tst_strerrno(TST_RET));
61 } else {
62 tst_res(TFAIL,
63 "unexpected returnd value - %ld : %s - "
64 "expected %d", TST_RET,
65 tst_strerrno(TST_RET), EBADF);
66 }
67 }
68
setup(void)69 static void setup(void)
70 {
71 /* Make WRONG_FD really wrong. */
72 retry:
73 errno = 0;
74 if (close(WRONG_FD) != 0) {
75 if (errno == EBADF) {} /* Good. Do nothing. */
76 if (errno == EINTR)
77 goto retry;
78 else if (errno == EIO)
79 tst_brk(TBROK,
80 "Unable to close a file descriptor(%d): %s\n",
81 WRONG_FD, tst_strerrno(EIO));
82 }
83 }
84
85 static struct tst_test test = {
86 .setup = setup,
87 .test = verify_fadvise,
88 .tcnt = ARRAY_SIZE(defined_advise),
89 };
90