Skip to content

Commit

Permalink
Refactor mqns_03 using new LTP API
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Cervesato <[email protected]>
Reviewed-by: Cyril Hrubis <[email protected]>
  • Loading branch information
acerv authored and metan-ucw committed Sep 14, 2023
1 parent 5a9d225 commit eff6e22
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 181 deletions.
4 changes: 2 additions & 2 deletions runtest/containers
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ mqns_01_unshare mqns_01 -m unshare
mqns_02 mqns_02
mqns_02_clone mqns_02 -m clone
mqns_02_unshare mqns_02 -m unshare
mqns_03 mqns_03
mqns_03_clone mqns_03 -clone
mqns_03_unshare mqns_03 -m unshare
mqns_03_clone mqns_03 -m clone
mqns_04 mqns_04
mqns_04_clone mqns_04 -clone

Expand Down
305 changes: 126 additions & 179 deletions testcases/kernel/containers/mqns/mqns_03.c
Original file line number Diff line number Diff line change
@@ -1,207 +1,154 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) International Business Machines Corp., 2009
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Serge Hallyn <[email protected]>
*
* Check ipcns+sb longevity
*
* Mount mqueue fs
* unshare
* In unshared process:
* Create "/mq1" with mq_open()
* Mount mqueuefs
* Check that /mq1 exists
* Create /dev/mqueue/mq2 through vfs (create(2))
* Umount /dev/mqueue
* Remount /dev/mqueue
* Check that both /mq1 and /mq2 exist
***************************************************************************/

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "mqns.h"
#include "mqns_helper.h"

char *TCID = "posixmq_namespace_03";
int TST_TOTAL = 1;

int p1[2];
int p2[2];

#define FNAM1 DEV_MQUEUE2 SLASH_MQ1
#define FNAM2 DEV_MQUEUE2 SLASH_MQ2

int check_mqueue(void *vtest)
* Copyright (c) International Business Machines Corp., 2009
* Copyright (c) Serge Hallyn <[email protected]>
* Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
*/

/*\
* [Description]
*
* Test mqueuefs from an isolated/forked process namespace.
*
* [Algorithm]
*
* Inside new IPC namespace:
*
* - mq_open() /MQ1
* - mount mqueue inside the temporary folder
* - check for /MQ1 existance
* - creat() /MQ2 inside the temporary folder
* - umount
* - mount mqueue inside the temporary folder
* - check /MQ1 existance
* - check /MQ2 existance
* - umount
*/

#include "tst_test.h"
#include "lapi/sched.h"
#include "tst_safe_posix_ipc.h"
#include "tst_safe_stdio.h"
#include "tst_safe_macros.h"

#define CHECK_MQ_OPEN_RET(x) ((x) >= 0 || ((x) == -1 && errno != EMFILE))

#define DEVDIR "ltp_mqueue"
#define MQNAME1 "/MQ1"
#define MQNAME2 "/MQ2"
#define MQUEUE1 DEVDIR MQNAME1
#define MQUEUE2 DEVDIR MQNAME2

static char *str_op;

static void check_mqueue(void)
{
char buf[30];
mqd_t mqd;
int rc;
struct stat statbuf;
mqd_t mqd;

(void) vtest;
tst_res(TINFO, "Creating %s mqueue from within child process", MQNAME1);

close(p1[1]);
close(p2[0]);
mqd = TST_RETRY_FUNC(
mq_open(MQNAME1, O_RDWR | O_CREAT | O_EXCL, 0777, NULL),
CHECK_MQ_OPEN_RET);
if (mqd == -1)
tst_brk(TBROK | TERRNO, "mq_open failed");

if (read(p1[0], buf, 3) != 3) { /* go */
perror("read failed");
exit(1);
}
SAFE_MQ_CLOSE(mqd);

mqd = tst_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDWR | O_CREAT | O_EXCL,
0755, NULL);
if (mqd == -1) {
write(p2[1], "mqfail", 7);
exit(1);
}
tst_res(TINFO, "Mount %s from within child process", DEVDIR);

mq_close(mqd);
SAFE_MOUNT("mqueue", DEVDIR, "mqueue", 0, NULL);

rc = mount("mqueue", DEV_MQUEUE2, "mqueue", 0, NULL);
if (rc == -1) {
write(p2[1], "mount1", 7);
exit(1);
}
if (access(MQUEUE1, F_OK))
tst_res(TFAIL, MQUEUE1 " does not exist at first mount");
else
tst_res(TPASS, MQUEUE1 " exists at first mount");

rc = stat(FNAM1, &statbuf);
if (rc == -1) {
write(p2[1], "stat1", 6);
exit(1);
}
tst_res(TINFO, "Creating %s from within child process", MQUEUE2);

rc = creat(FNAM2, 0755);
if (rc == -1) {
write(p2[1], "creat", 6);
exit(1);
}
rc = SAFE_CREAT(MQUEUE2, 0755);
SAFE_CLOSE(rc);

close(rc);
tst_res(TINFO, "Mount %s from within child process a second time", DEVDIR);

rc = umount(DEV_MQUEUE2);
if (rc == -1) {
write(p2[1], "umount", 7);
exit(1);
}
SAFE_UMOUNT(DEVDIR);
SAFE_MOUNT("mqueue", DEVDIR, "mqueue", 0, NULL);

