Whamcloud - gitweb
LU-15210 tests: fix sanity-lnet to handle duplicate IP
[fs/lustre-release.git] / lustre / tests / sendfile.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  */
29
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <time.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/sendfile.h>
39 #include <sys/stat.h>
40 #include <sys/socket.h>
41
42 #include <lustre/lustreapi.h>
43
44 #define syserr(str) { perror(str); exit(-1); }
45
46 int main(int argc, char *argv[])
47 {
48         char *sfile, *tfile;
49         struct stat stbuf;
50         int size;
51         unsigned long bufsize = 1024 * 1024;
52         int infd, outfd;
53         int sd[2];
54         int rc;
55         char *buf;
56         char cmd[1024];
57         loff_t pos;
58
59         if (argc < 3) {
60                 fprintf(stderr, "%s <source file> <dest file>\n", argv[0]);
61                 exit(-1);
62         }
63
64         sfile = argv[1];
65         tfile = argv[2];
66
67         if (stat(sfile, &stbuf) < 0) {
68                 if (errno == ENOENT) {
69                         /* assume doing non-object file testing */
70                         infd = open(sfile,
71                                     O_LOV_DELAY_CREATE | O_CREAT | O_RDWR,
72                                     0644);
73                         if (infd < 0)
74                                 syserr("open source file:");
75
76                         size = random() % (1 * 1024 * 1024) + 1024;
77                         if (ftruncate(infd, (off_t)size) < 0)
78                                 syserr("truncate file error:");
79                 } else {
80                         syserr("stat file: ");
81                 }
82         } else if (S_ISREG(stbuf.st_mode)) {
83                 size = (int)stbuf.st_size;
84                 infd = open(sfile, O_RDONLY, 0644);
85                 if (infd < 0)
86                         syserr("Open an existing file error:");
87         } else {
88                 fprintf(stderr, "%s is not a regular file\n", sfile);
89                 exit(-1);
90         }
91
92         outfd = open(tfile, O_WRONLY | O_TRUNC | O_CREAT, 0666);
93         if (outfd < 0)
94                 syserr("open dest file:");
95
96         rc = socketpair(AF_LOCAL, SOCK_STREAM, 0, sd);
97         if (rc < 0)
98                 syserr("socketpair");
99
100         rc = fcntl(sd[0], F_SETFL, O_NONBLOCK);
101         if (rc < 0)
102                 syserr("fcntl");
103
104         rc = setsockopt(sd[0], SOL_SOCKET, SO_SNDBUF,
105                         &bufsize, sizeof(bufsize));
106         if (rc)
107                 syserr("setsockopt");
108
109         srandom(time(NULL));
110
111         pos = 0;
112         while (size > 0) {
113                 int rc2;
114                 size_t seg_size;
115
116                 seg_size = random() % bufsize + 1;
117                 if (seg_size > size)
118                         seg_size = size;
119
120                 while (seg_size) {
121                         rc = sendfile(sd[0], infd, &pos, seg_size);
122                         if (rc < 0)
123                                 syserr("sendfile:");
124
125                         seg_size -= rc;
126                         size -= rc;
127                         if (size == 0)
128                                 close(sd[0]);
129
130                         buf = malloc(rc);
131                         if (read(sd[1], buf, rc) < 0)
132                                 syserr("read from socket:");
133
134                         rc2 = write(outfd, buf, rc);
135                         if (rc2 != rc)
136                                 syserr("write dest file error:");
137                         free(buf);
138                 }
139         }
140         close(sd[1]), close(infd), close(outfd);
141
142         snprintf(cmd, sizeof(cmd), "cmp %s %s\n", sfile, tfile);
143         return system(cmd);
144 }