Skip to content

Commit 54ecaea

Browse files
add open file limit warning
1 parent 27e2886 commit 54ecaea

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <filesystem>
1010
#include <iostream>
1111
#include <memory>
12+
#include <sys/resource.h>
1213
#include <sys/signalfd.h>
1314
#include <sysexits.h>
1415
#include <thread>
@@ -265,6 +266,26 @@ int main(int argc, char **argv) {
265266

266267
const auto FORCE_SHM = args.count("force") > 0;
267268

269+
// check ulimit
270+
std::size_t min_files =
271+
CONNECTIONS + 5; // number of connections + stderr + stdout + stdin + signal_fd + server socket
272+
if (SEPARATE) min_files += SEPARATE * 4;
273+
else if (SEPARATE_ALL)
274+
min_files += Modbus::TCP::Client_Poll::MAX_CLIENT_IDS * 4;
275+
else
276+
min_files += 4;
277+
struct rlimit limit;
278+
if (getrlimit(RLIMIT_NOFILE, &limit) != 0) {
279+
perror("getrlimit");
280+
return EX_OSERR;
281+
}
282+
283+
if (limit.rlim_cur < min_files) {
284+
std::cerr << Print_Time::iso << " WARNING: limit of open simultaneous files (" << limit.rlim_cur
285+
<< ") is below the possible maximum that is required for the current settings (" << min_files << ")"
286+
<< std::endl;
287+
}
288+
268289
// create shared memory object for modbus registers
269290
std::unique_ptr<Modbus::shm::Shm_Mapping> fallback_mapping;
270291
if (args.count("separate-all") == 0) {

0 commit comments

Comments
 (0)