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