From d46b0b6d113ec86c8d1443c1d354ac7ce216f684 Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Wed, 8 May 2024 12:58:20 +0100 Subject: [PATCH] radicle: Fix file limit setting for FreeBSD On FreeBSD systems, the file limits are of type `i64` in comparison to `u64` on Linux systems. Also fix `rad web` command name on Windows and FreeBSD. Co-authored-by: cloudhead Co-authored-by: Shawn Webb --- radicle-httpd/src/commands/web.rs | 2 ++ radicle-node/src/main.rs | 2 +- radicle/src/io.rs | 18 +++++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/radicle-httpd/src/commands/web.rs b/radicle-httpd/src/commands/web.rs index 3229ebf5..866f9d65 100644 --- a/radicle-httpd/src/commands/web.rs +++ b/radicle-httpd/src/commands/web.rs @@ -192,6 +192,8 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { } if options.open { + #[cfg(any(target_os = "freebsd", target_os = "windows"))] + let cmd_name = "echo"; #[cfg(target_os = "macos")] let cmd_name = "open"; #[cfg(target_os = "linux")] diff --git a/radicle-node/src/main.rs b/radicle-node/src/main.rs index cc99ea94..af50a3d2 100644 --- a/radicle-node/src/main.rs +++ b/radicle-node/src/main.rs @@ -120,7 +120,7 @@ fn execute() -> anyhow::Result<()> { config.node.listen.clone() }; - if let Err(e) = radicle::io::set_file_limit(config.node.limits.max_open_files as u64) { + if let Err(e) = radicle::io::set_file_limit(config.node.limits.max_open_files) { log::warn!(target: "node", "Unable to set process open file limit: {e}"); } diff --git a/radicle/src/io.rs b/radicle/src/io.rs index 86a6db7e..8af88728 100644 --- a/radicle/src/io.rs +++ b/radicle/src/io.rs @@ -1,9 +1,25 @@ +use std::fmt; use std::io; use libc::{getrlimit, rlimit, setrlimit, RLIMIT_NOFILE}; +#[cfg(not(target_os = "freebsd"))] +type Int = u64; +#[cfg(target_os = "freebsd")] +type Int = i64; + /// Sets the open file limit to the given value, or the maximum allowed value. -pub fn set_file_limit(n: u64) -> io::Result { +pub fn set_file_limit(n: N) -> io::Result +where + N: Copy + fmt::Display, + Int: TryFrom, +{ + let Ok(n) = Int::try_from(n) else { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("invalid file limit '{n}'"), + )); + }; let mut rlim = rlimit { rlim_cur: 0, // Initial soft limit value rlim_max: 0, // Initial hard limit value