xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getitimer/getitimer02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  *               03/2001 - Written by Wayne Boyer
5  * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Check that getitimer() call fails:
12  *
13  * 1. EFAULT with invalid itimerval pointer
14  * 2. EINVAL when called with an invalid first argument
15  */
16 
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <sys/time.h>
20 #include "tst_test.h"
21 #include "lapi/syscalls.h"
22 
23 static struct itimerval *value;
24 static struct itimerval *invalid;
25 
26 static struct tcase {
27        int which;
28        struct itimerval **val;
29        int exp_errno;
30 } tcases[] = {
31        {ITIMER_REAL,    &invalid, EFAULT},
32        {ITIMER_VIRTUAL, &invalid, EFAULT},
33        {-ITIMER_PROF,   &value,   EINVAL},
34 };
35 
sys_getitimer(int which,void * curr_value)36 static int sys_getitimer(int which, void *curr_value)
37 {
38         return tst_syscall(__NR_getitimer, which, curr_value);
39 }
40 
setup(void)41 static void setup(void)
42 {
43         value = SAFE_MALLOC(sizeof(struct itimerval));
44         invalid = (struct itimerval *)-1;
45 }
46 
verify_getitimer(unsigned int i)47 static void verify_getitimer(unsigned int i)
48 {
49         struct tcase *tc = &tcases[i];
50 
51         TST_EXP_FAIL(sys_getitimer(tc->which, *(tc->val)), tc->exp_errno);
52 }
53 
cleanup(void)54 static void cleanup(void)
55 {
56         free(value);
57         value = NULL;
58 }
59 
60 static struct tst_test test = {
61         .tcnt = ARRAY_SIZE(tcases),
62         .test = verify_getitimer,
63         .setup = setup,
64         .cleanup = cleanup,
65 };
66