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 *
7f08c3bdfSopenharmony_ci * DESCRIPTION
8f08c3bdfSopenharmony_ci *	hugeshmget02 - check for ENOENT, EEXIST and EINVAL errors
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#include <limits.h>
15f08c3bdfSopenharmony_ci#include "hugetlb.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_cistatic size_t shm_size;
18f08c3bdfSopenharmony_cistatic int shm_id_1 = -1;
19f08c3bdfSopenharmony_cistatic int shm_nonexistent_key = -1;
20f08c3bdfSopenharmony_cistatic key_t shmkey2;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic struct tcase {
23f08c3bdfSopenharmony_ci	int *skey;
24f08c3bdfSopenharmony_ci	int size_coe;
25f08c3bdfSopenharmony_ci	int flags;
26f08c3bdfSopenharmony_ci	int error;
27f08c3bdfSopenharmony_ci} tcases[] = {
28f08c3bdfSopenharmony_ci	/* EINVAL - size is 0 */
29f08c3bdfSopenharmony_ci	{&shmkey2, 0, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
30f08c3bdfSopenharmony_ci	/* EINVAL - size is larger than created segment */
31f08c3bdfSopenharmony_ci	{&shmkey, 2, SHM_HUGETLB | SHM_RW, EINVAL},
32f08c3bdfSopenharmony_ci	/* EEXIST - the segment exists and IPC_CREAT | IPC_EXCL is given */
33f08c3bdfSopenharmony_ci	{&shmkey, 1, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EEXIST},
34f08c3bdfSopenharmony_ci	/* ENOENT - no segment exists for the key and IPC_CREAT is not given */
35f08c3bdfSopenharmony_ci	/* use shm_nonexistend_key (-1) as the key */
36f08c3bdfSopenharmony_ci	{&shm_nonexistent_key, 1, SHM_HUGETLB | SHM_RW, ENOENT}
37f08c3bdfSopenharmony_ci};
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void test_hugeshmget(unsigned int i)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	int shm_id_2 = -1;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	if (*tcases[i].skey == -1) {
44f08c3bdfSopenharmony_ci		shm_id_2 = shmget(*(tcases[i].skey), 0, 0);
45f08c3bdfSopenharmony_ci		if (shm_id_2 != -1)
46f08c3bdfSopenharmony_ci			shmctl(shm_id_2, IPC_RMID, NULL);
47f08c3bdfSopenharmony_ci	}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	TEST(shmget(*(tcases[i].skey), tcases[i].size_coe * shm_size,
50f08c3bdfSopenharmony_ci					tcases[i].flags));
51f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
52f08c3bdfSopenharmony_ci		tst_res(TFAIL, "shmget succeeded unexpectedly");
53f08c3bdfSopenharmony_ci		return;
54f08c3bdfSopenharmony_ci	}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	if (TST_ERR != tcases[i].error) {
57f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
58f08c3bdfSopenharmony_ci			"shmget failed unexpectedly, expected %s",
59f08c3bdfSopenharmony_ci			tst_strerrno(tcases[i].error));
60f08c3bdfSopenharmony_ci		return;
61f08c3bdfSopenharmony_ci	}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	tst_res(TPASS | TTERRNO, "shmget failed as expected");
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_civoid setup(void)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	long hpage_size;
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	if (tst_hugepages == 0)
71f08c3bdfSopenharmony_ci		tst_brk(TCONF, "No enough hugepages for testing.");
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	shm_size = hpage_size * tst_hugepages / 2;
76f08c3bdfSopenharmony_ci	update_shm_size(&shm_size);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	shmkey = getipckey();
79f08c3bdfSopenharmony_ci	shmkey2 = shmkey + 1;
80f08c3bdfSopenharmony_ci	shm_id_1 = shmget(shmkey, shm_size,
81f08c3bdfSopenharmony_ci			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
82f08c3bdfSopenharmony_ci	if (shm_id_1 == -1)
83f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "shmget #setup");
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_civoid cleanup(void)
87f08c3bdfSopenharmony_ci{
88f08c3bdfSopenharmony_ci	rm_shm(shm_id_1);
89f08c3bdfSopenharmony_ci}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_cistatic struct tst_test test = {
92f08c3bdfSopenharmony_ci	.needs_root = 1,
93f08c3bdfSopenharmony_ci	.options = (struct tst_option[]) {
94f08c3bdfSopenharmony_ci		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
95f08c3bdfSopenharmony_ci		{}
96f08c3bdfSopenharmony_ci	},
97f08c3bdfSopenharmony_ci	.setup = setup,
98f08c3bdfSopenharmony_ci	.cleanup = cleanup,
99f08c3bdfSopenharmony_ci	.test = test_hugeshmget,
100f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
101f08c3bdfSopenharmony_ci	.hugepages = {128, TST_REQUEST},
102f08c3bdfSopenharmony_ci};
103