From: Serge E. H. <se...@us...> - 2008-08-11 14:10:32
|
Quoting Veerendra (ve...@li...): > Veerendra wrote: > > Signed-off-by: Veerendra C <vec...@in...> > > > > This patch consists of the files.. > > > > containers/libclone/libclone.h > > containers/libclone/libnetns.c > > containers/libclone/Makefile > > > > > > Regards > > Veerendra C > > > > > > > > > > Attaching the patch.. > > > --~--~---------~--~----~------------~-------~--~----~ > You received this message because you are subscribed to the Google Groups "lxc-dev" group. > To post to this group, send email to lx...@go... > To unsubscribe from this group, send email to lxc...@go... > For more options, visit this group at http://groups.google.com/group/lxc-dev?hl=en > -~----------~----~----~----~------~----~------~--~--- > > 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 The existing libclone.h already tries to define these for arches that need them. I certainly believe that there are distributions which are still funky, but let's understand why. Do you have a machine of your own on which you needed this? Can you remove this bit and let me know what happens? > + > +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 What on earth are you using clone64 and unshare64 for? They don't actually exist, and you certainly don't need them for network namespaces. > +#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,10 @@ 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)); > +} > + Again, you shouldn't need the above. > #endif > Index: containers/libclone/libnetns.c > =================================================================== > --- /dev/null > +++ containers/libclone/libnetns.c > @@ -0,0 +1,105 @@ > +/* > +* 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 "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> > + > +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, ret; > + long long flags = 0; > + char par[256], child[256]; > + char path[128]; > + > + if (tst_kvercmp(2,6,19) < 0) > + return 1; > + > + if (ret = getenv("LTPROOT")) { > + sprintf(path,"%s%s",getenv("LTPROOT"),"/testcases/kernel/containers/netns/"); > + } > + else{ > + printf("LTPROOT env variable is not set\n"); > + printf("Please set LTPROOT and re-run the test.. Thankyou\n"); > + return -1; > + } > + > + flags |= CLONE_NEWNS; > + flags |= CLONE_NEWNET; > + > + sprintf(par , "%s%s %s", path, "parentns.sh", p1); > + sprintf(child , "%s%s", path,"childns.sh"); > + > + if ((pid = fork()) == 0) { > + > + // Child. > + ret = unshare64(flags); This can't be succeeding on a stock kernel. Please test with git://git.kernel.org/pub/scm/linux/kernel/git/daveh/linux-2.6-lxc.git right now. I suspect what happened was you cut and pasted code from an experimental but obsolete ns_exec, but you need to use standard unshare() here. > + if (ret < 0) { > + perror("unshare"); > + return 1; > + } > + return crtchild(child,c1); > + } > + else{ > + > + //parent > + ret = system(par); > + if (ret != 0) { > + printf("Error not able to assign the pid to the net\n"); > + fflush(stdout); > + exit(1); > + } > + fflush(stdout); > + > + if ((ret = waitpid(pid, &status, __WALL)) < 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)) > |