Skip to content

Commit 517cb36

Browse files
acervmetan-ucw
authored andcommitted
Refactor mqns_01 using new LTP API
Signed-off-by: Andrea Cervesato <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent 04e8f2f commit 517cb36

File tree

2 files changed

+62
-126
lines changed

2 files changed

+62
-126
lines changed

runtest/containers

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pidns31 pidns31
1616
pidns32 pidns32
1717

1818
mqns_01 mqns_01
19-
mqns_01_clone mqns_01 -clone
19+
mqns_01_clone mqns_01 -m clone
20+
mqns_01_unshare mqns_01 -m unshare
2021
mqns_02 mqns_02
2122
mqns_02_clone mqns_02 -clone
2223
mqns_03 mqns_03
Lines changed: 60 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,83 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
12
/*
2-
* Copyright (c) International Business Machines Corp., 2009
3-
* Copyright (c) Nadia Derbey, 2009
4-
* This program is free software; you can redistribute it and/or modify
5-
* it under the terms of the GNU General Public License as published by
6-
* the Free Software Foundation; either version 2 of the License, or
7-
* (at your option) any later version.
8-
*
9-
* This program is distributed in the hope that it will be useful,
10-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12-
* the GNU General Public License for more details.
13-
* You should have received a copy of the GNU General Public License
14-
* along with this program; if not, write to the Free Software
15-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16-
*
17-
* Author: Nadia Derbey <[email protected]>
18-
*
19-
* Check mqns isolation: father mqns cannot be accessed from newinstance
20-
*
21-
* Mount mqueue fs
22-
* Create a posix mq -->mq1
23-
* unshare
24-
* In unshared process:
25-
* Mount newinstance mqueuefs
26-
* Check that mq1 is not readable from new ns
3+
* Copyright (c) International Business Machines Corp., 2009
4+
* Copyright (c) Nadia Derbey, 2009 <[email protected]>
5+
* Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
6+
*/
277

28-
***************************************************************************/
8+
/*\
9+
* [Description]
10+
*
11+
* Create a mqueue inside the parent and check if it can be accessed from
12+
* the child namespace. Isolated and unshared process can't access to parent,
13+
* but plain process can.
14+
*/
2915

30-
#ifndef _GNU_SOURCE
31-
#define _GNU_SOURCE
32-
#endif
33-
#include <sys/wait.h>
34-
#include <errno.h>
35-
#include <stdio.h>
36-
#include <stdlib.h>
37-
#include <string.h>
38-
#include <unistd.h>
39-
#include "mqns.h"
40-
#include "mqns_helper.h"
16+
#include "tst_test.h"
17+
#include "lapi/sched.h"
18+
#include "tst_safe_posix_ipc.h"
4119

42-
char *TCID = "posixmq_namespace_01";
43-
int TST_TOTAL = 1;
20+
#define MQNAME "/MQ1"
4421

45-
int p1[2];
46-
int p2[2];
22+
static mqd_t mqd;
23+
static char *str_op;
4724

