1/*
2 * Copyright (c) 2004, Bull SA. All rights reserved.
3 * Created by:  Laurent.Vivier@bull.net
4 * This file is licensed under the GPL license.  For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7 */
8
9/*
10 * assertion:
11 *
12 *	LIO_READ causes the entry to be submited as if by a call to aio_read().
13 *
14 * method:
15 *
16 *	- open a file
17 *	- write data
18 *	- submit read request to lio_listio
19 *	- check data is read correctly
20 *
21 */
22
23#define _XOPEN_SOURCE 600
24#include <stdio.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <string.h>
30#include <errno.h>
31#include <stdlib.h>
32#include <aio.h>
33
34#include "posixtest.h"
35
36#define TNAME "lio_listio/8-1.c"
37
38int main(void)
39{
40	char tmpfname[256];
41#define BUF_SIZE 512
42	unsigned char buf[BUF_SIZE];
43	unsigned char check[BUF_SIZE];
44	int fd;
45	struct aiocb aiocb;
46	struct aiocb *list[1];
47	int i;
48	int err;
49	int ret;
50
51	if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
52		exit(PTS_UNSUPPORTED);
53
54	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_lio_listio_8_1_%d",
55		 getpid());
56	unlink(tmpfname);
57	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
58	if (fd == -1) {
59		printf(TNAME " Error at open(): %s\n", strerror(errno));
60		exit(PTS_UNRESOLVED);
61	}
62
63	unlink(tmpfname);
64
65	for (i = 0; i < BUF_SIZE; i++)
66		buf[i] = i;
67
68	if (write(fd, buf, BUF_SIZE) != BUF_SIZE) {
69		printf(TNAME " Error at write(): %s\n", strerror(errno));
70		exit(PTS_UNRESOLVED);
71	}
72
73	memset(check, 0xaa, BUF_SIZE);
74	memset(&aiocb, 0, sizeof(struct aiocb));
75	aiocb.aio_fildes = fd;
76	aiocb.aio_buf = check;
77	aiocb.aio_nbytes = BUF_SIZE;
78	aiocb.aio_lio_opcode = LIO_READ;
79
80	list[0] = &aiocb;
81
82	if (lio_listio(LIO_WAIT, list, 1, NULL) == -1) {
83		printf(TNAME " Error at lio_listio(): %s\n", strerror(errno));
84
85		close(fd);
86		exit(PTS_FAIL);
87	}
88
89	/* Check return values */
90	err = aio_error(&aiocb);
91	ret = aio_return(&aiocb);
92
93	if (err != 0) {
94		printf(TNAME " Error at aio_error(): %s\n", strerror(errno));
95
96		close(fd);
97		exit(PTS_FAIL);
98	}
99
100	if (ret != BUF_SIZE) {
101		printf(TNAME " Error at aio_return(): %d\n", ret);
102
103		close(fd);
104		exit(PTS_FAIL);
105	}
106
107	/* check it */
108	for (i = 0; i < BUF_SIZE; i++) {
109		if (buf[i] != check[i]) {
110			printf(TNAME " read values are corrupted\n");
111			exit(PTS_FAIL);
112		}
113	}
114
115	close(fd);
116	printf("Test PASSED\n");
117	return PTS_PASS;
118}
119