blob: 8a8f83d7317a0bd64350cf98eb34674bcd60307d [file] [log] [blame]
Jan Stancekdef43922012-10-15 09:54:50 +02001/*
2 * Copyright (C) 2012 Linux Test Project, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it
13 * is free of the rightful claim of any third person regarding
14 * infringement or the like. Any license provided herein, whether
15 * implied or otherwise, applies only to this software file. Patent
16 * licenses, if any, provided herein do not apply to combinations of
17 * this program with other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 */
24
25/*
26 * errno tests for readahead() syscall
27 */
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <sys/syscall.h>
31#include <sys/socket.h>
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include "config.h"
37#include "test.h"
Jan Stancekdef43922012-10-15 09:54:50 +020038#include "safe_macros.h"
39#include "linux_syscall_numbers.h"
40
41char *TCID = "readahead01";
Wanlong Gao354ebb42012-12-07 10:10:04 +080042int TST_TOTAL = 1;
Jan Stancekdef43922012-10-15 09:54:50 +020043
44option_t options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080045 {NULL, NULL, NULL}
Jan Stancekdef43922012-10-15 09:54:50 +020046};
47
48#if defined(__NR_readahead)
49static void setup(void);
50static void cleanup(void);
51
52static int check_ret(long expected_ret)
53{
54 if (expected_ret == TEST_RETURN) {
55 tst_resm(TPASS, "expected ret success - "
Wanlong Gao354ebb42012-12-07 10:10:04 +080056 "returned value = %ld", TEST_RETURN);
Jan Stancekdef43922012-10-15 09:54:50 +020057 return 0;
58 }
59 tst_resm(TFAIL, "unexpected failure - "
Wanlong Gao354ebb42012-12-07 10:10:04 +080060 "returned value = %ld, expected: %ld",
61 TEST_RETURN, expected_ret);
Jan Stancekdef43922012-10-15 09:54:50 +020062 return 1;
63}
64
65static int check_errno(long expected_errno)
66{
67 if (TEST_ERRNO == expected_errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080068 tst_resm(TPASS | TTERRNO, "expected failure");
Jan Stancekdef43922012-10-15 09:54:50 +020069 return 0;
70 }
71
72 if (TEST_ERRNO == 0)
73 tst_resm(TFAIL, "call succeeded unexpectedly");
74 else
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 tst_resm(TFAIL | TTERRNO, "unexpected failure - "
76 "expected = %ld : %s, actual",
77 expected_errno, strerror(expected_errno));
Jan Stancekdef43922012-10-15 09:54:50 +020078 return 1;
79}
80
81static void test_bad_fd(void)
82{
83 char tempname[PATH_MAX] = "readahead01_XXXXXX";
84 int fd;
85
86 tst_resm(TINFO, "test_bad_fd -1");
Jan Stancek359980f2013-02-15 10:16:05 +010087 TEST(ltp_syscall(__NR_readahead, -1, 0, getpagesize()));
Jan Stancekdef43922012-10-15 09:54:50 +020088 check_ret(-1);
89 check_errno(EBADF);
90
91 tst_resm(TINFO, "test_bad_fd O_WRONLY");
92 fd = mkstemp(tempname);
93 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 tst_resm(TBROK | TERRNO, "mkstemp failed");
Jan Stancekdef43922012-10-15 09:54:50 +020095 close(fd);
96 fd = open(tempname, O_WRONLY);
97 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080098 tst_resm(TBROK | TERRNO, "Failed to open testfile");
Jan Stancek359980f2013-02-15 10:16:05 +010099 TEST(ltp_syscall(__NR_readahead, fd, 0, getpagesize()));
Jan Stancekdef43922012-10-15 09:54:50 +0200100 check_ret(-1);
101 check_errno(EBADF);
102 close(fd);
103 unlink(tempname);
104}
105
106static void test_invalid_fd(void)
107{
108 int fd[2];
109
110 tst_resm(TINFO, "test_invalid_fd pipe");
111 if (pipe(fd) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 tst_resm(TBROK | TERRNO, "Failed to create pipe");
Jan Stancek359980f2013-02-15 10:16:05 +0100113 TEST(ltp_syscall(__NR_readahead, fd[0], 0, getpagesize()));
Jan Stancekdef43922012-10-15 09:54:50 +0200114 check_ret(-1);
115 check_errno(EINVAL);
116 close(fd[0]);
117 close(fd[1]);
118
119 tst_resm(TINFO, "test_invalid_fd socket");
120 fd[0] = socket(AF_INET, SOCK_STREAM, 0);
121 if (fd[0] < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 tst_resm(TBROK | TERRNO, "Failed to create socket");
Jan Stancek359980f2013-02-15 10:16:05 +0100123 TEST(ltp_syscall(__NR_readahead, fd[0], 0, getpagesize()));
Jan Stancekdef43922012-10-15 09:54:50 +0200124 check_ret(-1);
125 check_errno(EINVAL);
126 close(fd[0]);
127}
128
129int main(int argc, char *argv[])
130{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200131 const char *msg;
Jan Stancekdef43922012-10-15 09:54:50 +0200132 int lc;
133
134 msg = parse_opts(argc, argv, options, NULL);
135 if (msg != NULL)
136 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
137
138 setup();
139 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800140 tst_count = 0;
Jan Stancekdef43922012-10-15 09:54:50 +0200141 test_bad_fd();
142 test_invalid_fd();
143 }
144 cleanup();
145 tst_exit();
146}
147
148static void setup(void)
149{
150 tst_require_root(NULL);
151 tst_tmpdir();
152
153 /* check if readahead syscall is supported */
Jan Stancek359980f2013-02-15 10:16:05 +0100154 ltp_syscall(__NR_readahead, 0, 0, 0);
Jan Stancekdef43922012-10-15 09:54:50 +0200155
156 TEST_PAUSE;
157}
158
159static void cleanup(void)
160{
Jan Stancekdef43922012-10-15 09:54:50 +0200161 tst_rmdir();
162}
163
164#else /* __NR_readahead */
165int main(void)
166{
167 tst_brkm(TCONF, NULL, "System doesn't support __NR_readahead");
168}
169#endif