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 "test.h"
37 #include "safe_macros.h"
38 #include "libclone.h"
39 #include "ipcns_helper.h"
40
41
42 #define TESTKEY 124426L
43 #define MSGSIZE 50
44 char *TCID = "msg_comm";
45 int TST_TOTAL = 1;
46
47 struct sysv_msg {
48 long mtype;
49 char mtext[MSGSIZE];
50 };
51
cleanup(void)52 static void cleanup(void)
53 {
54 tst_rmdir();
55 }
56
setup(void)57 static void setup(void)
58 {
59 tst_require_root();
60 check_newipc();
61 tst_tmpdir();
62 TST_CHECKPOINT_INIT(tst_rmdir);
63 }
64
chld1_msg(void * arg)65 int chld1_msg(void *arg)
66 {
67 int id, n, rval = 0;
68 struct sysv_msg m;
69 struct sysv_msg rec;
70
71 id = msgget(TESTKEY, IPC_CREAT | 0600);
72 if (id == -1) {
73 perror("msgget");
74 return 2;
75 }
76
77 m.mtype = 1;
78 m.mtext[0] = 'A';
79 if (msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0) == -1) {
80 perror("msgsnd");
81 msgctl(id, IPC_RMID, NULL);
82 return 2;
83 }
84
85 /* wait for child2 to write into the message queue */
86 TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
87
88 /* if child1 message queue has changed (by child2) report fail */
89 n = msgrcv(id, &rec, sizeof(struct sysv_msg) - sizeof(long),
90 2, IPC_NOWAIT);
91 if (n == -1 && errno != ENOMSG) {
92 perror("msgrcv");
93 msgctl(id, IPC_RMID, NULL);
94 return 2;
95 }
96 /* if mtype #2 was found in the message queue, it is fail */
97 if (n > 0) {
98 rval = 1;
99 }
100
101 /* tell child2 to continue */
102 TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
103
104 msgctl(id, IPC_RMID, NULL);
105 return rval;
106 }
107
chld2_msg(void * arg)108 int chld2_msg(void *arg)
109 {
110 int id;
111 struct sysv_msg m;
112
113 id = msgget(TESTKEY, IPC_CREAT | 0600);
114 if (id == -1) {
115 perror("msgget");
116 return 2;
117 }
118
119 m.mtype = 2;
120 m.mtext[0] = 'B';
121 if (msgsnd(id, &m, sizeof(struct sysv_msg) - sizeof(long), 0) == -1) {
122 perror("msgsnd");
123 msgctl(id, IPC_RMID, NULL);
124 return 2;
125 }
126
127 /* tell child1 to continue and wait for it */
128 TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
129
130 msgctl(id, IPC_RMID, NULL);
131 return 0;
132 }
133
test(void)134 static void test(void)
135 {
136 int status, ret = 0;
137
138 ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_msg, NULL);
139 if (ret == -1)
140 tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
141
142 ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_msg, NULL);
143 if (ret == -1)
144 tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
145
146
147 while (wait(&status) > 0) {
148 if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
149 ret = 1;
150 if (WIFEXITED(status) && WEXITSTATUS(status) == 2)
151 tst_brkm(TBROK | TERRNO, cleanup, "error in child");
152 if (WIFSIGNALED(status)) {
153 tst_resm(TFAIL, "child was killed with signal %s",
154 tst_strsig(WTERMSIG(status)));
155 return;
156 }
157 }
158
159 if (ret)
160 tst_resm(TFAIL, "SysV msg: communication with identical keys"
161 " between namespaces");
162 else
163 tst_resm(TPASS, "SysV msg: communication with identical keys"
164 " between namespaces");
165 }
166
main(int argc,char * argv[])167 int main(int argc, char *argv[])
168 {
169 int lc;
170
171 tst_parse_opts(argc, argv, NULL, NULL);
172
173 setup();
174
175 for (lc = 0; TEST_LOOPING(lc); lc++)
176 test();
177
178 cleanup();
179 tst_exit();
180 }
181