Signed-off-by: Veerendra C <[EMAIL PROTECTED]>
This patch consists of the 3 files .
libclone/Makefile -
libclone/libclone.h -
Where it adds the flags required for the netns and
function wrapper for unshare
libclone/libnetns.c -
Adds a function() to create the network namespaces.
Index: containers/libclone/Makefile
===================================================================
--- containers.orig/libclone/Makefile
+++ containers/libclone/Makefile
@@ -18,7 +18,7 @@
## ##
################################################################################
-TARGET=libclone.a
+TARGET=libclone.a libnetns.a
SRCS=$(wildcard *.c)
OBJS=$(patsubst %.c,%.o,$(SRCS))
Index: containers/libclone/libclone.h
===================================================================
--- containers.orig/libclone/libclone.h
+++ containers/libclone/libclone.h
@@ -51,6 +51,10 @@
#endif
#endif
+#ifndef __NR_unshare
+#define __NR_unshare SYS_unshare
+#endif
+
#ifdef __ia64__
#define clone2 __clone2
extern int __clone2(int (*fn) (void *arg), void *child_stack_base,
@@ -74,6 +78,14 @@ extern int __clone2(int (*fn) (void *ar
#define CLONE_NEWPID 0x20000000
#endif
+#ifndef CLONE_NEWNET
+#define CLONE_NEWNET 0x40000000
+#endif
+
+static inline int unshare(unsigned long flags)
+{
+ return syscall(__NR_unshare, flags);
+}
/*
* Run fn1 in a unshared environmnent, and fn2 in the original context
* Fn2 may be NULL.
Index: containers/libclone/libnetns.c
===================================================================
--- /dev/null
+++ containers/libclone/libnetns.c
@@ -0,0 +1,111 @@
+/*
+* Copyright (c) International Business Machines Corp., 2007
+* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*
+***************************************************************************/
+/*=========================================================================
+* This testcase creates the network namespace.
+* It creates veth pair veth8 & veth9. Also assigns IP addresses to the childNS.
+* Also it starts the sshd daemon @ port 7890
+*
+* Scripts Used: parentns.sh childns.sh
+=========================================================================*/
+
+#include <sys/utsname.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+#include <libgen.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include "libclone.h"
+
+int TST_TOTAL = 1;
+
+extern pid_t getpgid(pid_t pid);
+extern pid_t getsid(pid_t pid);
+
+int crtchild(char *s1 , char *s2)
+{
+ char *cmd[] = { "/bin/bash", s1, s2, (char *)0 };
+ execve("/bin/bash", cmd, __environ);
+ printf("The code would not reach here on success\n");
+ perror("execve");
+ return 1;
+}
+
+int create_net_namespace(char *p1, char *c1)
+{
+ int pid, status = 0, len, ret ;
+ long int flags = 0;
+ char *ltproot, *par, *child;
+
+ flags |= CLONE_NEWNS;
+ flags |= CLONE_NEWNET;
+
+ if (tst_kvercmp(2,6,19) < 0)
+ return 1;
+
+ ltproot = getenv("LTPROOT");
+
+ if ( !ltproot) {
+ printf("LTPROOT env variable is not set\n");
+ printf("Please set LTPROOT and re-run the test.. Thankyou\n");
+ return -1;
+ }
+
+ len = strlen(ltproot);
+ par = malloc(len + 70);
+ child = malloc(len + 70);
+
+ sprintf(par, "%s/testcases/kernel/containers/netns/parentns.sh %s" , ltproot, p1);
+ sprintf(child, "%s/testcases/kernel/containers/netns/childns.sh" , ltproot);
+
+ if ((pid = fork()) == 0) {
+
+ // Child.
+ ret = unshare(flags);
+ if (ret < 0) {
+ perror("unshare");
+ return 1;
+ }
+ return crtchild(child, c1);
+ }
+ else{
+
+ //parent
+ ret = system(par);
+ status = WEXITSTATUS(ret);
+ if (status != 0) {
+ printf("Error while running the script\n");
+ fflush(stdout);
+ exit(1);
+ }
+ fflush(stdout);
+
+ ret = waitpid(pid, &status, __WALL);
+ if (status != 0 || ret < 0){
+ printf("waitpid() returns %d, errno %d\n", ret, errno);
+ status = errno;
+ }
+
+ return 0;
+ }
+}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list