From bfc0ae30f68661e99ca3388893f121111b889552 Mon Sep 17 00:00:00 2001 From: Christian von Elm Date: Tue, 6 Feb 2024 07:53:57 +0100 Subject: [PATCH] Add error checks --- src/monitor/process_monitor_main.cpp | 11 +++++++++- src/util.cpp | 30 +++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/monitor/process_monitor_main.cpp b/src/monitor/process_monitor_main.cpp index caa98829..51ccea26 100644 --- a/src/monitor/process_monitor_main.cpp +++ b/src/monitor/process_monitor_main.cpp @@ -56,7 +56,16 @@ namespace monitor [[noreturn]] static void run_command(const std::vector& command_and_args) { struct rlimit initial_rlimit = initial_rlimit_fd(); - setrlimit(RLIMIT_NOFILE, &initial_rlimit); + + if (initial_rlimit.rlim_cur == 0) + { + Log::warn() << "Could not reset file descriptor limit to initial limit, the program under " + "measurement might behave erratically!"; + } + else + { + setrlimit(RLIMIT_NOFILE, &initial_rlimit); + } /* kill yourself if the parent dies */ prctl(PR_SET_PDEATHSIG, SIGHUP); diff --git a/src/util.cpp b/src/util.cpp index a71dcbff..ff4fc632 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -373,7 +373,12 @@ struct rlimit initial_rlimit_fd() if (current.rlim_cur == 0) { - getrlimit(RLIMIT_NOFILE, ¤t); + auto ret = getrlimit(RLIMIT_NOFILE, ¤t); + + if (ret == -1) + { + Log::warn() << "Could not read file descriptor resource limit!"; + } } return current; @@ -382,9 +387,28 @@ struct rlimit initial_rlimit_fd() void bump_rlimit_fd() { struct rlimit highest; - getrlimit(RLIMIT_NOFILE, &highest); + + auto ret = getrlimit(RLIMIT_NOFILE, &highest); + + if (ret == -1) + { + Log::warn() + << "Could not read file descriptor hard resource limit, lo2s might run out of file " + "descriptors!"; + Log::warn() + << "If you encounter issues, try to manually increase the file descriptor rlimit."; + return; + } highest.rlim_cur = highest.rlim_max; - setrlimit(RLIMIT_NOFILE, &highest); + ret = setrlimit(RLIMIT_NOFILE, &highest); + + if (ret == -1) + { + Log::warn() << "Could not increase file descriptor resource limit, lo2s might run out of " + "file descriptors!"; + Log::warn() << "If you encounter issues, try to manually increase the file descriptor " + "resource limit."; + } } } // namespace lo2s