public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Yuan Sun <sunyuan3@huawei.com>
To: jstancek@redhat.com
Cc: ltp-list@lists.sourceforge.net
Subject: Re: [LTP] [PATCH V2] containers: new testcase userns05
Date: Fri, 31 Jul 2015 17:16:21 +0800	[thread overview]
Message-ID: <55BB3CE5.2020406@huawei.com> (raw)
In-Reply-To: <1438323464-29672-1-git-send-email-sunyuan3@huawei.com>

I forgot to add .gitignore. Please ignore the patch.
On 2015/7/31 14:17, Yuan Sun wrote:
>    A process created via fork(2) or clone(2) without the CLONE_NEWUSER
> flag is a member of the same user namespace as its parent.
>    When unshare an user namespace, the calling process is moved into a
> new user namespace which is not shared with any previously existing
> process.
>
> Signed-off-by: Yuan Sun <sunyuan3@huawei.com>
> ---
>   testcases/kernel/containers/userns/userns05.c | 142 ++++++++++++++++++++++++++
>   1 file changed, 142 insertions(+)
>   create mode 100644 testcases/kernel/containers/userns/userns05.c
>
> diff --git a/testcases/kernel/containers/userns/userns05.c b/testcases/kernel/containers/userns/userns05.c
> new file mode 100644
> index 0000000..18178d0
> --- /dev/null
> +++ b/testcases/kernel/containers/userns/userns05.c
> @@ -0,0 +1,142 @@
> +/*
> + * Copyright (c) Huawei Technologies Co., Ltd., 2015
> + * 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.
> + */
> +
> +/*
> + * Verify that:
> + * A process created via fork(2) or clone(2) without the
> + * CLONE_NEWUSER flag is a member of the same user namespace as its
> + * parent.
> + * When unshare an user namespace, the calling process is moved into
> + * a new user namespace which is not shared with any previously
> + * existing process.
> + */
> +
> +#define _GNU_SOURCE
> +#include <sys/wait.h>
> +#include <assert.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include <errno.h>
> +#include "test.h"
> +#include "userns_helper.h"
> +
> +char *TCID = "user_namespace5";
> +int TST_TOTAL = 1;
> +
> +static void cleanup(void)
> +{
> +	tst_rmdir();
> +}
> +
> +/*
> + * child_fn1() - Inside a new user namespace
> + */
> +static int child_fn1(void)
> +{
> +	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
> +	return 0;
> +}
> +
> +static long getusernsidbypid(int pid)
> +{
> +	char path[BUFSIZ];
> +	char userid[BUFSIZ];
> +	long id = 0;
> +
> +	sprintf(path, "/proc/%d/ns/user", pid);
> +
> +	if (readlink(path, userid, BUFSIZ) == -1)
> +		tst_resm(TFAIL, "readlink failure.");
> +
> +	if (sscanf(userid, "user:[%ld]", &id) != 1)
> +		tst_resm(TFAIL | TERRNO, "sscanf failure.");
> +	return id;
> +}
> +
> +static void test_userns_id(void)
> +{
> +	int cpid1, cpid2, cpid3;
> +	long parentuserns, cpid1userns, cpid2userns, newparentuserns;
> +
> +	parentuserns = getusernsidbypid(getpid());
> +	cpid1 = ltp_clone_quick(SIGCHLD, (void *)child_fn1,
> +		NULL);
> +	if (cpid1 < 0)
> +		tst_brkm(TFAIL | TERRNO, cleanup, "clone failed");
> +	cpid1userns = getusernsidbypid(cpid1);
> +	TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
> +
> +	/* A process created via fork(2) or clone(2) without the
> +	CLONE_NEWUSER flag is a member of the same user namespace as its
> +	parent.*/
> +	if (parentuserns != cpid1userns)
> +		tst_resm(TFAIL, "userns:parent should be equal to cpid1");
> +
> +	cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
> +		(void *)child_fn1, NULL);
> +	if (cpid2 < 0)
> +		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
> +	cpid2userns = getusernsidbypid(cpid2);
> +	TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
> +
> +	if (parentuserns == cpid2userns)
> +		tst_resm(TFAIL, "userns:parent should be not equal to cpid2");
> +
> +	switch (cpid3 = fork()) {
> +	case -1:
> +		tst_brkm(TBROK | TERRNO, cleanup, "fork");
> +	case 0:
> +		if (unshare(CLONE_NEWUSER) == -1) {
> +			printf("parent pid unshare failure: (%d) %s",
> +				errno, strerror(errno));
> +			exit(1);
> +		}
> +		newparentuserns = getusernsidbypid(getpid());
> +
> +		/* When unshare an user namespace, the calling process
> +		is moved into a new user namespace which is not shared
> +		with any previously existing process.*/
> +		if (parentuserns == newparentuserns)
> +			exit(1);
> +		exit(0);
> +	}
> +
> +	tst_record_childstatus(cleanup, cpid1);
> +	tst_record_childstatus(cleanup, cpid2);
> +	tst_record_childstatus(cleanup, cpid3);
> +}
> +
> +static void setup(void)
> +{
> +	check_newuser();
> +
> +	tst_tmpdir();
> +	TST_CHECKPOINT_INIT(NULL);
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	int lc;
> +
> +	tst_parse_opts(argc, argv, NULL, NULL);
> +	setup();
> +
> +	for (lc = 0; TEST_LOOPING(lc); lc++) {
> +		tst_count = 0;
> +		test_userns_id();
> +	}
> +	cleanup();
> +	tst_exit();
> +}


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

      reply	other threads:[~2015-07-31  9:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-31  6:17 [LTP] [PATCH V2] containers: new testcase userns05 Yuan Sun
2015-07-31  9:16 ` Yuan Sun [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55BB3CE5.2020406@huawei.com \
    --to=sunyuan3@huawei.com \
    --cc=jstancek@redhat.com \
    --cc=ltp-list@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox