1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 1999-2018 The strace developers.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "defs.h"
33 #include <sys/resource.h>
34 
35 #include "xstring.h"
36 
37 #include "xlat/resources.h"
38 
39 static const char *
sprint_rlim64(uint64_t lim)40 sprint_rlim64(uint64_t lim)
41 {
42 	static char buf[sizeof(uint64_t)*3 + sizeof("*1024")];
43 
44 	if (lim == UINT64_MAX)
45 		return "RLIM64_INFINITY";
46 
47 	if (lim > 1024 && lim % 1024 == 0)
48 		xsprintf(buf, "%" PRIu64 "*1024", lim / 1024);
49 	else
50 		xsprintf(buf, "%" PRIu64, lim);
51 	return buf;
52 }
53 
54 static void
print_rlimit64(struct tcb * const tcp,const kernel_ulong_t addr)55 print_rlimit64(struct tcb *const tcp, const kernel_ulong_t addr)
56 {
57 	struct rlimit_64 {
58 		uint64_t rlim_cur;
59 		uint64_t rlim_max;
60 	} rlim;
61 
62 	if (!umove_or_printaddr(tcp, addr, &rlim)) {
63 		tprintf("{rlim_cur=%s,", sprint_rlim64(rlim.rlim_cur));
64 		tprintf(" rlim_max=%s}", sprint_rlim64(rlim.rlim_max));
65 	}
66 }
67 
68 #if !defined(current_wordsize) || current_wordsize == 4
69 
70 static const char *
sprint_rlim32(uint32_t lim)71 sprint_rlim32(uint32_t lim)
72 {
73 	static char buf[sizeof(uint32_t)*3 + sizeof("*1024")];
74 
75 	if (lim == UINT32_MAX)
76 		return "RLIM_INFINITY";
77 
78 	if (lim > 1024 && lim % 1024 == 0)
79 		xsprintf(buf, "%" PRIu32 "*1024", lim / 1024);
80 	else
81 		xsprintf(buf, "%" PRIu32, lim);
82 	return buf;
83 }
84 
85 static void
print_rlimit32(struct tcb * const tcp,const kernel_ulong_t addr)86 print_rlimit32(struct tcb *const tcp, const kernel_ulong_t addr)
87 {
88 	struct rlimit_32 {
89 		uint32_t rlim_cur;
90 		uint32_t rlim_max;
91 	} rlim;
92 
93 	if (!umove_or_printaddr(tcp, addr, &rlim)) {
94 		tprintf("{rlim_cur=%s,", sprint_rlim32(rlim.rlim_cur));
95 		tprintf(" rlim_max=%s}", sprint_rlim32(rlim.rlim_max));
96 	}
97 }
98 
99 static void
decode_rlimit(struct tcb * const tcp,const kernel_ulong_t addr)100 decode_rlimit(struct tcb *const tcp, const kernel_ulong_t addr)
101 {
102 	/*
103 	 * i386 is the only personality on X86_64 and X32
104 	 * with 32-bit rlim_t.
105 	 * When current_personality is X32, current_wordsize
106 	 * equals to 4 but rlim_t is 64-bit.
107 	 */
108 	if (current_klongsize == 4)
109 		print_rlimit32(tcp, addr);
110 	else
111 		print_rlimit64(tcp, addr);
112 }
113 
114 #else /* defined(current_wordsize) && current_wordsize != 4 */
115 
116 # define decode_rlimit print_rlimit64
117 
118 #endif
119 
SYS_FUNC(getrlimit)120 SYS_FUNC(getrlimit)
121 {
122 	if (entering(tcp)) {
123 		printxval(resources, tcp->u_arg[0], "RLIMIT_???");
124 		tprints(", ");
125 	} else {
126 		decode_rlimit(tcp, tcp->u_arg[1]);
127 	}
128 	return 0;
129 }
130 
SYS_FUNC(setrlimit)131 SYS_FUNC(setrlimit)
132 {
133 	printxval(resources, tcp->u_arg[0], "RLIMIT_???");
134 	tprints(", ");
135 	decode_rlimit(tcp, tcp->u_arg[1]);
136 
137 	return RVAL_DECODED;
138 }
139 
SYS_FUNC(prlimit64)140 SYS_FUNC(prlimit64)
141 {
142 	if (entering(tcp)) {
143 		tprintf("%d, ", (int) tcp->u_arg[0]);
144 		printxval(resources, tcp->u_arg[1], "RLIMIT_???");
145 		tprints(", ");
146 		print_rlimit64(tcp, tcp->u_arg[2]);
147 		tprints(", ");
148 	} else {
149 		print_rlimit64(tcp, tcp->u_arg[3]);
150 	}
151 	return 0;
152 }
153 
154 #include "xlat/usagewho.h"
155 
SYS_FUNC(getrusage)156 SYS_FUNC(getrusage)
157 {
158 	if (entering(tcp)) {
159 		printxval(usagewho, tcp->u_arg[0], "RUSAGE_???");
160 		tprints(", ");
161 	} else
162 		printrusage(tcp, tcp->u_arg[1]);
163 	return 0;
164 }
165 
166 #ifdef ALPHA
SYS_FUNC(osf_getrusage)167 SYS_FUNC(osf_getrusage)
168 {
169 	if (entering(tcp)) {
170 		printxval(usagewho, tcp->u_arg[0], "RUSAGE_???");
171 		tprints(", ");
172 	} else
173 		printrusage32(tcp, tcp->u_arg[1]);
174 	return 0;
175 }
176 #endif /* ALPHA */
177 
178 #include "xlat/priorities.h"
179 
SYS_FUNC(getpriority)180 SYS_FUNC(getpriority)
181 {
182 	printxval(priorities, tcp->u_arg[0], "PRIO_???");
183 	tprintf(", %d", (int) tcp->u_arg[1]);
184 
185 	return RVAL_DECODED;
186 }
187 
SYS_FUNC(setpriority)188 SYS_FUNC(setpriority)
189 {
190 	printxval(priorities, tcp->u_arg[0], "PRIO_???");
191 	tprintf(", %d, %d", (int) tcp->u_arg[1], (int) tcp->u_arg[2]);
192 
193 	return RVAL_DECODED;
194 }
195