xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/settimeofday/settimeofday02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2001-2024
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Check that settimeofday() sets errnos correctly.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_capability.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
21*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval tv;
22*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
23*49cdfc7eSAndroid Build Coastguard Worker 	char *message;
24*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
25*49cdfc7eSAndroid Build Coastguard Worker 	{{-1, 0}, EINVAL, "tv.tv_sec is negative"},
26*49cdfc7eSAndroid Build Coastguard Worker 	{{0, -1}, EINVAL, "tv.tv_usec is outside the range [0..999,999]"},
27*49cdfc7eSAndroid Build Coastguard Worker 	{{100, 100}, EPERM, "calling process without CAP_SYS_TIME capability"},
28*49cdfc7eSAndroid Build Coastguard Worker };
29*49cdfc7eSAndroid Build Coastguard Worker 
verify_settimeofday(unsigned int n)30*49cdfc7eSAndroid Build Coastguard Worker static void verify_settimeofday(unsigned int n)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "%s", tc->message);
35*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(settimeofday(&tc->tv, NULL), tc->exp_errno);
36*49cdfc7eSAndroid Build Coastguard Worker }
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
39*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_settimeofday,
40*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
41*49cdfc7eSAndroid Build Coastguard Worker 	.caps = (struct tst_cap []) {
42*49cdfc7eSAndroid Build Coastguard Worker 		TST_CAP(TST_CAP_DROP, CAP_SYS_TIME),
43*49cdfc7eSAndroid Build Coastguard Worker 		{}
44*49cdfc7eSAndroid Build Coastguard Worker 	},
45*49cdfc7eSAndroid Build Coastguard Worker };
46