1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2012 Linux Test Project, Inc.
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Verify that readahead() syscall fails with:
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * - EBADF when fd is not a valid file descriptor or is not open for reading.
12f08c3bdfSopenharmony_ci * - EINVAL when fd does not refer to a file type to which readahead()
13f08c3bdfSopenharmony_ci *          can be applied.
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci#define _GNU_SOURCE
16f08c3bdfSopenharmony_ci#include <errno.h>
17f08c3bdfSopenharmony_ci#include <fcntl.h>
18f08c3bdfSopenharmony_ci#include <stdio.h>
19f08c3bdfSopenharmony_ci#include <stdlib.h>
20f08c3bdfSopenharmony_ci#include <unistd.h>
21f08c3bdfSopenharmony_ci#include <sys/socket.h>
22f08c3bdfSopenharmony_ci#include <sys/stat.h>
23f08c3bdfSopenharmony_ci#include <sys/syscall.h>
24f08c3bdfSopenharmony_ci#include <sys/types.h>
25f08c3bdfSopenharmony_ci#include "config.h"
26f08c3bdfSopenharmony_ci#include "tst_test.h"
27f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci#if defined(__NR_readahead)
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void test_bad_fd(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	char tempname[PATH_MAX] = "readahead01_XXXXXX";
34f08c3bdfSopenharmony_ci	int fd;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s -1", __func__);
37f08c3bdfSopenharmony_ci	TST_EXP_FAIL(readahead(-1, 0, getpagesize()), EBADF);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s O_WRONLY", __func__);
40f08c3bdfSopenharmony_ci	fd = mkstemp(tempname);
41f08c3bdfSopenharmony_ci	if (fd == -1)
42f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "mkstemp failed");
43f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
44f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(tempname, O_WRONLY);
45f08c3bdfSopenharmony_ci	TST_EXP_FAIL(readahead(fd, 0, getpagesize()), EBADF);
46f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
47f08c3bdfSopenharmony_ci	unlink(tempname);
48f08c3bdfSopenharmony_ci}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void test_invalid_fd(void)
51f08c3bdfSopenharmony_ci{
52f08c3bdfSopenharmony_ci	int fd[2];
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s pipe", __func__);
55f08c3bdfSopenharmony_ci	SAFE_PIPE(fd);
56f08c3bdfSopenharmony_ci	TST_EXP_FAIL(readahead(fd[0], 0, getpagesize()), EINVAL);
57f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd[0]);
58f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd[1]);
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s socket", __func__);
61f08c3bdfSopenharmony_ci	fd[0] = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
62f08c3bdfSopenharmony_ci	TST_EXP_FAIL(readahead(fd[0], 0, getpagesize()), EINVAL);
63f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd[0]);
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic void test_readahead(void)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	test_bad_fd();
69f08c3bdfSopenharmony_ci	test_invalid_fd();
70f08c3bdfSopenharmony_ci}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_cistatic void setup(void)
73f08c3bdfSopenharmony_ci{
74f08c3bdfSopenharmony_ci	/* check if readahead syscall is supported */
75f08c3bdfSopenharmony_ci	tst_syscall(__NR_readahead, 0, 0, 0);
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic struct tst_test test = {
79f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
80f08c3bdfSopenharmony_ci	.setup = setup,
81f08c3bdfSopenharmony_ci	.test_all = test_readahead,
82f08c3bdfSopenharmony_ci};
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci#else /* __NR_readahead */
85f08c3bdfSopenharmony_ci	TST_TEST_TCONF("System doesn't support __NR_readahead");
86f08c3bdfSopenharmony_ci#endif
87