1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 */
5
6 /*
7 * DESCRIPTION
8 * test for an ENOSPC error by using up all available
9 * message queues.
10 *
11 */
12
13 #include <errno.h>
14 #include <sys/types.h>
15 #include <sys/ipc.h>
16 #include <sys/msg.h>
17 #include <stdlib.h>
18
19 #include "tst_test.h"
20 #include "tst_safe_sysv_ipc.h"
21 #include "libnewipc.h"
22
23 static int maxmsgs;
24 static int *queues;
25 static key_t msgkey;
26
verify_msgget(void)27 static void verify_msgget(void)
28 {
29 TEST(msgget(msgkey + maxmsgs, IPC_CREAT | IPC_EXCL));
30 if (TST_RET != -1)
31 tst_res(TFAIL, "msgget() succeeded unexpectedly");
32
33 if (TST_ERR == ENOSPC) {
34 tst_res(TPASS | TTERRNO, "msgget() failed as expected");
35 } else {
36 tst_res(TFAIL | TTERRNO, "msgget() failed unexpectedly,"
37 " expected ENOSPC");
38 }
39 }
40
setup(void)41 static void setup(void)
42 {
43 int res, num;
44
45 msgkey = GETIPCKEY();
46
47 SAFE_FILE_SCANF("/proc/sys/kernel/msgmni", "%i", &maxmsgs);
48
49 queues = SAFE_MALLOC(maxmsgs * sizeof(int));
50
51 for (num = 0; num < maxmsgs; num++) {
52 queues[num] = -1;
53
54 res = msgget(msgkey + num, IPC_CREAT | IPC_EXCL);
55 if (res != -1)
56 queues[num] = res;
57 }
58
59 tst_res(TINFO, "The maximum number of message queues (%d) reached",
60 maxmsgs);
61 }
62
cleanup(void)63 static void cleanup(void)
64 {
65 int num;
66
67 if (!queues)
68 return;
69
70 for (num = 0; num < maxmsgs; num++) {
71 if (queues[num] != -1)
72 SAFE_MSGCTL(queues[num], IPC_RMID, NULL);
73 }
74
75 free(queues);
76 }
77
78 static struct tst_test test = {
79 .needs_tmpdir = 1,
80 .setup = setup,
81 .cleanup = cleanup,
82 .test_all = verify_msgget
83 };
84