Skip to content

Commit b7c1014

Browse files
committed
fix(generic-snmp): fix glibc dependency issue
1 parent 1d2a21a commit b7c1014

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

.github/workflows/generic-plugins.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,30 @@ jobs:
4646
cargo test
4747
shell: bash
4848

49+
# The binary is built once and packaged for every supported distribution, so it must not
50+
# depend on the glibc of the runner: the oldest distribution we ship to (el8) provides
51+
# glibc 2.28 while ubuntu-24.04 provides 2.39, and glibc offers no forward compatibility.
52+
# Linking statically against musl removes the dependency on the system libc entirely.
4953
- name: Build optimized binary
5054
run: |
5155
cd rust-plugins
52-
cargo build --release
56+
rustup target add x86_64-unknown-linux-musl
57+
cargo build --release --target x86_64-unknown-linux-musl
58+
# Keep the artifact where the cache, the nfpm configuration and the test job expect it.
59+
mkdir -p target/release
60+
cp target/x86_64-unknown-linux-musl/release/centreon-plugin-rust-snmp target/release/
61+
shell: bash
62+
63+
- name: Check the binary has no dynamic dependency
64+
run: |
65+
cd rust-plugins
66+
# A statically linked binary has no NEEDED entry. Do not rely on `file`, which reports
67+
# "static-pie linked" rather than "statically linked" for this target.
68+
if objdump -p target/release/centreon-plugin-rust-snmp | grep NEEDED; then
69+
echo "::error::Binary links against shared libraries and will not run on every supported distribution"
70+
exit 1
71+
fi
72+
echo "Binary is statically linked, it does not depend on the system libc"
5373
shell: bash
5474

5575
- name: Save binary to cache

rust-plugins/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ Once done, you can compile it by running:
99
cargo build
1010
```
1111

12+
# Static linking
13+
14+
`cargo build` links the binary against the glibc of your own machine. The packaged binary has to
15+
run on every supported distribution, the oldest one being el8 with glibc 2.28, and glibc offers
16+
no forward compatibility: a binary built against a recent glibc fails to start on an older one.
17+
The CI therefore links statically against musl instead:
18+
19+
```bash
20+
rustup target add x86_64-unknown-linux-musl
21+
cargo build --release --target x86_64-unknown-linux-musl
22+
```
23+
24+
Use that command to reproduce a release binary locally. The CI fails the build if the produced
25+
binary still carries a dynamic dependency.
26+
27+
A statically linked musl binary does not use the glibc Name Service Switch (NSS). Host names are
28+
resolved by the musl resolver, which reads `/etc/hosts` and `/etc/resolv.conf` only, so the
29+
`nsswitch.conf` backends such as sssd, LDAP or mDNS are ignored, unlike with the Perl plugins.
30+
Targets given as IP addresses are unaffected, and so are host names resolvable through plain DNS.
31+
1232
# Description
1333

1434
## generic-snmp

0 commit comments

Comments
 (0)