Skip to content

Commit

Permalink
log better errors of local file creation and add option to use altera…
Browse files Browse the repository at this point in the history
…ntive way (#2890)

* log better errors of local file creation and add option to use alternative way

* also pass errno
  • Loading branch information
aviramha authored Nov 3, 2024
1 parent ef15671 commit 1fb8350
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/2889.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log better errors of local file creation and add option to use alternative way
8 changes: 8 additions & 0 deletions mirrord-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,14 @@
"boolean",
"null"
]
},
"use_dev_null": {
"title": "_experimental_ use_dev_null {#experimental-use_dev_null}",
"description": "Uses /dev/null for creating local fake files (should be better than using /tmp)",
"type": [
"boolean",
"null"
]
}
},
"additionalProperties": false
Expand Down
6 changes: 6 additions & 0 deletions mirrord/config/src/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ pub struct ExperimentalConfig {
/// This option applies only on macOS.
#[config(default = false)]
pub disable_reuseaddr: bool,

/// ### _experimental_ use_dev_null {#experimental-use_dev_null}
///
/// Uses /dev/null for creating local fake files (should be better than using /tmp)
#[config(default = false)]
pub use_dev_null: bool,
}

impl CollectAnalytics for &ExperimentalConfig {
Expand Down
6 changes: 3 additions & 3 deletions mirrord/layer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ pub(crate) enum HookError {
#[error("mirrord-layer: Failed converting `to_str` with `{0}`!")]
Utf8(#[from] std::str::Utf8Error),

#[error("mirrord-layer: Failed creating local file for `remote_fd` `{0}`!")]
LocalFileCreation(u64),
#[error("mirrord-layer: Failed creating local file for `remote_fd` `{0}`! with errno `{1}`")]
LocalFileCreation(u64, i32),

#[cfg(target_os = "macos")]
#[error("mirrord-layer: SIP patch failed with error `{0}`!")]
Expand Down Expand Up @@ -289,7 +289,7 @@ impl From<HookError> for i64 {
HookError::DNSNoName => libc::EFAULT,
HookError::Utf8(_) => libc::EINVAL,
HookError::NullPointer => libc::EINVAL,
HookError::LocalFileCreation(_) => libc::EINVAL,
HookError::LocalFileCreation(_, err) => err,
#[cfg(target_os = "macos")]
HookError::FailedSipPatch(_) => libc::EACCES,
HookError::SocketUnsuportedIpv6 => libc::EAFNOSUPPORT,
Expand Down
22 changes: 21 additions & 1 deletion mirrord/layer/src/file/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,42 @@ fn get_remote_fd(local_fd: RawFd) -> Detour<u64> {
/// Create temporary local file to get a valid local fd.
#[mirrord_layer_macro::instrument(level = "trace", ret)]
fn create_local_fake_file(remote_fd: u64) -> Detour<RawFd> {
if crate::setup().experimental().use_dev_null {
return create_local_devnull_file(remote_fd);
}
let random_string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
let file_name = format!("{remote_fd}-{random_string}");
let file_path = env::temp_dir().join(file_name);
let file_c_string = CString::new(file_path.to_string_lossy().to_string())?;
let file_path_ptr = file_c_string.as_ptr();
let local_file_fd: RawFd = unsafe { FN_OPEN(file_path_ptr, O_RDONLY | O_CREAT) };
if local_file_fd == -1 {
let error = errno::errno();
// Close the remote file if creating a tmp local file failed and we have an invalid local fd
close_remote_file_on_failure(remote_fd)?;
Detour::Error(HookError::LocalFileCreation(remote_fd))
Detour::Error(HookError::LocalFileCreation(remote_fd, error.0))
} else {
unsafe { unlink(file_path_ptr) };
Detour::Success(local_file_fd)
}
}

/// Open /dev/null to get a valid file fd
#[mirrord_layer_macro::instrument(level = "trace", ret)]
fn create_local_devnull_file(remote_fd: u64) -> Detour<RawFd> {
let file_c_string = CString::new("/dev/null")?;
let file_path_ptr = file_c_string.as_ptr();
let local_file_fd: RawFd = unsafe { FN_OPEN(file_path_ptr, O_RDONLY) };
if local_file_fd == -1 {
let error = errno::errno();
// Close the remote file if creating a tmp local file failed and we have an invalid local fd
close_remote_file_on_failure(remote_fd)?;
Detour::Error(HookError::LocalFileCreation(remote_fd, error.0))
} else {
Detour::Success(local_file_fd)
}
}

/// Close the remote file if the call to [`libc::shm_open`] failed and we have an invalid local fd.
#[mirrord_layer_macro::instrument(level = "trace", ret)]
fn close_remote_file_on_failure(fd: u64) -> Result<()> {
Expand Down

0 comments on commit 1fb8350

Please sign in to comment.