1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *
4  * This program is free software;  you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.
16  */
17 
18 /*
19  * DESCRIPTION
20  * test for an ENOSPC error by using up all available
21  * message queues.
22  *
23  */
24 
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/ipc.h>
28 #include <sys/msg.h>
29 #include <stdlib.h>
30 
31 #include "tst_test.h"
32 #include "tst_safe_sysv_ipc.h"
33 #include "libnewipc.h"
34 
35 static int maxmsgs;
36 static int *queues;
37 static key_t msgkey;
38 
verify_msgget(void)39 static void verify_msgget(void)
40 {
41 	TEST(msgget(msgkey + maxmsgs, IPC_CREAT | IPC_EXCL));
42 	if (TST_RET != -1)
43 		tst_res(TFAIL, "msgget() succeeded unexpectedly");
44 
45 	if (TST_ERR == ENOSPC) {
46 		tst_res(TPASS | TTERRNO, "msgget() failed as expected");
47 	} else {
48 		tst_res(TFAIL | TTERRNO, "msgget() failed unexpectedly,"
49 			" expected ENOSPC");
50 	}
51 }
52 
setup(void)53 static void setup(void)
54 {
55 	int res, num;
56 
57 	msgkey = GETIPCKEY();
58 
59 	SAFE_FILE_SCANF("/proc/sys/kernel/msgmni", "%i", &maxmsgs);
60 
61 	queues = SAFE_MALLOC(maxmsgs * sizeof(int));
62 
63 	for (num = 0; num < maxmsgs; num++) {
64 		queues[num] = -1;
65 
66 		res = msgget(msgkey + num, IPC_CREAT | IPC_EXCL);
67 		if (res != -1)
68 			queues[num] = res;
69 	}
70 
71 	tst_res(TINFO, "The maximum number of message queues (%d) reached",
72 		maxmsgs);
73 }
74 
cleanup(void)75 static void cleanup(void)
76 {
77 	int num;
78 
79 	if (!queues)
80 		return;
81 
82 	for (num = 0; num < maxmsgs; num++) {
83 		if (queues[num] != -1)
84 			SAFE_MSGCTL(queues[num], IPC_RMID, NULL);
85 	}
86 
87 	free(queues);
88 }
89 
90 static struct tst_test test = {
91 	.needs_tmpdir = 1,
92 	.setup = setup,
93 	.cleanup = cleanup,
94 	.test_all = verify_msgget
95 };
96