Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 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 | #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 Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 38 | #include "safe_macros.h" |
| 39 | #include "linux_syscall_numbers.h" |
| 40 | |
| 41 | char *TCID = "readahead01"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 42 | int TST_TOTAL = 1; |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 43 | |
| 44 | option_t options[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | {NULL, NULL, NULL} |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | #if defined(__NR_readahead) |
| 49 | static void setup(void); |
| 50 | static void cleanup(void); |
| 51 | |
| 52 | static int check_ret(long expected_ret) |
| 53 | { |
| 54 | if (expected_ret == TEST_RETURN) { |
| 55 | tst_resm(TPASS, "expected ret success - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 56 | "returned value = %ld", TEST_RETURN); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | tst_resm(TFAIL, "unexpected failure - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | "returned value = %ld, expected: %ld", |
| 61 | TEST_RETURN, expected_ret); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | static int check_errno(long expected_errno) |
| 66 | { |
| 67 | if (TEST_ERRNO == expected_errno) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 68 | tst_resm(TPASS | TTERRNO, "expected failure"); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | if (TEST_ERRNO == 0) |
| 73 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 74 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 75 | tst_resm(TFAIL | TTERRNO, "unexpected failure - " |
| 76 | "expected = %ld : %s, actual", |
| 77 | expected_errno, strerror(expected_errno)); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | static 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 Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 87 | TEST(ltp_syscall(__NR_readahead, -1, 0, getpagesize())); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 88 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | tst_resm(TBROK | TERRNO, "mkstemp failed"); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 95 | close(fd); |
| 96 | fd = open(tempname, O_WRONLY); |
| 97 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | tst_resm(TBROK | TERRNO, "Failed to open testfile"); |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 99 | TEST(ltp_syscall(__NR_readahead, fd, 0, getpagesize())); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 100 | check_ret(-1); |
| 101 | check_errno(EBADF); |
| 102 | close(fd); |
| 103 | unlink(tempname); |
| 104 | } |
| 105 | |
| 106 | static 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 112 | tst_resm(TBROK | TERRNO, "Failed to create pipe"); |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 113 | TEST(ltp_syscall(__NR_readahead, fd[0], 0, getpagesize())); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 114 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | tst_resm(TBROK | TERRNO, "Failed to create socket"); |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 123 | TEST(ltp_syscall(__NR_readahead, fd[0], 0, getpagesize())); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 124 | check_ret(-1); |
| 125 | check_errno(EINVAL); |
| 126 | close(fd[0]); |
| 127 | } |
| 128 | |
| 129 | int main(int argc, char *argv[]) |
| 130 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 131 | const char *msg; |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 132 | 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 Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 140 | tst_count = 0; |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 141 | test_bad_fd(); |
| 142 | test_invalid_fd(); |
| 143 | } |
| 144 | cleanup(); |
| 145 | tst_exit(); |
| 146 | } |
| 147 | |
| 148 | static void setup(void) |
| 149 | { |
| 150 | tst_require_root(NULL); |
| 151 | tst_tmpdir(); |
| 152 | |
| 153 | /* check if readahead syscall is supported */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 154 | ltp_syscall(__NR_readahead, 0, 0, 0); |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 155 | |
| 156 | TEST_PAUSE; |
| 157 | } |
| 158 | |
| 159 | static void cleanup(void) |
| 160 | { |
Jan Stancek | def4392 | 2012-10-15 09:54:50 +0200 | [diff] [blame] | 161 | tst_rmdir(); |
| 162 | } |
| 163 | |
| 164 | #else /* __NR_readahead */ |
| 165 | int main(void) |
| 166 | { |
| 167 | tst_brkm(TCONF, NULL, "System doesn't support __NR_readahead"); |
| 168 | } |
| 169 | #endif |