You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.7 KiB

  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/resource.h>
  19. namespace resources
  20. {
  21. int
  22. reset_umask(void)
  23. {
  24. umask(0);
  25. return 0;
  26. }
  27. int
  28. maxout_rlimit(const int resource)
  29. {
  30. int rv;
  31. struct rlimit rlim;
  32. rlim.rlim_cur = RLIM_INFINITY;
  33. rlim.rlim_max = RLIM_INFINITY;
  34. rv = ::setrlimit(resource,&rlim);
  35. if(rv == 0)
  36. return 0;
  37. rv = ::getrlimit(resource,&rlim);
  38. if(rv == -1)
  39. return -1;
  40. rv = 0;
  41. rlim.rlim_cur = rlim.rlim_max;
  42. while(rv == 0)
  43. {
  44. rv = ::setrlimit(resource,&rlim);
  45. rlim.rlim_max *= 2;
  46. rlim.rlim_cur = rlim.rlim_max;
  47. }
  48. return rv;
  49. }
  50. int
  51. maxout_rlimit_nofile(void)
  52. {
  53. return maxout_rlimit(RLIMIT_NOFILE);
  54. }
  55. int
  56. maxout_rlimit_fsize(void)
  57. {
  58. return maxout_rlimit(RLIMIT_FSIZE);
  59. }
  60. int
  61. setpriority(const int prio)
  62. {
  63. const int SELF = 0;
  64. return ::setpriority(PRIO_PROCESS,SELF,prio);
  65. }
  66. }