shim_getrlimit.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_getrlimit.c
  17. *
  18. * Implementation of system call "getrlimit" and "setrlimit".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_checkpoint.h>
  22. #include <shim_table.h>
  23. #include <shim_utils.h>
  24. #include <shim_vma.h>
  25. #include <asm/resource.h>
  26. /*
  27. * TODO: implement actual limitation on each resource.
  28. *
  29. * The current behavor(i.e. sys_stack_size, brk_max_size) may be subject
  30. * to be fixed.
  31. */
  32. #define _STK_LIM (8*1024*1024)
  33. #define MAX_THREADS (0x3fffffff / 2)
  34. #define DEFAULT_MAX_FDS (1024)
  35. #define MAX_MAX_FDS (65536) /* 4096: Linux initial value */
  36. #define MLOCK_LIMIT (64*1024)
  37. #define MQ_BYTES_MAX 819200
  38. struct __kernel_rlimit __rlim[RLIM_NLIMITS] __attribute_migratable = {
  39. [RLIMIT_CPU] = { RLIM_INFINITY, RLIM_INFINITY },
  40. [RLIMIT_FSIZE] = { RLIM_INFINITY, RLIM_INFINITY },
  41. /* For now __rlim[RLIMIT_DATA] isn't used. See the implementation */
  42. [RLIMIT_DATA] = { RLIM_INFINITY, RLIM_INFINITY },
  43. /* For now __rlim[RLIMIT_STACK] isn't used. See the implementation */
  44. [RLIMIT_STACK] = { _STK_LIM, RLIM_INFINITY },
  45. [RLIMIT_CORE] = { 0, RLIM_INFINITY },
  46. [RLIMIT_RSS] = { RLIM_INFINITY, RLIM_INFINITY },
  47. [RLIMIT_NPROC] = { MAX_THREADS, MAX_THREADS },
  48. [RLIMIT_NOFILE] = { DEFAULT_MAX_FDS, MAX_MAX_FDS },
  49. [RLIMIT_MEMLOCK] = { MLOCK_LIMIT, MLOCK_LIMIT },
  50. [RLIMIT_AS] = { RLIM_INFINITY, RLIM_INFINITY },
  51. [RLIMIT_LOCKS] = { RLIM_INFINITY, RLIM_INFINITY },
  52. /* [RLIMIT_SIGPENDING] = [RLIMIT_NPROC] for initial value */
  53. [RLIMIT_SIGPENDING] = { MAX_THREADS, MAX_THREADS },
  54. [RLIMIT_MSGQUEUE] = { MQ_BYTES_MAX, MQ_BYTES_MAX },
  55. [RLIMIT_NICE] = { 0, 0 },
  56. [RLIMIT_RTPRIO] = { 0, 0 },
  57. [RLIMIT_RTTIME] = { RLIM_INFINITY, RLIM_INFINITY },
  58. };
  59. int shim_do_getrlimit (int resource, struct __kernel_rlimit * rlim)
  60. {
  61. if (resource < 0 || RLIM_NLIMITS <= resource)
  62. return -EINVAL;
  63. switch (resource) {
  64. case RLIMIT_STACK:
  65. rlim->rlim_cur = sys_stack_size;
  66. rlim->rlim_max = sys_stack_size;
  67. return 0;
  68. case RLIMIT_DATA:
  69. rlim->rlim_cur = brk_max_size;
  70. rlim->rlim_max = __rlim[resource].rlim_max;
  71. return 0;
  72. default:
  73. *rlim = __rlim[resource];
  74. return 0;
  75. }
  76. }
  77. int shim_do_setrlimit (int resource, struct __kernel_rlimit * rlim)
  78. {
  79. if (resource < 0 || RLIM_NLIMITS <= resource)
  80. return -EINVAL;
  81. if (!rlim || test_user_memory(rlim, sizeof(*rlim), false))
  82. return -EFAULT;
  83. if (rlim->rlim_cur > rlim->rlim_max)
  84. return -EINVAL;
  85. if (rlim->rlim_cur > __rlim->rlim_max)
  86. return -EINVAL;
  87. switch (resource) {
  88. case RLIMIT_STACK:
  89. sys_stack_size = rlim->rlim_cur;
  90. return 0;
  91. default:
  92. __rlim[resource].rlim_cur = rlim->rlim_cur;
  93. return 0;
  94. }
  95. }