resource.c revision 59432dbb333959618b5afdb9a7513d1ab0289a14
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 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $Id$ 31 */ 32 33#include "defs.h" 34 35#include <sys/resource.h> 36#ifdef LINUX 37#include <sys/times.h> 38#include <linux/kernel.h> 39#endif /* LINUX */ 40#if defined(SVR4) || defined(FREEBSD) 41#include <sys/times.h> 42#include <sys/time.h> 43#endif 44 45#if HAVE_LONG_LONG_RLIM_T 46/* 47 * Hacks for systems that have a long long rlim_t 48 */ 49 50#define rlimit64 rlimit /* Ugly hack */ 51#define rlim64_t rlim_t /* Ugly hack */ 52#define RLIM64_INFINITY RLIM_INFINITY /* You guessed it */ 53 54#define sys_getrlimit64 sys_getrlimit 55#define sys_setrlimit64 sys_setrlimit 56#endif 57 58static const struct xlat resources[] = { 59#ifdef RLIMIT_AS 60 { RLIMIT_AS, "RLIMIT_AS" }, 61#endif 62#ifdef RLIMIT_CORE 63 { RLIMIT_CORE, "RLIMIT_CORE" }, 64#endif 65#ifdef RLIMIT_CPU 66 { RLIMIT_CPU, "RLIMIT_CPU" }, 67#endif 68#ifdef RLIMIT_DATA 69 { RLIMIT_DATA, "RLIMIT_DATA" }, 70#endif 71#ifdef RLIMIT_FSIZE 72 { RLIMIT_FSIZE, "RLIMIT_FSIZE" }, 73#endif 74#ifdef RLIMIT_LOCKS 75 { RLIMIT_LOCKS, "RLIMIT_LOCKS" }, 76#endif 77#ifdef RLIMIT_MEMLOCK 78 { RLIMIT_MEMLOCK, "RLIMIT_MEMLOCK" }, 79#endif 80#ifdef RLIMIT_MSGQUEUE 81 { RLIMIT_MSGQUEUE, "RLIMIT_MSGQUEUE" }, 82#endif 83#ifdef RLIMIT_NICE 84 { RLIMIT_NICE, "RLIMIT_NICE" }, 85#endif 86#ifdef RLIMIT_NOFILE 87 { RLIMIT_NOFILE, "RLIMIT_NOFILE" }, 88#endif 89#ifdef RLIMIT_NPROC 90 { RLIMIT_NPROC, "RLIMIT_NPROC" }, 91#endif 92#ifdef RLIMIT_RSS 93 { RLIMIT_RSS, "RLIMIT_RSS" }, 94#endif 95#ifdef RLIMIT_RTPRIO 96 { RLIMIT_RTPRIO, "RLIMIT_RTPRIO" }, 97#endif 98#ifdef RLIMIT_SIGPENDING 99 { RLIMIT_SIGPENDING, "RLIMIT_SIGPENDING" }, 100#endif 101#ifdef RLIMIT_STACK 102 { RLIMIT_STACK, "RLIMIT_STACK" }, 103#endif 104#ifdef RLIMIT_VMEM 105 { RLIMIT_VMEM, "RLIMIT_VMEM" }, 106#endif 107 { 0, NULL }, 108}; 109 110#if !HAVE_LONG_LONG_RLIM_T 111static char * 112sprintrlim(lim) 113long lim; 114{ 115 static char buf[32]; 116 117 if (lim == RLIM_INFINITY) 118 sprintf(buf, "RLIM_INFINITY"); 119 else if (lim > 1024 && lim%1024 == 0) 120 sprintf(buf, "%ld*1024", lim/1024); 121 else 122 sprintf(buf, "%ld", lim); 123 return buf; 124} 125 126int 127sys_getrlimit(tcp) 128struct tcb *tcp; 129{ 130 struct rlimit rlim; 131 132 if (entering(tcp)) { 133 printxval(resources, tcp->u_arg[0], "RLIMIT_???"); 134 tprintf(", "); 135 } 136 else { 137 if (syserror(tcp) || !verbose(tcp)) 138 tprintf("%#lx", tcp->u_arg[1]); 139 else if (umove(tcp, tcp->u_arg[1], &rlim) < 0) 140 tprintf("{...}"); 141 else { 142 tprintf("{rlim_cur=%s,", sprintrlim(rlim.rlim_cur)); 143 tprintf(" rlim_max=%s}", sprintrlim(rlim.rlim_max)); 144 } 145 } 146 return 0; 147} 148 149int 150sys_setrlimit(tcp) 151struct tcb *tcp; 152{ 153 struct rlimit rlim; 154 155 if (entering(tcp)) { 156 printxval(resources, tcp->u_arg[0], "RLIMIT_???"); 157 tprintf(", "); 158 if (!verbose(tcp)) 159 tprintf("%#lx", tcp->u_arg[1]); 160 else if (umove(tcp, tcp->u_arg[1], &rlim) < 0) 161 tprintf("{...}"); 162 else { 163 tprintf("{rlim_cur=%s,", sprintrlim(rlim.rlim_cur)); 164 tprintf(" rlim_max=%s}", sprintrlim(rlim.rlim_max)); 165 } 166 } 167 return 0; 168} 169#endif /* !HAVE_LONG_LONG_RLIM_T */ 170 171#if _LFS64_LARGEFILE || HAVE_LONG_LONG_RLIM_T 172static char * 173sprintrlim64(lim) 174rlim64_t lim; 175{ 176 static char buf[64]; 177 178 if (lim == RLIM64_INFINITY) 179 sprintf(buf, "RLIM64_INFINITY"); 180 else if (lim > 1024 && lim%1024 == 0) 181 sprintf(buf, "%lld*1024", (long long) lim/1024); 182 else 183 sprintf(buf, "%lld", (long long) lim); 184 return buf; 185} 186 187int 188sys_getrlimit64(tcp) 189struct tcb *tcp; 190{ 191 struct rlimit64 rlim; 192 193 if (entering(tcp)) { 194 printxval(resources, tcp->u_arg[0], "RLIMIT_???"); 195 tprintf(", "); 196 } 197 else { 198 if (syserror(tcp) || !verbose(tcp)) 199 tprintf("%#lx", tcp->u_arg[1]); 200 else if (umove(tcp, tcp->u_arg[1], &rlim) < 0) 201 tprintf("{...}"); 202 else { 203 tprintf("{rlim_cur=%s,", sprintrlim64(rlim.rlim_cur)); 204 tprintf(" rlim_max=%s}", sprintrlim64(rlim.rlim_max)); 205 } 206 } 207 return 0; 208} 209 210int 211sys_setrlimit64(tcp) 212struct tcb *tcp; 213{ 214 struct rlimit64 rlim; 215 216 if (entering(tcp)) { 217 printxval(resources, tcp->u_arg[0], "RLIMIT_???"); 218 tprintf(", "); 219 if (!verbose(tcp)) 220 tprintf("%#lx", tcp->u_arg[1]); 221 else if (umove(tcp, tcp->u_arg[1], &rlim) < 0) 222 tprintf("{...}"); 223 else { 224 tprintf("{rlim_cur=%s,", sprintrlim64(rlim.rlim_cur)); 225 tprintf(" rlim_max=%s}", sprintrlim64(rlim.rlim_max)); 226 } 227 } 228 return 0; 229} 230#endif /* _LFS64_LARGEFILES || HAVE_LONG_LONG_RLIM_T */ 231 232#ifndef SVR4 233 234static const struct xlat usagewho[] = { 235 { RUSAGE_SELF, "RUSAGE_SELF" }, 236 { RUSAGE_CHILDREN, "RUSAGE_CHILDREN" }, 237#ifdef RUSAGE_BOTH 238 { RUSAGE_BOTH, "RUSAGE_BOTH" }, 239#endif 240 { 0, NULL }, 241}; 242 243#ifdef ALPHA 244void 245printrusage32(tcp, addr) 246struct tcb *tcp; 247long addr; 248{ 249 struct timeval32 250 { 251 unsigned tv_sec; 252 unsigned tv_usec; 253 }; 254 struct rusage32 255 { 256 struct timeval32 ru_utime; /* user time used */ 257 struct timeval32 ru_stime; /* system time used */ 258 long ru_maxrss; /* maximum resident set size */ 259 long ru_ixrss; /* integral shared memory size */ 260 long ru_idrss; /* integral unshared data size */ 261 long ru_isrss; /* integral unshared stack size */ 262 long ru_minflt; /* page reclaims */ 263 long ru_majflt; /* page faults */ 264 long ru_nswap; /* swaps */ 265 long ru_inblock; /* block input operations */ 266 long ru_oublock; /* block output operations */ 267 long ru_msgsnd; /* messages sent */ 268 long ru_msgrcv; /* messages received */ 269 long ru_nsignals; /* signals received */ 270 long ru_nvcsw; /* voluntary context switches */ 271 long ru_nivcsw; /* involuntary " */ 272 } ru; 273 274 if (!addr) 275 tprintf("NULL"); 276 else if (syserror(tcp) || !verbose(tcp)) 277 tprintf("%#lx", addr); 278 else if (umove(tcp, addr, &ru) < 0) 279 tprintf("{...}"); 280 else if (!abbrev(tcp)) { 281 tprintf("{ru_utime={%lu, %lu}, ru_stime={%lu, %lu}, ", 282 (long) ru.ru_utime.tv_sec, (long) ru.ru_utime.tv_usec, 283 (long) ru.ru_stime.tv_sec, (long) ru.ru_stime.tv_usec); 284 tprintf("ru_maxrss=%lu, ru_ixrss=%lu, ", 285 ru.ru_maxrss, ru.ru_ixrss); 286 tprintf("ru_idrss=%lu, ru_isrss=%lu, ", 287 ru.ru_idrss, ru.ru_isrss); 288 tprintf("ru_minflt=%lu, ru_majflt=%lu, ru_nswap=%lu, ", 289 ru.ru_minflt, ru.ru_majflt, ru.ru_nswap); 290 tprintf("ru_inblock=%lu, ru_oublock=%lu, ", 291 ru.ru_inblock, ru.ru_oublock); 292 tprintf("ru_msgsnd=%lu, ru_msgrcv=%lu, ", 293 ru.ru_msgsnd, ru.ru_msgrcv); 294 tprintf("ru_nsignals=%lu, ru_nvcsw=%lu, ru_nivcsw=%lu}", 295 ru.ru_nsignals, ru.ru_nvcsw, ru.ru_nivcsw); 296 } 297 else { 298 tprintf("{ru_utime={%lu, %lu}, ru_stime={%lu, %lu}, ...}", 299 (long) ru.ru_utime.tv_sec, (long) ru.ru_utime.tv_usec, 300 (long) ru.ru_stime.tv_sec, (long) ru.ru_stime.tv_usec); 301 } 302} 303#endif 304 305void 306printrusage(tcp, addr) 307struct tcb *tcp; 308long addr; 309{ 310 struct rusage ru; 311 312 if (!addr) 313 tprintf("NULL"); 314 else if (syserror(tcp) || !verbose(tcp)) 315 tprintf("%#lx", addr); 316 else if (umove(tcp, addr, &ru) < 0) 317 tprintf("{...}"); 318 else if (!abbrev(tcp)) { 319 tprintf("{ru_utime={%lu, %lu}, ru_stime={%lu, %lu}, ", 320 (long) ru.ru_utime.tv_sec, (long) ru.ru_utime.tv_usec, 321 (long) ru.ru_stime.tv_sec, (long) ru.ru_stime.tv_usec); 322 tprintf("ru_maxrss=%lu, ru_ixrss=%lu, ", 323 ru.ru_maxrss, ru.ru_ixrss); 324 tprintf("ru_idrss=%lu, ru_isrss=%lu, ", 325 ru.ru_idrss, ru.ru_isrss); 326 tprintf("ru_minflt=%lu, ru_majflt=%lu, ru_nswap=%lu, ", 327 ru.ru_minflt, ru.ru_majflt, ru.ru_nswap); 328 tprintf("ru_inblock=%lu, ru_oublock=%lu, ", 329 ru.ru_inblock, ru.ru_oublock); 330 tprintf("ru_msgsnd=%lu, ru_msgrcv=%lu, ", 331 ru.ru_msgsnd, ru.ru_msgrcv); 332 tprintf("ru_nsignals=%lu, ru_nvcsw=%lu, ru_nivcsw=%lu}", 333 ru.ru_nsignals, ru.ru_nvcsw, ru.ru_nivcsw); 334 } 335 else { 336 tprintf("{ru_utime={%lu, %lu}, ru_stime={%lu, %lu}, ...}", 337 (long) ru.ru_utime.tv_sec, (long) ru.ru_utime.tv_usec, 338 (long) ru.ru_stime.tv_sec, (long) ru.ru_stime.tv_usec); 339 } 340} 341 342int 343sys_getrusage(tcp) 344struct tcb *tcp; 345{ 346 if (entering(tcp)) { 347 printxval(usagewho, tcp->u_arg[0], "RUSAGE_???"); 348 tprintf(", "); 349 } 350 else 351 printrusage(tcp, tcp->u_arg[1]); 352 return 0; 353} 354 355#ifdef ALPHA 356int 357sys_osf_getrusage(tcp) 358struct tcb *tcp; 359{ 360 if (entering(tcp)) { 361 printxval(usagewho, tcp->u_arg[0], "RUSAGE_???"); 362 tprintf(", "); 363 } 364 else 365 printrusage32(tcp, tcp->u_arg[1]); 366 return 0; 367} 368#endif /* ALPHA */ 369 370#endif /* !SVR4 */ 371 372#ifdef LINUX 373 374int 375sys_sysinfo(tcp) 376struct tcb *tcp; 377{ 378 struct sysinfo si; 379 380 if (exiting(tcp)) { 381 if (syserror(tcp) || !verbose(tcp)) 382 tprintf("%#lx", tcp->u_arg[0]); 383 else if (umove(tcp, tcp->u_arg[0], &si) < 0) 384 tprintf("{...}"); 385 else { 386 tprintf("{uptime=%lu, loads=[%lu, %lu, %lu] ", 387 si.uptime, si.loads[0], si.loads[1], 388 si.loads[2]); 389 tprintf("totalram=%lu, freeram=%lu, ", 390 si.totalram, si.freeram); 391 tprintf("sharedram=%lu, bufferram=%lu} ", 392 si.sharedram, si.bufferram); 393 tprintf("totalswap=%lu, freeswap=%lu, procs=%hu}", 394 si.totalswap, si.freeswap, si.procs); 395 } 396 } 397 return 0; 398} 399 400#endif /* LINUX */ 401 402static const struct xlat priorities[] = { 403 { PRIO_PROCESS, "PRIO_PROCESS" }, 404 { PRIO_PGRP, "PRIO_PGRP" }, 405 { PRIO_USER, "PRIO_USER" }, 406 { 0, NULL }, 407}; 408 409int 410sys_getpriority(tcp) 411struct tcb *tcp; 412{ 413 if (entering(tcp)) { 414 printxval(priorities, tcp->u_arg[0], "PRIO_???"); 415 tprintf(", %lu", tcp->u_arg[1]); 416 } 417 return 0; 418} 419 420int 421sys_setpriority(tcp) 422struct tcb *tcp; 423{ 424 if (entering(tcp)) { 425 printxval(priorities, tcp->u_arg[0], "PRIO_???"); 426 tprintf(", %lu, %ld", tcp->u_arg[1], tcp->u_arg[2]); 427 } 428 return 0; 429} 430 431int 432sys_nice(tcp) 433struct tcb *tcp; 434{ 435 if (entering(tcp)) 436 tprintf("%ld", tcp->u_arg[0]); 437 return 0; 438} 439 440#ifndef SUNOS4 441 442int 443sys_times(tcp) 444struct tcb *tcp; 445{ 446 struct tms tbuf; 447 448 if (exiting(tcp)) { 449 if (tcp->u_arg[0] == 0) 450 tprintf("NULL"); 451 else if (syserror(tcp)) 452 tprintf("%#lx", tcp->u_arg[0]); 453 else if (umove(tcp, tcp->u_arg[0], &tbuf) < 0) 454 tprintf("{...}"); 455 else { 456 tprintf("{tms_utime=%lu, tms_stime=%lu, ", 457 tbuf.tms_utime, tbuf.tms_stime); 458 tprintf("tms_cutime=%lu, tms_cstime=%lu}", 459 tbuf.tms_cutime, tbuf.tms_cstime); 460 } 461 } 462 return 0; 463} 464 465#endif /* !SUNOS4 */ 466