Repository navigation
fix(logging-sink): replace libc syslog FFI with safe syslog crate - #3562
Merged
Merged
Conversation
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]`.
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
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.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
#[allow(unsafe_code)]escape hatch onlogging_sink::syslogand replace thelibc::openlog/libc::syslog/libc::closelogFFI with the safesyslogcrate (v7).libc = \"0.2\"dependency fromcrates/logging-sink/Cargo.toml; addsyslog = \"7\"under[target.'cfg(unix)'.dependencies]instead.logging_sink::syslogis unchanged:SyslogFacility,SyslogConfig,SyslogGuard,SyslogPriority,syslog_message, andDEFAULT_SYSLOG_TAGkeep 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.rsdeclares#![deny(unsafe_code)]but had#[allow(unsafe_code)]onpub mod syslog;so the module could call libc FFI directly. internal docs's unsafe-code policy restricts unsafe tometadata,fast_io,checksums,engine, andprotocol;logging-sinkis 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
syslogcrate 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::opennow builds asyslog::Formatter3164from the configured facility/tag/PID and callssyslog::unix(formatter), which connects to/dev/logon Linux and falls back to/var/run/syslogon macOS (mirroringopenlog(3)behaviour).syslog::Loggeris stashed in a process-wideOnceLock<Mutex<Option<Logger>>>;SyslogGuard::dropclears the slot to release the socket.syslog_messagelooks 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.open(e.g. no syslogd running in CI) leave the slot empty, sosyslog_messagebecomes a no-op until the next successfulopen. This means tests can run in environments without a syslog socket without panicking, matching the previous behaviour.Verification
unsafeand nolibc::references remain anywhere incrates/logging-sink/(only doc-comment mentions explaining what the crate no longer does).#![deny(unsafe_code)]stays inlib.rs; the#[allow(unsafe_code)]line on thesyslogmodule is gone.crates/logging-sink/src/syslog.rsstill target the same public API and behaviour. The two_match_libc_constantstests were replaced with ato_wire_covers_all_variantssmoke test plus apriority_covers_all_severity_levelstest, sincelibcis no longer a dependency.Test plan