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#include <fcntl.h>
24#include <unistd.h>
25#include <signal.h>
26#include <errno.h>
27#include <string.h>
28
29#include "tst_test.h"
30#include "lapi/syscalls.h"
31
32#define WRONG_FD       42	/* The number has no meaning.
33				   Just used as something wrong fd */
34
35int defined_advise[] = {
36	POSIX_FADV_NORMAL,
37	POSIX_FADV_SEQUENTIAL,
38	POSIX_FADV_RANDOM,
39	POSIX_FADV_NOREUSE,
40	POSIX_FADV_WILLNEED,
41	POSIX_FADV_DONTNEED,
42};
43
44static void verify_fadvise(unsigned int n)
45{
46	TEST(posix_fadvise
47	     (WRONG_FD, 0, 0, defined_advise[n]));
48
49	if (TST_RET == 0) {
50		tst_res(TFAIL, "call succeeded unexpectedly");
51		return;
52	}
53
54	/* Man page says:
55	   "On error, an error number is returned." */
56	if (TST_RET == EBADF) {
57		tst_res(TPASS, "expected failure - "
58			"returned value = %ld : %s",
59			TST_RET, tst_strerrno(TST_RET));
60	} else {
61		tst_res(TFAIL,
62			"unexpected returnd value - %ld : %s - "
63			"expected %d", TST_RET,
64			tst_strerrno(TST_RET), EBADF);
65	}
66}
67
68static void setup(void)
69{
70	/* Make WRONG_FD really wrong. */
71retry:
72	errno = 0;
73	if (close(WRONG_FD) != 0) {
74		if (errno == EBADF) {}	/* Good. Do nothing. */
75		if (errno == EINTR)
76			goto retry;
77		else if (errno == EIO)
78			tst_brk(TBROK,
79				"Unable to close a file descriptor(%d): %s\n",
80				WRONG_FD, tst_strerrno(EIO));
81	}
82}
83
84static struct tst_test test = {
85	.setup = setup,
86	.test = verify_fadvise,
87	.tcnt = ARRAY_SIZE(defined_advise),
88};
89