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 * The aio_write() function shall write aio_nbytes to the files associated 13 * with aio_fildes from the buffer pointer to by aio_buf. 14 * 15 * method: 16 * 17 * - open file 18 * - write 2*1024 0x00 bytes using synchronous write 19 * - write 1024 0xaa bytes at offset 512 using aio_write 20 * - read 2*1024 bytes 21 * - check read data 22 * 23 */ 24 25#define _XOPEN_SOURCE 600 26#include <stdio.h> 27#include <sys/types.h> 28#include <unistd.h> 29#include <sys/stat.h> 30#include <fcntl.h> 31#include <string.h> 32#include <errno.h> 33#include <stdlib.h> 34#include <aio.h> 35 36#include "posixtest.h" 37 38#define TNAME "aio_write/1-2.c" 39 40int main(void) 41{ 42 char tmpfname[256]; 43#define BUF_SIZE 1024 44 char buf[BUF_SIZE]; 45 char check[BUF_SIZE * 2]; 46 int fd; 47 struct aiocb aiocb; 48 int err; 49 int ret; 50 51 if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L) 52 return PTS_UNSUPPORTED; 53 54 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_write_1_2_%d", 55 getpid()); 56 unlink(tmpfname); 57 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); 58 if (fd == -1) { 59 printf(TNAME " Error at open(): %s\n", strerror(errno)); 60 exit(PTS_UNRESOLVED); 61 } 62 63 unlink(tmpfname); 64 65 memset(buf, 0x00, BUF_SIZE); 66 67 if (write(fd, buf, BUF_SIZE) != BUF_SIZE) { 68 printf(TNAME " Error at write(): %s\n", strerror(errno)); 69 exit(PTS_UNRESOLVED); 70 } 71 72 if (write(fd, buf, BUF_SIZE) != BUF_SIZE) { 73 printf(TNAME " Error at write(): %s\n", strerror(errno)); 74 exit(PTS_UNRESOLVED); 75 } 76 77 memset(buf, 0xaa, BUF_SIZE); 78 memset(&aiocb, 0, sizeof(struct aiocb)); 79 aiocb.aio_fildes = fd; 80 aiocb.aio_buf = buf; 81 aiocb.aio_nbytes = BUF_SIZE; 82 aiocb.aio_offset = BUF_SIZE / 2; 83 84 if (aio_write(&aiocb) == -1) { 85 printf(TNAME " Error at aio_write(): %s\n", strerror(errno)); 86 exit(PTS_FAIL); 87 } 88 89 /* Wait until end of transaction */ 90 do { 91 usleep(10000); 92 err = aio_error(&aiocb); 93 } while (err == EINPROGRESS); 94 95 ret = aio_return(&aiocb); 96 97 if (err != 0) { 98 printf(TNAME " Error at aio_error() : %s\n", strerror(err)); 99 close(fd); 100 exit(PTS_FAIL); 101 } 102 103 if (ret != BUF_SIZE) { 104 printf(TNAME " Error at aio_return()\n"); 105 close(fd); 106 exit(PTS_FAIL); 107 } 108 109 /* check the values written */ 110 111 if (lseek(fd, 0, SEEK_SET) == -1) { 112 printf(TNAME " Error at lseek(): %s\n", strerror(errno)); 113 close(fd); 114 exit(PTS_FAIL); 115 } 116 117 memset(check, 0x01, BUF_SIZE * 2); 118 if (read(fd, check, BUF_SIZE * 2) != BUF_SIZE * 2) { 119 printf(TNAME " Error at read(): %s\n", strerror(errno)); 120 close(fd); 121 exit(PTS_FAIL); 122 } 123 124 if (check[BUF_SIZE / 2 - 1] != 0) { 125 printf(TNAME " write at bad offset\n"); 126 close(fd); 127 exit(PTS_FAIL); 128 } 129 130 if (check[BUF_SIZE / 2 + BUF_SIZE] != 0) { 131 printf(TNAME " bad size written\n"); 132 close(fd); 133 exit(PTS_FAIL); 134 } 135 136 if (memcmp(buf, check + BUF_SIZE / 2, BUF_SIZE)) { 137 printf(TNAME " Bad value in buffer\n"); 138 close(fd); 139 exit(PTS_FAIL); 140 } 141 142 close(fd); 143 printf("Test PASSED\n"); 144 return PTS_PASS; 145} 146