Home | History | Annotate | Download | only in aio_write
      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 /*
     10  * assertion:
     11  *
     12  *	The aio_write() function shall return the value zero if operation is
     13  *	successfuly queued.
     14  *
     15  * method:
     16  *
     17  *	- open file
     18  *	- write 512 bytes using aio_write
     19  *	- check return code
     20  *
     21  */
     22 
     23 #define _XOPEN_SOURCE 600
     24 #include <stdio.h>
     25 #include <sys/types.h>
     26 #include <unistd.h>
     27 #include <sys/stat.h>
     28 #include <fcntl.h>
     29 #include <string.h>
     30 #include <errno.h>
     31 #include <stdlib.h>
     32 #include <aio.h>
     33 
     34 #include "posixtest.h"
     35 
     36 #define TNAME "aio_write/5-1.c"
     37 
     38 int main(void)
     39 {
     40 	char tmpfname[256];
     41 #define BUF_SIZE 512
     42 	char buf[BUF_SIZE];
     43 	int fd;
     44 	struct aiocb aiocb;
     45 	int err;
     46 	int ret;
     47 
     48 	if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
     49 		return PTS_UNSUPPORTED;
     50 
     51 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_write_5_1_%d",
     52 		 getpid());
     53 	unlink(tmpfname);
     54 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
     55 	if (fd == -1) {
     56 		printf(TNAME " Error at open(): %s\n", strerror(errno));
     57 		exit(PTS_UNRESOLVED);
     58 	}
     59 
     60 	unlink(tmpfname);
     61 
     62 	memset(&aiocb, 0, sizeof(struct aiocb));
     63 	aiocb.aio_fildes = fd;
     64 	aiocb.aio_buf = buf;
     65 	aiocb.aio_nbytes = BUF_SIZE;
     66 
     67 	if (aio_write(&aiocb) == -1) {
     68 		printf(TNAME " Error at aio_write(): %s\n", strerror(errno));
     69 		exit(PTS_FAIL);
     70 	}
     71 
     72 	/* Wait until completion */
     73 	do {
     74 		usleep(10000);
     75 		err = aio_error(&aiocb);
     76 	} while (err == EINPROGRESS);
     77 
     78 	ret = aio_return(&aiocb);
     79 
     80 	if (err != 0) {
     81 		printf(TNAME " Error at aio_error() : %s\n", strerror(err));
     82 		close(fd);
     83 		exit(PTS_FAIL);
     84 	}
     85 
     86 	if (ret != BUF_SIZE) {
     87 		printf(TNAME " Error at aio_return()\n");
     88 		close(fd);
     89 		exit(PTS_FAIL);
     90 	}
     91 
     92 	close(fd);
     93 	printf("Test PASSED\n");
     94 	return PTS_PASS;
     95 }
     96