Back to index

glibc  2.9
setrlimit.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999, 2000, 2003, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <sys/param.h>
00021 #include <sys/resource.h>
00022 
00023 #include <sysdep.h>
00024 #include <sys/syscall.h>
00025 #include <shlib-compat.h>
00026 #include <bp-checks.h>
00027 
00028 #include <kernel-features.h>
00029 
00030 extern int __new_setrlimit (enum __rlimit_resource resource,
00031                          const struct rlimit *__unboundedrlimits);
00032 
00033 /* Linux 2.3.25 introduced a new system call since the types used for
00034    the limits are now unsigned.  */
00035 #if defined __NR_ugetrlimit && !defined __ASSUME_NEW_GETRLIMIT_SYSCALL
00036 extern int __have_no_new_getrlimit; /* from getrlimit.c */
00037 #endif
00038 
00039 int
00040 __new_setrlimit (enum __rlimit_resource resource, const struct rlimit *rlimits)
00041 {
00042 #ifdef __ASSUME_NEW_GETRLIMIT_SYSCALL
00043   return INLINE_SYSCALL (setrlimit, 2, resource, CHECK_1 (rlimits));
00044 #else
00045   struct rlimit rlimits_small;
00046 
00047 # ifdef __NR_ugetrlimit
00048   if (__have_no_new_getrlimit == 0)
00049     {
00050       /* Check if the new ugetrlimit syscall exists.  We must do this
00051         first because older kernels don't reject negative rlimit
00052         values in setrlimit.  */
00053       int result = INLINE_SYSCALL (ugetrlimit, 2, resource, __ptrvalue (&rlimits_small));
00054       if (result != -1 || errno != ENOSYS)
00055        /* The syscall exists.  */
00056        __have_no_new_getrlimit = -1;
00057       else
00058        /* The syscall does not exist.  */
00059        __have_no_new_getrlimit = 1;
00060     }
00061   if (__have_no_new_getrlimit < 0)
00062     return INLINE_SYSCALL (setrlimit, 2, resource, CHECK_1 (rlimits));
00063 # endif
00064 
00065   /* We might have to correct the limits values.  Since the old values
00066      were signed the new values might be too large.  */
00067   rlimits_small.rlim_cur = MIN ((unsigned long int) rlimits->rlim_cur,
00068                             RLIM_INFINITY >> 1);
00069   rlimits_small.rlim_max = MIN ((unsigned long int) rlimits->rlim_max,
00070                             RLIM_INFINITY >> 1);
00071 
00072   /* Use the adjusted values.  */
00073   return INLINE_SYSCALL (setrlimit, 2, resource, __ptrvalue (&rlimits_small));
00074 #endif
00075 }
00076 
00077 weak_alias (__new_setrlimit, __setrlimit);
00078 versioned_symbol (libc, __new_setrlimit, setrlimit, GLIBC_2_2);