From 04a828f7c09526a82dc86121f456b2b16fc4a58e Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Mon, 7 Feb 2022 11:16:16 -0500 Subject: [PATCH 1/3] Make `memfd_create` syscall directly Fixes #1879 --- tracing-journald/src/memfd.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tracing-journald/src/memfd.rs b/tracing-journald/src/memfd.rs index 5292db2928..d47abd2649 100644 --- a/tracing-journald/src/memfd.rs +++ b/tracing-journald/src/memfd.rs @@ -8,7 +8,13 @@ use std::os::raw::c_uint; use std::os::unix::prelude::{FromRawFd, RawFd}; fn create(flags: c_uint) -> Result { - let fd = unsafe { memfd_create("tracing-journald\0".as_ptr() as *const c_char, flags) }; + let fd = unsafe { + syscall( + SYS_memfd_create, + "tracing-journald\0".as_ptr() as *const c_char, + flags, + ) + }; if fd < 0 { Err(Error::last_os_error()) } else { From 62120ef669404ad0547712839a3659d80faaf872 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Mon, 7 Feb 2022 12:21:55 -0500 Subject: [PATCH 2/3] Note why we make the memfd_create syscall directly --- tracing-journald/src/memfd.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tracing-journald/src/memfd.rs b/tracing-journald/src/memfd.rs index d47abd2649..ead9f62d93 100644 --- a/tracing-journald/src/memfd.rs +++ b/tracing-journald/src/memfd.rs @@ -8,6 +8,11 @@ use std::os::raw::c_uint; use std::os::unix::prelude::{FromRawFd, RawFd}; fn create(flags: c_uint) -> Result { + // 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 let fd = unsafe { syscall( SYS_memfd_create, From d88cfb272d28adeac0cd9e4bf0902d825895b653 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Mon, 7 Feb 2022 14:03:54 -0500 Subject: [PATCH 3/3] Extract `memfd_create` syscall to separate function --- tracing-journald/src/memfd.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/tracing-journald/src/memfd.rs b/tracing-journald/src/memfd.rs index ead9f62d93..e1e9418f4a 100644 --- a/tracing-journald/src/memfd.rs +++ b/tracing-journald/src/memfd.rs @@ -8,22 +8,27 @@ use std::os::raw::c_uint; use std::os::unix::prelude::{FromRawFd, RawFd}; fn create(flags: c_uint) -> Result { - // 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 - let fd = unsafe { + 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 { + unsafe { syscall( SYS_memfd_create, "tracing-journald\0".as_ptr() as *const c_char, flags, ) - }; - if fd < 0 { - Err(Error::last_os_error()) - } else { - Ok(unsafe { File::from_raw_fd(fd as RawFd) }) } }