summaryrefslogtreecommitdiff
path: root/libaio-0.3.109/harness/cases/16.t
blob: c3157cc331dc1f9b517110c9b6fc79416791bc9d (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
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
/* 16.t
- eventfd tests.
*/
#include <stdint.h>
#include <err.h>
#include <sys/syscall.h>   /* For SYS_xxx definitions */

#ifndef SYS_eventfd
#if defined(__i386__)
#define SYS_eventfd 323
#elif defined(__x86_64__)
#define SYS_eventfd 284
#elif defined(__ia64__)
#define SYS_eventfd 1309
#elif defined(__PPC__)
#define SYS_eventfd 307
#elif defined(__s390__)
#define SYS_eventfd 318
#elif defined(__alpha__)
#define SYS_eventfd 478
#else
#error define SYS_eventfd for your arch!
#endif
#endif

int test_main(void)
{
	/* 10 MB takes long enough that we would fail if eventfd
	 * returned immediately. */
#define SIZE	10000000
	char *buf;
	struct io_event io_event;
	struct iocb iocb;
	struct iocb *iocbs[] = { &iocb };
	int rwfd, efd;
	int res;
	io_context_t	io_ctx;
	uint64_t event;
	struct timespec	notime = { .tv_sec = 0, .tv_nsec = 0 };

	buf = malloc(SIZE);				assert(buf);
	efd = syscall(SYS_eventfd, 0);
	if (efd < 0) {
		if (errno == ENOSYS) {
			printf("No eventfd support.  [SKIPPING]\n");
			exit(0);
		}
		err(1, "Failed to get eventfd");
	}

	rwfd = open("testdir/rwfile", O_RDWR);		assert(rwfd != -1);
	res = ftruncate(rwfd, 0);			assert(res == 0);
	memset(buf, 0x42, SIZE);

	/* Write test. */
	res = io_queue_init(1024, &io_ctx);		assert(res == 0);
	io_prep_pwrite(&iocb, rwfd, buf, SIZE, 0);
	io_set_eventfd(&iocb, efd);
	res = io_submit(io_ctx, 1, iocbs);		assert(res == 1);

	alarm(30);
	res = read(efd, &event, sizeof(event));		assert(res == sizeof(event));
	assert(event == 1);

	/* This should now be ready. */
	res = io_getevents(io_ctx, 0, 1, &io_event, &notime);
	if (res != 1)
		err(1, "io_getevents did not return 1 event after eventfd");
	assert(io_event.res == SIZE);
	printf("eventfd write test [SUCCESS]\n");

	/* Read test. */
	memset(buf, 0, SIZE);
	io_prep_pread(&iocb, rwfd, buf, SIZE, 0);
	io_set_eventfd(&iocb, efd);
	res = io_submit(io_ctx, 1, iocbs);		assert(res == 1);

	alarm(30);
	res = read(efd, &event, sizeof(event));		assert(res == sizeof(event));
	assert(event == 1);

	/* This should now be ready. */
	res = io_getevents(io_ctx, 0, 1, &io_event, &notime);
	if (res != 1)
		err(1, "io_getevents did not return 1 event after eventfd");
	assert(io_event.res == SIZE);

	for (res = 0; res < SIZE; res++)
		assert(buf[res] == 0x42);
	printf("eventfd read test  [SUCCESS]\n");

	return 0;
}