rc = mount("mqueue", DEV_MQUEUE2, "mqueue", 0, NULL);
if (rc == -1) {
write(p2[1], "mount2", 7);
exit(1);
}
if (access(MQUEUE1, F_OK))
tst_res(TFAIL, MQUEUE1 " does not exist at second mount");
else
tst_res(TPASS, MQUEUE1 " exists at second mount");

rc = stat(FNAM1, &statbuf);
if (rc == -1) {
write(p2[1], "stat2", 7);
exit(1);
}
if (access(MQUEUE2, F_OK))
tst_res(TFAIL, MQUEUE2 " does not exist at second mount");
else
tst_res(TPASS, MQUEUE2 " exists at second mount");

rc = stat(FNAM2, &statbuf);
if (rc == -1) {
write(p2[1], "stat3", 7);
exit(1);
}
SAFE_UMOUNT(DEVDIR);

write(p2[1], "done", 5);
SAFE_MQ_UNLINK(MQNAME1);
SAFE_MQ_UNLINK(MQNAME2);
}

exit(0);
static void run(void)
{
const struct tst_clone_args clone_args = {
.flags = CLONE_NEWIPC,
.exit_signal = SIGCHLD
};

if (str_op && !strcmp(str_op, "clone")) {
tst_res(TINFO, "Spawning isolated process");

if (!SAFE_CLONE(&clone_args)) {
check_mqueue();
return;
}
} else if (str_op && !strcmp(str_op, "unshare")) {
tst_res(TINFO, "Spawning unshared process");

if (!SAFE_FORK()) {
SAFE_UNSHARE(CLONE_NEWIPC);
check_mqueue();
return;
}
}
}

static void setup(void)
{
tst_require_root();
check_mqns();
if (!str_op || (strcmp(str_op, "clone") && strcmp(str_op, "unshare")))
tst_brk(TCONF, "Please specify clone|unshare child isolation");

SAFE_MKDIR(DEVDIR, 0755);
}

int main(int argc, char *argv[])
static void cleanup(void)
{
int r;
char buf[30];
int use_clone = T_UNSHARE;

setup();

if (argc == 2 && strcmp(argv[1], "-clone") == 0) {
tst_resm(TINFO, "Testing posix mq namespaces through clone(2)");
use_clone = T_CLONE;
} else
tst_resm(TINFO,
"Testing posix mq namespaces through unshare(2)");

if (pipe(p1) == -1 || pipe(p2) == -1)
tst_brkm(TBROK | TERRNO, NULL, "pipe failed");

/* fire off the test */
r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL);
if (r < 0) {
tst_brkm(TBROK | TERRNO, NULL, "failed clone/unshare");
}

tst_resm(TINFO, "Checking correct umount+remount of mqueuefs");

mkdir(DEV_MQUEUE2, 0755);

close(p1[0]);
close(p2[1]);
write(p1[1], "go", 3);

read(p2[0], buf, 7);
r = TFAIL;
if (!strcmp(buf, "mqfail")) {
tst_resm(TFAIL, "child process could not create mqueue");
goto fail;
} else if (!strcmp(buf, "mount1")) {
tst_resm(TFAIL, "child process could not mount mqueue");
goto fail;
} else if (!strcmp(buf, "stat1x")) {
tst_resm(TFAIL, "mq created by child is not in mqueuefs");
goto fail;
} else if (!strcmp(buf, "creat")) {
tst_resm(TFAIL, "child couldn't creat mq through mqueuefs");
goto fail;
} else if (!strcmp(buf, "umount")) {
tst_resm(TFAIL, "child couldn't umount mqueuefs");
goto fail;
} else if (!strcmp(buf, "mount2")) {
tst_resm(TFAIL, "child couldn't remount mqueuefs");
goto fail;
} else if (!strcmp(buf, "stat2")) {
tst_resm(TFAIL,
"mq_open()d file gone after remount of mqueuefs");
goto fail;
} else if (!strcmp(buf, "stat3")) {
tst_resm(TFAIL,
"creat(2)'d file gone after remount of mqueuefs");
goto fail;
}
if (!access(MQUEUE1, F_OK))
SAFE_MQ_UNLINK(MQNAME1);

tst_resm(TPASS, "umount+remount of mqueuefs remounted the right fs");
if (!access(MQUEUE2, F_OK))
SAFE_MQ_UNLINK(MQNAME2);

r = 0;
fail:
umount(DEV_MQUEUE2);
rmdir(DEV_MQUEUE2);
tst_exit();
if (tst_is_mounted(DEVDIR))
SAFE_UMOUNT(DEVDIR);
}

static struct tst_test test = {
.test_all = run,
.setup = setup,
.cleanup = cleanup,
.needs_root = 1,
.forks_child = 1,
.needs_tmpdir = 1,
.options = (struct tst_option[]) {
{ "m:", &str_op, "Child process isolation <clone|unshare>" },
{},
},
.needs_kconfigs = (const char *[]) {
"CONFIG_USER_NS",
NULL
},
};

0 comments on commit eff6e22

Please sign in to comment.