process.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/process.h>
  6. #include <sys/resource.h>
  7. #include <sys/time.h>
  8. #include <unistd.h>
  9. int aws_get_pid(void) {
  10. return (int)getpid();
  11. }
  12. size_t aws_get_soft_limit_io_handles(void) {
  13. struct rlimit rlimit;
  14. AWS_ZERO_STRUCT(rlimit);
  15. AWS_FATAL_ASSERT(
  16. !getrlimit(RLIMIT_NOFILE, &rlimit) &&
  17. "getrlimit() should never fail for RLIMIT_NOFILE regardless of user permissions");
  18. return rlimit.rlim_cur;
  19. }
  20. size_t aws_get_hard_limit_io_handles(void) {
  21. struct rlimit rlimit;
  22. AWS_ZERO_STRUCT(rlimit);
  23. AWS_FATAL_ASSERT(
  24. !getrlimit(RLIMIT_NOFILE, &rlimit) &&
  25. "getrlimit() should never fail for RLIMIT_NOFILE regardless of user permissions");
  26. return rlimit.rlim_max;
  27. }
  28. int aws_set_soft_limit_io_handles(size_t max_handles) {
  29. size_t hard_limit = aws_get_hard_limit_io_handles();
  30. if (max_handles > hard_limit) {
  31. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  32. }
  33. struct rlimit rlimit = {
  34. .rlim_cur = max_handles,
  35. .rlim_max = hard_limit,
  36. };
  37. if (setrlimit(RLIMIT_NOFILE, &rlimit)) {
  38. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  39. }
  40. return AWS_OP_SUCCESS;
  41. }