summaryrefslogtreecommitdiff
path: root/test/misc/tst-syscall6.c
blob: fa51c275e4a5ae083c58d8c6af830c26394ba9c2 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <linux/fs.h> /* for RWF_HIPRI */

int main()
{
#if defined SYS_preadv2 && defined SYS_pwritev2
	char tmp[] = "/tmp/tst-preadv2-XXXXXX";
	int fd;
	struct iovec iov[2];
	char *str0 = "hello ";
	char *str1 = "world\n";
	char input[16];
	int nio;

	fd = mkstemp (tmp);
	if (fd == -1) {
		puts ("mkstemp failed");
		return 1;
	}

	iov[0].iov_base = str0;
	iov[0].iov_len = strlen(str0);
	iov[1].iov_base = str1;
	iov[1].iov_len = strlen(str1) + 1; /* null terminator */

	nio = syscall(SYS_pwritev2, fd, iov, 2, 0, 0, RWF_DSYNC);

	if (nio <= 0) {
		puts ("failed to write to fd");
		return 1;
	}

	/* Read in the second string into the first buffer */
	iov[0].iov_base = input;
	iov[0].iov_len = strlen(str1) + 1; /* null terminator */
	nio = syscall(SYS_preadv2, fd, iov, 1, strlen(str0), 0, RWF_HIPRI);
	if (nio <= 0) {
		printf ("failed to read fd %d\n", nio);
		return 1;
	}

	if (strncmp(iov[0].iov_base, iov[1].iov_base, strlen(str1)) == 0)
		printf ("syscall(SYS_preadv2) read %s", (char *) iov[0].iov_base);

	if (close(fd) != 0) {
		puts ("failed to close read fd");
		return 1;
	}

	if (unlink(tmp) != 0) {
		puts ("failed to unlink file");
		return 1;
	}

	return 0;
#else
  return 23;
#endif
}