1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Red Hat, Inc.
4f08c3bdfSopenharmony_ci * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Test SysV IPC message passing through different processes.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * [Algorithm]
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * 1. Clones two child processes with CLONE_NEWIPC flag, each child
15f08c3bdfSopenharmony_ci *    gets System V message queue (msg) with the _identical_ key.
16f08c3bdfSopenharmony_ci * 2. Child1 appends a message with identifier #1 to the message queue.
17f08c3bdfSopenharmony_ci * 3. Child2 appends a message with identifier #2 to the message queue.
18f08c3bdfSopenharmony_ci * 4. Appends to the message queue with the identical key but from
19f08c3bdfSopenharmony_ci *    two different IPC namespaces should not interfere with each other
20f08c3bdfSopenharmony_ci *    and so child1 checks whether its message queue doesn't contain
21f08c3bdfSopenharmony_ci *    a message with identifier #2, if it doesn't test passes, otherwise
22f08c3bdfSopenharmony_ci *    test fails.
23f08c3bdfSopenharmony_ci */
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#define _GNU_SOURCE
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#include <sys/wait.h>
28f08c3bdfSopenharmony_ci#include <sys/msg.h>
29f08c3bdfSopenharmony_ci#include <sys/types.h>
30f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
31f08c3bdfSopenharmony_ci#include "tst_test.h"
32f08c3bdfSopenharmony_ci#include "common.h"
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci#define TESTKEY 124426L
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistruct sysv_msg {
37f08c3bdfSopenharmony_ci	long mtype;
38f08c3bdfSopenharmony_ci	char mtext[1];
39f08c3bdfSopenharmony_ci};
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void chld1_msg(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	int id;
44f08c3bdfSopenharmony_ci	struct sysv_msg m = {
45f08c3bdfSopenharmony_ci		.mtype = 1,
46f08c3bdfSopenharmony_ci		.mtext = "A",
47f08c3bdfSopenharmony_ci	};
48f08c3bdfSopenharmony_ci	struct sysv_msg rec;
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	id = SAFE_MSGGET(TESTKEY, IPC_CREAT | 0600);
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	SAFE_MSGSND(id, &m, sizeof(m.mtext), 0);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAIT(0);
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	TEST(msgrcv(id, &rec, sizeof(rec.mtext), 2, IPC_NOWAIT));
57f08c3bdfSopenharmony_ci	if (TST_RET < 0 && TST_ERR != ENOMSG)
58f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "msgrcv error");
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	/* if child1 message queue has changed (by child2) report fail */
61f08c3bdfSopenharmony_ci	if (TST_RET > 0)
62f08c3bdfSopenharmony_ci		tst_res(TFAIL, "messages leak between namespacess");
63f08c3bdfSopenharmony_ci	else
64f08c3bdfSopenharmony_ci		tst_res(TPASS, "messages does not leak between namespaces");
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAKE(0);
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	SAFE_MSGCTL(id, IPC_RMID, NULL);
69f08c3bdfSopenharmony_ci}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_cistatic void chld2_msg(void)
72f08c3bdfSopenharmony_ci{
73f08c3bdfSopenharmony_ci	int id;
74f08c3bdfSopenharmony_ci	struct sysv_msg m = {
75f08c3bdfSopenharmony_ci		.mtype = 2,
76f08c3bdfSopenharmony_ci		.mtext = "B",
77f08c3bdfSopenharmony_ci	};
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	id = SAFE_MSGGET(TESTKEY, IPC_CREAT | 0600);
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	SAFE_MSGSND(id, &m, sizeof(m.mtext), 0);
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAKE_AND_WAIT(0);
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	SAFE_MSGCTL(id, IPC_RMID, NULL);
86f08c3bdfSopenharmony_ci}
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_cistatic void run(void)
89f08c3bdfSopenharmony_ci{
90f08c3bdfSopenharmony_ci	clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_msg);
91f08c3bdfSopenharmony_ci	clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_msg);
92f08c3bdfSopenharmony_ci}
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_cistatic struct tst_test test = {
95f08c3bdfSopenharmony_ci	.test_all = run,
96f08c3bdfSopenharmony_ci	.needs_root = 1,
97f08c3bdfSopenharmony_ci	.needs_checkpoints = 1,
98f08c3bdfSopenharmony_ci	.forks_child = 1,
99f08c3bdfSopenharmony_ci};
100