Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rustix instead of libc #24

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.20", default-features = false, features = ["std", "fs", "net"] }

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

//! 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";

// We use a static buffer size, since we don't send arbitrary control messages
// here; we just need enough space to send a single file descriptor. This way
// we get away without additional allocations.
const CMSG_BUFSIZE: usize = 64;

pub struct JournalClient {
socket: UnixDatagram,
}
Expand All @@ -39,7 +53,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 +65,32 @@ 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 = [0; CMSG_BUFSIZE];
assert!(scm_rights.size() <= buffer.len());
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)
}
}
3 changes: 1 addition & 2 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
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.