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