| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/process.h> |
| 7 |
|
| 8 |
#include <sys/resource.h> |
| 9 |
#include <sys/time.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
int aws_get_pid(void) { |
| 13 |
return (int)getpid(); |
| 14 |
} |
| 15 |
|
| 16 |
size_t aws_get_soft_limit_io_handles(void) { |
| 17 |
struct rlimit rlimit; |
| 18 |
AWS_ZERO_STRUCT(rlimit); |
| 19 |
|
| 20 |
AWS_FATAL_ASSERT( |
| 21 |
!getrlimit(RLIMIT_NOFILE, &rlimit) && |
| 22 |
"getrlimit() should never fail for RLIMIT_NOFILE regardless of user permissions"); |
| 23 |
return rlimit.rlim_cur; |
| 24 |
} |
| 25 |
|
| 26 |
size_t aws_get_hard_limit_io_handles(void) { |
| 27 |
struct rlimit rlimit; |
| 28 |
AWS_ZERO_STRUCT(rlimit); |
| 29 |
|
| 30 |
AWS_FATAL_ASSERT( |
| 31 |
!getrlimit(RLIMIT_NOFILE, &rlimit) && |
| 32 |
"getrlimit() should never fail for RLIMIT_NOFILE regardless of user permissions"); |
| 33 |
return rlimit.rlim_max; |
| 34 |
} |
| 35 |
|
| 36 |
int aws_set_soft_limit_io_handles(size_t max_handles) { |
| 37 |
size_t hard_limit = aws_get_hard_limit_io_handles(); |
| 38 |
|
| 39 |
if (max_handles > hard_limit) { |
| 40 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 41 |
} |
| 42 |
|
| 43 |
struct rlimit rlimit = { |
| 44 |
.rlim_cur = max_handles, |
| 45 |
.rlim_max = hard_limit, |
| 46 |
}; |
| 47 |
|
| 48 |
if (setrlimit(RLIMIT_NOFILE, &rlimit)) { |
| 49 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 50 |
} |
| 51 |
|
| 52 |
return AWS_OP_SUCCESS; |
| 53 |
} |