1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) International Business Machines Corp., 2001 4 */ 5 6/*\ 7 * [Description] 8 * 9 * Test for ENOSPC error. 10 * 11 * ENOSPC - a semaphore set exceed the maximum number of semaphore sets(SEMMNI) 12 */ 13 14#include <stdio.h> 15#include <stdlib.h> 16#include <unistd.h> 17#include <sys/types.h> 18#include <sys/ipc.h> 19#include "lapi/sem.h" 20#include "tst_test.h" 21#include "libnewipc.h" 22#include "tst_safe_sysv_ipc.h" 23 24static int *sem_id_arr; 25static int maxsems, array_cnt, used_cnt; 26static key_t semkey; 27 28static void verify_semget(void) 29{ 30 TST_EXP_FAIL2(semget(semkey + maxsems, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA), 31 ENOSPC, "semget(%i, %i, %i)", semkey + maxsems, PSEMS, 32 IPC_CREAT | IPC_EXCL | SEM_RA); 33} 34 35static void setup(void) 36{ 37 int res, num; 38 39 semkey = GETIPCKEY(); 40 used_cnt = GET_USED_ARRAYS(); 41 tst_res(TINFO, "Current environment %d semaphore arrays are already in use", 42 used_cnt); 43 SAFE_FILE_SCANF("/proc/sys/kernel/sem", "%*d %*d %*d %d", &maxsems); 44 45 /* Prevent timeout due to high semaphore array limit */ 46 tst_set_max_runtime(maxsems / 200); 47 48 sem_id_arr = SAFE_MALLOC((maxsems - used_cnt) * sizeof(int)); 49 for (num = 0; num < maxsems - used_cnt; num++) { 50 res = semget(semkey + num, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA); 51 if (res == -1) 52 tst_brk(TBROK | TERRNO, "semget failed unexpectedly"); 53 54 sem_id_arr[array_cnt++] = res; 55 } 56 tst_res(TINFO, "The maximum number of semaphore arrays (%d) has been reached", 57 maxsems); 58} 59 60static void cleanup(void) 61{ 62 int num; 63 64 if (!sem_id_arr) 65 return; 66 67 for (num = 0; num < array_cnt; num++) 68 SAFE_SEMCTL(sem_id_arr[num], PSEMS, IPC_RMID); 69 70 free(sem_id_arr); 71} 72 73static struct tst_test test = { 74 .needs_tmpdir = 1, 75 .setup = setup, 76 .cleanup = cleanup, 77 .test_all = verify_semget, 78 .save_restore = (const struct tst_path_val[]){ 79 {"/proc/sys/kernel/sem", NULL, 80 TST_SR_TCONF_MISSING | TST_SR_SKIP_RO}, 81 {} 82 } 83}; 84