1 /* 2 * Copyright (c) 2004, Bull SA. All rights reserved. 3 * Created by: Laurent.Vivier (at) 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 * If the aiocbp is used to submit another asynchronous operation, 13 * then aio_return may be successfully used to retrieve the return status. 14 * 15 * method: 16 * 17 * - open a file 18 * - fill in an aiocb for writing 19 * - call aio_write using this aiocb 20 * - call aio_return to get the aiocb status (number of bytes written) 21 * - call aio_return again, return status should be -1 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_return/3-2.c" 38 #define BUF_SIZE 4096 39 40 int main(void) 41 { 42 char tmpfname[256]; 43 char buf[BUF_SIZE]; 44 struct aiocb aiocb; 45 int fd, retval; 46 47 if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L) 48 return PTS_UNSUPPORTED; 49 50 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_return_3_2_%d", 51 getpid()); 52 unlink(tmpfname); 53 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); 54 55 if (fd == -1) { 56 printf(TNAME " Error at open(): %s\n", strerror(errno)); 57 return PTS_UNRESOLVED; 58 } 59 60 unlink(tmpfname); 61 62 memset(buf, 0xaa, BUF_SIZE); 63 memset(&aiocb, 0, sizeof(struct aiocb)); 64 aiocb.aio_fildes = fd; 65 aiocb.aio_buf = buf; 66 aiocb.aio_nbytes = BUF_SIZE; 67 68 if (aio_write(&aiocb) == -1) { 69 close(fd); 70 printf(TNAME " Error at aio_write(): %s\n", 71 strerror(aio_error(&aiocb))); 72 return PTS_FAIL; 73 } 74 75 do { 76 usleep(10000); 77 retval = aio_error(&aiocb); 78 } while (retval == EINPROGRESS); 79 80 retval = aio_return(&aiocb); 81 82 if (retval == -1) { 83 printf(TNAME " Error at aio_error(): %s\n", 84 strerror(aio_error(&aiocb))); 85 return PTS_UNRESOLVED; 86 } else { 87 88 if (retval != BUF_SIZE) { 89 close(fd); 90 printf(TNAME " Error at aio_return(): %d, %s\n", retval, 91 strerror(aio_error(&aiocb))); 92 return PTS_FAIL; 93 } 94 95 retval = aio_return(&aiocb); 96 97 if (retval != -1 && aio_error(&aiocb) != EINVAL) { 98 close(fd); 99 printf(TNAME " aio_return() may fail with (-1, %d); " 100 "failed with (%d, %d) instead\n", 101 EINVAL, retval, aio_error(&aiocb)); 102 return PTS_UNTESTED; 103 } 104 105 } 106 107 close(fd); 108 printf("Test PASSED\n"); 109 return PTS_PASS; 110 } 111