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