Back to index

glibc  2.9
ulimit.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1992,1994-1998,2001,2005 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 <stdarg.h>
00021 #include <sysdep.h>
00022 #include <ulimit.h>
00023 #include <unistd.h>
00024 #include <sys/resource.h>
00025 
00026 
00027 extern int _etext;
00028 
00029 /* Function depends on CMD:
00030    1 = Return the limit on the size of a file, in units of 512 bytes.
00031    2 = Set the limit on the size of a file to NEWLIMIT.  Only the
00032        super-user can increase the limit.
00033    3 = Return the maximum possible address of the data segment.
00034    4 = Return the maximum number of files that the calling process
00035        can open.
00036    Returns -1 on errors.  */
00037 long int
00038 ulimit (int cmd, ...)
00039 {
00040   struct rlimit limit;
00041   va_list va;
00042   long int result = -1;
00043 
00044   va_start (va, cmd);
00045 
00046   switch (cmd)
00047     {
00048     case UL_GETFSIZE:
00049       /* Get limit on file size.  */
00050       if (getrlimit (RLIMIT_FSIZE, &limit) == 0)
00051        /* Convert from bytes to 512 byte units.  */
00052        result = limit.rlim_cur / 512;
00053       break;
00054 
00055     case UL_SETFSIZE:
00056       /* Set limit on file size.  */
00057       {
00058        long int newlimit = va_arg (va, long int);
00059 
00060        if ((rlim_t) newlimit > RLIM_INFINITY / 512)
00061          {
00062            limit.rlim_cur = RLIM_INFINITY;
00063            limit.rlim_max = RLIM_INFINITY;
00064          }
00065        else
00066          {
00067            limit.rlim_cur = newlimit * 512;
00068            limit.rlim_max = newlimit * 512;
00069          }
00070 
00071        result = setrlimit (RLIMIT_FSIZE, &limit);
00072       }
00073       break;
00074 
00075     case __UL_GETMAXBRK:
00076       /* Get maximum address for `brk'.  */
00077       if (getrlimit (RLIMIT_DATA, &limit) == 0)
00078        result = ((long int) &_etext) + limit.rlim_cur;
00079       break;
00080 
00081     case __UL_GETOPENMAX:
00082       result = sysconf (_SC_OPEN_MAX);
00083       break;
00084 
00085     default:
00086       __set_errno (EINVAL);
00087     }
00088 
00089   va_end (va);
00090 
00091   return result;
00092 }