File: copy_file_range.c

package info (click to toggle)
systemtap 4.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 39,000 kB
  • sloc: cpp: 78,785; ansic: 62,419; xml: 49,443; exp: 42,735; sh: 11,254; python: 3,062; perl: 2,252; tcl: 1,305; makefile: 1,072; lisp: 105; awk: 101; asm: 91; java: 56; sed: 16
file content (89 lines) | stat: -rw-r--r-- 2,930 bytes parent folder | download | duplicates (4)
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
/* COVERAGE: copy_file_range */
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <limits.h>

// We'd like to use the gilbc wrapper here, but it only exists in
// glibc 2.27+. When it does exist, we use our own wrapper.
#if defined(SYS_copy_file_range) && !__GLIBC_PREREQ(2, 27)
static loff_t
copy_file_range(int fd_in, loff_t *off_in, int fd_out,
		loff_t *off_out, size_t len, unsigned int flags)
{
    return syscall(SYS_copy_file_range, fd_in, off_in, fd_out,
		   off_out, len, flags);
}
#endif

int main ()
{
#ifdef SYS_copy_file_range
    int fd_in, fd_out;
    loff_t off_in, off_out;
    char buf[] = "Hello world abcdefghijklmnopqrstuvwxyz 01234567890123456789";
    loff_t buf_size = sizeof(buf) - 1;

    /* Create a file with something in it. */
    fd_in = open("foobar", O_WRONLY|O_CREAT, 0666);
    write(fd_in, buf, buf_size);
    fsync(fd_in);
    close(fd_in);

    fd_in = open("foobar", O_RDONLY);
    fd_out = open("foobar2", O_WRONLY|O_CREAT, 0666);

    /* Copy 'foobar' to 'foobar2'. */
    off_in = off_out = 0;
    copy_file_range(fd_in, &off_in, fd_out, &off_out, buf_size, 0);
    //staptest// copy_file_range (NNNN, XXXX, NNNN, XXXX, 59, 0x0) = 59

    /* Test variations of the copy_file range to make sure that the
     * syscall tapset prints out the correct values. */
    copy_file_range(-1, &off_in, fd_out, &off_out, buf_size, 0);
    //staptest// copy_file_range (-1, XXXX, NNNN, XXXX, 59, 0x0) = -NNNN

    copy_file_range(fd_in, (loff_t*)-1, fd_out, &off_out, buf_size, 0);
#ifdef __s390__
    //staptest// copy_file_range (NNNN, 0x[7]?[f]+, NNNN, XXXX, 59, 0x0) = -NNNN
#else
    //staptest// copy_file_range (NNNN, 0x[f]+, NNNN, XXXX, 59, 0x0) = -NNNN
#endif

    copy_file_range(fd_in, &off_in, -1, &off_out, buf_size, 0);
    //staptest// copy_file_range (NNNN, XXXX, -1, XXXX, 59, 0x0) = -NNNN

    copy_file_range(fd_in, &off_in, fd_out, (loff_t*)-1, buf_size, 0);
#ifdef __s390__
    //staptest// copy_file_range (NNNN, XXXX, NNNN, 0x[7]?[f]+, 59, 0x0) = -NNNN
#else
    //staptest// copy_file_range (NNNN, XXXX, NNNN, 0x[f]+, 59, 0x0) = -NNNN
#endif

    copy_file_range(fd_in, &off_in, fd_out, &off_out, -1L, 0);
#if __WORDSIZE == 64
    //staptest// copy_file_range (NNNN, XXXX, NNNN, XXXX, 18446744073709551615, 0x0) = NNNN
#else
    //staptest// copy_file_range (NNNN, XXXX, NNNN, XXXX, 4294967295, 0x0) = NNNN
#endif

    /* Note: flags is unused and should be set to 0, otherwise an
     * error occurs. This may change if the syscall is developed. */
    copy_file_range(fd_in, &off_in, fd_out, &off_out, buf_size, -1);
    //staptest// copy_file_range (NNNN, XXXX, NNNN, XXXX, 59, 0xffffffff) = NNNN

    close(fd_out);
    close(fd_in);
    unlink("foobar");
    unlink("foobar2");
#endif

    return 0;
}