xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getrusage/getrusage02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4  * AUTHOR : Saji Kumar.V.R <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Verify that getrusage() fails with:
11  *
12  * - EINVAL with invalid who
13  * - EFAULT with invalid usage pointer
14  */
15 
16 #include <errno.h>
17 #include <sched.h>
18 #include <sys/resource.h>
19 #include "tst_test.h"
20 #include "lapi/syscalls.h"
21 
22 static struct rusage usage;
23 
libc_getrusage(int who,void * usage)24 static int libc_getrusage(int who, void *usage)
25 {
26 	return getrusage(who, usage);
27 }
28 
sys_getrusage(int who,void * usage)29 static int sys_getrusage(int who, void *usage)
30 {
31 	return tst_syscall(__NR_getrusage, who, usage);
32 }
33 
34 struct tc_t {
35 	int who;
36 	struct rusage *usage;
37 	int exp_errno;
38 } tc[] = {
39 	{-2, &usage, EINVAL},
40 	{RUSAGE_SELF, (struct rusage *)-1, EFAULT}
41 };
42 
43 static struct test_variants
44 {
45 	int (*getrusage)(int who, void *usage);
46 	char *desc;
47 } variants[] = {
48 	{ .getrusage = libc_getrusage, .desc = "libc getrusage()"},
49 #if (__NR_getrusage != __LTP__NR_INVALID_SYSCALL)
50 	{ .getrusage = sys_getrusage,  .desc = "__NR_getrusage syscall"},
51 #endif
52 };
53 
verify_getrusage(unsigned int i)54 static void verify_getrusage(unsigned int i)
55 {
56 	struct test_variants *tv = &variants[tst_variant];
57 
58 	if (tc[i].exp_errno == EFAULT &&
59 	    tv->getrusage == libc_getrusage) {
60 		tst_res(TCONF, "EFAULT is skipped for libc variant");
61 		return;
62 	}
63 
64 	TST_EXP_FAIL(tv->getrusage(tc[i].who, tc[i].usage), tc[i].exp_errno,
65 	             "getrusage(%i, %p)", tc[i].who, tc[i].usage);
66 }
67 
setup(void)68 static void setup(void)
69 {
70 	struct test_variants *tv = &variants[tst_variant];
71 
72 	tst_res(TINFO, "Testing variant: %s", tv->desc);
73 }
74 
75 static struct tst_test test = {
76 	.test = verify_getrusage,
77 	.setup = setup,
78 	.tcnt = ARRAY_SIZE(tc),
79 	.test_variants = ARRAY_SIZE(variants),
80 };
81