1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2007
4f08c3bdfSopenharmony_ci *				Serge Hallyn <serue@us.ibm.com>
5f08c3bdfSopenharmony_ci * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Test if SysV IPC shared memory with a specific key is shared between
12f08c3bdfSopenharmony_ci * processes and namespaces.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#define _GNU_SOURCE
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <sys/wait.h>
18f08c3bdfSopenharmony_ci#include <sys/msg.h>
19f08c3bdfSopenharmony_ci#include <sys/types.h>
20f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
21f08c3bdfSopenharmony_ci#include "tst_test.h"
22f08c3bdfSopenharmony_ci#include "common.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#define TESTKEY 0xEAEAEA
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic char *str_op;
27f08c3bdfSopenharmony_cistatic int use_clone;
28f08c3bdfSopenharmony_cistatic int ipc_id = -1;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void check_shmid(void)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	TEST(shmget(TESTKEY, 100, 0));
33f08c3bdfSopenharmony_ci	if (TST_RET < 0) {
34f08c3bdfSopenharmony_ci		if (use_clone == T_NONE)
35f08c3bdfSopenharmony_ci			tst_res(TFAIL, "plain cloned process didn't find shmid");
36f08c3bdfSopenharmony_ci		else
37f08c3bdfSopenharmony_ci			tst_res(TPASS, "%s: child process didn't find shmid", str_op);
38f08c3bdfSopenharmony_ci	} else {
39f08c3bdfSopenharmony_ci		if (use_clone == T_NONE)
40f08c3bdfSopenharmony_ci			tst_res(TPASS, "plain cloned process found shmid");
41f08c3bdfSopenharmony_ci		else
42f08c3bdfSopenharmony_ci			tst_res(TFAIL, "%s: child process found shmid", str_op);
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void run(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmid);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	use_clone = get_clone_unshare_enum(str_op);
54f08c3bdfSopenharmony_ci	ipc_id = shmget(TESTKEY, 100, IPC_CREAT);
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic void cleanup(void)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	if (ipc_id != -1) {
60f08c3bdfSopenharmony_ci		tst_res(TINFO, "Destroying shared memory");
61f08c3bdfSopenharmony_ci		SAFE_SHMCTL(ipc_id, IPC_RMID, NULL);
62f08c3bdfSopenharmony_ci	}
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic struct tst_test test = {
66f08c3bdfSopenharmony_ci	.test_all = run,
67f08c3bdfSopenharmony_ci	.setup = setup,
68f08c3bdfSopenharmony_ci	.cleanup = cleanup,
69f08c3bdfSopenharmony_ci	.forks_child = 1,
70f08c3bdfSopenharmony_ci	.needs_root = 1,
71f08c3bdfSopenharmony_ci	.needs_checkpoints = 1,
72f08c3bdfSopenharmony_ci	.options = (struct tst_option[]) {
73f08c3bdfSopenharmony_ci		{ "m:", &str_op, "Test execution mode <clone|unshare|none>" },
74f08c3bdfSopenharmony_ci		{},
75f08c3bdfSopenharmony_ci	},
76f08c3bdfSopenharmony_ci};
77