| 1 |
|
| 2 |
"""authenticate.c must build on supported systems without O_CLOEXEC.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import shlex |
| 6 |
import shutil |
| 7 |
import subprocess |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
from rsyncfns import SCRATCHDIR, rmtree, test_fail, test_skipped |
| 11 |
|
| 12 |
|
| 13 |
def makefile_vars(repo, names): |
| 14 |
"""Read variables out of the tree's configured Makefile. |
| 15 |
|
| 16 |
The probe has to compile the production translation unit the same way the |
| 17 |
build does. Where a dependency lives outside the default search path -- |
| 18 |
openssl from brew on macOS, say -- configure records that in CPPFLAGS, and |
| 19 |
a probe that hard-codes its own include list fails to find <openssl/sha.h> |
| 20 |
for reasons that have nothing to do with O_CLOEXEC. |
| 21 |
""" |
| 22 |
found = {} |
| 23 |
makefile = repo / "Makefile" |
| 24 |
if makefile.exists(): |
| 25 |
for line in makefile.read_text(errors="replace").splitlines(): |
| 26 |
for name in names: |
| 27 |
if line.startswith(name + "="): |
| 28 |
found[name] = line[len(name) + 1:].strip() |
| 29 |
return found |
| 30 |
|
| 31 |
|
| 32 |
repo = Path(os.environ.get( |
| 33 |
"RSYNC_SOURCE_UNDER_TEST", Path(__file__).resolve().parent.parent)) |
| 34 |
source_path = repo / "authenticate.c" |
| 35 |
config_path = repo / "config.h" |
| 36 |
if not source_path.exists() or not config_path.exists(): |
| 37 |
test_skipped(f"configured rsync source tree unavailable at {repo}") |
| 38 |
|
| 39 |
build_vars = makefile_vars(repo, ("CC", "CPPFLAGS")) |
| 40 |
|
| 41 |
|
| 42 |
def compiler(): |
| 43 |
value = os.environ.get("CC") or build_vars.get("CC") |
| 44 |
if value: |
| 45 |
return shlex.split(value) |
| 46 |
for name in ("cc", "clang", "gcc"): |
| 47 |
path = shutil.which(name) |
| 48 |
if path: |
| 49 |
return [path] |
| 50 |
return None |
| 51 |
|
| 52 |
|
| 53 |
cc = compiler() |
| 54 |
if not cc: |
| 55 |
test_skipped("no C compiler available for the O_CLOEXEC feature probe") |
| 56 |
|
| 57 |
base = SCRATCHDIR / "authenticate-no-ocloexec-build-regression" |
| 58 |
rmtree(base) |
| 59 |
base.mkdir(parents=True) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
source = source_path.read_text() |
| 64 |
needle = '#include "rsync.h"\n' |
| 65 |
if source.count(needle) != 1: |
| 66 |
test_fail(f"cannot locate feature-injection point in {source_path}") |
| 67 |
source = source.replace(needle, needle + "#undef O_CLOEXEC\n", 1) |
| 68 |
probe_c = base / "authenticate-no-ocloexec.c" |
| 69 |
probe_o = base / "authenticate-no-ocloexec.o" |
| 70 |
probe_c.write_text(source) |
| 71 |
|
| 72 |
includes = [f"-I{repo}", f"-I{repo / 'popt'}", f"-I{repo / 'zlib'}", |
| 73 |
"-DHAVE_CONFIG_H", "-O0", "-g", "-Wall", "-Wextra"] |
| 74 |
includes += shlex.split(build_vars.get("CPPFLAGS", "")) |
| 75 |
build = subprocess.run( |
| 76 |
cc + includes + ["-c", str(probe_c), "-o", str(probe_o)], |
| 77 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, |
| 78 |
) |
| 79 |
if build.returncode != 0: |
| 80 |
test_fail("authenticate.c does not compile without O_CLOEXEC:\n" + build.stdout) |
| 81 |
|
| 82 |
print("authenticate.c compiles without O_CLOEXEC") |