From 9d1e6c8c8e1257195f99111bc4df8525c7cbabe8 Mon Sep 17 00:00:00 2001 From: Cameron Bytheway Date: Tue, 19 Sep 2023 16:00:44 -0600 Subject: [PATCH] build: use feature probes for CLOEXEC (#4206) --- tests/features/S2N_CLOEXEC_SUPPORTED.c | 20 +++++++++++++++++ tests/features/S2N_CLOEXEC_SUPPORTED.flags | 1 + tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.c | 22 +++++++++++++++++++ .../S2N_CLOEXEC_XOPEN_SUPPORTED.flags | 1 + utils/s2n_random.c | 8 +++++-- 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/features/S2N_CLOEXEC_SUPPORTED.c create mode 100644 tests/features/S2N_CLOEXEC_SUPPORTED.flags create mode 100644 tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.c create mode 100644 tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.flags diff --git a/tests/features/S2N_CLOEXEC_SUPPORTED.c b/tests/features/S2N_CLOEXEC_SUPPORTED.c new file mode 100644 index 00000000000..2e6e7d003b1 --- /dev/null +++ b/tests/features/S2N_CLOEXEC_SUPPORTED.c @@ -0,0 +1,20 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +#include + +int main() { + return O_CLOEXEC; +} diff --git a/tests/features/S2N_CLOEXEC_SUPPORTED.flags b/tests/features/S2N_CLOEXEC_SUPPORTED.flags new file mode 100644 index 00000000000..2f41be663b2 --- /dev/null +++ b/tests/features/S2N_CLOEXEC_SUPPORTED.flags @@ -0,0 +1 @@ +-Werror diff --git a/tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.c b/tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.c new file mode 100644 index 00000000000..ed2b748d8e5 --- /dev/null +++ b/tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.c @@ -0,0 +1,22 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +#define _XOPEN_SOURCE 700 +#include +#undef _XOPEN_SOURCE + +int main() { + return O_CLOEXEC; +} diff --git a/tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.flags b/tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.flags new file mode 100644 index 00000000000..2f41be663b2 --- /dev/null +++ b/tests/features/S2N_CLOEXEC_XOPEN_SUPPORTED.flags @@ -0,0 +1 @@ +-Werror diff --git a/utils/s2n_random.c b/utils/s2n_random.c index 485229dfdcf..6fc3d27ad72 100644 --- a/utils/s2n_random.c +++ b/utils/s2n_random.c @@ -28,13 +28,17 @@ * 700. In glibc 2.11 and earlier, one obtains the definitions by * defining _GNU_SOURCE. * - * # Relavent Links + * We use two feature probes to detect the need to perform this workaround. + * It is only applied if we can't get CLOEXEC without it and the build doesn't + * fail with _XOPEN_SOURCE being defined. + * + * # Relevent Links * * - POSIX.1-2017: https://pubs.opengroup.org/onlinepubs/9699919799 * - https://stackoverflow.com/a/5724485 * - https://stackoverflow.com/a/5583764 */ -#ifndef _XOPEN_SOURCE +#if !defined(S2N_CLOEXEC_SUPPORTED) && defined(S2N_CLOEXEC_XOPEN_SUPPORTED) && !defined(_XOPEN_SOURCE) #define _XOPEN_SOURCE 700 #include #undef _XOPEN_SOURCE