xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/mqns/mqns_03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2009
4  * Copyright (c) Serge Hallyn <[email protected]>
5  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Test mqueuefs from an isolated/forked process namespace.
12  *
13  * [Algorithm]
14  *
15  * Inside new IPC namespace:
16  *
17  * - mq_open() /MQ1
18  * - mount mqueue inside the temporary folder
19  * - check for /MQ1 existance
20  * - creat() /MQ2 inside the temporary folder
21  * - umount
22  * - mount mqueue inside the temporary folder
23  * - check /MQ1 existance
24  * - check /MQ2 existance
25  * - umount
26  */
27 
28 #include "tst_test.h"
29 #include "lapi/sched.h"
30 #include "tst_safe_posix_ipc.h"
31 #include "tst_safe_stdio.h"
32 #include "tst_safe_macros.h"
33 
34 #define CHECK_MQ_OPEN_RET(x) ((x) >= 0 || ((x) == -1 && errno != EMFILE))
35 
36 #define DEVDIR "ltp_mqueue"
37 #define MQNAME1 "/MQ1"
38 #define MQNAME2 "/MQ2"
39 #define MQUEUE1 DEVDIR MQNAME1
40 #define MQUEUE2 DEVDIR MQNAME2
41 
42 static char *str_op;
43 
check_mqueue(void)44 static void check_mqueue(void)
45 {
46 	int rc;
47 	mqd_t mqd;
48 
49 	tst_res(TINFO, "Creating %s mqueue from within child process", MQNAME1);
50 
51 	mqd = TST_RETRY_FUNC(
52 		mq_open(MQNAME1, O_RDWR | O_CREAT | O_EXCL, 0777, NULL),
53 		CHECK_MQ_OPEN_RET);
54 	if (mqd == -1)
55 		tst_brk(TBROK | TERRNO, "mq_open failed");
56 
57 	SAFE_MQ_CLOSE(mqd);
58 
59 	tst_res(TINFO, "Mount %s from within child process", DEVDIR);
60 
61 	SAFE_MOUNT("mqueue", DEVDIR, "mqueue", 0, NULL);
62 
63 	if (access(MQUEUE1, F_OK))
64 		tst_res(TFAIL, MQUEUE1 " does not exist at first mount");
65 	else
66 		tst_res(TPASS, MQUEUE1 " exists at first mount");
67 
68 	tst_res(TINFO, "Creating %s from within child process", MQUEUE2);
69 
70 	rc = SAFE_CREAT(MQUEUE2, 0755);
71 	SAFE_CLOSE(rc);
72 
73 	tst_res(TINFO, "Mount %s from within child process a second time", DEVDIR);
74 
75 	SAFE_UMOUNT(DEVDIR);
76 	SAFE_MOUNT("mqueue", DEVDIR, "mqueue", 0, NULL);
77 
78 	if (access(MQUEUE1, F_OK))
79 		tst_res(TFAIL, MQUEUE1 " does not exist at second mount");
80 	else
81 		tst_res(TPASS, MQUEUE1 " exists at second mount");
82 
83 	if (access(MQUEUE2, F_OK))
84 		tst_res(TFAIL, MQUEUE2 " does not exist at second mount");
85 	else
86 		tst_res(TPASS, MQUEUE2 " exists at second mount");
87 
88 	SAFE_UMOUNT(DEVDIR);
89 
90 	SAFE_MQ_UNLINK(MQNAME1);
91 	SAFE_MQ_UNLINK(MQNAME2);
92 }
93 
run(void)94 static void run(void)
95 {
96 	const struct tst_clone_args clone_args = {
97 		.flags = CLONE_NEWIPC,
98 		.exit_signal = SIGCHLD
99 	};
100 
101 	if (str_op && !strcmp(str_op, "clone")) {
102 		tst_res(TINFO, "Spawning isolated process");
103 
104 		if (!SAFE_CLONE(&clone_args)) {
105 			check_mqueue();
106 			return;
107 		}
108 	} else if (str_op && !strcmp(str_op, "unshare")) {
109 		tst_res(TINFO, "Spawning unshared process");
110 
111 		if (!SAFE_FORK()) {
112 			SAFE_UNSHARE(CLONE_NEWIPC);
113 			check_mqueue();
114 			return;
115 		}
116 	}
117 }
118 
setup(void)119 static void setup(void)
120 {
121 	if (!str_op || (strcmp(str_op, "clone") && strcmp(str_op, "unshare")))
122 		tst_brk(TCONF, "Please specify clone|unshare child isolation");
123 
124 	SAFE_MKDIR(DEVDIR, 0755);
125 }
126 
cleanup(void)127 static void cleanup(void)
128 {
129 	if (!access(MQUEUE1, F_OK))
130 		SAFE_MQ_UNLINK(MQNAME1);
131 
132 	if (!access(MQUEUE2, F_OK))
133 		SAFE_MQ_UNLINK(MQNAME2);
134 
135 	if (tst_is_mounted(DEVDIR))
136 		SAFE_UMOUNT(DEVDIR);
137 }
138 
139 static struct tst_test test = {
140 	.test_all = run,
141 	.setup = setup,
142 	.cleanup = cleanup,
143 	.needs_root = 1,
144 	.forks_child = 1,
145 	.needs_tmpdir = 1,
146 	.options = (struct tst_option[]) {
147 		{ "m:", &str_op, "Child process isolation <clone|unshare>" },
148 		{},
149 	},
150 	.needs_kconfigs = (const char *[]) {
151 		"CONFIG_USER_NS",
152 		NULL
153 	},
154 };
155