From 3ecd835f58f9c839b10429127ca9cc00ca4d525f Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 21 Jan 2024 11:54:26 -0800 Subject: [PATCH] Clean up some stale comments and restore libc usage in flog_safe flog_safe should be explicitly async-signal-safe functions; let's avoid nix in that module for this reason. --- src/common.rs | 2 +- src/fork_exec/flog_safe.rs | 4 +++- src/history/file.rs | 1 - src/reader.rs | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/common.rs b/src/common.rs index 433508ed4b1b..9b4c55d4c87f 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1468,7 +1468,7 @@ fn can_be_encoded(wc: char) -> bool { } /// Call read, blocking and repeating on EINTR. Exits on EAGAIN. -/// \return the number of bytes read, or 0 on EOF. On EAGAIN, returns -1 if nothing was read. +/// \return the number of bytes read, or 0 on EOF, or an error. pub fn read_blocked(fd: RawFd, buf: &mut [u8]) -> nix::Result { loop { let res = nix::unistd::read(fd, buf); diff --git a/src/fork_exec/flog_safe.rs b/src/fork_exec/flog_safe.rs index 164db4f16344..d1c0a922b013 100644 --- a/src/fork_exec/flog_safe.rs +++ b/src/fork_exec/flog_safe.rs @@ -84,7 +84,9 @@ pub fn flog_impl_async_safe(fd: i32, s: impl FloggableDisplayAsyncSafe) { let bytes: &[u8] = s.to_flog_str_async_safe(&mut storage); // Note we deliberately do not retry on signals, etc. // This is used to report error messages after fork() in the child process. - let _ = nix::unistd::write(fd, bytes); + unsafe { + let _ = libc::write(fd, bytes.as_ptr() as *const libc::c_void, bytes.len()); + } } /// Variant of FLOG which is async-safe to use after fork(). diff --git a/src/history/file.rs b/src/history/file.rs index 887ea92ea1df..6abbf6ff2d06 100644 --- a/src/history/file.rs +++ b/src/history/file.rs @@ -228,7 +228,6 @@ fn should_mmap() -> bool { } /// Read from `fd` to fill `dest`, zeroing any unused space. -// Return true on success, false on failure. fn read_from_fd(fd: RawFd, mut dest: &mut [u8]) -> nix::Result<()> { while !dest.is_empty() { match nix::unistd::read(fd, dest) { diff --git a/src/reader.rs b/src/reader.rs index ad3a46d380f3..85bf99d1734c 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -684,7 +684,7 @@ fn read_ni(parser: &Parser, fd: RawFd, io: &IoChain) -> i32 { return 1; } - // Read all data into a std::string. + // Read all data into a vec. let mut fd_contents = Vec::with_capacity(usize::try_from(buf.st_size).unwrap()); loop { let mut buff = [0_u8; 4096];