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