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 * aiocbp may be be used as an argument to aio_error() and aio_return().
13 *
14 * method: (short read)
15 *
16 *	- write data to a file
17 *	- read file using aio_read
18 *	- check error and return codes using previous aiocb
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/3-2.c"
35
36int main(void)
37{
38	char tmpfname[256];
39#define BUF_SIZE 1024
40	char buf[BUF_SIZE];
41	int fd;
42	int ret;
43	struct aiocb aiocb;
44
45	if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
46		return PTS_UNSUPPORTED;
47
48	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_read_3_2_%d",
49		 getpid());
50	unlink(tmpfname);
51	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
52	if (fd == -1) {
53		printf(TNAME " Error at open(): %s\n", strerror(errno));
54		exit(PTS_UNRESOLVED);
55	}
56
57	unlink(tmpfname);
58
59	if (write(fd, buf, BUF_SIZE / 2) != BUF_SIZE / 2) {
60		printf(TNAME " Error at write(): %s\n", strerror(errno));
61		exit(PTS_UNRESOLVED);
62	}
63
64	/* try to read BUF_SIZE bytes whereas the file is BUF_SIZE/2 long */
65
66	memset(&aiocb, 0, sizeof(struct aiocb));
67	aiocb.aio_fildes = fd;
68	aiocb.aio_buf = buf;
69	aiocb.aio_nbytes = BUF_SIZE;
70
71	if (aio_read(&aiocb) == -1) {
72		printf(TNAME " Error at aio_read(): %s\n", strerror(errno));
73		exit(PTS_FAIL);
74	}
75
76	/* Wait for request completion */
77	do {
78		usleep(10000);
79		ret = aio_error(&aiocb);
80	} while (ret == EINPROGRESS);
81
82	/* error status shall be 0 and return status shall be BUF_SIZE/2 */
83	if (ret != 0) {
84		printf(TNAME " Error at aio_error()\n");
85		exit(PTS_FAIL);
86	}
87
88	if (aio_return(&aiocb) != BUF_SIZE / 2) {
89		printf(TNAME " Error at aio_return()\n");
90		exit(PTS_FAIL);
91	}
92
93	close(fd);
94	printf("Test PASSED\n");
95	return PTS_PASS;
96}
97