Thsi patch contains clone library API's for doing unshare and plain testing.
Signed-off-by: Rishikesh K Rajak <[EMAIL PROTECTED]>
Cc: Serge Hallyn <[EMAIL PROTECTED]>
---
testcases/kernel/containers/Makefile | 2
testcases/kernel/containers/libclone/Makefile | 15 ++
testcases/kernel/containers/libclone/libclone.c | 132 ++++++++++++++++++++++++
testcases/kernel/containers/libclone/libclone.h | 85 +++++++++++++++
4 files changed, 233 insertions(+), 1 deletion(-)
Index: ltp-full-20070331/testcases/kernel/containers/libclone/libclone.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testcases/kernel/containers/libclone/libclone.c 2007-04-19 15:10:22.000000000 +0530
@@ -0,0 +1,132 @@
+#include "libclone.h"
+
+/* Serge: should I be passing in strings for error messages? */
+
+int do_clone_tests(unsigned long clone_flags,
+ int(*fn1)(void *arg), void *arg1,
+ int(*fn2)(void *arg), void *arg2)
+{
+ int ret;
+ void *childstack, *stack = malloc(getpagesize());
+
+ if (!stack) {
+ perror("malloc");
+ return -1;
+ }
+
+ childstack = stack + getpagesize();
+
+ ret = clone(fn1, childstack, clone_flags | SIGCHLD, arg1);
+ if (ret == -1) {
+ perror("clone");
+ free(stack);
+ return -1;
+ }
+ if (fn2)
+ ret = fn2(arg2);
+ else
+ ret = 0;
+
+ return ret;
+}
+
+int do_unshare_tests(unsigned long clone_flags,
+ int (*fn1)(void *arg), void *arg1,
+ int (*fn2)(void *arg), void *arg2)
+{
+ int pid, ret = 0;
+ int retpipe[2];
+ char buf[2];
+
+ if (pipe(retpipe) == -1) {
+ perror("pipe");
+ return -1;
+ }
+ pid = fork();
+ if (pid == -1) {
+ perror("fork");
+ close(retpipe[0]);
+ close(retpipe[1]);
+ return -1;
+ }
+ if (pid == 0) {
+ close(retpipe[0]);
+ ret = syscall(SYS_unshare, clone_flags);
+ if (ret == -1) {
+ write(retpipe[1], "0", 2);
+ close(retpipe[1]);
+ perror("unshare");
+ exit(1);
+ } else
+ write(retpipe[1], "1", 2);
+ close(retpipe[1]);
+ ret = fn1(arg1);
+ exit(ret);
+ } else {
+ close(retpipe[1]);
+ read(retpipe[0], &buf, 2);
+ close(retpipe[0]);
+ if (*buf == '0')
+ return -1;
+ if (fn2)
+ ret = fn2(arg2);
+ }
+
+ return ret;
+}
+
+int do_plain_tests(int (*fn1)(void *arg), void *arg1,
+ int (*fn2)(void *arg), void *arg2)
+{
+ int ret = 0, pid;
+
+ pid = fork();
+ if (pid == -1) {
+ perror("fork");
+ return -1;
+ }
+ if (pid == 0)
+ return fn1(arg1);
+ if (fn2)
+ ret = fn2(arg2);
+ return ret;
+}
+
+int do_clone_unshare_test(int use_clone, unsigned long clone_flags,
+ int (*fn1)(void *arg), void *arg1)
+{
+ switch (use_clone) {
+ case T_NONE:
+ return do_plain_tests(fn1, arg1, NULL, NULL);
+ case T_CLONE:
+ return do_clone_tests(clone_flags, fn1, arg1, NULL, NULL);
+ case T_UNSHARE:
+ return do_unshare_tests(clone_flags, fn1, arg1, NULL, NULL);
+ default:
+ printf("%s: bad use_clone option: %d\n", __FUNCTION__,
+ use_clone);
+ return -1;
+ }
+}
+
+
+/*
+ * Run fn1 in a unshared environmnent, and fn2 in the original context
+ */
+int do_clone_unshare_tests(int use_clone, unsigned long clone_flags,
+ int (*fn1)(void *arg), void *arg1,
+ int (*fn2)(void *arg), void *arg2)
+{
+ switch (use_clone) {
+ case T_NONE:
+ return do_plain_tests(fn1, arg1, fn2, arg2);
+ case T_CLONE:
+ return do_clone_tests(clone_flags, fn1, arg1, fn2, arg2);
+ case T_UNSHARE:
+ return do_unshare_tests(clone_flags, fn1, arg1, fn2, arg2);
+ default:
+ printf("%s: bad use_clone option: %d\n", __FUNCTION__,
+ use_clone);
+ return -1;
+ }
+}
Index: ltp-full-20070331/testcases/kernel/containers/libclone/libclone.h
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testcases/kernel/containers/libclone/libclone.h 2007-04-19 15:10:22.000000000 +0530
@@ -0,0 +1,85 @@
+#ifndef __LIBCLONE_H
+#define __LIBCLONE_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sched.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <libgen.h>
+#include <sched.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <signal.h>
+
+#define T_UNSHARE 0
+#define T_CLONE 1
+#define T_NONE 2
+
+#ifndef SYS_unshare
+#ifdef __NR_unshare
+#define SYS_unshare __NR_unshare
+#elif __i386__
+#define SYS_unshare 310
+#elif __ia64__
+#define SYS_unshare 1296
+#elif __x86_64__
+#define SYS_unshare 272
+#elif __s390x__
+#define SYS_unshare 303
+#elif __powerpc__
+#define SYS_unshare 282
+#else
+#error "unshare not supported on this architecure."
+#endif
+#endif
+
+#ifndef CLONE_NEWUTS
+#define CLONE_NEWUTS 0x04000000
+#endif
+
+#ifndef CLONE_NEWIPC
+#define CLONE_NEWIPC 0x08000000
+#endif
+
+#ifndef CLONE_NEWUSER
+#define CLONE_NEWUSER 0x10000000
+#endif
+
+#ifndef CLONE_NEWNET2
+#define CLONE_NEWNET2 0x20000000
+#endif
+
+#ifndef CLONE_NEWNET3
+#define CLONE_NEWNET3 0x40000000
+#endif
+
+#ifndef CLONE_NEWPID
+#define CLONE_NEWPID 0x80000000
+#endif
+
+/*
+ * Run fn1 in a unshared environmnent, and fn2 in the original context
+ * Fn2 may be NULL.
+ */
+
+int do_clone_tests(unsigned long clone_flags,
+ int(*fn1)(void *arg), void *arg1,
+ int(*fn2)(void *arg), void *arg2);
+
+int do_unshare_tests(unsigned long clone_flags,
+ int (*fn1)(void *arg), void *arg1,
+ int (*fn2)(void *arg), void *arg2);
+
+int do_fork_tests(int (*fn1)(void *arg), void *arg1,
+ int (*fn2)(void *arg), void *arg2);
+
+int do_clone_unshare_test(int use_clone, unsigned long clone_flags,
+ int (*fn1)(void *arg), void *arg1);
+
+int do_clone_unshare_tests(int use_clone, unsigned long clone_flags,
+ int (*fn1)(void *arg), void *arg1,
+ int (*fn2)(void *arg), void *arg2);
+
+#endif
Index: ltp-full-20070331/testcases/kernel/containers/libclone/Makefile
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testcases/kernel/containers/libclone/Makefile 2007-04-19 15:10:22.000000000 +0530
@@ -0,0 +1,15 @@
+TARGET=libclone.a
+SRCS=$(wildcard *.c)
+OBJS=$(patsubst %.c,%.o,$(SRCS))
+
+all noltp : $(TARGET)
+
+$(TARGET): $(OBJS)
+ $(AR) -cr $@ $^
+
+clean:
+ rm -f $(TARGET) $(OBJS)
+
+noltp_check: noltp
+
+install:
Index: ltp-full-20070331/testcases/kernel/containers/Makefile
===================================================================
--- ltp-full-20070331.orig/testcases/kernel/containers/Makefile 2007-04-19 15:09:51.000000000 +0530
+++ ltp-full-20070331/testcases/kernel/containers/Makefile 2007-04-19 15:11:58.000000000 +0530
@@ -1,4 +1,4 @@
-SUBDIRS = libclone
+SUBDIRS = libclone utsname
all noltp noltp_check:
@set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i $@; done
-------------------------------------------------------------------------
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-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list