1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) International Business Machines Corp., 2001 4 * 03/2001 - Written by Wayne Boyer 5 */ 6 7/*\ 8 * [Description] 9 * 10 * Test for ENOSPC error. 11 * 12 * ENOSPC - All possible shared memory segments have been taken (SHMMNI). 13 */ 14#include <errno.h> 15#include <sys/types.h> 16#include <sys/ipc.h> 17#include <stdlib.h> 18#include <pwd.h> 19#include <sys/shm.h> 20#include "tst_test.h" 21#include "tst_safe_sysv_ipc.h" 22#include "libnewipc.h" 23 24static int *queues; 25static int maxshms, queue_cnt, used_cnt; 26static key_t shmkey; 27 28static void verify_shmget(void) 29{ 30 TST_EXP_FAIL2(shmget(shmkey + maxshms, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW), ENOSPC, 31 "shmget(%i, %i, %i)", shmkey + maxshms, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW); 32} 33 34static void setup(void) 35{ 36 int res, num; 37 38 shmkey = GETIPCKEY(); 39 used_cnt = GET_USED_SEGMENTS(); 40 tst_res(TINFO, "Current environment %d shared memory segments are already in use", 41 used_cnt); 42 SAFE_FILE_SCANF("/proc/sys/kernel/shmmni", "%i", &maxshms); 43 44 queues = SAFE_MALLOC((maxshms - used_cnt) * sizeof(int)); 45 for (num = 0; num < maxshms - used_cnt; num++) { 46 res = shmget(shmkey + num, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW); 47 if (res == -1) 48 tst_brk(TBROK | TERRNO, "shmget failed unexpectedly"); 49 50 queues[queue_cnt++] = res; 51 } 52 tst_res(TINFO, "The maximum number of memory segments (%d) has been reached", 53 maxshms); 54} 55 56static void cleanup(void) 57{ 58 int num; 59 60 if (!queues) 61 return; 62 63 for (num = 0; num < queue_cnt; num++) 64 SAFE_SHMCTL(queues[num], IPC_RMID, NULL); 65 66 free(queues); 67} 68 69static struct tst_test test = { 70 .needs_tmpdir = 1, 71 .setup = setup, 72 .cleanup = cleanup, 73 .test_all = verify_shmget, 74}; 75