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