10dc076565f772bb1953209fb69ea150b494aaa40robbiew/*
20dc076565f772bb1953209fb69ea150b494aaa40robbiew * Copyright (c) 2004, Bull SA. All rights reserved.
30dc076565f772bb1953209fb69ea150b494aaa40robbiew * Created by:  Laurent.Vivier@bull.net
40dc076565f772bb1953209fb69ea150b494aaa40robbiew * This file is licensed under the GPL license.  For the full content
52c28215423293e443469a07ae7011135d058b671Garrett Cooper * of this license, see the COPYING file at the top level of this
60dc076565f772bb1953209fb69ea150b494aaa40robbiew * source tree.
70dc076565f772bb1953209fb69ea150b494aaa40robbiew */
80dc076565f772bb1953209fb69ea150b494aaa40robbiew
90dc076565f772bb1953209fb69ea150b494aaa40robbiew/*
100dc076565f772bb1953209fb69ea150b494aaa40robbiew * assertion:
110dc076565f772bb1953209fb69ea150b494aaa40robbiew *
120dc076565f772bb1953209fb69ea150b494aaa40robbiew *	If the aiocbp is used to submit another asynchronous operation,
130dc076565f772bb1953209fb69ea150b494aaa40robbiew *	then aio_return may be successfully used to retrieve the return status.
140dc076565f772bb1953209fb69ea150b494aaa40robbiew *
150dc076565f772bb1953209fb69ea150b494aaa40robbiew * method:
160dc076565f772bb1953209fb69ea150b494aaa40robbiew *
170dc076565f772bb1953209fb69ea150b494aaa40robbiew *	- open a file
180dc076565f772bb1953209fb69ea150b494aaa40robbiew *	- fill in an aiocb for writing
190dc076565f772bb1953209fb69ea150b494aaa40robbiew *	- call aio_write using this aiocb
200dc076565f772bb1953209fb69ea150b494aaa40robbiew *	- call aio_return to get the aiocb status (number of bytes written)
210dc076565f772bb1953209fb69ea150b494aaa40robbiew *	- reuse the aiocb for writing
220dc076565f772bb1953209fb69ea150b494aaa40robbiew *	- call aio_write using this aiocb
230dc076565f772bb1953209fb69ea150b494aaa40robbiew *	- call aio_return to get the aiocb status (number of bytes written)
240dc076565f772bb1953209fb69ea150b494aaa40robbiew */
250dc076565f772bb1953209fb69ea150b494aaa40robbiew
260dc076565f772bb1953209fb69ea150b494aaa40robbiew#define _XOPEN_SOURCE 600
270dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdio.h>
280dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sys/types.h>
290dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <unistd.h>
300dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <sys/stat.h>
310dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <fcntl.h>
320dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <string.h>
330dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <errno.h>
340dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdlib.h>
350dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <aio.h>
360dc076565f772bb1953209fb69ea150b494aaa40robbiew
370dc076565f772bb1953209fb69ea150b494aaa40robbiew#include "posixtest.h"
380dc076565f772bb1953209fb69ea150b494aaa40robbiew
390dc076565f772bb1953209fb69ea150b494aaa40robbiew#define TNAME "aio_return/3-1.c"
40fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak#define BUF_SIZE 4096
410dc076565f772bb1953209fb69ea150b494aaa40robbiew
42fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modakint main(void)
430dc076565f772bb1953209fb69ea150b494aaa40robbiew{
440dc076565f772bb1953209fb69ea150b494aaa40robbiew	char tmpfname[256];
450dc076565f772bb1953209fb69ea150b494aaa40robbiew	char buf[BUF_SIZE];
460dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct aiocb aiocb;
47fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak	int fd, retval;
480dc076565f772bb1953209fb69ea150b494aaa40robbiew
49264074b49e13b99960b37be37fd3731e525b5461Garrett Cooper	if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
50ec5557833ef9b56de0a174b976d06dce396b4112Garrett Cooper		return PTS_UNSUPPORTED;
510dc076565f772bb1953209fb69ea150b494aaa40robbiew
522c28215423293e443469a07ae7011135d058b671Garrett Cooper	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_return_3_1_%d",
53fd3016779b4a2c1063ddf65c035341dd163fc1b9Wanlong Gao		 getpid());
540dc076565f772bb1953209fb69ea150b494aaa40robbiew	unlink(tmpfname);
55fd3016779b4a2c1063ddf65c035341dd163fc1b9Wanlong Gao	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
56fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak
57fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak	if (fd == -1) {
58fd3016779b4a2c1063ddf65c035341dd163fc1b9Wanlong Gao		printf(TNAME " Error at open(): %s\n", strerror(errno));
59775d84c8aa7bb34d2c0f65c522d5e24297506ac1Cyril Hrubis		return PTS_UNRESOLVED;
600dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
610dc076565f772bb1953209fb69ea150b494aaa40robbiew
620dc076565f772bb1953209fb69ea150b494aaa40robbiew	unlink(tmpfname);
630dc076565f772bb1953209fb69ea150b494aaa40robbiew
640dc076565f772bb1953209fb69ea150b494aaa40robbiew	memset(buf, 0xaa, BUF_SIZE);
650dc076565f772bb1953209fb69ea150b494aaa40robbiew	memset(&aiocb, 0, sizeof(struct aiocb));
660dc076565f772bb1953209fb69ea150b494aaa40robbiew	aiocb.aio_fildes = fd;
67fd3016779b4a2c1063ddf65c035341dd163fc1b9Wanlong Gao	aiocb.aio_buf = buf;
680dc076565f772bb1953209fb69ea150b494aaa40robbiew	aiocb.aio_nbytes = BUF_SIZE;
69332cdb8e48d7380d2a11ec7ef96a2bf44b6da9eaGarrett Cooper
70fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak	if (aio_write(&aiocb) == -1) {
71fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak		close(fd);
720dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf(TNAME " Error at aio_write(): %s\n",
732a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper		       strerror(aio_error(&aiocb)));
74775d84c8aa7bb34d2c0f65c522d5e24297506ac1Cyril Hrubis		return PTS_FAIL;
750dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
760dc076565f772bb1953209fb69ea150b494aaa40robbiew
7726813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao	do {
7826813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao		usleep(10000);
7926813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao		retval = aio_error(&aiocb);
8026813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao	} while (retval == EINPROGRESS);
8126813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao
820dc076565f772bb1953209fb69ea150b494aaa40robbiew	retval = aio_return(&aiocb);
83fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak
842a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper	if (retval == -1) {
85fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak		close(fd);
860dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf(TNAME " Error at aio_return(): %d, %s\n", retval,
872a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper		       strerror(aio_error(&aiocb)));
88775d84c8aa7bb34d2c0f65c522d5e24297506ac1Cyril Hrubis		return PTS_FAIL;
890dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
900dc076565f772bb1953209fb69ea150b494aaa40robbiew
910dc076565f772bb1953209fb69ea150b494aaa40robbiew	memset(&aiocb, 0, sizeof(struct aiocb));
920dc076565f772bb1953209fb69ea150b494aaa40robbiew	aiocb.aio_fildes = fd;
93fd3016779b4a2c1063ddf65c035341dd163fc1b9Wanlong Gao	aiocb.aio_buf = buf;
94fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak	aiocb.aio_nbytes = BUF_SIZE / 2;
952c28215423293e443469a07ae7011135d058b671Garrett Cooper
96fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak	if (aio_write(&aiocb) == -1) {
97fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak		close(fd);
980dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf(TNAME " Error at aio_write(): %s\n",
992a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper		       strerror(aio_error(&aiocb)));
100775d84c8aa7bb34d2c0f65c522d5e24297506ac1Cyril Hrubis		return PTS_FAIL;
1010dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
1020dc076565f772bb1953209fb69ea150b494aaa40robbiew
10326813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao	do {
10426813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao		usleep(10000);
10526813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao		retval = aio_error(&aiocb);
10626813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao	} while (retval == EINPROGRESS);
10726813cd41ecd11f5a5e983fec9ed0813dbfba2b4Wanlong Gao
1082a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper	retval = aio_return(&aiocb);
1092a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper
1102a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper	if (retval == -1) {
111bf531a8d54487b2666b2bb9ecdfef08f98ffc350Garrett Cooper		close(fd);
1122a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper		printf(TNAME " Error at aio_return(): %s\n",
1132a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper		       strerror(aio_error(&aiocb)));
114775d84c8aa7bb34d2c0f65c522d5e24297506ac1Cyril Hrubis		return PTS_UNRESOLVED;
1152a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper	} else {
1162a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper
1172a9ad0bdf07565bac32eeb02d2e01f78b41a32d1Garrett Cooper		if (retval != (BUF_SIZE / 2)) {
118fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak			close(fd);
119332cdb8e48d7380d2a11ec7ef96a2bf44b6da9eaGarrett Cooper			printf(TNAME " aio_return() didn't fail as expected: "
120fd3016779b4a2c1063ddf65c035341dd163fc1b9Wanlong Gao			       "%d, %s\n", retval, strerror(aio_error(&aiocb)));
121775d84c8aa7bb34d2c0f65c522d5e24297506ac1Cyril Hrubis			return PTS_UNRESOLVED;
1220dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
123fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak
1240dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
1252c28215423293e443469a07ae7011135d058b671Garrett Cooper
126fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak	close(fd);
127fc03c4feda65ea77df4efab3fc76fd774f476775subrata_modak	printf("Test PASSED\n");
128775d84c8aa7bb34d2c0f65c522d5e24297506ac1Cyril Hrubis	return PTS_PASS;
129ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
130