xref: /freebsd/tests/sys/aio/local.h (revision b3e76948)
1f3215338SJohn Baldwin /*-
2f3215338SJohn Baldwin  * Copyright (c) 2016 Chelsio Communications, Inc.
3f3215338SJohn Baldwin  * All rights reserved.
4f3215338SJohn Baldwin  * Written by: John Baldwin <jhb@FreeBSD.org>
5f3215338SJohn Baldwin  *
6f3215338SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
7f3215338SJohn Baldwin  * modification, are permitted provided that the following conditions
8f3215338SJohn Baldwin  * are met:
9f3215338SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
10f3215338SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
11f3215338SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
12f3215338SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
13f3215338SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
14f3215338SJohn Baldwin  *
15f3215338SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16f3215338SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f3215338SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f3215338SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f3215338SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f3215338SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f3215338SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f3215338SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f3215338SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f3215338SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f3215338SJohn Baldwin  * SUCH DAMAGE.
26f3215338SJohn Baldwin  */
27f3215338SJohn Baldwin 
28f3215338SJohn Baldwin #ifndef _AIO_TEST_LOCAL_H_
29f3215338SJohn Baldwin #define	_AIO_TEST_LOCAL_H_
30f3215338SJohn Baldwin 
31f3215338SJohn Baldwin #include <sys/types.h>
32f3215338SJohn Baldwin #include <sys/sysctl.h>
33f3215338SJohn Baldwin #include <errno.h>
34f3215338SJohn Baldwin #include <stdio.h>
35f3215338SJohn Baldwin #include <string.h>
36f3215338SJohn Baldwin #include <unistd.h>
37f3215338SJohn Baldwin 
38f3215338SJohn Baldwin #include <atf-c.h>
39f3215338SJohn Baldwin 
405fec77f1SEnji Cooper static const char	*sysctl_oid_name = "vfs.aio.enable_unsafe";
415fec77f1SEnji Cooper 
425fec77f1SEnji Cooper static int
is_unsafe_aio_enabled(void)435fec77f1SEnji Cooper is_unsafe_aio_enabled(void)
445fec77f1SEnji Cooper {
455fec77f1SEnji Cooper 	size_t len;
465fec77f1SEnji Cooper 	int unsafe;
475fec77f1SEnji Cooper 
485fec77f1SEnji Cooper 	len = sizeof(unsafe);
495fec77f1SEnji Cooper 	if (sysctlbyname(sysctl_oid_name, &unsafe, &len, NULL, 0) < 0) {
505fec77f1SEnji Cooper 		if (errno == ENOENT)
515fec77f1SEnji Cooper 			return (-1);
525fec77f1SEnji Cooper 		return (0);
535fec77f1SEnji Cooper 	}
545fec77f1SEnji Cooper 	return (unsafe == 0 ? 0 : 1);
555fec77f1SEnji Cooper }
565fec77f1SEnji Cooper 
57f3215338SJohn Baldwin #define	ATF_REQUIRE_UNSAFE_AIO() do {						\
585fec77f1SEnji Cooper 	switch (is_unsafe_aio_enabled()) {					\
595fec77f1SEnji Cooper 	case -1:								\
605fec77f1SEnji Cooper 		atf_libc_error(errno, "Failed to read %s", sysctl_oid_name);	\
615fec77f1SEnji Cooper 		break;								\
625fec77f1SEnji Cooper 	case 0:									\
63f3215338SJohn Baldwin 		atf_tc_skip("Unsafe AIO is disabled");				\
645fec77f1SEnji Cooper 		break;								\
655fec77f1SEnji Cooper 	default:								\
66175e856fSEnji Cooper 		printf("Unsafe AIO is enabled\n");				\
675fec77f1SEnji Cooper 		break;								\
685fec77f1SEnji Cooper 	}									\
69f3215338SJohn Baldwin } while (0)
70f3215338SJohn Baldwin 
71f3215338SJohn Baldwin #define	PLAIN_REQUIRE_UNSAFE_AIO(_exit_code) do {				\
725fec77f1SEnji Cooper 	switch (is_unsafe_aio_enabled()) {					\
735fec77f1SEnji Cooper 	case -1:								\
745fec77f1SEnji Cooper 		printf("Failed to read %s", sysctl_oid_name);			\
75f3215338SJohn Baldwin 		_exit(_exit_code);						\
765fec77f1SEnji Cooper 		break;								\
775fec77f1SEnji Cooper 	case 0:									\
785fec77f1SEnji Cooper 		printf("Unsafe AIO is disabled\n");				\
795fec77f1SEnji Cooper 		_exit(_exit_code);						\
805fec77f1SEnji Cooper 		break;								\
815fec77f1SEnji Cooper 	default:								\
82175e856fSEnji Cooper 		printf("Unsafe AIO is enabled\n");				\
835fec77f1SEnji Cooper 		break;								\
84f3215338SJohn Baldwin 	}									\
85f3215338SJohn Baldwin } while (0)
86f3215338SJohn Baldwin 
87f3215338SJohn Baldwin #endif /* !_AIO_TEST_LOCAL_H_ */
88