aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/readahead/readahead01.c
blob: e86a73e3e6ba59ac05268309ec10cfe21f030f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2012 Linux Test Project, Inc.
 * Copyright (C) 2023-2024 Cyril Hrubis <chrubis@suse.cz>
 */

/*\
 * [Description]
 *
 * Verify that readahead() syscall fails with:
 *
 * - EBADF when fd is not a valid file descriptor or is not open for reading.
 * - EINVAL when fd does not refer to a file type to which readahead()
 *          can be applied.
 */
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include "config.h"
#include "tst_test.h"
#include "lapi/syscalls.h"

#if defined(__NR_readahead)

static void test_bad_fd(void)
{
	int fd[2];

	TST_EXP_FAIL(readahead(-1, 0, getpagesize()), EBADF,
	             "readahead() with fd = -1");

	SAFE_PIPE(fd);
	SAFE_CLOSE(fd[0]);
	SAFE_CLOSE(fd[1]);

	TST_EXP_FAIL(readahead(fd[0], 0, getpagesize()), EBADF,
	             "readahead() with invalid fd");
}

static void test_invalid_fd(struct tst_fd *fd)
{

	switch (fd->type) {
	/* These succeed */
	case TST_FD_FILE:
	case TST_FD_MEMFD:
	case TST_FD_PROC_MAPS:
		return;
	default:
		break;
	}

	int exp_errnos[] = {EBADF, EINVAL, ESPIPE};

	TST_EXP_FAIL_ARR(readahead(fd->fd, 0, getpagesize()), exp_errnos,
			"readahead() on %s", tst_fd_desc(fd));
}

static void test_readahead(void)
{
	test_bad_fd();

	TST_FD_FOREACH(fd)
		test_invalid_fd(&fd);
}

static void setup(void)
{
	/* check if readahead syscall is supported */
	tst_syscall(__NR_readahead, 0, 0, 0);
}

static struct tst_test test = {
	.needs_tmpdir = 1,
	.setup = setup,
	.test_all = test_readahead,
};

#else /* __NR_readahead */
	TST_TEST_TCONF("System doesn't support __NR_readahead");
#endif