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
24 static int maxmsgs, queue_cnt, used_cnt;
25 static int *queues;
26 static key_t msgkey;
27
verify_msgget(void)28 static 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
setup(void)34 static 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 SAFE_FILE_SCANF("/proc/sys/kernel/msgmni", "%i", &maxmsgs);
45
46 queues = SAFE_MALLOC((maxmsgs - used_cnt) * sizeof(int));
47
48 for (num = 0; num < maxmsgs - used_cnt; num++) {
49 res = msgget(msgkey + num, IPC_CREAT | IPC_EXCL);
50 if (res == -1)
51 tst_brk(TBROK | TERRNO, "msgget failed unexpectedly");
52 queues[queue_cnt++] = res;
53 }
54
55 tst_res(TINFO, "The maximum number of message queues (%d) reached",
56 maxmsgs);
57 }
58
cleanup(void)59 static void cleanup(void)
60 {
61 int num;
62
63 if (!queues)
64 return;
65
66 for (num = 0; num < queue_cnt; num++)
67 SAFE_MSGCTL(queues[num], IPC_RMID, NULL);
68
69 free(queues);
70 }
71
72 static struct tst_test test = {
73 .needs_tmpdir = 1,
74 .setup = setup,
75 .cleanup = cleanup,
76 .test_all = verify_msgget
77 };
78