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