1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * Description:
6f08c3bdfSopenharmony_ci * Cross verify the _high fields being set to 0 by the kernel.
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci#include "lapi/sembuf.h"
9f08c3bdfSopenharmony_ci#include "lapi/sem.h"
10f08c3bdfSopenharmony_ci#include "tst_test.h"
11f08c3bdfSopenharmony_ci#include "libnewipc.h"
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#ifdef HAVE_SEMID64_DS_TIME_HIGH
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_cistatic void run(void)
16f08c3bdfSopenharmony_ci{
17f08c3bdfSopenharmony_ci	struct semid64_ds buf_ds = {
18f08c3bdfSopenharmony_ci		.sem_otime_high = 0x0A0A,
19f08c3bdfSopenharmony_ci		.sem_ctime_high = 0x0A0A,
20f08c3bdfSopenharmony_ci	};
21f08c3bdfSopenharmony_ci	int semid;
22f08c3bdfSopenharmony_ci	union semun arg;
23f08c3bdfSopenharmony_ci	key_t key;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	/* get an IPC resource key */
26f08c3bdfSopenharmony_ci	key = GETIPCKEY();
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci	semid = semget(key, 1, SEM_RA | IPC_CREAT);
29f08c3bdfSopenharmony_ci	if (semid == -1)
30f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "couldn't create semaphore");
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	arg.buf = (struct semid_ds *)&buf_ds;
33f08c3bdfSopenharmony_ci	TEST(semctl(semid, 0, IPC_STAT, arg));
34f08c3bdfSopenharmony_ci	if (TST_RET == -1)
35f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO, "semctl() failed");
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	if (buf_ds.sem_otime_high || buf_ds.sem_ctime_high)
38f08c3bdfSopenharmony_ci		tst_res(TFAIL, "time_high fields aren't cleared by the kernel");
39f08c3bdfSopenharmony_ci	else
40f08c3bdfSopenharmony_ci		tst_res(TPASS, "time_high fields cleared by the kernel");
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	if (semctl(semid, 0, IPC_RMID, arg) == -1)
43f08c3bdfSopenharmony_ci		tst_res(TINFO, "WARNING: semaphore deletion failed.");
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic struct tst_test test = {
47f08c3bdfSopenharmony_ci	.test_all = run,
48f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
49f08c3bdfSopenharmony_ci};
50f08c3bdfSopenharmony_ci#else
51f08c3bdfSopenharmony_ciTST_TEST_TCONF("test requires struct semid64_ds to have the time_high fields");
52f08c3bdfSopenharmony_ci#endif
53