linux_resource.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* $OpenBSD: linux_resource.c,v 1.6 2012/09/05 17:13:37 deraadt Exp $ */
  2. /*
  3. * Copyright (c) 2000 Niklas Hallqvist
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. * Linux "resource" syscall emulation
  28. */
  29. #include <sys/param.h>
  30. #include <sys/systm.h>
  31. #include <sys/mount.h>
  32. #include <sys/proc.h>
  33. #include <sys/resource.h>
  34. #include <sys/resourcevar.h>
  35. #include <sys/syscallargs.h>
  36. #include <compat/linux/linux_types.h>
  37. #include <compat/linux/linux_resource.h>
  38. #include <compat/linux/linux_signal.h>
  39. #include <compat/linux/linux_syscallargs.h>
  40. /* linux_resource.c */
  41. int linux_to_bsd_rlimit(u_int);
  42. int linux_dogetrlimit(struct proc *, void *, register_t *, rlim_t);
  43. static u_int linux_to_bsd_rlimit_map[] = {
  44. RLIMIT_CPU,
  45. RLIMIT_FSIZE,
  46. RLIMIT_DATA,
  47. RLIMIT_STACK,
  48. RLIMIT_CORE,
  49. RLIMIT_RSS,
  50. RLIMIT_NPROC,
  51. RLIMIT_NOFILE,
  52. RLIMIT_MEMLOCK,
  53. RLIM_NLIMITS /* LINUX_RLIMIT_AS not supported */
  54. };
  55. int
  56. linux_to_bsd_rlimit(which)
  57. u_int which;
  58. {
  59. if (which >= LINUX_RLIM_NLIMITS)
  60. return (RLIM_NLIMITS);
  61. return (linux_to_bsd_rlimit_map[which]);
  62. }
  63. struct compat_sys_setrlimit_args {
  64. syscallarg(int) which;
  65. syscallarg(struct olimit *) rlp;
  66. };
  67. struct compat_linux_rlimit {
  68. int32_t rlim_cur; /* current (soft) limit */
  69. int32_t rlim_max; /* maximum value for rlim_cur */
  70. };
  71. int compat_sys_setrlimit(struct proc *p, void *v, register_t *retval);
  72. int
  73. compat_sys_setrlimit(struct proc *p, void *v, register_t *retval)
  74. {
  75. struct compat_sys_setrlimit_args *uap = v;
  76. struct compat_linux_rlimit olim;
  77. struct rlimit lim;
  78. int error;
  79. error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&olim,
  80. sizeof (olim));
  81. if (error)
  82. return (error);
  83. lim.rlim_cur = olim.rlim_cur;
  84. lim.rlim_max = olim.rlim_max;
  85. return (dosetrlimit(p, SCARG(uap, which), &lim));
  86. }
  87. int
  88. linux_sys_setrlimit(p, v, retval)
  89. struct proc *p;
  90. void *v;
  91. register_t *retval;
  92. {
  93. struct linux_sys_setrlimit_args /* {
  94. syscallarg(u_int) which;
  95. syscallarg(struct linux_rlimit *) rlp;
  96. } */ *uap = v;
  97. SCARG(uap, which) = linux_to_bsd_rlimit(SCARG(uap, which));
  98. if (SCARG(uap, which) == RLIM_NLIMITS)
  99. return (EINVAL);
  100. return (compat_sys_setrlimit(p, v, retval));
  101. }
  102. int
  103. linux_dogetrlimit(p, v, retval, max)
  104. struct proc *p;
  105. void *v;
  106. register_t *retval;
  107. rlim_t max;
  108. {
  109. struct linux_sys_getrlimit_args /* {
  110. syscallarg(u_int) which;
  111. syscallarg(struct linux_rlimit *) rlp;
  112. } */ *uap = v;
  113. u_int which;
  114. struct linux_rlimit rlim;
  115. which = linux_to_bsd_rlimit(SCARG(uap, which));
  116. if (which == RLIM_NLIMITS)
  117. return (EINVAL);
  118. rlim.rlim_cur = MIN(p->p_rlimit[which].rlim_cur, max);
  119. rlim.rlim_max = MIN(p->p_rlimit[which].rlim_max, max);
  120. return (copyout(&rlim, SCARG(uap, rlp), sizeof rlim));
  121. }
  122. int
  123. linux_sys_getrlimit(p, v, retval)
  124. struct proc *p;
  125. void *v;
  126. register_t *retval;
  127. {
  128. return (linux_dogetrlimit(p, v, retval, LINUX_OLD_RLIM_INFINITY));
  129. }
  130. int
  131. linux_sys_ugetrlimit(p, v, retval)
  132. struct proc *p;
  133. void *v;
  134. register_t *retval;
  135. {
  136. return (linux_dogetrlimit(p, v, retval, LINUX_RLIM_INFINITY));
  137. }