1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Red Hat Inc., 2007
4 */
5
6 /*
7 * NAME
8 * posix_fadvise04.c
9 *
10 * DESCRIPTION
11 * Check the value that posix_fadvise returns for pipe descriptor.
12 *
13 * USAGE
14 * posix_fadvise04
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
32 #include "lapi/syscalls.h"
33
34 static int pipedes[2];
35
36 static 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 (pipedes[0], 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 == ESPIPE) {
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 return value - %ld : %s - "
64 "expected %d", TST_RET,
65 tst_strerrno(TST_RET), ESPIPE);
66 }
67 }
68
setup(void)69 void setup(void)
70 {
71 SAFE_PIPE(pipedes);
72
73 SAFE_CLOSE(pipedes[1]);
74 }
75
cleanup(void)76 void cleanup(void)
77 {
78 if (pipedes[0] > 0)
79 SAFE_CLOSE(pipedes[0]);
80 }
81
82 static struct tst_test test = {
83 .setup = setup,
84 .cleanup = cleanup,
85 .test = verify_fadvise,
86 .tcnt = ARRAY_SIZE(defined_advise),
87 .min_kver = "2.6.16",
88 };
89