• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2004
4  * Copyright (c) Linux Test Project, 2004-2017
5  *
6  *
7  * DESCRIPTION
8  *	hugeshmget02 - check for ENOENT, EEXIST and EINVAL errors
9  *
10  * HISTORY
11  *	03/2001 - Written by Wayne Boyer
12  *	04/2004 - Updated by Robbie Williamson
13  */
14 #include <limits.h>
15 #include "hugetlb.h"
16 
17 static size_t shm_size;
18 static int shm_id_1 = -1;
19 static int shm_nonexistent_key = -1;
20 static key_t shmkey2;
21 
22 static struct tst_option options[] = {
23 	{"s:", &nr_opt, "-s num   Set the number of the been allocated hugepages"},
24 	{NULL, NULL, NULL}
25 };
26 
27 static struct tcase {
28 	int *skey;
29 	int size_coe;
30 	int flags;
31 	int error;
32 } tcases[] = {
33 	/* EINVAL - size is 0 */
34 	{&shmkey2, 0, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
35 	/* EINVAL - size is larger than created segment */
36 	{&shmkey, 2, SHM_HUGETLB | SHM_RW, EINVAL},
37 	/* EEXIST - the segment exists and IPC_CREAT | IPC_EXCL is given */
38 	{&shmkey, 1, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EEXIST},
39 	/* ENOENT - no segment exists for the key and IPC_CREAT is not given */
40 	/* use shm_nonexistend_key (-1) as the key */
41 	{&shm_nonexistent_key, 1, SHM_HUGETLB | SHM_RW, ENOENT}
42 };
43 
test_hugeshmget(unsigned int i)44 static void test_hugeshmget(unsigned int i)
45 {
46 	int shm_id_2 = -1;
47 
48 	if (*tcases[i].skey == -1) {
49 		shm_id_2 = shmget(*(tcases[i].skey), 0, 0);
50 		if (shm_id_2 != -1)
51 			shmctl(shm_id_2, IPC_RMID, NULL);
52 	}
53 
54 	TEST(shmget(*(tcases[i].skey), tcases[i].size_coe * shm_size,
55 					tcases[i].flags));
56 	if (TST_RET != -1) {
57 		tst_res(TFAIL, "shmget succeeded unexpectedly");
58 		return;
59 	}
60 
61 	if (TST_ERR != tcases[i].error) {
62 		tst_res(TFAIL | TTERRNO,
63 			"shmget failed unexpectedly, expected %s",
64 			tst_strerrno(tcases[i].error));
65 		return;
66 	}
67 
68 	tst_res(TPASS | TTERRNO, "shmget failed as expected");
69 }
70 
setup(void)71 void setup(void)
72 {
73 	long hpage_size;
74 
75 	if (tst_hugepages == 0)
76 		tst_brk(TCONF, "No enough hugepages for testing.");
77 
78 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
79 
80 	shm_size = hpage_size * tst_hugepages / 2;
81 	update_shm_size(&shm_size);
82 
83 	shmkey = getipckey();
84 	shmkey2 = shmkey + 1;
85 	shm_id_1 = shmget(shmkey, shm_size,
86 			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
87 	if (shm_id_1 == -1)
88 		tst_brk(TBROK | TERRNO, "shmget #setup");
89 }
90 
cleanup(void)91 void cleanup(void)
92 {
93 	rm_shm(shm_id_1);
94 }
95 
96 static struct tst_test test = {
97 	.needs_root = 1,
98 	.options = options,
99 	.setup = setup,
100 	.cleanup = cleanup,
101 	.test = test_hugeshmget,
102 	.tcnt = ARRAY_SIZE(tcases),
103 	.request_hugepages = 128,
104 };
105