-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrlimit.cpp
More file actions
41 lines (34 loc) · 879 Bytes
/
Copy pathrlimit.cpp
File metadata and controls
41 lines (34 loc) · 879 Bytes
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
/*
* Copyright (c) 2022 Light Labs Inc.
* All Rights Reserved
* Licensed under the MIT license.
*/
#include "rlimit.h"
#include <iostream>
#include <fstream>
res_limit_t FsMaxFiles()
{
std::ifstream fm( "/proc/sys/fs/file-max", std::ifstream::in );
res_limit_t maxFiles;
return fm >> maxFiles, maxFiles ? maxFiles : RLIM64_INFINITY;
}
res_limit_t GetFDLimit()
{
struct rlimit64 out;
getrlimit64( RLIMIT_NOFILE, &out );
return out.rlim_cur;
}
bool SetFDLimit( res_limit_t maxFiles )
{
struct rlimit64 set, out;
set.rlim_max = maxFiles;
set.rlim_cur = maxFiles;
return ( prlimit64( getpid(), RLIMIT_NOFILE, &set, &out ) >= 0 )
|| ( setrlimit64( RLIMIT_NOFILE, &set ) >= 0 );
}
bool RaiseFdLimit()
{
res_limit_t maxFiles = FsMaxFiles();
return ( maxFiles > GetFDLimit() )
&& SetFDLimit( maxFiles );
}