1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2004
4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2004-2017
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * DESCRIPTION
7f08c3bdfSopenharmony_ci *	hugeshmget01 - test that shmget() correctly creates a large
8f08c3bdfSopenharmony_ci *			shared memory segment
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * HISTORY
11f08c3bdfSopenharmony_ci *	03/2001 - Written by Wayne Boyer
12f08c3bdfSopenharmony_ci *	04/2004 - Updated by Robbie Williamson
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <limits.h>
16f08c3bdfSopenharmony_ci#include "hugetlb.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic size_t shm_size;
19f08c3bdfSopenharmony_cistatic int shm_id_1 = -1;
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic void test_hugeshmget(void)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	struct shmid_ds buf;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
26f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO,	"shmctl(IPC_STAT)");
27f08c3bdfSopenharmony_ci		return;
28f08c3bdfSopenharmony_ci	}
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	if (buf.shm_segsz != shm_size) {
31f08c3bdfSopenharmony_ci		tst_res(TFAIL, "seqment size is not correct");
32f08c3bdfSopenharmony_ci		return;
33f08c3bdfSopenharmony_ci	}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	if (buf.shm_cpid != getpid()) {
36f08c3bdfSopenharmony_ci		tst_res(TFAIL, "creator pid is not correct");
37f08c3bdfSopenharmony_ci		return;
38f08c3bdfSopenharmony_ci	}
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW) & MODE_MASK)) {
41f08c3bdfSopenharmony_ci		tst_res(TFAIL, "segment mode is not correct");
42f08c3bdfSopenharmony_ci		return;
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	tst_res(TPASS, "size, pid & mode are correct");
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void setup(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	long hpage_size;
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	if (tst_hugepages == 0)
53f08c3bdfSopenharmony_ci		tst_brk(TCONF, "No enough hugepages for testing.");
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	shm_size = hpage_size * tst_hugepages / 2;
58f08c3bdfSopenharmony_ci	update_shm_size(&shm_size);
59f08c3bdfSopenharmony_ci	shmkey = getipckey();
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	shm_id_1 = shmget(shmkey, shm_size,
62f08c3bdfSopenharmony_ci			SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
63f08c3bdfSopenharmony_ci	if (shm_id_1 == -1)
64f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "shmget");
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic void cleanup(void)
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	rm_shm(shm_id_1);
70f08c3bdfSopenharmony_ci}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_cistatic struct tst_test test = {
73f08c3bdfSopenharmony_ci	.needs_root = 1,
74f08c3bdfSopenharmony_ci	.options = (struct tst_option[]) {
75f08c3bdfSopenharmony_ci		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
76f08c3bdfSopenharmony_ci		{}
77f08c3bdfSopenharmony_ci	},
78f08c3bdfSopenharmony_ci	.setup = setup,
79f08c3bdfSopenharmony_ci	.cleanup = cleanup,
80f08c3bdfSopenharmony_ci	.test_all = test_hugeshmget,
81f08c3bdfSopenharmony_ci	.hugepages = {128, TST_REQUEST},
82f08c3bdfSopenharmony_ci};
83