1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * Test Description:
6f08c3bdfSopenharmony_ci *   Verify that the system call stime() fails to set the system's idea
7f08c3bdfSopenharmony_ci *   of data and time if invoked by "non-root" user.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Expected Result:
10f08c3bdfSopenharmony_ci *  stime() should fail with return value -1 and set errno to EPERM.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * History
13f08c3bdfSopenharmony_ci *	07/2001 John George
14f08c3bdfSopenharmony_ci *		-Ported
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <sys/types.h>
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci#include <time.h>
20f08c3bdfSopenharmony_ci#include <pwd.h>
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include "tst_test.h"
23f08c3bdfSopenharmony_ci#include "stime_var.h"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic time_t new_time;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic void run(void)
28f08c3bdfSopenharmony_ci{
29f08c3bdfSopenharmony_ci	TEST(do_stime(&new_time));
30f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
31f08c3bdfSopenharmony_ci		tst_res(TFAIL,
32f08c3bdfSopenharmony_ci			"stime() returned %ld, expected -1 EPERM", TST_RET);
33f08c3bdfSopenharmony_ci		return;
34f08c3bdfSopenharmony_ci	}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	if (TST_ERR == EPERM) {
37f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "stime(2) fails, Caller not root");
38f08c3bdfSopenharmony_ci		return;
39f08c3bdfSopenharmony_ci	}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	tst_res(TFAIL | TTERRNO,
42f08c3bdfSopenharmony_ci		"stime(2) fails, Caller not root, expected errno:%d", EPERM);
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic void setup(void)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	time_t curr_time;
48f08c3bdfSopenharmony_ci	struct passwd *ltpuser;
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	stime_info();
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	ltpuser = SAFE_GETPWNAM("nobody");
53f08c3bdfSopenharmony_ci	SAFE_SETUID(ltpuser->pw_uid);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	if ((curr_time = time(NULL)) < 0)
56f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "time() failed");
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	new_time = curr_time + 10;
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic struct tst_test test = {
62f08c3bdfSopenharmony_ci	.test_all = run,
63f08c3bdfSopenharmony_ci	.setup = setup,
64f08c3bdfSopenharmony_ci	.needs_root = 1,
65f08c3bdfSopenharmony_ci	.test_variants = TEST_VARIANTS,
66f08c3bdfSopenharmony_ci};
67