]> granicus.if.org Git - strace/blob - tests/prlimit64.c
strace: terminate itself if interrupted by a signal
[strace] / tests / prlimit64.c
1 /*
2  * Check decoding of prlimit64 syscall.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11 #include <asm/unistd.h>
12
13 #ifdef __NR_prlimit64
14
15 # include <inttypes.h>
16 # include <stdio.h>
17 # include <stdint.h>
18 # include <sys/resource.h>
19 # include <unistd.h>
20
21 # include "xlat.h"
22 # include "xlat/resources.h"
23
24 const char *
25 sprint_rlim(uint64_t lim)
26 {
27         if (lim == -1ULL)
28                 return "RLIM64_INFINITY";
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], "%" PRIu64 "*1024", lim / 1024);
35         else
36                 sprintf(buf[i], "%" PRIu64, lim);
37
38         return buf[i++];
39 }
40
41 int
42 main(void)
43 {
44         unsigned long pid =
45                 (unsigned long) 0xdefaced00000000ULL | (unsigned) getpid();
46         uint64_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_prlimit64, pid, res, 0, rlimit);
52                 if (rc)
53                         printf("prlimit64(%d, %s, NULL, %p) = %ld %s (%m)\n",
54                                (unsigned) pid, xlat->str, rlimit,
55                                rc, errno2name());
56                 else
57                         printf("prlimit64(%d, %s, NULL"
58                                ", {rlim_cur=%s, rlim_max=%s}) = 0\n",
59                                (unsigned) pid, xlat->str,
60                                sprint_rlim(rlimit[0]),
61                                sprint_rlim(rlimit[1]));
62         }
63
64         puts("+++ exited with 0 +++");
65         return 0;
66 }
67
68 #else
69
70 SKIP_MAIN_UNDEFINED("__NR_prlimit64")
71
72 #endif