robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 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 |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 5 | * of this license, see the COPYING file at the top level of this |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 6 | * source tree. |
| 7 | */ |
| 8 | |
| 9 | #define _XOPEN_SOURCE 600 |
| 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/8-1.c" |
| 23 | |
Cyril Hrubis | 4ca2bbd | 2013-03-25 17:26:44 +0100 | [diff] [blame] | 24 | int main(void) |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 25 | { |
| 26 | char tmpfname[256]; |
| 27 | #define BUF_SIZE 111 |
| 28 | char buf[BUF_SIZE]; |
| 29 | int fd; |
Wanlong Gao | 580f583 | 2012-07-24 10:51:38 +0800 | [diff] [blame] | 30 | int ret; |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 31 | struct aiocb aiocb_write; |
| 32 | struct aiocb aiocb_fsync; |
| 33 | |
Garrett Cooper | 264074b | 2010-12-21 12:25:33 -0800 | [diff] [blame] | 34 | if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L) |
Garrett Cooper | ec55578 | 2010-07-10 21:16:48 -0700 | [diff] [blame] | 35 | return PTS_UNSUPPORTED; |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 36 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 37 | snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_fsync_8_1_%d", |
Wanlong Gao | 074ada6 | 2012-12-07 09:43:49 +0800 | [diff] [blame] | 38 | getpid()); |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 39 | unlink(tmpfname); |
Wanlong Gao | 074ada6 | 2012-12-07 09:43:49 +0800 | [diff] [blame] | 40 | fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); |
Wanlong Gao | aa22e58 | 2012-07-24 14:44:02 +0800 | [diff] [blame] | 41 | if (fd == -1) { |
Wanlong Gao | 074ada6 | 2012-12-07 09:43:49 +0800 | [diff] [blame] | 42 | printf(TNAME " Error at open(): %s\n", strerror(errno)); |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 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 | |
Wanlong Gao | aa22e58 | 2012-07-24 14:44:02 +0800 | [diff] [blame] | 53 | if (aio_write(&aiocb_write) == -1) { |
Wanlong Gao | 074ada6 | 2012-12-07 09:43:49 +0800 | [diff] [blame] | 54 | printf(TNAME " Error at aio_write(): %s\n", strerror(errno)); |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 55 | exit(PTS_FAIL); |
| 56 | } |
| 57 | |
Wanlong Gao | 580f583 | 2012-07-24 10:51:38 +0800 | [diff] [blame] | 58 | do { |
| 59 | usleep(10000); |
| 60 | ret = aio_error(&aiocb_write); |
| 61 | } while (ret == EINPROGRESS); |
| 62 | if (ret < 0) { |
| 63 | printf(TNAME " Error at aio_error() : %s\n", strerror(ret)); |
| 64 | exit(PTS_FAIL); |
| 65 | } |
| 66 | |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 67 | memset(&aiocb_fsync, 0, sizeof(aiocb_fsync)); |
| 68 | aiocb_fsync.aio_fildes = fd; |
| 69 | aiocb_fsync.aio_nbytes = -1; |
| 70 | |
Wanlong Gao | aa22e58 | 2012-07-24 14:44:02 +0800 | [diff] [blame] | 71 | if (aio_fsync(O_SYNC, &aiocb_fsync) != 0) { |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 72 | printf(TNAME " Error at aio_fsync(): %s\n", strerror(errno)); |
| 73 | exit(PTS_FAIL); |
| 74 | } |
| 75 | |
Jan Stancek | 4aca18c | 2013-08-12 13:03:25 +0200 | [diff] [blame^] | 76 | /* wait for aio_fsync */ |
| 77 | do { |
| 78 | usleep(10000); |
| 79 | ret = aio_error(&aiocb_fsync); |
| 80 | } while (ret == EINPROGRESS); |
| 81 | |
| 82 | ret = aio_return(&aiocb_fsync); |
| 83 | if (ret) { |
| 84 | printf(TNAME " Error at aio_return(): %d (%s)\n", |
| 85 | ret, strerror(errno)); |
| 86 | close(fd); |
| 87 | return PTS_FAIL; |
| 88 | } |
| 89 | |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 90 | close(fd); |
Wanlong Gao | aa22e58 | 2012-07-24 14:44:02 +0800 | [diff] [blame] | 91 | printf("Test PASSED\n"); |
robbiew | 0dc0765 | 2005-06-03 16:29:48 +0000 | [diff] [blame] | 92 | return PTS_PASS; |
Wanlong Gao | aa22e58 | 2012-07-24 14:44:02 +0800 | [diff] [blame] | 93 | } |