-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect the running libc at startup & select the correct policy file based on this. For musllinux, we select only one musllinux policy depending on the running version of musl.
- Loading branch information
Showing
7 changed files
with
273 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AuditwheelException(Exception): | ||
pass | ||
|
||
|
||
class InvalidLibc(AuditwheelException): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import logging | ||
from enum import IntEnum | ||
|
||
from .error import InvalidLibc | ||
from .musllinux import find_musl_libc | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Libc(IntEnum): | ||
GLIBC = 1, | ||
MUSL = 2, | ||
|
||
|
||
def get_libc() -> Libc: | ||
try: | ||
find_musl_libc() | ||
logger.debug("Detected musl libc") | ||
return Libc.MUSL | ||
except InvalidLibc: | ||
logger.debug("Falling back to GNU libc") | ||
return Libc.GLIBC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import logging | ||
import pathlib | ||
import subprocess | ||
import re | ||
from typing import NamedTuple | ||
|
||
from auditwheel.error import InvalidLibc | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class MuslVersion(NamedTuple): | ||
major: int | ||
minor: int | ||
patch: int | ||
|
||
|
||
def find_musl_libc() -> pathlib.Path: | ||
try: | ||
ldd = subprocess.check_output(["ldd", "/bin/ls"], errors='strict') | ||
except (subprocess.CalledProcessError, FileNotFoundError): | ||
LOG.error("Failed to determine libc version", exc_info=True) | ||
raise InvalidLibc | ||
|
||
match = re.search( | ||
r"libc\.musl-(?P<platform>\w+)\.so.1 " # TODO drop the platform | ||
r"=> (?P<path>[/\-\w.]+)", | ||
ldd) | ||
|
||
if not match: | ||
raise InvalidLibc | ||
|
||
return pathlib.Path(match.group("path")) | ||
|
||
|
||
def get_musl_version(ld_path: pathlib.Path) -> MuslVersion: | ||
try: | ||
ld = subprocess.run( | ||
[ld_path], | ||
check=False, | ||
errors='strict', | ||
stderr=subprocess.PIPE | ||
).stderr | ||
except FileNotFoundError: | ||
LOG.error("Failed to determine musl version", exc_info=True) | ||
raise InvalidLibc | ||
|
||
match = re.search( | ||
r"Version " | ||
r"(?P<major>\d+)." | ||
r"(?P<minor>\d+)." | ||
r"(?P<patch>\d+)", | ||
ld) | ||
if not match: | ||
raise InvalidLibc | ||
|
||
return MuslVersion( | ||
int(match.group("major")), | ||
int(match.group("minor")), | ||
int(match.group("patch"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[ | ||
{"name": "linux", | ||
"aliases": [], | ||
"priority": 0, | ||
"symbol_versions": {}, | ||
"lib_whitelist": [] | ||
}, | ||
{"name": "musllinux_1_1", | ||
"aliases": [], | ||
"priority": 100, | ||
"symbol_versions": { | ||
"i686": { | ||
}, | ||
"x86_64": { | ||
}, | ||
"aarch64": { | ||
}, | ||
"ppc64le": { | ||
}, | ||
"s390x": { | ||
}, | ||
"armv7l": { | ||
} | ||
}, | ||
"lib_whitelist": [ | ||
"libc.so" | ||
]}, | ||
{"name": "musllinux_1_2", | ||
"aliases": [], | ||
"priority": 90, | ||
"symbol_versions": { | ||
"i686": { | ||
}, | ||
"x86_64": { | ||
}, | ||
"aarch64": { | ||
}, | ||
"ppc64le": { | ||
}, | ||
"s390x": { | ||
}, | ||
"armv7l": { | ||
} | ||
}, | ||
"lib_whitelist": [ | ||
"libc.so" | ||
]} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import subprocess | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from auditwheel.musllinux import find_musl_libc, get_musl_version | ||
from auditwheel.error import InvalidLibc | ||
|
||
|
||
@patch("auditwheel.musllinux.subprocess.check_output") | ||
def test_find_musllinux_no_ldd(check_output_mock): | ||
check_output_mock.side_effect = FileNotFoundError() | ||
with pytest.raises(InvalidLibc): | ||
find_musl_libc() | ||
|
||
|
||
@patch("auditwheel.musllinux.subprocess.check_output") | ||
def test_find_musllinux_ldd_error(check_output_mock): | ||
check_output_mock.side_effect = subprocess.CalledProcessError(1, "ldd") | ||
with pytest.raises(InvalidLibc): | ||
find_musl_libc() | ||
|
||
|
||
@patch("auditwheel.musllinux.subprocess.check_output") | ||
def test_find_musllinux_not_found(check_output_mock): | ||
check_output_mock.return_value = "" | ||
with pytest.raises(InvalidLibc): | ||
find_musl_libc() | ||
|
||
|
||
def test_get_musl_version_invalid_path(): | ||
with pytest.raises(InvalidLibc): | ||
get_musl_version("/tmp/no/executable/here") | ||
|
||
|
||
@patch("auditwheel.musllinux.subprocess.run") | ||
def test_get_musl_version_invalid_version(run_mock): | ||
run_mock.return_value = subprocess.CompletedProcess([], 1, None, "Version 1.1") | ||
with pytest.raises(InvalidLibc): | ||
get_musl_version("anything") | ||
|
||
|
||
@patch("auditwheel.musllinux.subprocess.run") | ||
def test_get_musl_version_valid_version(run_mock): | ||
run_mock.return_value = subprocess.CompletedProcess([], 1, None, "Version 5.6.7") | ||
version = get_musl_version("anything") | ||
assert version.major == 5 | ||
assert version.minor == 6 | ||
assert version.patch == 7 |