Skip to content

Make memfd_create syscall directly #1912

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

Merged
merged 4 commits into from
Feb 7, 2022
Merged
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
18 changes: 17 additions & 1 deletion tracing-journald/src/memfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,30 @@ use std::os::raw::c_uint;
use std::os::unix::prelude::{FromRawFd, RawFd};

fn create(flags: c_uint) -> Result<File> {
let fd = unsafe { memfd_create("tracing-journald\0".as_ptr() as *const c_char, flags) };
let fd = memfd_create_syscall(flags);
if fd < 0 {
Err(Error::last_os_error())
} else {
Ok(unsafe { File::from_raw_fd(fd as RawFd) })
}
}

/// Make the `memfd_create` syscall ourself instead of going through `libc`;
/// `memfd_create` isn't supported on `glibc<2.27` so this allows us to
/// support old-but-still-used distros like Ubuntu Xenial, Debian Stretch,
/// RHEL 7, etc.
///
/// See: https://github.com/tokio-rs/tracing/issues/1879
fn memfd_create_syscall(flags: c_uint) -> i64 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is broken in 32-bit environments where c_long is, of course, 32 bit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good catch, that should return a c_long

unsafe {
syscall(
SYS_memfd_create,
"tracing-journald\0".as_ptr() as *const c_char,
flags,
)
}
}

pub fn create_sealable() -> Result<File> {
create(MFD_ALLOW_SEALING | MFD_CLOEXEC)
}
Expand Down