1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2020 Cyril Hrubis <chrubis@suse.cz>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Test for a SHM_LOCK and SHM_UNLOCK.
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#define _GNU_SOURCE
13f08c3bdfSopenharmony_ci#include <stdio.h>
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
16f08c3bdfSopenharmony_ci#include "libnewipc.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#define SHM_SIZE 2048
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic int shm_id = -1;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic void verify_shmlock(void)
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	struct shmid_ds ds;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	TEST(shmctl(shm_id, SHM_LOCK, NULL));
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci	if (TST_RET != 0)
29f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "shmctl(%i, SHM_LOCK, NULL)", shm_id);
30f08c3bdfSopenharmony_ci	else
31f08c3bdfSopenharmony_ci		tst_res(TPASS, "shmctl(%i, SHM_LOCK, NULL)", shm_id);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	SAFE_SHMCTL(shm_id, IPC_STAT, &ds);
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	if (ds.shm_perm.mode & SHM_LOCKED)
37f08c3bdfSopenharmony_ci		tst_res(TPASS, "SMH_LOCKED bit is on in shm_perm.mode");
38f08c3bdfSopenharmony_ci	else
39f08c3bdfSopenharmony_ci		tst_res(TFAIL, "SHM_LOCKED bit is off in shm_perm.mode");
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	TEST(shmctl(shm_id, SHM_UNLOCK, NULL));
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	if (TST_RET != 0)
44f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "shmctl(%i, SHM_UNLOCK, NULL)", shm_id);
45f08c3bdfSopenharmony_ci	else
46f08c3bdfSopenharmony_ci		tst_res(TPASS, "shmctl(%i, SHM_UNLOCK, NULL)", shm_id);
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	SAFE_SHMCTL(shm_id, IPC_STAT, &ds);
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	if (ds.shm_perm.mode & SHM_LOCKED)
51f08c3bdfSopenharmony_ci		tst_res(TFAIL, "SMH_LOCKED bit is on in shm_perm.mode");
52f08c3bdfSopenharmony_ci	else
53f08c3bdfSopenharmony_ci		tst_res(TPASS, "SHM_LOCKED bit is off in shm_perm.mode");
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic void setup(void)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	shm_id = SAFE_SHMGET(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | SHM_RW);
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic void cleanup(void)
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	if (shm_id >= 0)
64f08c3bdfSopenharmony_ci		SAFE_SHMCTL(shm_id, IPC_RMID, NULL);
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic struct tst_test test = {
68f08c3bdfSopenharmony_ci	.setup = setup,
69f08c3bdfSopenharmony_ci	.cleanup = cleanup,
70f08c3bdfSopenharmony_ci	.test_all = verify_shmlock,
71f08c3bdfSopenharmony_ci};
72