-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcore_util.cpp
More file actions
49 lines (41 loc) · 1.22 KB
/
Copy pathcore_util.cpp
File metadata and controls
49 lines (41 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <core/core_util.hpp>
#include <ctime>
#ifndef _WIN32
#include <sys/resource.h>
#endif
namespace core
{
uint32_t timestamp()
{
return std::time(nullptr);
}
uint64_t raise_nofile_limit(uint64_t target)
{
#ifndef _WIN32
struct rlimit rl{};
if (getrlimit(RLIMIT_NOFILE, &rl) != 0)
return 0;
if (rl.rlim_cur != RLIM_INFINITY && static_cast<uint64_t>(rl.rlim_cur) < target) {
struct rlimit want = rl;
// Clamp to the hard limit — unprivileged processes cannot exceed it.
if (rl.rlim_max == RLIM_INFINITY
|| static_cast<uint64_t>(rl.rlim_max) >= target)
want.rlim_cur = static_cast<rlim_t>(target);
else
want.rlim_cur = rl.rlim_max;
// Best effort: on failure the original soft limit stays in effect.
(void)setrlimit(RLIMIT_NOFILE, &want);
}
if (getrlimit(RLIMIT_NOFILE, &rl) != 0)
return 0;
if (rl.rlim_cur == RLIM_INFINITY)
return UINT64_MAX;
return static_cast<uint64_t>(rl.rlim_cur);
#else
// Windows: no setrlimit; socket handles are not fd-table bound the same
// way. Report "unsupported" and let the caller log accordingly.
(void)target;
return 0;
#endif
}
} // namespace core