1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) International Business Machines  Corp., 2001
4 * Ported to LTP 07/2001 John George
5 * Testcase to check that settimeofday() sets errnos correctly.
6 */
7
8#include <stdio.h>
9#include <sys/time.h>
10#include <errno.h>
11#include "tst_capability.h"
12#include "tst_test.h"
13#include "lapi/syscalls.h"
14
15static struct tcase {
16	struct timeval tv;
17	int exp_errno;
18	char *message;
19} tcases[] = {
20	{{-1, 0}, EINVAL, "tv.tv_sec is negative"},
21	{{0, -1}, EINVAL, "tv.tv_usec is outside the range [0..999,999]"},
22	{{100, 100}, EPERM, "calling process without CAP_SYS_TIME capability"},
23};
24
25static void verify_settimeofday(unsigned int n)
26{
27	struct tcase *tc = &tcases[n];
28
29	tst_res(TINFO, "%s", tc->message);
30	TEST(settimeofday(&tc->tv, NULL));
31	if (TST_RET != -1) {
32		tst_res(TFAIL, "settimeofday() succeeded unexpectedly");
33		return;
34	}
35
36	if (TST_ERR != tc->exp_errno)
37		tst_res(TFAIL | TTERRNO, "Expected %s got ", tst_strerrno(tc->exp_errno));
38	else
39		tst_res(TPASS | TTERRNO, "Received expected errno");
40}
41
42static struct tst_test test = {
43	.test = verify_settimeofday,
44	.tcnt = ARRAY_SIZE(tcases),
45	.caps = (struct tst_cap []) {
46		TST_CAP(TST_CAP_DROP, CAP_SYS_TIME),
47		{}
48	},
49};
50