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