[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ltp-cvs
Subject:    [Ltp-cvs] ltp/testcases/kernel/containers/utsname Makefile, NONE,
From:       Subrata <subrata_modak () users ! sourceforge ! net>
Date:       2007-04-26 11:02:54
Message-ID: E1Hh1kb-0005vA-1s () mail ! sourceforge ! net
[Download RAW message or body]

Update of /cvsroot/ltp/ltp/testcases/kernel/containers/utsname
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv6999/testcases/kernel/containers/utsname


Added Files:
	Makefile README check_utsns_enabled.c runtests_noltp.sh 
	runutstest.sh utstest.c 
Log Message:
Integration of UTS Namespace Testcases to LTP as submitted by \
<risrajak@linux.vnet.ibm.com>


--- NEW FILE: check_utsns_enabled.c ---
/*
 * Copyright 2007 IBM
 * Author: Serge Hallyn <serue@us.ibm.com>
 *
 * uts namespaces were introduced around 2.6.19.  Kernels before that,
 * assume they are not enabled.  Kernels after that, check for -EINVAL
 * when trying to use CLONE_NEWUTS.
 */

#include <sys/utsname.h>
#include <sched.h>
#include <stdio.h>
#include "../libclone/libclone.h"
#include "test.h"

int dummy(void *v)
{
	return 0;
}

/*
 * Not really expecting anyone to use this on a 2.6.19-rc kernel,
 * else we may get some false positives here.
 */
#if 0
int kernel_version_newenough()
{
	int ret;
	struct utsname buf;
	char *s;
	int maj, min, micro;

	ret = uname(&buf);
	if (ret == -1) {
		perror("uname");
		return 0;
	}
	s = buf.release;
	sscanf(s, "%d.%d.%d", &maj, &min, &micro);
	if (maj < 2)
		return 0;
	if (min < 6)
		return 0;
	if (micro < 19)
		return 0;
	return 1;
}
#endif  /* Library is already provided by LTP*/
int main()
{
	void *childstack, *stack;
	int pid;

	//if (!kernel_version_newenough())
	if (tst_kvercmp(2,6,19) < 0)
		return 1;
	stack = malloc(getpagesize());
	if (!stack) {
		perror("malloc");
		return 2;
	}

	childstack = stack + getpagesize();

	pid = clone(dummy, childstack, CLONE_NEWUTS, NULL);

	if (pid == -1)
		return 3;
	return 0;
}

--- NEW FILE: utstest.c ---
/*
 * Copyright 2007 IBM
 * Author: Serge Hallyn <serue@us.ibm.com>
 *
 * test1:
	P1: A=gethostname
	P2: B=gethostname
	Ensure(A==B)

 * test2:
	P1: sethostname(A);
	P2: (wait); B=gethostname
	Ensure (A==B)

 * test3:
	P1: A=gethostname; unshare(utsname); sethostname(newname); C=gethostname
	P2: B=gethostname; (wait); (wait); D=gethostname
	Ensure (A==B && A==D && C!=D)

 * test4:
	P1: A=gethostname; unshare(utsname); (wait); C=gethostname
	P2: B=gethostname; (wait); sethostname(newname); D=gethostname
	Ensure (A==B && A==C && C!=D)

 * test5:
	P1: drop_privs(); unshare(utsname); (wait); C=gethostname
	P2: (wait); sethostname(B); D=gethostname
	Ensure (B==C==D) and state is ok.
 *
 */

#define _GNU_SOURCE 1
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#ifndef NO_LTP
#include <test.h>
#include <libclone.h>
#else
#include "../libclone/libclone.h"
#endif

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

#ifdef NO_LTP
#define TFAIL "FAILURE: "
#define TPASS "PASS: "
#define tst_resm(x, format, arg...) printf("%s:" format, x, ## arg)
#define tst_exit() exit(1)
#endif

int drop_root()
{
	int ret;
	ret = setresuid(1000, 1000, 1000);
	if (ret) {
		perror("setresuid");
		exit(4);
	}
	return 1;
}

int p1fd[2], p2fd[2];
pid_t cpid;

#define HLEN 100
#define NAME1 "serge1"
#define NAME2 "serge2"

void picknewhostname(char *orig, char *new)
{
	memset(new, 0, HLEN);
	if (strcmp(orig, NAME1) == 0)
		strcpy(new, NAME2);
	else
		strcpy(new, NAME1);
}

void zeroize(char *s)
{
	memset(s, 0, HLEN);
}

char *tsttype;
int P1(void *vtest)
{
	char hostname[HLEN], newhostname[HLEN], rhostname[HLEN];
	int err;
	int len;
	int testnum;

	testnum = atoi((char *)vtest);

	close(p1fd[1]);
	close(p2fd[0]);

	switch(testnum) {
	case 1:
		gethostname(hostname, HLEN);
		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		if (strcmp(hostname, rhostname) == 0) {
			tst_resm(TPASS, "test 1 (%s): success\n", tsttype);
			tst_exit();
		}
		tst_resm(TFAIL, "test 1 (%s): hostname 1 %s, hostname 2 %s\n",
			tsttype, hostname, rhostname);
		tst_exit();
	case 2:
		gethostname(hostname, HLEN);
		picknewhostname(hostname, newhostname);
		err = sethostname(newhostname, strlen(newhostname));
		write(p2fd[1], "1", 1);
		if (err == -1) {
			tst_resm(TFAIL, "test 2 (%s): failed to sethostname",
					tsttype);
			tst_exit();
		}
		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		if (strcmp(newhostname, rhostname) == 0) {
			tst_resm(TPASS, "test 2 (%s): success\n",
					tsttype);
			tst_exit();
		}
		tst_resm(TFAIL, "test 2 (%s) hostname 1 %s, hostname 2 %s\n",
				tsttype, newhostname, rhostname);
		tst_exit();
	case 3:
		gethostname(hostname, HLEN);
		picknewhostname(hostname, newhostname);
		err = sethostname(newhostname, strlen(newhostname));
		write(p2fd[1], "1", 1);
		if (err == -1) {
			tst_resm(TFAIL, "test 3 (%s): failed to sethostname",
						tsttype);
			tst_exit();
		}

		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		if (strcmp(newhostname, rhostname) == 0) {
			tst_resm(TFAIL, "test 3 (%s): hostname 1 %s, hostname 2 %s, these should have been \
different\n",  tsttype, newhostname, rhostname);
			tst_exit();
		}
		if (strcmp(hostname, rhostname) == 0) {
			tst_resm(TPASS, "test 3 (%s): success\n", tsttype);
			tst_exit();
		}
		tst_resm(TFAIL, "test 3 (%s): hostname 1 %s, hostname 2 %s, should have been \
same\n",  tsttype, hostname, rhostname);
		tst_exit();

	case 4:
		gethostname(hostname, HLEN);
		write(p2fd[1], "1", 1); /* tell p2 to go ahead and sethostname */
		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		gethostname(newhostname, HLEN);
		if (strcmp(hostname, newhostname) != 0) {
			tst_resm(TFAIL, "test 4 (%s): hostname 1 %s, hostname 2 %s, should be same\n",
				tsttype, hostname, newhostname);
			tst_exit();
		}
		if (strcmp(hostname, rhostname) == 0) {
			tst_resm(TFAIL, "test 4 (%s): hostname 1 %s, hostname 2 %s, should be different",
				tsttype, hostname, rhostname);
			tst_exit();
		}
		tst_resm(TPASS, "test 4 (%s): successful\n", tsttype);
		tst_exit();
	case 5:
		write(p2fd[1], "1", 1); /* tell p2 to go ahead and sethostname */
		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		gethostname(newhostname, HLEN);
		if (strcmp(rhostname, newhostname) != 0) {
			tst_resm(TFAIL, "test 5 (%s): hostnames %s and %s should be same\n",
				tsttype, rhostname, newhostname);
			tst_exit();
		}
		tst_resm(TPASS, "test 5 (%s): successful", tsttype);
		tst_exit();
	default:
		break;
	}
	return -1;
}

int P2(void *vtest)
{
	char hostname[HLEN], newhostname[HLEN];
	int len;
	int testnum;

	testnum = atoi((char *)vtest);

	close(p1fd[0]);
	close(p2fd[1]);

	switch(testnum) {
	case 1:
		gethostname(hostname, HLEN);
		write(p1fd[1], hostname, strlen(hostname));
		break;
	case 2:
	case 3:
		len = 0;
		while (!len) {
			len = read(p2fd[0], hostname, 1);
		}
		gethostname(hostname, HLEN);
		write(p1fd[1], hostname, strlen(hostname));
		break;
	case 4:
	case 5:
		len = 0;
		while (!len) {
			len = read(p2fd[0], hostname, 1);
		}
		if (hostname[0] == '0') {
			tst_resm(TPASS, "P2: P1 claims error\n");
			tst_exit();
			exit(0);
		}
		gethostname(hostname, HLEN);
		picknewhostname(hostname, newhostname);
		sethostname(newhostname, strlen(newhostname));
		write(p1fd[1], newhostname, strlen(newhostname));
		break;
	default:
		tst_resm(TFAIL, "undefined test: %d\n", testnum);
		break;
	}
	tst_exit();
	return 0;
}

#define UNSHARESTR "unshare"
#define CLONESTR "clone"
int main(int argc, char *argv[])
{
	int r, pid, use_clone = T_UNSHARE;
	int testnum;
	void *vtest;

	if (argc != 3) {
		tst_resm(TFAIL, "Usage: %s <clone|unshare> <testnum>\n", argv[0]);
		tst_resm(TFAIL, " where clone or unshare specifies unshare method,");
		tst_resm(TFAIL, " and testnum is between 1 and 5 inclusive\n");
		exit(2);
	}
	if (pipe(p1fd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
	if (pipe(p2fd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }

	tsttype = UNSHARESTR;
	if (strcmp(argv[1], "clone") == 0) {
		use_clone = T_CLONE;
		tsttype = CLONESTR;
	}

	testnum = atoi(argv[2]);

	vtest = (void *)argv[2];
	switch(testnum) {
	case 1:
	case 2: r = do_clone_unshare_tests(T_NONE, 0,
					P1, vtest, P2, vtest);
		break;
	case 3:
	case 4:
		r = do_clone_unshare_tests(use_clone, CLONE_NEWUTS,
					P1, vtest, P2, vtest);
		break;
	case 5:
		pid = fork();
		if (pid == -1) {
			perror("fork");
			exit(2);
		}
		if (pid == 0) {
			if (!drop_root()) {
				tst_resm(TFAIL, "failed to drop root.\n");
				tst_exit();
				exit(1);
			}
			r = do_clone_unshare_test(use_clone, CLONE_NEWUTS,
					P1, vtest);
			write(p2fd[1], "0", 1); /* don't let p2 hang */
			exit(0);
		} else {
			P2(vtest);
		}
		break;
	default:
		tst_resm(TFAIL, "testnum should be between 1 and 5 inclusive.\n");
		break;
	}

	tst_exit();
}

--- NEW FILE: Makefile ---
CC=gcc

CFLAGS += -I../../../../include -I../libclone -Wall
LDLIBS += -L../../../../lib -L../libclone ../libclone/libclone.a -lltp

SRCS    = $(wildcard *.c)
TARGETS = $(patsubst %.c,%,$(SRCS))
NOLTP_TARGETS = $(patsubst %.c,%_noltp,$(SRCS))

%_noltp : %.c
	$(CC) -g -DNO_LTP -o $@ $< ../libclone/libclone.a

all: $(TARGETS)

noltp:  $(NOLTP_TARGETS)

clean:
	rm -f $(TARGETS) *.o $(NOLTP_TARGETS)

install:
	@set -e; for i in $(TARGETS) runutstest.sh check_utsns_enabled; do ln -f $$i \
../../../bin/$$i ; chmod +x runutstest.sh ; done

noltp_check: noltp
	./runtests_noltp.sh

--- NEW FILE: README ---
This contains five tests for the uts namespace unsharing functionality.

To enable this functionality, you currently must use a -mm kernel (see
kernel.org). Then to run these tests, just type

	sh runutstest.sh

The tests are intended to do the following:

test 1: check that after fork, two children see the same utsname
	P1: A=gethostname
	P2: B=gethostname
	Ensure(A==B)
test 2: check that after fork, two children are in the same utsname namespace.
	P1: sethostname(newname); A=gethostname
	P2: (wait); B=gethostname
	Ensure (A==B)

test 3: check that after unshare, processes are in different utsname namespaces.
	P1: A=gethostname; unshare(utsname); sethostname(newname); C=gethostname
	P2: B=gethostname; (wait); (wait); D=gethostname
	Ensure (A==B && A==D && C!=D)

test 4: similar to test 3, but other child changes hostname.
	P1: A=gethostname; unshare(utsname); (wait); C=gethostname
	P2: B=gethostname; (wait); sethostname(newname); D=gethostname
	Ensure (A==B && A==C && C!=D)

test 5: check that unsharing utsname without required permissions (CAP_SYS_AUDIT)
	fails.
	P1: A=gethostname; unshare(utsname) without suff. perms; (wait); C=gethostname
	P2: B=gethostname; (wait); sethostname(newname); D=gethostname
	Ensure (A==B==C==D) and state is ok.

--- NEW FILE: runutstest.sh ---
#!/bin/sh

echo "unshare tests"
for i in `seq 1 5`; do
	echo "test $i (unshare)"
	utstest unshare $i
done
echo "clone tests"
for i in `seq 1 5`; do
	echo "test $i (clone)"
	utstest clone $i
done

--- NEW FILE: runtests_noltp.sh ---
#!/bin/sh

exit_code=0
echo "unshare tests"
for i in `seq 1 5`; do
	echo "test $i (unshare)"
	./utstest_noltp unshare $i
	if [ $? -ne 0 ]; then
		exit_code=$?
	fi
done
echo "clone tests"
for i in `seq 1 5`; do
	echo "test $i (clone)"
	./utstest_noltp clone $i
	if [ $? -ne 0 ]; then
		exit_code=$?
	fi
done
exit $exit_code


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Ltp-cvs mailing list
Ltp-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-cvs


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic