1 /*
2 * Copyright (c) International Business Machines Corp., 2004
3 * Copyright (c) Linux Test Project, 2004-2017
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 */
15
16 /*
17 * DESCRIPTION
18 * hugeshmget01 - test that shmget() correctly creates a large
19 * shared memory segment
20 *
21 * HISTORY
22 * 03/2001 - Written by Wayne Boyer
23 * 04/2004 - Updated by Robbie Williamson
24 */
25
26 #include <limits.h>
27 #include "hugetlb.h"
28 #include "hugetlb.h"
29
30 static size_t shm_size;
31 static int shm_id_1 = -1;
32
33 static long hugepages = 128;
34 static struct tst_option options[] = {
35 {"s:", &nr_opt, "-s num Set the number of the been allocated hugepages"},
36 {NULL, NULL, NULL}
37 };
38
test_hugeshmget(void)39 static void test_hugeshmget(void)
40 {
41 struct shmid_ds buf;
42
43 if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
44 tst_res(TFAIL | TERRNO, "shmctl(IPC_STAT)");
45 return;
46 }
47
48 if (buf.shm_segsz != shm_size) {
49 tst_res(TFAIL, "seqment size is not correct");
50 return;
51 }
52
53 if (buf.shm_cpid != getpid()) {
54 tst_res(TFAIL, "creator pid is not correct");
55 return;
56 }
57
58 if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW) & MODE_MASK)) {
59 tst_res(TFAIL, "segment mode is not correct");
60 return;
61 }
62
63 tst_res(TPASS, "size, pid & mode are correct");
64 }
65
setup(void)66 static void setup(void)
67 {
68 long hpage_size;
69
70 save_nr_hugepages();
71 if (nr_opt)
72 hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
73
74 set_sys_tune("nr_hugepages", hugepages, 1);
75 hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
76
77 shm_size = hpage_size * hugepages / 2;
78 update_shm_size(&shm_size);
79 shmkey = getipckey();
80
81 shm_id_1 = shmget(shmkey, shm_size,
82 SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
83 if (shm_id_1 == -1)
84 tst_brk(TBROK | TERRNO, "shmget");
85 }
86
cleanup(void)87 static void cleanup(void)
88 {
89 rm_shm(shm_id_1);
90 restore_nr_hugepages();
91 }
92
93 static struct tst_test test = {
94 .needs_root = 1,
95 .options = options,
96 .setup = setup,
97 .cleanup = cleanup,
98 .test_all = test_hugeshmget,
99 };
100