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 * The file is read at offset given by aio_offset.
13 *
14 * method:
15 *
16 *	- write data to a file
17 *	- read file using aio_read at a given offset
18 *	- check data is consistent
19 */
20
21#define _XOPEN_SOURCE 600
22#include <stdio.h>
23#include <sys/types.h>
24#include <unistd.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <string.h>
28#include <errno.h>
29#include <stdlib.h>
30#include <aio.h>
31
32#include "posixtest.h"
33
34#define TNAME "aio_read/4-1.c"
35
36int main(void)
37{
38	char tmpfname[256];
39#define BUF_SIZE 512
40	unsigned char buf[BUF_SIZE * 2];
41	unsigned char check[BUF_SIZE];
42	int fd;
43	struct aiocb aiocb;
44	int i;
45
46	if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
47		return PTS_UNSUPPORTED;
48
49	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_read_4_1_%d",
50		 getpid());
51	unlink(tmpfname);
52	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
53	if (fd == -1) {
54		printf(TNAME " Error at open(): %s\n", strerror(errno));
55		exit(PTS_UNRESOLVED);
56	}
57
58	unlink(tmpfname);
59
60	memset(&buf[0], 1, BUF_SIZE);
61	memset(&buf[BUF_SIZE], 2, BUF_SIZE);
62
63	if (write(fd, buf, BUF_SIZE * 2) != BUF_SIZE * 2) {
64		printf(TNAME " Error at write(): %s\n", strerror(errno));
65		exit(PTS_UNRESOLVED);
66	}
67
68	memset(check, 0xaa, BUF_SIZE);
69	memset(&aiocb, 0, sizeof(struct aiocb));
70	aiocb.aio_fildes = fd;
71	aiocb.aio_buf = check;
72	aiocb.aio_nbytes = BUF_SIZE;
73	aiocb.aio_offset = BUF_SIZE;
74
75	if (aio_read(&aiocb) == -1) {
76		printf(TNAME " Error at aio_read(): %s\n", strerror(errno));
77		exit(PTS_FAIL);
78	}
79
80	int err;
81	int ret;
82
83	/* Wait until end of transaction */
84	do {
85		usleep(10000);
86		err = aio_error(&aiocb);
87	} while (err == EINPROGRESS);
88
89	ret = aio_return(&aiocb);
90
91	if (err != 0) {
92		printf(TNAME " Error at aio_error() : %s\n", strerror(err));
93		close(fd);
94		exit(PTS_FAIL);
95	}
96
97	if (ret != BUF_SIZE) {
98		printf(TNAME " Error at aio_return()\n");
99		close(fd);
100		exit(PTS_FAIL);
101	}
102
103	/* check it */
104	for (i = 0; i < BUF_SIZE; i++) {
105		if (check[i] != 2) {
106			printf(TNAME " read values are corrupted\n");
107			close(fd);
108			exit(PTS_FAIL);
109		}
110	}
111
112	close(fd);
113	printf("Test PASSED\n");
114	return PTS_PASS;
115}
116