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
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 aiocbp argument points to the AIO control block to be canceled.
13 *
14 * method:
15 *
16 *	- create a valid aiocb with a call to aio_write()
17 *	- call aio_cancel() with this aiocb and check return value is not -1
18 *	-> aio_cancel() works with a valid (finished or not) aiocb
19 *
20 */
21
22#define _XOPEN_SOURCE 600
23#include <stdio.h>
24#include <sys/types.h>
25#include <unistd.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <string.h>
29#include <errno.h>
30#include <stdlib.h>
31#include <aio.h>
32
33#include "posixtest.h"
34
35#define TNAME "aio_cancel/1-1.c"
36
37int main()
38{
39	char tmpfname[256];
40#define BUF_SIZE 1024
41	char buf[BUF_SIZE];
42	int fd;
43	struct aiocb aiocb;
44
45#if _POSIX_ASYNCHRONOUS_IO != 200112L
46	return PTS_UNSUPPORTED;
47#endif
48
49	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_cancel_1_1_%d",
50		  getpid());
51	unlink(tmpfname);
52	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
53		  S_IRUSR | S_IWUSR);
54	if (fd == -1)
55	{
56		printf(TNAME " Error at open(): %s\n",
57		       strerror(errno));
58		return PTS_UNRESOLVED;
59	}
60
61	unlink(tmpfname);
62
63	memset(buf, 0xaa, BUF_SIZE);
64	memset(&aiocb, 0, sizeof(struct aiocb));
65	aiocb.aio_fildes = fd;
66	aiocb.aio_buf = buf;
67	aiocb.aio_nbytes = BUF_SIZE;
68
69	if (aio_write(&aiocb) == -1)
70	{
71		printf(TNAME " Error at aio_write(): %s\n",
72		       strerror(errno));
73		return PTS_FAIL;
74	}
75
76	if (aio_cancel(fd, &aiocb) == -1)
77	{
78		printf(TNAME " Error at aio_cancel(): %s\n",
79		       strerror(errno));
80		return PTS_FAIL;
81	}
82
83	close(fd);
84	printf ("Test PASSED\n");
85	return PTS_PASS;
86}
87