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 -  All possible message queues have been taken (MSGMNI)
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <errno.h>
15f08c3bdfSopenharmony_ci#include <sys/types.h>
16f08c3bdfSopenharmony_ci#include <sys/ipc.h>
17f08c3bdfSopenharmony_ci#include <sys/msg.h>
18f08c3bdfSopenharmony_ci#include <stdlib.h>
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include "tst_test.h"
21f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
22f08c3bdfSopenharmony_ci#include "libnewipc.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic int maxmsgs, queue_cnt, used_cnt;
25f08c3bdfSopenharmony_cistatic int *queues;
26f08c3bdfSopenharmony_cistatic key_t msgkey;
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void verify_msgget(void)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(msgget(msgkey + maxmsgs, IPC_CREAT | IPC_EXCL), ENOSPC,
31f08c3bdfSopenharmony_ci		"msgget(%i, %i)", msgkey + maxmsgs, IPC_CREAT | IPC_EXCL);
32f08c3bdfSopenharmony_ci}
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic void setup(void)
35f08c3bdfSopenharmony_ci{
36f08c3bdfSopenharmony_ci	int res, num;
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	msgkey = GETIPCKEY();
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	used_cnt = GET_USED_QUEUES();
41f08c3bdfSopenharmony_ci	tst_res(TINFO, "Current environment %d message queues are already in use",
42f08c3bdfSopenharmony_ci		used_cnt);
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	maxmsgs = used_cnt + 32;
45f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF("/proc/sys/kernel/msgmni", "%i", maxmsgs);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	queues = SAFE_MALLOC((maxmsgs - used_cnt) * sizeof(int));
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	for (num = 0; num < maxmsgs - used_cnt; num++) {
50f08c3bdfSopenharmony_ci		res = msgget(msgkey + num, IPC_CREAT | IPC_EXCL);
51f08c3bdfSopenharmony_ci		if (res == -1)
52f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO, "msgget failed unexpectedly");
53f08c3bdfSopenharmony_ci		queues[queue_cnt++] = res;
54f08c3bdfSopenharmony_ci	}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	tst_res(TINFO, "The maximum number of message queues (%d) reached",
57f08c3bdfSopenharmony_ci		maxmsgs);
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void cleanup(void)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	int num;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	if (!queues)
65f08c3bdfSopenharmony_ci		return;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	for (num = 0; num < queue_cnt; num++)
68f08c3bdfSopenharmony_ci		SAFE_MSGCTL(queues[num], IPC_RMID, NULL);
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	free(queues);
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_msgget,
78f08c3bdfSopenharmony_ci	.save_restore = (const struct tst_path_val[]){
79f08c3bdfSopenharmony_ci		{"/proc/sys/kernel/msgmni", NULL, TST_SR_TCONF},
80f08c3bdfSopenharmony_ci		{}
81f08c3bdfSopenharmony_ci	}
82f08c3bdfSopenharmony_ci};
83