2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / ulimit.c
blob0b87599fea3d5e9025d42822519e8316946c0679
1 /* Copyright (C) 1991,92,1994-1998,2000,2001,2008
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <sysdep.h>
23 #include <ulimit.h>
24 #include <unistd.h>
25 #include <sys/resource.h>
27 /* Function depends on CMD:
28 1 = Return the limit on the size of a file, in units of 512 bytes.
29 2 = Set the limit on the size of a file to NEWLIMIT. Only the
30 super-user can increase the limit.
31 3 = illegal due to shared libraries; normally is
32 (Return the maximum possible address of the data segment.)
33 4 = Return the maximum number of files that the calling process
34 can open.
35 Returns -1 on errors. */
36 long int
37 __ulimit (int cmd, ...)
39 struct rlimit limit;
40 va_list va;
41 long int result = -1;
43 va_start (va, cmd);
45 switch (cmd)
47 case UL_GETFSIZE:
48 /* Get limit on file size. */
49 if (__getrlimit (RLIMIT_FSIZE, &limit) == 0)
50 /* Convert from bytes to 512 byte units. */
51 result = (limit.rlim_cur == RLIM_INFINITY
52 ? LONG_MAX : limit.rlim_cur / 512);
53 break;
55 case UL_SETFSIZE:
56 /* Set limit on file size. */
58 long int newlimit = va_arg (va, long int);
59 long int newlen;
61 if ((rlim_t) newlimit > RLIM_INFINITY / 512)
63 limit.rlim_cur = RLIM_INFINITY;
64 limit.rlim_max = RLIM_INFINITY;
65 newlen = LONG_MAX;
67 else
69 limit.rlim_cur = newlimit * 512;
70 limit.rlim_max = newlimit * 512;
71 newlen = newlimit;
74 result = __setrlimit (RLIMIT_FSIZE, &limit);
75 if (result != -1)
76 result = newlen;
78 break;
80 case __UL_GETOPENMAX:
81 result = __sysconf (_SC_OPEN_MAX);
82 break;
84 default:
85 __set_errno (EINVAL);
88 va_end (va);
90 return result;
93 weak_alias (__ulimit, ulimit);