Home | History | Annotate | Download | only in aio_fsync
      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 #include <stdio.h>
     10 #include <sys/types.h>
     11 #include <unistd.h>
     12 #include <sys/stat.h>
     13 #include <fcntl.h>
     14 #include <string.h>
     15 #include <errno.h>
     16 #include <stdlib.h>
     17 #include <aio.h>
     18 
     19 #include "posixtest.h"
     20 
     21 #define TNAME "aio_fsync/5-1.c"
     22 
     23 #define BUF_SIZE 1024
     24 
     25 int main(void)
     26 {
     27 	char tmpfname[256];
     28 	char buf[BUF_SIZE];
     29 	int fd;
     30 	struct aiocb aiocb_write;
     31 	struct aiocb aiocb_fsync;
     32 	int ret, err;
     33 
     34 	if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
     35 		return PTS_UNSUPPORTED;
     36 
     37 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_fsync_5_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(): %s\n", strerror(errno));
     63 		exit(PTS_FAIL);
     64 	}
     65 
     66 	ret = PTS_UNTESTED;
     67 	if (aio_error(&aiocb_fsync) == EINPROGRESS)
     68 		ret = PTS_PASS;
     69 
     70 	/* allow to check if aio_error() move from EINPROGRESS to
     71 	 * something else otherwise test hangs
     72 	 */
     73 	do {
     74 		usleep(10000);
     75 		err = aio_error(&aiocb_fsync);
     76 	} while (err == EINPROGRESS);
     77 	if (err < 0) {
     78 		printf(TNAME " Error at aio_error() : %s\n", strerror(ret));
     79 		exit(PTS_FAIL);
     80 	}
     81 
     82 	close(fd);
     83 
     84 	/* we didn't check if the operation is really performed */
     85 	if (ret == PTS_PASS)
     86 		printf("Test PASSED\n");
     87 
     88 	return ret;
     89 }
     90