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_return() may be called exactly once to retrieve the return status.
13 *
14 * method:
15 *
16 *	- open a file
17 *	- fill in an aiocb for writing
18 *	- call aio_write using this aiocb
19 *	- call aio_return to get the aiocb status (number of bytes written)
20 *	- call aio_return again, return status should be -1
21 */
22
23#define _XOPEN_SOURCE 600
24#include <sys/stat.h>
25#include <aio.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include "posixtest.h"
33
34#define TNAME "aio_return/2-1.c"
35#define BUF_SIZE 111
36
37int main(void)
38{
39	char tmpfname[256];
40	char buf[BUF_SIZE];
41	struct aiocb aiocb;
42	int fd, retval;
43
44	if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
45		return PTS_UNSUPPORTED;
46
47	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_return_2_1_%d",
48		 getpid());
49	unlink(tmpfname);
50	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
51
52	if (fd == -1) {
53		printf(TNAME " Error at open(): %s\n", strerror(errno));
54		return PTS_UNRESOLVED;
55	}
56
57	unlink(tmpfname);
58
59	memset(buf, 0xaa, BUF_SIZE);
60	memset(&aiocb, 0, sizeof(struct aiocb));
61	aiocb.aio_fildes = fd;
62	aiocb.aio_buf = buf;
63	aiocb.aio_nbytes = BUF_SIZE;
64
65	if (aio_write(&aiocb) == -1) {
66		close(fd);
67		printf(TNAME " Error at aio_write(): %s\n",
68		       strerror(aio_error(&aiocb)));
69		return PTS_FAIL;
70	}
71
72	do {
73		usleep(10000);
74		retval = aio_error(&aiocb);
75	} while (retval == EINPROGRESS);
76
77	retval = aio_return(&aiocb);
78
79	if (0 < retval) {
80
81		if (retval != BUF_SIZE) {
82			close(fd);
83			printf(TNAME " aio_return didn't return expected size: "
84			       "%d\n", retval);
85			return PTS_FAIL;
86		}
87
88		retval = aio_return(&aiocb);
89
90		if (retval != -1) {
91			close(fd);
92			printf(TNAME " Second call to aio_return() may "
93			       "return -1; aio_return() returned %d\n", retval);
94			return PTS_UNTESTED;
95		}
96
97	} else {
98		close(fd);
99		printf(TNAME " Error at aio_error(): %s\n",
100		       strerror(aio_error(&aiocb)));
101		return PTS_UNRESOLVED;
102	}
103
104	close(fd);
105	printf("Test PASSED\n");
106	return PTS_PASS;
107}
108