File: test_sendfile.c

package info (click to toggle)
firebuild 0.8.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 2,552 kB
  • sloc: cpp: 13,693; ansic: 6,086; python: 2,313; sh: 261; makefile: 40; awk: 33
file content (132 lines) | stat: -rw-r--r-- 3,028 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (c) 2022 Firebuild Inc.
 * All rights reserved.
 *
 * Free for personal use and commercial trial.
 * Non-trial commercial use requires licenses available from https://firebuild.com.
 * Modification and redistribution are permitted, but commercial use of derivative
 * works is subject to the same requirements of this license
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#define _GNU_SOURCE

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <sys/socket.h>
#include <sys/uio.h>
#else
#include <sys/sendfile.h>
#endif
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#define TOSTR(x) TOSTR2(x)
#define TOSTR2(x) #x
#define LOC "[" __FILE__ ":" TOSTR(__LINE__) "]"

int main() {
  int fd1, fd2;
#ifdef O_TMPFILE
  fd1 = open(".", O_RDWR | O_TMPFILE, 0644);
  if (fd1 == -1) {
    if (errno == ENOTSUP || errno == EISDIR) {
      return 0;
    } else {
      perror("open" LOC);
      exit(1);
    }
  }
#else
  /* sendfile() will fail, but that still excercises most of the code. */
  // TODO(rbalint) create socket and test with that
  fd1 = open("/dev/null", O_WRONLY);
  if (fd1 == -1) {
    perror("open" LOC);
    exit(1);
  }

#endif
  fd2 = open("integration.bats", O_RDWR);
  if (fd2 == -1) {
    perror("open" LOC);
    close(fd1);
    exit(1);
  }

#ifdef __APPLE__
  off_t len = 10;
  sendfile(fd2, fd1, 0, &len, NULL, 0);
  syscall(SYS_sendfile, fd1, fd2, NULL, 10);
#else
  if (sendfile(fd1, fd2, NULL, 10) == -1) {
    perror("sendfile" LOC);
    close(fd1);
    close(fd2);
    exit(1);
  }

  if (syscall(SYS_sendfile, fd1, fd2, NULL, 10) == -1) {
    perror("SYS_sendfile" LOC);
    close(fd1);
    close(fd2);
    exit(1);
  }
#endif

#ifndef __APPLE__
  if (copy_file_range(fd2, NULL, fd1, NULL, 10, 0) == -1) {
    perror("copy_file_range" LOC);
    close(fd1);
    close(fd2);
    exit(1);
  }

  /* test inherited fds*/
  loff_t long_offset = 0;
  if (copy_file_range(0, NULL, 1, &long_offset, 10, 0) == -1) {
    perror("copy_file_range" LOC);
    close(fd1);
    close(fd2);
    exit(1);
  }
  if (copy_file_range(0, &long_offset, 1, NULL, 10, 0) == -1) {
    perror("copy_file_range" LOC);
    close(fd1);
    close(fd2);
    exit(1);
  }
  if (sendfile(1, 0, NULL, 10) == -1) {
    perror("sendfile" LOC);
    close(fd1);
    close(fd2);
    exit(1);
  }

  off_t offset = 0;
  if (sendfile(1, 0, &offset, 10) == -1) {
    perror("sendfile" LOC);
    close(fd1);
    close(fd2);
    exit(1);
  }

#endif

  close(fd1);
  close(fd2);

  return 0;
}