• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #define _GNU_SOURCE
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/syscall.h>
37 #include <sys/types.h>
38 #include "config.h"
39 #include "test.h"
40 #include "safe_macros.h"
41 #include "lapi/syscalls.h"
42 
43 char *TCID = "readahead01";
44 int TST_TOTAL = 1;
45 
46 option_t options[] = {
47 	{NULL, NULL, NULL}
48 };
49 
50 #if defined(__NR_readahead)
51 static void setup(void);
52 static void cleanup(void);
53 
check_ret(long expected_ret)54 static int check_ret(long expected_ret)
55 {
56 	if (expected_ret == TEST_RETURN) {
57 		tst_resm(TPASS, "expected ret success - "
58 			 "returned value = %ld", TEST_RETURN);
59 		return 0;
60 	}
61 	tst_resm(TFAIL, "unexpected failure - "
62 		 "returned value = %ld, expected: %ld",
63 		 TEST_RETURN, expected_ret);
64 	return 1;
65 }
66 
check_errno(long expected_errno)67 static int check_errno(long expected_errno)
68 {
69 	if (TEST_ERRNO == expected_errno) {
70 		tst_resm(TPASS | TTERRNO, "expected failure");
71 		return 0;
72 	}
73 
74 	if (TEST_ERRNO == 0)
75 		tst_resm(TFAIL, "call succeeded unexpectedly");
76 	else
77 		tst_resm(TFAIL | TTERRNO, "unexpected failure - "
78 			 "expected = %ld : %s, actual",
79 			 expected_errno, strerror(expected_errno));
80 	return 1;
81 }
82 
test_bad_fd(void)83 static void test_bad_fd(void)
84 {
85 	char tempname[PATH_MAX] = "readahead01_XXXXXX";
86 	int fd;
87 
88 	tst_resm(TINFO, "test_bad_fd -1");
89 	TEST(readahead(-1, 0, getpagesize()));
90 	check_ret(-1);
91 	check_errno(EBADF);
92 
93 	tst_resm(TINFO, "test_bad_fd O_WRONLY");
94 	fd = mkstemp(tempname);
95 	if (fd == -1)
96 		tst_resm(TBROK | TERRNO, "mkstemp failed");
97 	close(fd);
98 	fd = open(tempname, O_WRONLY);
99 	if (fd == -1)
100 		tst_resm(TBROK | TERRNO, "Failed to open testfile");
101 	TEST(readahead(fd, 0, getpagesize()));
102 	check_ret(-1);
103 	check_errno(EBADF);
104 	close(fd);
105 	unlink(tempname);
106 }
107 
test_invalid_fd(void)108 static void test_invalid_fd(void)
109 {
110 	int fd[2];
111 
112 	tst_resm(TINFO, "test_invalid_fd pipe");
113 	if (pipe(fd) < 0)
114 		tst_resm(TBROK | TERRNO, "Failed to create pipe");
115 	TEST(readahead(fd[0], 0, getpagesize()));
116 	check_ret(-1);
117 	check_errno(EINVAL);
118 	close(fd[0]);
119 	close(fd[1]);
120 
121 	tst_resm(TINFO, "test_invalid_fd socket");
122 	fd[0] = socket(AF_INET, SOCK_STREAM, 0);
123 	if (fd[0] < 0)
124 		tst_resm(TBROK | TERRNO, "Failed to create socket");
125 	TEST(readahead(fd[0], 0, getpagesize()));
126 	check_ret(-1);
127 	check_errno(EINVAL);
128 	close(fd[0]);
129 }
130 
main(int argc,char * argv[])131 int main(int argc, char *argv[])
132 {
133 	int lc;
134 
135 	tst_parse_opts(argc, argv, options, NULL);
136 
137 	setup();
138 	for (lc = 0; TEST_LOOPING(lc); lc++) {
139 		tst_count = 0;
140 		test_bad_fd();
141 		test_invalid_fd();
142 	}
143 	cleanup();
144 	tst_exit();
145 }
146 
setup(void)147 static void setup(void)
148 {
149 	tst_require_root();
150 	tst_tmpdir();
151 
152 	/* check if readahead syscall is supported */
153 	ltp_syscall(__NR_readahead, 0, 0, 0);
154 
155 	TEST_PAUSE;
156 }
157 
cleanup(void)158 static void cleanup(void)
159 {
160 	tst_rmdir();
161 }
162 
163 #else /* __NR_readahead */
main(void)164 int main(void)
165 {
166 	tst_brkm(TCONF, NULL, "System doesn't support __NR_readahead");
167 }
168 #endif
169