Dmitry V. Levin | 4e085d0 | 2016-06-06 21:26:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Check decoding of getrlimit/ugetrlimit syscall. |
| 3 | * |
| 4 | * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <sys/resource.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #include "kernel_types.h" |
| 37 | #include "xlat.h" |
| 38 | #include "xlat/resources.h" |
| 39 | |
| 40 | const char * |
| 41 | sprint_rlim(kernel_ulong_t lim) |
| 42 | { |
| 43 | if (sizeof(lim) == sizeof(uint64_t)) { |
| 44 | if (lim == -1ULL) |
| 45 | return "RLIM64_INFINITY"; |
| 46 | } else { |
| 47 | if (lim == -1U) |
| 48 | return "RLIM_INFINITY"; |
| 49 | } |
| 50 | |
| 51 | static char buf[2][sizeof(lim)*3 + sizeof("*1024")]; |
| 52 | static int i; |
| 53 | i &= 1; |
| 54 | if (lim > 1024 && lim % 1024 == 0) |
| 55 | sprintf(buf[i], "%llu*1024", (unsigned long long) lim / 1024); |
| 56 | else |
| 57 | sprintf(buf[i], "%llu", (unsigned long long) lim); |
| 58 | |
| 59 | return buf[i++]; |
| 60 | } |
| 61 | |
| 62 | #ifdef NR_GETRLIMIT |
| 63 | |
| 64 | int |
| 65 | main(void) |
| 66 | { |
| 67 | kernel_ulong_t *const rlimit = tail_alloc(sizeof(*rlimit) * 2); |
| 68 | const struct xlat *xlat; |
| 69 | |
| 70 | for (xlat = resources; xlat->str; ++xlat) { |
| 71 | unsigned long res = 0xfacefeed00000000 | xlat->val; |
| 72 | long rc = syscall(NR_GETRLIMIT, res, 0); |
| 73 | if (rc && ENOSYS == errno) |
| 74 | perror_msg_and_skip(STR_GETRLIMIT); |
| 75 | printf("%s(%s, NULL) = %ld %s (%m)\n", |
| 76 | STR_GETRLIMIT, xlat->str, rc, errno2name()); |
| 77 | |
| 78 | rc = syscall(NR_GETRLIMIT, res, rlimit); |
| 79 | if (rc) |
| 80 | printf("%s(%s, NULL) = %ld %s (%m)\n", |
| 81 | STR_GETRLIMIT, xlat->str, rc, errno2name()); |
| 82 | else |
| 83 | printf("%s(%s, {rlim_cur=%s, rlim_max=%s})" |
| 84 | " = 0\n", STR_GETRLIMIT, xlat->str, |
| 85 | sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1])); |
| 86 | } |
| 87 | |
| 88 | puts("+++ exited with 0 +++"); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | #endif /* NR_GETRLIMIT */ |