yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org> |
| 4 | * |
| 5 | * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 6 | */ |
| 7 | |
| 8 | #include <sys/syscall.h> |
| 9 | |
| 10 | #include <stdarg.h> |
| 11 | #include <unistd.h> |
| 12 | #include <ulimit.h> |
| 13 | #include <sys/resource.h> |
| 14 | |
| 15 | |
| 16 | long int ulimit(int cmd, ...) |
| 17 | { |
| 18 | va_list va; |
| 19 | struct rlimit limit; |
| 20 | long int result = -1; |
| 21 | va_start (va, cmd); |
| 22 | switch (cmd) { |
| 23 | /* Get limit on file size. */ |
| 24 | case UL_GETFSIZE: |
| 25 | if (getrlimit(RLIMIT_FSIZE, &limit) == 0) |
| 26 | result = limit.rlim_cur / 512; /* bytes to 512 byte blocksize */ |
| 27 | break; |
| 28 | /* Set limit on file size. */ |
| 29 | case UL_SETFSIZE: |
| 30 | result = va_arg (va, long int); |
| 31 | if ((rlim_t) result > RLIM_INFINITY / 512) { |
| 32 | limit.rlim_cur = RLIM_INFINITY; |
| 33 | limit.rlim_max = RLIM_INFINITY; |
| 34 | } else { |
| 35 | limit.rlim_cur = result * 512; |
| 36 | limit.rlim_max = result * 512; |
| 37 | } |
| 38 | result = setrlimit(RLIMIT_FSIZE, &limit); |
| 39 | break; |
| 40 | case __UL_GETOPENMAX: |
| 41 | result = sysconf(_SC_OPEN_MAX); |
| 42 | break; |
| 43 | default: |
| 44 | __set_errno(EINVAL); |
| 45 | } |
| 46 | va_end (va); |
| 47 | return result; |
| 48 | } |