Skip to content

fix(logging-sink): replace libc syslog FFI with safe syslog crate - #3562

Merged
oferchen merged 2 commits into
masterfrom
fix/logging-sink-tracing-syslog
May 2, 2026
Merged

oferchen merged 2 commits into
masterfrom
fix/logging-sink-tracing-syslog

Conversation

@oferchen

@oferchen oferchen commented May 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Remove the #[allow(unsafe_code)] escape hatch on logging_sink::syslog and replace the libc::openlog/libc::syslog/libc::closelog FFI with the safe syslog crate (v7).
  • Drop the libc = \"0.2\" dependency from crates/logging-sink/Cargo.toml; add syslog = \"7\" under [target.'cfg(unix)'.dependencies] instead.
  • Public API of logging_sink::syslog is unchanged: SyslogFacility, SyslogConfig, SyslogGuard, SyslogPriority, syslog_message, and DEFAULT_SYSLOG_TAG keep their names, signatures, and semantics. The only call site (crates/daemon/src/daemon/sections/server_runtime/accept_loop.rs:73-81) needs no updates.

Why

crates/logging-sink/src/lib.rs declares #![deny(unsafe_code)] but had #[allow(unsafe_code)] on pub mod syslog; so the module could call libc FFI directly. internal docs's unsafe-code policy restricts unsafe to metadata, fast_io, checksums, engine, and protocol; logging-sink is not on that allow-list.

The same policy says: "prefer safe wrapper crates ... or move the unsafe code into one of the permitted crates above and expose a safe public API." The syslog crate speaks the BSD/RFC 3164 wire protocol directly over a Unix socket - zero libc FFI, zero unsafe blocks - which is the cleanest fix.

Implementation notes

  • SyslogConfig::open now builds a syslog::Formatter3164 from the configured facility/tag/PID and calls syslog::unix(formatter), which connects to /dev/log on Linux and falls back to /var/run/syslog on macOS (mirroring openlog(3) behaviour).
  • The opened syslog::Logger is stashed in a process-wide OnceLock<Mutex<Option<Logger>>>; SyslogGuard::drop clears the slot to release the socket.
  • syslog_message looks the slot up and dispatches to the matching severity method (emerg/alert/crit/err/warning/notice/info/debug). Errors from the underlying socket write are swallowed so a transient logging failure never tears down the daemon - same opportunistic semantics as the old libc path.
  • Connection failures during open (e.g. no syslogd running in CI) leave the slot empty, so syslog_message becomes a no-op until the next successful open. This means tests can run in environments without a syslog socket without panicking, matching the previous behaviour.

Verification

  • No unsafe and no libc:: references remain anywhere in crates/logging-sink/ (only doc-comment mentions explaining what the crate no longer does).
  • #![deny(unsafe_code)] stays in lib.rs; the #[allow(unsafe_code)] line on the syslog module is gone.
  • All existing tests in crates/logging-sink/src/syslog.rs still target the same public API and behaviour. The two _match_libc_constants tests were replaced with a to_wire_covers_all_variants smoke test plus a priority_covers_all_severity_levels test, since libc is no longer a dependency.

Test plan

  • CI fmt+clippy passes on the branch.
  • CI nextest passes on Linux musl, macOS, Windows.
  • Daemon accept-loop test coverage stays green (no public-API changes).

The crate declared `#![deny(unsafe_code)]` but carried an
`#[allow(unsafe_code)]` escape hatch on `pub mod syslog` so it could
call `libc::openlog`/`libc::syslog`/`libc::closelog`. That violates the
unsafe-code policy in CLAUDE.md, which restricts unsafe to
metadata/fast_io/checksums/engine/protocol and asks us to prefer safe
wrapper crates for everything else.

Switch the implementation to the `syslog` crate (v7), which speaks the
BSD/RFC 3164 wire protocol directly over a Unix socket - no libc FFI,
no unsafe blocks. The crate's `unix(formatter)` constructor connects
to `/dev/log` (and falls back to `/var/run/syslog` on macOS), matching
upstream openlog(3) behaviour.

The public API of `logging_sink::syslog` is unchanged: `SyslogFacility`,
`SyslogConfig`, `SyslogGuard`, `SyslogPriority`, `syslog_message`, and
`DEFAULT_SYSLOG_TAG` keep their names, signatures, and semantics, so
the daemon accept-loop call site needs no updates. The opened
`syslog::Logger` lives in a process-wide
`OnceLock<Mutex<Option<Logger>>>` populated by `SyslogConfig::open` and
cleared when the returned `SyslogGuard` is dropped, preserving the
existing "open returns a guard, free function logs through it" shape.

After this change the crate builds cleanly with `#![deny(unsafe_code)]`
and no `#[allow(unsafe_code)]` overrides remain. The `libc` dependency
is removed; `syslog = "7"` replaces it under
`[target.'cfg(unix)'.dependencies]`.
@github-actions github-actions Bot added the bug Something isn't working label May 2, 2026
CI failed with 'Cargo.lock needs to be updated but --locked was passed'.
Adds syslog v7.0.0 and its hostname v0.4.2 transitive dep to the lockfile.
Both crates are MIT-licensed and pass cargo-deny.
@oferchen
oferchen merged commit a663283 into master May 2, 2026
38 checks passed
@oferchen
oferchen deleted the fix/logging-sink-tracing-syslog branch May 2, 2026 16:54
oferchen added a commit that referenced this pull request May 5, 2026
)

* fix(logging-sink): replace libc syslog FFI with safe syslog crate

The crate declared `#![deny(unsafe_code)]` but carried an
`#[allow(unsafe_code)]` escape hatch on `pub mod syslog` so it could
call `libc::openlog`/`libc::syslog`/`libc::closelog`. That violates the
unsafe-code policy in <internal-tool-doc>, which restricts unsafe to
metadata/fast_io/checksums/engine/protocol and asks us to prefer safe
wrapper crates for everything else.

Switch the implementation to the `syslog` crate (v7), which speaks the
BSD/RFC 3164 wire protocol directly over a Unix socket - no libc FFI,
no unsafe blocks. The crate's `unix(formatter)` constructor connects
to `/dev/log` (and falls back to `/var/run/syslog` on macOS), matching
upstream openlog(3) behaviour.

The public API of `logging_sink::syslog` is unchanged: `SyslogFacility`,
`SyslogConfig`, `SyslogGuard`, `SyslogPriority`, `syslog_message`, and
`DEFAULT_SYSLOG_TAG` keep their names, signatures, and semantics, so
the daemon accept-loop call site needs no updates. The opened
`syslog::Logger` lives in a process-wide
`OnceLock<Mutex<Option<Logger>>>` populated by `SyslogConfig::open` and
cleared when the returned `SyslogGuard` is dropped, preserving the
existing "open returns a guard, free function logs through it" shape.

After this change the crate builds cleanly with `#![deny(unsafe_code)]`
and no `#[allow(unsafe_code)]` overrides remain. The `libc` dependency
is removed; `syslog = "7"` replaces it under
`[target.'cfg(unix)'.dependencies]`.

* chore(lockfile): refresh after syslog crate swap

CI failed with 'Cargo.lock needs to be updated but --locked was passed'.
Adds syslog v7.0.0 and its hostname v0.4.2 transitive dep to the lockfile.
Both crates are MIT-licensed and pass cargo-deny.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant