Back to index

glibc  2.9
ulimit.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,1994-1998,2000,2001,2008
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stdarg.h>
00022 #include <sysdep.h>
00023 #include <ulimit.h>
00024 #include <unistd.h>
00025 #include <sys/resource.h>
00026 
00027 /* Function depends on CMD:
00028    1 = Return the limit on the size of a file, in units of 512 bytes.
00029    2 = Set the limit on the size of a file to NEWLIMIT.  Only the
00030        super-user can increase the limit.
00031    3 = illegal due to shared libraries; normally is
00032        (Return the maximum possible address of the data segment.)
00033    4 = Return the maximum number of files that the calling process
00034        can open.
00035    Returns -1 on errors.  */
00036 long int
00037 __ulimit (int cmd, ...)
00038 {
00039   struct rlimit limit;
00040   va_list va;
00041   long int result = -1;
00042 
00043   va_start (va, cmd);
00044 
00045   switch (cmd)
00046     {
00047     case UL_GETFSIZE:
00048       /* Get limit on file size.  */
00049       if (__getrlimit (RLIMIT_FSIZE, &limit) == 0)
00050        /* Convert from bytes to 512 byte units.  */
00051        result =  (limit.rlim_cur == RLIM_INFINITY
00052                  ? LONG_MAX : 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        long int newlen;
00060 
00061        if ((rlim_t) newlimit > RLIM_INFINITY / 512)
00062          {
00063            limit.rlim_cur = RLIM_INFINITY;
00064            limit.rlim_max = RLIM_INFINITY;
00065            newlen = LONG_MAX;
00066          }
00067        else
00068          {
00069            limit.rlim_cur = newlimit * 512;
00070            limit.rlim_max = newlimit * 512;
00071            newlen = newlimit;
00072          }
00073 
00074        result = __setrlimit (RLIMIT_FSIZE, &limit);
00075        if (result != -1)
00076          result = newlen;
00077       }
00078       break;
00079 
00080     case __UL_GETOPENMAX:
00081       result = __sysconf (_SC_OPEN_MAX);
00082       break;
00083 
00084     default:
00085       __set_errno (EINVAL);
00086     }
00087 
00088   va_end (va);
00089 
00090   return result;
00091 }
00092 
00093 weak_alias (__ulimit, ulimit);