plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * sendfile02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to test the basic functionality of the sendfile(2) system call. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * 1. call sendfile(2) with offset = 0 |
| 29 | * 2. call sendfile(2) with offset in the middle of the file |
| 30 | * |
| 31 | * USAGE: <for command-line> |
| 32 | * sendfile02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
robbiew | 9827066 | 2003-05-14 14:17:45 +0000 | [diff] [blame] | 33 | * where, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | * -f : Turn off functionality Testing. |
| 35 | * -i n : Execute test n times. |
| 36 | * -I x : Execute test for x seconds. |
| 37 | * -P x : Pause for x seconds between iterations. |
| 38 | * -t : Turn on syscall timing. |
| 39 | * |
| 40 | * HISTORY |
| 41 | * 07/2001 Ported by Wayne Boyer |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 42 | * 08/2002 Make it use a socket so it works with 2.5 kernel |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * |
| 44 | * RESTRICTIONS |
| 45 | * NONE |
| 46 | */ |
| 47 | #include <stdio.h> |
| 48 | #include <errno.h> |
| 49 | #include <fcntl.h> |
| 50 | #include <sys/stat.h> |
plars | 8b6d9bf | 2002-03-05 13:55:58 +0000 | [diff] [blame] | 51 | #include <sys/sendfile.h> |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 52 | #include <sys/types.h> |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 53 | #include <sys/wait.h> |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 54 | #include <sys/socket.h> |
| 55 | #include <netinet/in.h> |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 56 | #include <arpa/inet.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | #include "usctest.h" |
| 58 | #include "test.h" |
| 59 | |
| 60 | char *TCID = "sendfile02"; |
| 61 | int TST_TOTAL = 4; |
| 62 | extern int Tst_count; |
| 63 | |
| 64 | char in_file[100]; |
| 65 | char out_file[100]; |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 66 | int out_fd; |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 67 | pid_t child_pid; |
robbiew | 9827066 | 2003-05-14 14:17:45 +0000 | [diff] [blame] | 68 | static int sockfd, s; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
| 70 | void cleanup(void); |
| 71 | void setup(void); |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 72 | int create_server(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | struct test_case_t { |
| 75 | char *desc; |
| 76 | int offset; |
| 77 | int exp_retval; |
| 78 | } testcases[] = { |
| 79 | { "Test sendfile(2) with offset = 0", 0, 26 }, |
| 80 | { "Test sendfile(2) with offset in the middle of file", 2, 24 }, |
| 81 | { "Test sendfile(2) with offset in the middle of file", 4, 22 }, |
| 82 | { "Test sendfile(2) with offset in the middle of file", 6, 20 } |
| 83 | }; |
| 84 | |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 85 | void do_sendfile(off_t offset, int i) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | { |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 87 | int in_fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | struct stat sb; |
robbiew | 33b7774 | 2002-10-02 14:56:00 +0000 | [diff] [blame] | 89 | int wait_status; |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 90 | int wait_stat; |
| 91 | |
| 92 | out_fd = create_server(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | if ((in_fd = open(in_file, O_RDONLY)) < 0) { |
| 95 | tst_brkm(TBROK, cleanup, "open failed: %d", errno); |
| 96 | /*NOTREACHED*/ |
| 97 | } |
| 98 | if (stat(in_file, &sb) < 0) { |
| 99 | tst_brkm(TBROK, cleanup, "stat failed: %d", errno); |
| 100 | /*NOTREACHED*/ |
| 101 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
| 103 | TEST(sendfile(out_fd, in_fd, &offset, sb.st_size - offset)); |
| 104 | |
| 105 | if (STD_FUNCTIONAL_TEST) { |
robbiew | 9827066 | 2003-05-14 14:17:45 +0000 | [diff] [blame] | 106 | /* Close the sockets */ |
| 107 | shutdown(sockfd, SHUT_RDWR); |
| 108 | shutdown(s, SHUT_RDWR); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | if (TEST_RETURN != testcases[i].exp_retval) { |
| 110 | tst_resm(TFAIL, "sendfile(2) failed to return " |
| 111 | "expected value, expected: %d, " |
| 112 | "got: %d", testcases[i].exp_retval, |
| 113 | TEST_RETURN); |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 114 | kill(child_pid, SIGKILL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | } else { |
| 116 | tst_resm(TPASS, "functionality of sendfile() is " |
| 117 | "correct"); |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 118 | wait_status = waitpid(-1, &wait_stat, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | } |
| 120 | } else { |
| 121 | tst_resm(TPASS, "call succeeded"); |
robbiew | 9827066 | 2003-05-14 14:17:45 +0000 | [diff] [blame] | 122 | /* Close the sockets */ |
| 123 | shutdown(sockfd, SHUT_RDWR); |
| 124 | shutdown(s, SHUT_RDWR); |
| 125 | if (TEST_RETURN != testcases[i].exp_retval) { |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 126 | kill(child_pid, SIGKILL); |
robbiew | 9827066 | 2003-05-14 14:17:45 +0000 | [diff] [blame] | 127 | } else { |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 128 | wait_status = waitpid(-1, &wait_stat, 0); |
robbiew | 9827066 | 2003-05-14 14:17:45 +0000 | [diff] [blame] | 129 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | close(in_fd); |
robbiew | 33b7774 | 2002-10-02 14:56:00 +0000 | [diff] [blame] | 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
| 137 | * setup() - performs all ONE TIME setup for this test. |
| 138 | */ |
| 139 | void |
| 140 | setup() |
| 141 | { |
| 142 | int fd; |
| 143 | char buf[100]; |
| 144 | |
| 145 | /* capture signals */ |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 146 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | |
| 148 | /* Pause if that option was specified */ |
| 149 | TEST_PAUSE; |
| 150 | |
| 151 | /* make a temporary directory and cd to it */ |
| 152 | tst_tmpdir(); |
| 153 | sprintf(in_file, "in.%d", getpid()); |
| 154 | if ((fd = creat(in_file, 00700)) < 0) { |
| 155 | tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d", |
| 156 | errno); |
| 157 | /*NOTREACHED*/ |
| 158 | } |
| 159 | sprintf(buf, "abcdefghijklmnopqrstuvwxyz"); |
| 160 | if (write(fd, buf, strlen(buf)) < 0) { |
| 161 | tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno); |
| 162 | /*NOTREACHED*/ |
| 163 | } |
| 164 | close(fd); |
| 165 | sprintf(out_file, "out.%d", getpid()); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 170 | * completion or premature exit. |
| 171 | */ |
| 172 | void |
| 173 | cleanup() |
| 174 | { |
| 175 | /* |
| 176 | * print timing stats if that option was specified. |
| 177 | * print errno log if that option was specified. |
| 178 | */ |
| 179 | TEST_CLEANUP; |
| 180 | |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 181 | close(out_fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | /* delete the test directory created in setup() */ |
| 183 | tst_rmdir(); |
| 184 | |
| 185 | /* exit with return code appropriate for results */ |
| 186 | tst_exit(); |
| 187 | } |
| 188 | |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 189 | int create_server(void) { |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 190 | int lc; |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 191 | int length; |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 192 | char rbuf[4096]; |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 193 | struct sockaddr_in sin1; |
| 194 | static int count=0; |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 195 | |
| 196 | sockfd = socket(PF_INET, SOCK_DGRAM, 0); |
| 197 | if(sockfd < 0) { |
| 198 | tst_brkm(TBROK, cleanup, "call to socket() failed: %s", |
| 199 | strerror(errno)); |
| 200 | return -1; |
| 201 | } |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 202 | sin1.sin_family = AF_INET; |
| 203 | sin1.sin_port = htons((getpid() % 32768) + 11000 + count++); |
| 204 | sin1.sin_addr.s_addr = INADDR_ANY; |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 205 | if(bind(sockfd, (struct sockaddr*)&sin1, sizeof(sin1)) < 0) { |
| 206 | tst_brkm(TBROK, cleanup, "call to bind() failed: %s", |
| 207 | strerror(errno)); |
| 208 | return -1; |
| 209 | } |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 210 | child_pid = fork(); |
| 211 | if(child_pid < 0) { |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 212 | tst_brkm(TBROK, cleanup, "client/server fork failed: %s", |
| 213 | strerror(errno)); |
| 214 | return -1; |
| 215 | } |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 216 | if(!child_pid) { |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 217 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
robbiew | 31f6f67 | 2003-01-28 14:50:49 +0000 | [diff] [blame] | 218 | length = sizeof(sin1); |
| 219 | recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr*)&sin1, &length); |
plars | 16bfb44 | 2002-08-05 22:07:16 +0000 | [diff] [blame] | 220 | } |
| 221 | exit(0); |
| 222 | } |
| 223 | |
| 224 | s = socket(PF_INET, SOCK_DGRAM, 0); |
| 225 | inet_aton("127.0.0.1", &sin1.sin_addr); |
| 226 | if(s < 0) { |
| 227 | tst_brkm(TBROK, cleanup, "call to socket() failed: %s", |
| 228 | strerror(errno)); |
| 229 | return -1; |
| 230 | } |
| 231 | if (connect(s, (struct sockaddr*)&sin1, sizeof(sin1)) < 0) { |
| 232 | tst_brkm(TBROK, cleanup, "call to connect() failed: %s", |
| 233 | strerror(errno)); |
| 234 | } |
| 235 | return s; |
| 236 | |
| 237 | } |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 238 | |
| 239 | int main(int ac, char **av) |
| 240 | { |
| 241 | int i; |
| 242 | int lc; /* loop counter */ |
| 243 | char *msg; /* parse_opts() return message */ |
| 244 | |
| 245 | if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| 246 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 247 | /*NOTREACHED*/ |
| 248 | } |
| 249 | |
| 250 | setup(); |
| 251 | |
| 252 | /* |
| 253 | * The following loop checks looping state if -c option given |
| 254 | */ |
| 255 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 256 | Tst_count = 0; |
| 257 | |
| 258 | for (i = 0; i < TST_TOTAL; ++i) { |
| 259 | do_sendfile(testcases[i].offset, i); |
| 260 | } |
| 261 | } |
| 262 | cleanup(); |
| 263 | |
| 264 | /*NOTREACHED*/ |
| 265 | return(0); |
| 266 | } |
| 267 | |