1 /* Copyright (c) 2014 Red Hat, Inc.
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of version 2 the GNU General Public License as
5 * published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 ***********************************************************************
15 * File: msg_comm.c
16 *
17 * Description:
18 * 1. Clones two child processes with CLONE_NEWIPC flag, each child
19 * gets System V message queue (msg) with the _identical_ key.
20 * 2. Child1 appends a message with identifier #1 to the message queue.
21 * 3. Child2 appends a message with identifier #2 to the message queue.
22 * 4. Appends to the message queue with the identical key but from
23 * two different IPC namespaces should not interfere with each other
24 * and so child1 checks whether its message queue doesn't contain
25 * a message with identifier #2, if it doesn't test passes, otherwise
26 * test fails.
27 */
28
29 #define _GNU_SOURCE
30 #include <sys/ipc.h>
31 #include <sys/msg.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include "ipcns_helper.h"
37 #include "test.h"
38 #include "safe_macros.h"
39
40 #define TESTKEY 124426L
41 #define MSGSIZE 50
42 char *TCID = "msg_comm";
43 int TST_TOTAL = 1;
44
45 struct sysv_msg {
46 long mtype;
47 char mtext[MSGSIZE];
48 };
49
cleanup(void)50 static void cleanup(void)
51 {
52 tst_rmdir();
53 }
54
setup(void)55 static void setup(void)
56 {
57 tst_require_root();
58 check_newipc();
59 tst_tmpdir();
60 TST_CHECKPOINT_INIT(tst_rmdir);
61 }
62
chld1_msg(void * arg)63 int chld1_msg(void *arg)
64 {
65 int id, n, rval = 0;
66 struct sysv_msg m;
67 struct sysv_msg rec;
68
69 id = msgget(TESTKEY, IPC_CREAT | 0600);
70 if (id == -1) {
71 perror("msgget");
72 return 2;
73 }
74
75 m.mtype = 1;
76 m.mtext[0] = 'A';
77 if (msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0) == -1) {
78 perror("msgsnd");
79 msgctl(id, IPC_RMID, NULL);
80 return 2;
81 }
82
83 /* wait for child2 to write into the message queue */
84 TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
85
86 /* if child1 message queue has changed (by child2) report fail */
87 n = msgrcv(id, &rec, sizeof(struct sysv_msg) - sizeof(long),
88 2, IPC_NOWAIT);
89 if (n == -1 && errno != ENOMSG) {
90 perror("msgrcv");
91 msgctl(id, IPC_RMID, NULL);
92 return 2;
93 }
94 /* if mtype #2 was found in the message queue, it is fail */
95 if (n > 0) {
96 rval = 1;
97 }
98
99 /* tell child2 to continue */
100 TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
101
102 msgctl(id, IPC_RMID, NULL);
103 return rval;
104 }
105
chld2_msg(void * arg)106 int chld2_msg(void *arg)
107 {
108 int id;
109 struct sysv_msg m;
110
111 id = msgget(TESTKEY, IPC_CREAT | 0600);
112 if (id == -1) {
113 perror("msgget");
114 return 2;
115 }
116
117 m.mtype = 2;
118 m.mtext[0] = 'B';
119 if (msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0) == -1) {
120 perror("msgsnd");
121 msgctl(id, IPC_RMID, NULL);
122 return 2;
123 }
124
125 /* tell child1 to continue and wait for it */
126 TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
127
128 msgctl(id, IPC_RMID, NULL);
129 return 0;
130 }
131
test(void)132 static void test(void)
133 {
134 int status, ret = 0;
135
136 ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_msg, NULL);
137 if (ret == -1)
138 tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
139
140 ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_msg, NULL);
141 if (ret == -1)
142 tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
143
144
145 while (wait(&status) > 0) {
146 if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
147 ret = 1;
148 if (WIFEXITED(status) && WEXITSTATUS(status) == 2)
149 tst_brkm(TBROK | TERRNO, cleanup, "error in child");
150 if (WIFSIGNALED(status)) {
151 tst_resm(TFAIL, "child was killed with signal %s",
152 tst_strsig(WTERMSIG(status)));
153 return;
154 }
155 }
156
157 if (ret)
158 tst_resm(TFAIL, "SysV msg: communication with identical keys"
159 " between namespaces");
160 else
161 tst_resm(TPASS, "SysV msg: communication with identical keys"
162 " between namespaces");
163 }
164
main(int argc,char * argv[])165 int main(int argc, char *argv[])
166 {
167 int lc;
168
169 tst_parse_opts(argc, argv, NULL, NULL);
170
171 setup();
172
173 for (lc = 0; TEST_LOOPING(lc); lc++)
174 test();
175
176 cleanup();
177 tst_exit();
178 }
179