File: sys-preadv2_pwritev2.c

package info (click to toggle)
valgrind 1%3A3.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 170,080 kB
  • sloc: ansic: 782,981; exp: 26,134; xml: 22,780; asm: 14,072; cpp: 7,903; makefile: 6,768; perl: 6,097; sh: 5,669; javascript: 981; awk: 148
file content (79 lines) | stat: -rw-r--r-- 2,082 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
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/uio.h>
#include <string.h>
#include "../../memcheck.h"

#include <errno.h>

int main(int argc, char **argv)
{
    char str0[] = "hello ";
    char str1[] = "world\n";
    struct iovec iov[2];
    int fd;

    fd = open("prwv2_source", O_CREAT | O_RDWR, 0644);
    if (fd == -1) {
        perror("prwv2_source");
        exit(EXIT_FAILURE);
    }

    iov[0].iov_base = str0;
    iov[0].iov_len = strlen(str0);
    iov[1].iov_base = str1;
    iov[1].iov_len = strlen(str1);

    /* Check pwritev2 and preadv2 called with the correct arguments works. */
    if (pwritev2(fd, iov, 2, 0, 0) == -1) {
        perror("pwritev2");
        exit(EXIT_FAILURE);
    }

    if (preadv2(fd, iov, 2, 0, 0) == -1) {
        perror("preadv2");
        printf("errno: %d\n", errno);
        exit(EXIT_FAILURE);
    }

    /* Check valgrind will produce expected warnings for the
       various wrong arguments. */
    do {
        /* always allocate 16 bytes to not to have different .exps for different reg sizes */
        char *mem = malloc(16);
        void *t = (void *) &mem[0];
        void *z = (void *) -1;
	int c = *((int *) &mem[4]);
        int flag = *((int *) &mem[8]);
        pwritev2(fd, NULL, 2, 0, 0);
        pwritev2(fd, z, 2, 0, 0);
        pwritev2(fd, t, 2, 0, 0);
        pwritev2(fd, iov, -1, 0, 0);
        pwritev2(fd, iov, c, 0, 0);
        pwritev2(fd, iov, 2, -5, 0);
        pwritev2(-1, iov, 2, -5, 0);
        pwritev2(fd, iov, 2, -5, flag);

        preadv2(fd, NULL, 2, 0, 0);
        preadv2(fd, z, 2, 0, 0);
        preadv2(fd, t, 2, 0, 0);
        preadv2(fd, iov, -1, 0, 0);
        preadv2(fd, iov, c, 0, 0);
        preadv2(fd, iov, 2, -5, 0);
        preadv2(-1, iov, 2, -5, 0);

        iov[1].iov_base = (void *) -1;
        pwritev2(fd, iov, 2, 0, 0);
        preadv2(fd, iov, 2, 0, 0);
        free(mem);
    } while (0);

    close(fd);
    unlink("prwv2_source");
    exit(EXIT_SUCCESS);
}