*** empty log message ***
[arla.git] / tests / sendfile.c
blobdbb5ba68a6a6252c2881ed41ea9d0ff6eafe3d51
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/sendfile.h>
8 #include <fcntl.h>
9 #include <stdio.h>
11 #define BUFSIZE 2048
13 int
14 main(void) {
15 const char *filename = "/afs/stacken.kth.se/test/TEXT.txt";
16 struct stat stat_buf;
17 char buf[BUFSIZE];
18 int sockets[2];
19 off_t offset;
20 size_t len;
21 int ret;
22 int fd;
24 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
25 perror("creating socketpair");
26 exit(1);
29 fd = open(filename, O_RDONLY);
30 if (fd == -1) {
31 perror("opening file");
32 exit(1);
35 fstat(fd, &stat_buf);
37 offset = 0;
38 len = stat_buf.st_size;
40 assert(len < BUFSIZE);
42 ret = sendfile(sockets[0], fd, &offset, len);
43 if (ret == -1) {
44 perror("sendfile");
45 exit(1);
47 if (ret != len) {
48 fprintf(stderr, "sendfile sent %d of %d bytes\n", ret, len);
49 exit(1);
51 close(fd);
53 ret = read(sockets[1], buf, BUFSIZE);
54 if (ret == -1) {
55 perror("read failed");
56 exit(1);
58 if (ret != len) {
59 fprintf(stderr, "read %d of %d bytes\n", ret, len);
60 exit(1);
62 buf[len] = '\0';
64 close(sockets[0]);
65 close(sockets[1]);
67 fprintf(stderr, "happy: sent and read %d bytes\n", ret);
68 printf("%s\n", buf);
70 return 0;