1 /*
2  * Copyright (c) 2016 Fei Jie <feij.fnst@cn.fujitsu.com>
3  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2016-2017 The strace developers.
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 "tests.h"
31 #include <asm/unistd.h>
32 
33 #ifdef __NR_getrusage
34 
35 # include <stdio.h>
36 # include <stdint.h>
37 # include <sys/resource.h>
38 # include <unistd.h>
39 # include <errno.h>
40 
41 # include "xlat.h"
42 # include "xlat/usagewho.h"
43 
44 int
45 invoke_print(int who, const char *who_str, struct rusage *usage)
46 {
47 	int rc = syscall(__NR_getrusage, who, usage);
48 	int saved_errno = errno;
49 	printf("getrusage(%s, {ru_utime={tv_sec=%lld, tv_usec=%llu}"
50 	       ", ru_stime={tv_sec=%lld, tv_usec=%llu}, ru_maxrss=%lu"
51 	       ", ru_ixrss=%lu, ru_idrss=%lu, ru_isrss=%lu, ru_minflt=%lu"
52 	       ", ru_majflt=%lu, ru_nswap=%lu, ru_inblock=%lu"
53 	       ", ru_oublock=%lu, ru_msgsnd=%lu, ru_msgrcv=%lu"
54 	       ", ru_nsignals=%lu, ru_nvcsw=%lu, ru_nivcsw=%lu}) = %s\n",
55 	       who_str,
56 	       (long long) usage->ru_utime.tv_sec,
57 	       zero_extend_signed_to_ull(usage->ru_utime.tv_usec),
58 	       (long long) usage->ru_stime.tv_sec,
59 	       zero_extend_signed_to_ull(usage->ru_stime.tv_usec),
60 	       usage->ru_maxrss, usage->ru_ixrss, usage->ru_idrss,
61 	       usage->ru_isrss, usage->ru_minflt, usage->ru_majflt,
62 	       usage->ru_nswap, usage->ru_inblock, usage->ru_oublock,
63 	       usage->ru_msgsnd, usage->ru_msgrcv, usage->ru_nsignals,
64 	       usage->ru_nvcsw, usage->ru_nivcsw, sprintrc(rc));
65 	errno = saved_errno;
66 	return rc;
67 }
68 
69 int
70 main(void)
71 {
72 	TAIL_ALLOC_OBJECT_CONST_PTR(struct rusage, usage);
73 	if (invoke_print(ARG_STR(RUSAGE_SELF), usage)) {
74 		perror_msg_and_fail("RUSAGE_SELF");
75 	}
76 	if (invoke_print(ARG_STR(RUSAGE_THREAD), usage) && errno != EINVAL) {
77 		perror_msg_and_fail("RUSAGE_THREAD");
78 	}
79 
80 	puts("+++ exited with 0 +++");
81 	return 0;
82 }
83 
84 #else
85 
86 SKIP_MAIN_UNDEFINED("__NR_getrusage")
87 
88 #endif
89