From: Subrata M. <su...@li...> - 2008-08-22 22:11:12
|
On Sat, 2008-08-23 at 03:38 +0530, Subrata Modak wrote: > On Fri, 2008-08-22 at 19:08 +0530, Veerendra wrote: > > Subrata Modak wrote: > > > Do you want me to apply it over Veerendra´s patches (once they get > > > ACK-ed) ? > > > > > > Regards-- > > > Subrata > > > > > > > > Subrata, > > > > The changes suggested by Sudhir were more of coding styles. We > > internally discussed this and agreed that these changes are not > > necessary. So you can consider my original patch . > > Anyways, you would like to get these ACK-ed by Benjamin or Daniel after > Serge´s ACK, as suggested by Serge. Moreover, what about improving the > README as i suggested, which is very important to me :-) Also, what what do you feel about providing/posting the Test results on running this patch on a NETNS enabled kernel ? Regards-- Subrata > > Regards-- > Subrata > > > > > Thanks > > Veerendra C > > > > > > > On Fri, 2008-08-22 at 12:27 +0530, Sudhir Kumar wrote: > > > > > >> This patch adds a small static library which provides a C function to > > >> create a new network namespace. The API accepts two scripts, which will > > >> be executed in the two namespaces. > > >> > > >> Signed-off-by: Sudhir Kumar <sk...@li...> > > >> Signed-off-by: Veerendra C <vee...@li...> > > >> Index: containers/libclone/libclone.h > > >> =================================================================== > > >> --- containers.orig/libclone/libclone.h > > >> +++ containers/libclone/libclone.h > > >> @@ -58,6 +58,46 @@ extern int __clone2(int (*fn) (void *ar > > >> pid_t *parent_tid, void *tls, pid_t *child_tid); > > >> #endif > > >> > > >> +#ifndef HAVE_UNSHARE > > >> + > > >> +#if __i386__ > > >> +# define __NR_unshare 310 > > >> +#elif __x86_64__ > > >> +# define __NR_unshare 272 > > >> +#elif __ia64__ > > >> +# define __NR_unshare 1296 > > >> +#elif __s390x__ > > >> +# define __NR_unshare 303 > > >> +#elif __powerpc__ > > >> +# define __NR_unshare 282 > > >> +#else > > >> +# error "Architecture not supported" > > >> +#endif > > >> + > > >> +static inline int unshare(unsigned long flags) > > >> +{ > > >> + return syscall(__NR_unshare, flags); > > >> +} > > >> + > > >> +#endif /* HAVE_UNSHARE */ > > >> + > > >> +#if __i386__ > > >> +# define __NR_clone64 333 > > >> +# define __NR_unshare64 334 > > >> +#elif __powerpc__ > > >> +# define __NR_clone64 313 > > >> +# define __NR_unshare64 314 > > >> +#elif __s390x__ || __s390__ > > >> +# define __NR_clone64 322 > > >> +# define __NR_unshare64 323 > > >> +#elif __x86_64__ > > >> +# define __NR_clone64 295 > > >> +# define __NR_unshare64 296 > > >> +#else > > >> +# error "Architecture not supported" > > >> +#endif > > >> + > > >> + > > >> #ifndef CLONE_NEWUTS > > >> #define CLONE_NEWUTS 0x04000000 > > >> #endif > > >> @@ -74,6 +114,10 @@ extern int __clone2(int (*fn) (void *ar > > >> #define CLONE_NEWPID 0x20000000 > > >> #endif > > >> > > >> +#ifndef CLONE_NEWNET > > >> +#define CLONE_NEWNET 0x40000000 > > >> +#endif > > >> + > > >> /* > > >> * Run fn1 in a unshared environmnent, and fn2 in the original context > > >> * Fn2 may be NULL. > > >> @@ -97,4 +141,16 @@ int do_clone_unshare_tests(int use_clone > > >> int (*fn1)(void *arg), void *arg1, > > >> int (*fn2)(void *arg), void *arg2); > > >> > > >> +static inline int unshare64(unsigned long long int flags) > > >> +{ > > >> + return syscall(__NR_unshare64, (unsigned long) (flags >> 32), > > >> + (unsigned long) (flags & 0xffffffff)); > > >> +} > > >> + > > >> +/* Functions to be used by libnetns library */ > > >> +extern pid_t getpgid(pid_t pid); > > >> +extern pid_t getsid(pid_t pid); > > >> +int create_child(char *s1 , char *s2); > > >> +int create_net_namespace(char *parexe, char *childexe, char *option); > > >> + > > >> #endif > > >> Index: containers/libclone/libnetns.c > > >> =================================================================== > > >> --- /dev/null > > >> +++ containers/libclone/libnetns.c > > >> @@ -0,0 +1,114 @@ > > >> +/* > > >> +* Copyright (c) International Business Machines Corp., 2008 > > >> +* 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 > > >> +* > > >> +***************************************************************************/ > > >> +/*========================================================================= > > >> +* These functions are to create a small library which is to be used for > > >> +* creation of containers network namespace. The API create_net_namespace() is > > >> +* used to create a network namespace. The API takes 3 parameters > > >> +*1. The name of the parent script > > >> +*2. The name of the child script > > >> +*3. bash option > > >> +* > > >> +* The parent and the child script contain the testcode to be executed. > > >> +* > > >> +Author Date Email Id > > >> +Veerendra C 07/08/08 <vee...@li...> > > >> +Sudhir Kumar 21/08/08 <sk...@li...> > > >> + > > >> +=========================================================================*/ > > >> + > > >> +#include <sys/utsname.h> > > >> +#include <sched.h> > > >> +#include <stdio.h> > > >> +#include <stdlib.h> > > >> +#include "libclone.h" > > >> +#include <sched.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> > > >> + > > >> +int create_child(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; > > >> +} > > >> + > > >> +/* Receives the names of both executable and the shell option */ > > >> +int create_net_namespace(char *parexe, char *childexe, char *option) > > >> +{ > > >> + int pid, status, ret; > > >> + long long flags = 0; > > >> + char parexepath[FILENAME_MAX], childexepath[FILENAME_MAX]; > > >> + char *ltproot; > > >> + > > >> + if (tst_kvercmp(2, 6, 19) < 0) > > >> + return 1; > > >> + > > >> + ltproot = getenv("LTPROOT"); > > >> + if (ltproot != NULL) { > > >> + sprintf(parexepath, "%s/testcases/kernel/containers/netns/%s", > > >> + ltproot, parexe); > > >> + sprintf(childexepath, "%s/testcases/kernel/containers/netns/%s", > > >> + ltproot, childexe); > > >> + } else { > > >> + printf("LTPROOT env variable is not set " > > >> + "Please set LTPROOT and re-run the test.. Thankyou\n"); > > >> + return -1; > > >> + } > > >> + > > >> + flags |= CLONE_NEWNS; > > >> + flags |= CLONE_NEWNET; > > >> + > > >> + pid = fork(); > > >> + if (pid == 0) { > > >> + /* Child */ > > >> + ret = unshare64(flags); > > >> + if (ret < 0) { > > >> + perror("unshare"); > > >> + return 1; > > >> + } > > >> + return create_child(childexepath, option); > > >> + } else { > > >> + /* parent */ > > >> + ret = system(parexepath); > > >> + if (ret != 0 || WEXITSTATUS(ret) != 0) { > > >> + printf("Error while running the parent script.\n"); > > >> + fflush(stdout); > > >> + /* I think it should first kill all the children > > >> + * otherwise those will become orphan. How to handle > > >> + * this situation ???*/ > > >> + exit(1); > > >> + } > > >> + fflush(stdout); > > >> + > > >> + ret = waitpid(pid, &status, __WALL); > > >> + if (ret < 0 || status != 0) { > > >> + printf("waitpid() returns %d, errno %d\n", ret, errno); > > >> + return errno; > > >> + } > > >> + return 0; > > >> + > > >> + } > > >> +} > > >> 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)) > > >> > > >> > > > > > > > > |