48-
int check_mqueue(void *vtest)
25+
static void run(void)
4926
{
50-
char buf[30];
51-
mqd_t mqd;
27+
const struct tst_clone_args clone_args = { CLONE_NEWIPC, SIGCHLD };
5228

53-
(void) vtest;
29+
tst_res(TINFO, "Checking namespaces isolation from parent to child");
5430

55-
close(p1[1]);
56-
close(p2[0]);
31+
if (str_op && !strcmp(str_op, "clone")) {
32+
tst_res(TINFO, "Spawning isolated process");
5733

58-
if (read(p1[0], buf, strlen("go") + 1) < 0) {
59-
printf("read(p1[0], ...) failed: %s\n", strerror(errno));
60-
exit(1);
61-
}
62-
mqd = tst_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDONLY);
63-
if (mqd == -1) {
64-
if (write(p2[1], "notfnd", strlen("notfnd") + 1) < 0) {
65-
perror("write(p2[1], ...) failed");
66-
exit(1);
34+
if (!SAFE_CLONE(&clone_args)) {
35+
TST_EXP_FAIL(mq_open(MQNAME, O_RDONLY), ENOENT);
36+
return;
37+
}
38+
} else if (str_op && !strcmp(str_op, "unshare")) {
39+
tst_res(TINFO, "Spawning unshared process");
40+
41+
if (!SAFE_FORK()) {
42+
SAFE_UNSHARE(CLONE_NEWIPC);
43+
TST_EXP_FAIL(mq_open(MQNAME, O_RDONLY), ENOENT);
44+
return;
6745
}
6846
} else {
69-
if (write(p2[1], "exists", strlen("exists") + 1) < 0) {
70-
perror("write(p2[1], \"exists\", 7) failed");
71-
exit(1);
72-
} else if (mq_close(mqd) < 0) {
73-
perror("mq_close(mqd) failed");
74-
exit(1);
47+
tst_res(TINFO, "Spawning plain process");
48+
49+
if (!SAFE_FORK()) {
50+
TST_EXP_POSITIVE(mq_open(MQNAME, O_RDONLY));
51+
return;
7552
}
7653
}
77-
78-
exit(0);
7954
}
8055

8156
static void setup(void)
8257
{
83-
tst_require_root();
84-
check_mqns();
58+
mqd = SAFE_MQ_OPEN(MQNAME, O_RDWR | O_CREAT | O_EXCL, 0777, NULL);
8559
}
8660

87-
int main(int argc, char *argv[])
61+
static void cleanup(void)
8862
{
89-
int r;
90-
mqd_t mqd;
91-
char buf[30];
92-
int use_clone = T_UNSHARE;
93-
94-
setup();
95-
96-
if (argc == 2 && strcmp(argv[1], "-clone") == 0) {
97-
tst_resm(TINFO,
98-
"Testing posix mq namespaces through clone(2).");
99-
use_clone = T_CLONE;
100-
} else
101-
tst_resm(TINFO,
102-
"Testing posix mq namespaces through unshare(2).");
103-
104-
if (pipe(p1) == -1 || pipe(p2) == -1) {
105-
tst_brkm(TBROK | TERRNO, NULL, "pipe failed");
106-
}
107-
108-
mqd = tst_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDWR | O_CREAT | O_EXCL,
109-
0777, NULL);
110-
if (mqd == -1) {
111-
perror("mq_open");
112-
tst_brkm(TFAIL, NULL, "mq_open failed");
63+
if (mqd != -1) {
64+
SAFE_MQ_CLOSE(mqd);
65+
SAFE_MQ_UNLINK(MQNAME);
11366
}
114-
115-
tst_resm(TINFO, "Checking namespaces isolation from parent to child");
116-
/* fire off the test */
117-
r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL);
118-
if (r < 0) {
119-
tst_resm(TFAIL, "failed clone/unshare");
120-
mq_close(mqd);
121-
tst_syscall(__NR_mq_unlink, NOSLASH_MQ1);
122-
tst_exit();
123-
}
124-
125-
close(p1[0]);
126-
close(p2[1]);
127-
if (write(p1[1], "go", strlen("go") + 1) < 0)
128-
tst_resm(TBROK | TERRNO, "write(p1[1], \"go\", ...) failed");
129-
else if (read(p2[0], buf, 7) < 0)
130-
tst_resm(TBROK | TERRNO, "read(p2[0], buf, ...) failed");
131-
else {
132-
if (!strcmp(buf, "exists")) {
133-
tst_resm(TFAIL, "child process found mqueue");
134-
} else if (!strcmp(buf, "notfnd")) {
135-
tst_resm(TPASS, "child process didn't find mqueue");
136-
} else {
137-
tst_resm(TFAIL, "UNKNOWN RESULT");
138-
}
139-
}
140-
141-
/* destroy the mqueue */
142-
if (mq_close(mqd) == -1) {
143-
tst_brkm(TBROK | TERRNO, NULL, "mq_close failed");
144-
}
145-
tst_syscall(__NR_mq_unlink, NOSLASH_MQ1);
146-
147-
tst_exit();
14867
}
68+
69+
static struct tst_test test = {
70+
.test_all = run,
71+
.setup = setup,
72+
.cleanup = cleanup,
73+
.needs_root = 1,
74+
.forks_child = 1,
75+
.options = (struct tst_option[]) {
76+
{ "m:", &str_op, "Child process isolation <clone|unshare>" },
77+
{},
78+
},
79+
.needs_kconfigs = (const char *[]) {
80+
"CONFIG_USER_NS",
81+
NULL
82+
},
83+
};

0 commit comments

Comments
 (0)