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() 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 _POSIX_ASYNCHRONOUS_IO != 200112L 52 exit(PTS_UNSUPPORTED); 53#endif 54 55 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_write_1_2_%d", 56 getpid()); 57 unlink(tmpfname); 58 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, 59 S_IRUSR | S_IWUSR); 60 if (fd == -1) 61 { 62 printf(TNAME " Error at open(): %s\n", 63 strerror(errno)); 64 exit(PTS_UNRESOLVED); 65 } 66 67 unlink(tmpfname); 68 69 memset(buf, 0x00, BUF_SIZE); 70 71 if (write(fd, buf, BUF_SIZE) != BUF_SIZE) 72 { 73 printf(TNAME " Error at write(): %s\n", 74 strerror(errno)); 75 exit(PTS_UNRESOLVED); 76 } 77 78 if (write(fd, buf, BUF_SIZE) != BUF_SIZE) 79 { 80 printf(TNAME " Error at write(): %s\n", 81 strerror(errno)); 82 exit(PTS_UNRESOLVED); 83 } 84 85 memset(buf, 0xaa, BUF_SIZE); 86 memset(&aiocb, 0, sizeof(struct aiocb)); 87 aiocb.aio_fildes = fd; 88 aiocb.aio_buf = buf; 89 aiocb.aio_nbytes = BUF_SIZE; 90 aiocb.aio_offset = BUF_SIZE / 2; 91 92 if (aio_write(&aiocb) == -1) 93 { 94 printf(TNAME " Error at aio_write(): %s\n", 95 strerror(errno)); 96 exit(PTS_FAIL); 97 } 98 99 /* Wait until end of transaction */ 100 while (aio_error (&aiocb) == EINPROGRESS); 101 102 err = aio_error(&aiocb); 103 ret = aio_return(&aiocb); 104 105 if (err != 0) 106 { 107 printf (TNAME " Error at aio_error() : %s\n", strerror (err)); 108 close (fd); 109 exit(PTS_FAIL); 110 } 111 112 if (ret != BUF_SIZE) 113 { 114 printf(TNAME " Error at aio_return()\n"); 115 close(fd); 116 exit(PTS_FAIL); 117 } 118 119 /* check the values written */ 120 121 if (lseek(fd, 0, SEEK_SET) == -1) 122 { 123 printf(TNAME " Error at lseek(): %s\n", 124 strerror(errno)); 125 close(fd); 126 exit(PTS_FAIL); 127 } 128 129 memset(check, 0x01, BUF_SIZE * 2); 130 if (read(fd, check, BUF_SIZE * 2) != BUF_SIZE * 2) 131 { 132 printf(TNAME " Error at read(): %s\n", 133 strerror(errno)); 134 close(fd); 135 exit(PTS_FAIL); 136 } 137 138 if (check[BUF_SIZE / 2 - 1] != 0) 139 { 140 printf(TNAME " write at bad offset\n"); 141 close(fd); 142 exit(PTS_FAIL); 143 } 144 145 if (check[BUF_SIZE / 2 + BUF_SIZE] != 0) 146 { 147 printf(TNAME " bad size written\n"); 148 close(fd); 149 exit(PTS_FAIL); 150 } 151 152 if (memcmp(buf, check + BUF_SIZE / 2, BUF_SIZE)) 153 { 154 printf(TNAME " Bad value in buffer\n"); 155 close(fd); 156 exit(PTS_FAIL); 157 } 158 159 close(fd); 160 printf ("Test PASSED\n"); 161 return PTS_PASS; 162} 163