1  /* Copyright (C) 2010-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library 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 GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <sys/resource.h>
      19  #include <sysdep.h>
      20  
      21  /* For ports that support the 64-bit ABI we do not need to define prlimit
      22     and instead prlimit aliases to prlimit64.  See the prlimit64
      23     implementation.  */
      24  #if !__RLIM_T_MATCHES_RLIM64_T
      25  int
      26  prlimit (__pid_t pid, enum __rlimit_resource resource,
      27  	 const struct rlimit *new_rlimit, struct rlimit *old_rlimit)
      28  {
      29    struct rlimit64 new_rlimit64_mem;
      30    struct rlimit64 *new_rlimit64 = NULL;
      31    struct rlimit64 old_rlimit64_mem;
      32    struct rlimit64 *old_rlimit64 = (old_rlimit != NULL
      33  				   ? &old_rlimit64_mem : NULL);
      34  
      35    if (new_rlimit != NULL)
      36      {
      37        if (new_rlimit->rlim_cur == RLIM_INFINITY)
      38  	new_rlimit64_mem.rlim_cur = RLIM64_INFINITY;
      39        else
      40  	new_rlimit64_mem.rlim_cur = new_rlimit->rlim_cur;
      41        if (new_rlimit->rlim_max == RLIM_INFINITY)
      42  	new_rlimit64_mem.rlim_max = RLIM64_INFINITY;
      43        else
      44  	new_rlimit64_mem.rlim_max = new_rlimit->rlim_max;
      45        new_rlimit64 = &new_rlimit64_mem;
      46      }
      47  
      48    int res = INLINE_SYSCALL (prlimit64, 4, pid, resource, new_rlimit64,
      49  			    old_rlimit64);
      50  
      51    if (res == 0 && old_rlimit != NULL)
      52      {
      53        /* The prlimit64 syscall is ill-designed for 32-bit machines.
      54  	 We have to provide a 32-bit variant since otherwise the LFS
      55  	 system would not work.  The infinity value can be translated,
      56  	 but otherwise what shall we do if the syscall succeeds but the
      57  	 old values do not fit into a rlimit structure?  We cannot return
      58  	 an error because the operation itself worked.  Best is perhaps
      59  	 to return RLIM_INFINITY.  */
      60        old_rlimit->rlim_cur = old_rlimit64_mem.rlim_cur;
      61        if (old_rlimit->rlim_cur != old_rlimit64_mem.rlim_cur)
      62  	{
      63  	  if ((new_rlimit == NULL)
      64  	      && (old_rlimit64_mem.rlim_cur != RLIM64_INFINITY))
      65  	    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
      66  	  old_rlimit->rlim_cur = RLIM_INFINITY;
      67  	}
      68        old_rlimit->rlim_max = old_rlimit64_mem.rlim_max;
      69        if (old_rlimit->rlim_max != old_rlimit64_mem.rlim_max)
      70  	{
      71  	  if ((new_rlimit == NULL)
      72  	      && (old_rlimit64_mem.rlim_max != RLIM64_INFINITY))
      73  	    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
      74  	  old_rlimit->rlim_max = RLIM_INFINITY;
      75  	}
      76      }
      77  
      78    return res;
      79  }
      80  #endif /* __RLIM_T_MATCHES_RLIM64_T  */