]> granicus.if.org Git - strace/blob - tests/xgetrlimit.c
Update copyright headers
[strace] / tests / xgetrlimit.c
1 /*
2  * Check decoding of getrlimit/ugetrlimit syscall.
3  *
4  * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include <errno.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <sys/resource.h>
14 #include <unistd.h>
15
16 #include "xlat.h"
17 #include "xlat/resources.h"
18
19 const char *
20 sprint_rlim(kernel_ulong_t lim)
21 {
22         if (sizeof(lim) == sizeof(uint64_t)) {
23                 if (lim == (kernel_ulong_t) -1ULL)
24                         return "RLIM64_INFINITY";
25         } else {
26                 if (lim == (kernel_ulong_t) -1U)
27                         return "RLIM_INFINITY";
28         }
29
30         static char buf[2][sizeof(lim)*3 + sizeof("*1024")];
31         static int i;
32         i &= 1;
33         if (lim > 1024 && lim % 1024 == 0)
34                 sprintf(buf[i], "%llu*1024", (unsigned long long) lim / 1024);
35         else
36                 sprintf(buf[i], "%llu", (unsigned long long) lim);
37
38         return buf[i++];
39 }
40
41 #ifdef NR_GETRLIMIT
42
43 int
44 main(void)
45 {
46         kernel_ulong_t *const rlimit = tail_alloc(sizeof(*rlimit) * 2);
47         const struct xlat *xlat;
48
49         for (xlat = resources; xlat->str; ++xlat) {
50                 unsigned long res = 0xfacefeed00000000ULL | xlat->val;
51                 long rc = syscall(NR_GETRLIMIT, res, 0);
52                 if (rc && ENOSYS == errno)
53                         perror_msg_and_skip(STR_GETRLIMIT);
54                 printf("%s(%s, NULL) = %ld %s (%m)\n",
55                        STR_GETRLIMIT, xlat->str, rc, errno2name());
56
57                 rc = syscall(NR_GETRLIMIT, res, rlimit);
58                 if (rc)
59                         printf("%s(%s, NULL) = %ld %s (%m)\n",
60                                STR_GETRLIMIT, xlat->str, rc, errno2name());
61                 else
62                         printf("%s(%s, {rlim_cur=%s, rlim_max=%s})"
63                                " = 0\n", STR_GETRLIMIT, xlat->str,
64                                sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1]));
65         }
66
67         puts("+++ exited with 0 +++");
68         return 0;
69 }
70
71 #endif /* NR_GETRLIMIT */