Skip to content

Commit

Permalink
Use rustix instead of libc
Browse files Browse the repository at this point in the history
rustix provides safe alternatives to everything we do with libc and
lets us get rid of a bunch of weird code.
  • Loading branch information
swsnr committed Oct 19, 2023
1 parent a959325 commit de6e118
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 172 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Run [`cargo-release`][cr] to publish a release.

## [Unreleased]

### Changed
- Depend on rustix instead of libc to get rid of unsafe code (see [GH-24]).

[GH-24]: https://github.com/swsnr/systemd-journal-logger.rs/pull/24

## [2.0.0] – 2023-10-01

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rust-version = "1.71"
# feature unconditionally. We require std for the set_boxed_logger field in
# init_with_extra_fields.
log = { version = "^0.4", features = ["std", "kv_unstable"] }
libc = "0.2.148"
rustix = { version = "0.38.19", default-features = false, features = ["std", "fs", "net"] }

[dev-dependencies]
similar-asserts = "1.5.0"
Expand Down
39 changes: 34 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@

//! A journald client.
use std::fs::File;
use std::io::prelude::*;
use std::os::fd::AsFd;
use std::os::unix::net::UnixDatagram;

use crate::{memfd, socket};
use rustix::fs::fcntl_add_seals;
use rustix::fs::memfd_create;
use rustix::fs::MemfdFlags;
use rustix::fs::SealFlags;
use rustix::io::Errno;
use rustix::net::sendmsg_unix;
use rustix::net::SendAncillaryBuffer;
use rustix::net::SendFlags;
use rustix::net::SocketAddrUnix;

const JOURNALD_PATH: &str = "/run/systemd/journal/socket";

Expand All @@ -39,7 +48,7 @@ impl JournalClient {
self.socket
.send_to(payload, JOURNALD_PATH)
.or_else(|error| {
if Some(libc::EMSGSIZE) == error.raw_os_error() {
if Some(Errno::MSGSIZE) == Errno::from_io_error(&error) {
self.send_large_payload(payload)
} else {
Err(error)
Expand All @@ -51,11 +60,31 @@ impl JournalClient {
// If the payload's too large for a single datagram, send it through a memfd, see
// https://systemd.io/JOURNAL_NATIVE_PROTOCOL/
// Write the whole payload to a memfd
let mut mem = memfd::create_sealable()?;
let mut mem: File = memfd_create(
"systemd-journal-logger",
MemfdFlags::ALLOW_SEALING | MemfdFlags::CLOEXEC,
)?
.into();
mem.write_all(payload)?;
// Fully seal the memfd to signal journald that its backing data won't resize anymore
// and so is safe to mmap.
memfd::seal_fully(mem.as_fd())?;
socket::send_one_fd_to(&self.socket, mem.as_fd(), JOURNALD_PATH)
fcntl_add_seals(
&mem,
SealFlags::SEAL | SealFlags::SHRINK | SealFlags::WRITE | SealFlags::GROW,
)?;
// Send the FD over to journald.
let fds = &[mem.as_fd()];
let scm_rights = rustix::net::SendAncillaryMessage::ScmRights(fds);
let mut buffer = vec![0; scm_rights.size()];
let mut buffer = SendAncillaryBuffer::new(&mut buffer);
assert!(buffer.push(scm_rights), "Failed to push ScmRights message");
let size = sendmsg_unix(
&self.socket,
&SocketAddrUnix::new(JOURNALD_PATH)?,
&[],
&mut buffer,
SendFlags::NOSIGNAL,
)?;
Ok(size)
}
}
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
//! output formats of `journalctl`, e.g. `journalctl --output=json`.
#![deny(warnings, missing_docs, clippy::all)]
#![forbid(unsafe_code)]

use std::io::prelude::*;
use std::os::fd::AsFd;
Expand All @@ -70,8 +71,6 @@ use log::{Level, Log, Metadata, Record, SetLoggerError};

mod client;
mod fields;
mod memfd;
mod socket;

use fields::*;

Expand Down Expand Up @@ -364,7 +363,7 @@ impl Log for JournalLog {
// We can't really handle errors here, so simply discard them.
// The alternative would be to panic, but a failed logging call should
// not bring the entire process down.
let _ = self.journal_send(record);
self.journal_send(record).unwrap();
}

/// Flush log records.
Expand Down
49 changes: 0 additions & 49 deletions src/memfd.rs

This file was deleted.

114 changes: 0 additions & 114 deletions src/socket.rs

This file was deleted.

0 comments on commit de6e118

Please sign in to comment.