1 /* 2 * Copyright (c) 2004, Bull SA. All rights reserved. 3 * Copyright (c) 2011, Wanlong Gao <gaowanlong (at) cn.fujitsu.com> 4 * Created by: Laurent.Vivier (at) bull.net 5 * This file is licensed under the GPL license. For the full content 6 * of this license, see the COPYING file at the top level of this 7 * source tree. 8 */ 9 10 #include <stdio.h> 11 #include <sys/types.h> 12 #include <unistd.h> 13 #include <sys/stat.h> 14 #include <fcntl.h> 15 #include <string.h> 16 #include <errno.h> 17 #include <stdlib.h> 18 #include <aio.h> 19 20 #include "posixtest.h" 21 22 #define TNAME "aio_fsync/4-1.c" 23 24 int main(void) 25 { 26 char tmpfname[256]; 27 #define BUF_SIZE 111 28 char buf[BUF_SIZE]; 29 int fd; 30 int ret = 0; 31 struct aiocb aiocb_write; 32 struct aiocb aiocb_fsync; 33 34 if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L) 35 return PTS_UNSUPPORTED; 36 37 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_fsync_4_1_%d", 38 getpid()); 39 unlink(tmpfname); 40 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); 41 if (fd == -1) { 42 printf(TNAME " Error at open(): %s\n", strerror(errno)); 43 exit(PTS_UNRESOLVED); 44 } 45 46 unlink(tmpfname); 47 48 memset(&aiocb_write, 0, sizeof(aiocb_write)); 49 aiocb_write.aio_fildes = fd; 50 aiocb_write.aio_buf = buf; 51 aiocb_write.aio_nbytes = BUF_SIZE; 52 53 if (aio_write(&aiocb_write) == -1) { 54 printf(TNAME " Error at aio_write(): %s\n", strerror(errno)); 55 exit(PTS_FAIL); 56 } 57 58 memset(&aiocb_fsync, 0, sizeof(aiocb_fsync)); 59 aiocb_fsync.aio_fildes = fd; 60 61 if (aio_fsync(O_SYNC, &aiocb_fsync) != 0) { 62 printf(TNAME " Error at aio_fsync()\n"); 63 exit(PTS_FAIL); 64 } 65 66 do { 67 usleep(10000); 68 ret = aio_error(&aiocb_fsync); 69 } while (ret == EINPROGRESS); 70 if (ret < 0) { 71 printf(TNAME " Error at aio_error() : %s\n", strerror(ret)); 72 exit(PTS_FAIL); 73 } 74 75 /* Upon successful completion, fsync() shall return 0. 76 * Otherwise, -1 shall be returned and errno set to indicate the error. 77 */ 78 if (aio_return(&aiocb_fsync)) { 79 printf(TNAME " Error at aio_return()\n"); 80 exit(PTS_FAIL); 81 } 82 83 close(fd); 84 printf("Test PASSED\n"); 85 return PTS_PASS; 86 } 87