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  *	aio_write() shall fail or the error status of the operation shall be [EBADF] if:
     13  *	aio_fildes argument is not a valid file descriptor open for writing.
     14  *
     15  * method: Test with an invalid file descriptor opened read-only
     16  *
     17  *	- setup an aiocb with an read-only aio_fildes
     18  *	- call aio_write with this aiocb
     19  *	- check return code and errno
     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/8-2.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 ret = 0;
     46 
     47 	if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
     48 		return PTS_UNSUPPORTED;
     49 
     50 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_write_8_2_%d",
     51 		 getpid());
     52 	unlink(tmpfname);
     53 	fd = open(tmpfname, O_CREAT | O_RDONLY | O_EXCL, S_IRUSR | S_IWUSR);
     54 	if (fd == -1) {
     55 		printf(TNAME " Error at open(): %s\n", strerror(errno));
     56 		exit(PTS_UNRESOLVED);
     57 	}
     58 
     59 	unlink(tmpfname);
     60 
     61 	memset(buf, 0xaa, BUF_SIZE);
     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 	/*
     68 	 * EBADF is encountered at a later stage
     69 	 * and should be collected by aio_error()
     70 	 */
     71 
     72 	if (aio_write(&aiocb) != 0) {
     73 		printf(TNAME " bad aio_write return value()\n");
     74 		exit(PTS_FAIL);
     75 	}
     76 
     77 	do {
     78 		usleep(10000);
     79 		ret = aio_error(&aiocb);
     80 	} while (ret == EINPROGRESS);
     81 
     82 	if (ret != EBADF) {
     83 		printf(TNAME " errno is not EBADF %s\n", strerror(errno));
     84 		exit(PTS_FAIL);
     85 	}
     86 
     87 	close(fd);
     88 	printf("Test PASSED\n");
     89 	return PTS_PASS;
     90 }
     91