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