use Suka's v11 api
PATCH 1/2 use Suka's v11 api
This patch:
1. changes restart to pass the right values to clone-with-pids.
2. updates the clone_s390x.c to work with the new kernel.
All tests under cr_tests/ pass with this patch.
--- clone_s390x.c | 92 +++++++++++++++++++++++++++++++++++++-------------------- restart.c | 14 +++++---- 2 files changed, 68 insertions(+), 38 deletions(-)
clone_s390x.c +60 -32
@@ -14,6 +14,7 @@#include <unistd.h> #include <errno.h> +#include <string.h> #include <sys/types.h> #include <sys/syscall.h> #include <asm/unistd.h>
@@ -25,48 +26,75 @@#include <linux/checkpoint.h> #if defined(__NR_clone_with_pids) -/* this really belongs to some kernel header ! */ struct pid_set {int num_pids;pid_t *pids; }; -/* (see: http://lkml.indiana.edu/hypermail/linux/kernel/9604.3/0204.html) */ +typedef unsigned long long u64 ; +typedef unsigned int u32 ; +typedef int pid_t ; +struct clone_args { +u64 clone_flags_high; -#define do_clone_with_pids (stack, flags, ptid, ctid, setp) ({ \ - register unsigned long int __r2 asm ("2") = (unsigned long int)(stack);\ - register unsigned long int __r3 asm ("3") = (unsigned long int)(flags);\ - register unsigned long int __r4 asm ("4") = (unsigned long int)(ptid); \ - register unsigned long int __r5 asm ("5") = (unsigned long int)(ctid); \ - register unsigned long int __r6 asm ("6") = (unsigned long int)(NULL); \ - register unsigned long int __r7 asm ("7") = (unsigned long int)(setp); \ - register unsigned long int __result asm ("2"); \ - __asm__ __volatile__( \ - " lghi %%r1,%7\n" \ - " svc 0\n" \ - : "=d" (__result) \ - : "0" (__r2), "d" (__r3), \ - "d" (__r4), "d" (__r5), "d" (__r6), "d" (__r7), \ - "i" (__NR_clone_with_pids) \ - : "1", "cc", "memory" \ - ); \ - __result; \ - }) +u64 child_stack_base; +u64 child_stack_size; -int clone_with_pids (int (*fn)(void *),void *child_stack,int flags, +u64 parent_tid_ptr; +u64 child_tid_ptr; + +u32 nr_pids; + +u32 reserved0; +u64 reserved1; +}; + +#define do_cwp (flags, pids, args, sz) \ +( { \ + register unsigned long int __r1 asm ("1") = (unsigned long int)(__NR_clone_with_pids); \ + register unsigned long int __r2 asm ("2") = (unsigned long int)(flags); \ + register unsigned long int __r3 asm ("3") = (unsigned long int)(args); \ + register unsigned long int __r4 asm ("4") = (unsigned long int)(sz); \ + register unsigned long int __r5 asm ("5") = (unsigned long int)(pids); \ + register long int __result asm ("2"); \ + __asm__ __volatile__( \ + " svc 0\n"/* do __NR_cwp syscall */ \ + " ltgr %%r2,%%r2\n" /* returned 0? */ \ + " jnz 1f\n" /* if not goto label 1 */ \ + " lg %%r3,0(%%r15)\n" /* get fnarg off stack into arg 1 */ \ + " lg %%r2,8(%%r15)\n" /* get fn off stack int r3 basr*/ \ + " lgr %%r1,%%r15\n" /* tmp store old stack pointer */ \ + " aghi %%r15,-160\n" /* move the stack */ \ + " stg %%r1,0(%%r15)\n" /* and save old stack pointer */ \ + " basr %%r14,%%r3\n" /* call fn(arg) */ \ + " svc 1\n" /* call exit */ \ + " 1:\n" \ + : "=d" (__result) \ + : "d" (__r1), "0" (__r2), "d" (__r3), "d" (__r4), "d" (__r5) \ +: " memory"); \ +__result; \ +} ) + +int clone_with_pids(int (*fn)(void *), void *child_stack, + unsigned long stack_size, unsigned long flags, struct pid_set *target_pids, void *arg) { -long retval; - retval =do_clone_with_pids (child_stack, flags,NULL ,NULL , - target_pids); +struct clone_args clone_args, *ca = &clone_args; +u64 *s; + +memset (ca,0 ,sizeof (struct clone_args )); + ca->nr_pids = target_pids->num_pids; + ca->child_stack_size = stack_size -16 ; + ca->child_stack_base = (u64 ) child_stack; +if (child_stack) { + s = (u64 *) (ca->child_stack_base + ca->child_stack_size); + *--s = (u64 ) arg; + *--s = (u64 ) fn; + ca->child_stack_size -=16 ; + } -if (retval <0 ) { - errno = -retval; -return -1 ; - }else if (retval ==0 ) { -return fn (arg); - }else -return retval; +return do_cwp (flags, target_pids->pids, ca, +sizeof (struct clone_args )); }#endif /* !defined(__NR_clone_with_pids) */
restart.c +8 -6
@@ -43,10 +43,12 @@ struct pid_set {/* (until it's supported by libc) from clone_ARCH.c */ #if defined(__NR_clone_with_pids) && defined(ARCH_HAS_CLONE_WITH_PID ) -extern int clone_with_pids (int (*fn)(void *),void *child_stack,int flags, +extern int clone_with_pids (int (*fn)(void *),void *child_stack, +unsigned long stack_size,int flags,struct pid_set *target_pids,void *arg);#else -static int clone_with_pids (int (*fn)(void *),void *child_stack,int flags, +static int clone_with_pids (int (*fn)(void *),void *child_stack, +unsigned long stack_size,int flags,struct pid_set *target_pids,void *arg) {return clone (fn, child_stack, flags, arg);
@@ -1749,18 +1751,17 @@ static pid_t ckpt_fork_child(struct ckpt_ctx *ctx, struct task *child) {struct pid_set pid_set;char *stack_region; -char *stack_start;unsigned long flags =SIGCHLD ; +unsigned long stack_size =PTHREAD_STACK_MIN ;pid_t pid =0 ;ckpt_dbg ("forking child vpid %d flags %#x\n" , child->pid, child->flags); - stack_region =malloc (PTHREAD_STACK_MIN ); + stack_region =malloc (stack_size);if (!stack_region) {perror ("stack malloc" );return -1 ; } - stack_start = stack_region +PTHREAD_STACK_MIN -1 ; pid_set.pids = &pid; pid_set.num_pids =1 ;
@@ -1788,7 +1789,8 @@ static pid_t ckpt_fork_child(struct ckpt_ctx *ctx, struct task *child)else child->real_parent =_getpid (); - pid =clone_with_pids (ckpt_fork_stub, stack_start, flags, &pid_set, child); + pid =clone_with_pids (ckpt_fork_stub, stack_region, stack_size -16 , + flags, &pid_set, child);if (pid <0 ) {perror ("clone" );free (stack_region);
Signature · 1 line
1.6.1.1
A message that is not in the archive (sent off-list or not yet received): 20091110165839.GA19222-r/[email protected]
PATCH 2/2 add nsexeccwp to test clone-with-pids
One of the concerns with clone-with-pids is whether the stack handling is all correct and robust enough to withstand real usage. Little testcases playing with pid values are also necessary, but can't replace really using clone-with-pids to start a shell from which to keep working.
This patch tweaks the old ns_exec.c namespace manipulation program to add a -z option to specify a pid. So you can:
nsexeccwp -cmp /bin/bash # start a shell in a new pidns+mntns mount -t proc proc /proc # mount private /proc echo $$ 1 nsexeccwp -z /bin/bash # start a shell with pid 999 echo $$ 999
--- Makefile | 5 +- clone.h | 54 +++++++++ nsexeccwp.c | 352 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 410 insertions(+), 1 deletions(-) create mode 100644 clone.h create mode 100644 nsexeccwp.c
Makefile +4 -1
@@ -20,7 +20,7 @@ CFLAGS += -g $(WARNS) $(CKPT_INCLUDE) $(DEBUG) # install dir INSTALL_DIR = /bin -PROGS = checkpoint restart ckptinfo +PROGS = checkpoint restart ckptinfo nsexeccwp # other cleanup OTHER = ckptinfo_types.c
@@ -39,11 +39,14 @@ restart: CFLAGS += -D__REENTRANT -pthread ifneq ($(SUBARCH),) restart: clone_$(SUBARCH).o restart: CFLAGS += -DARCH_HAS_CLONE_WITH_PID +nsexeccwp: clone_$(SUBARCH).o +nsexeccwp: CFLAGS += -DARCH_HAS_CLONE_WITH_PID endif # on powerpc, need also assembly file ifeq ($(SUBARCH),ppc) restart: clone_$(SUBARCH)_.o +nsexeccwp: clone_$(SUBARCH)_.o endif # ckptinfo dependencies
clone.h new file +54 -0
54 lines not on this page · raw message
nsexeccwp.c new file +352 -0
352 lines not on this page · raw message
Signature · 1 line
1.6.1.1
A message that is not in the archive (sent off-list or not yet received): 20091110165922.GA19263-r/[email protected]
Re: [PATCH user-cr 2/2] add nsexeccwp to test clone-with-pids
Ok, will add this to user-cr (v19-rc2).
BTW, where is the original nsexec source maintained ?
Oren.
Quoted 472 lines · Serge E. Hallyn
One of the concerns with clone-with-pids is whether the stack handling is all correct and robust enough to withstand real usage. Little testcases playing with pid values are also necessary, but can't replace really using clone-with-pids to start a shell from which to keep working.
This patch tweaks the old
ns_exec.cnamespace manipulation program to add a -z option to specify a pid. So you can:nsexeccwp -cmp /bin/bash # start a shell in a new pidns+mntns mount -t proc proc /proc # mount private /proc echo $$ 1 nsexeccwp -z /bin/bash # start a shell with pid 999 echo $$ 999Signed-off-by Serge E. Hallyn <serue-r/[email protected]>--- Makefile | 5 +- clone.h | 54 +++++++++ nsexeccwp.c | 352 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 410 insertions(+), 1 deletions(-) create mode 100644 clone.h create mode 100644 nsexeccwp.cMakefile +4
-1@@ -20,7 +20,7 @@ CFLAGS += -g $(WARNS) $(CKPT_INCLUDE) $(DEBUG) # install dir INSTALL_DIR = /bin -PROGS = checkpoint restart ckptinfo +PROGS = checkpoint restart ckptinfo nsexeccwp # other cleanup OTHER = ckptinfo_types.c@@ -39,11 +39,14 @@ restart: CFLAGS += -D__REENTRANT -pthread ifneq ($(SUBARCH),) restart: clone_$(SUBARCH).o restart: CFLAGS += -DARCH_HAS_CLONE_WITH_PID +nsexeccwp: clone_$(SUBARCH).o +nsexeccwp: CFLAGS += -DARCH_HAS_CLONE_WITH_PID endif # on powerpc, need also assembly file ifeq ($(SUBARCH),ppc) restart: clone_$(SUBARCH)_.o +nsexeccwp: clone_$(SUBARCH)_.o endif # ckptinfo dependenciesclone.h new file +54
-0@@ -0,0 +1,54 @@ +#ifndef CLONE_H +#define CLONE_H +/* +* Copyright (C) 2007 IBM Corporation +* +* Author: Cedric Le Goater <clg-NmTC/[email protected]> +* +* 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, version 2 of the +* License. +* +*/ +#include <sys/syscall.h> + +#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 + +#endif /* HAVE_UNSHARE */ + +#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_NEWPID +#define CLONE_NEWPID 0x20000000 +#endif + +#ifndef CLONE_NEWNET +#define CLONE_NEWNET 0x40000000 +#endif + +#endif /* CLONE_H */ nsexeccwp.c new file +352
-0@@ -0,0 +1,352 @@ +/* +* Copyright 2008,2009 IBM Corp. +*/ + +#include <stdio.h> +#include <stdlib.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/stat.h> +#include <sys/types.h> +#include <sys/wait.h> + +#include "clone.h" + +struct pid_set { +int num_pids; +pid_t *pids; +}; + +typedef unsigned long long u64 ; +typedef unsigned int u32 ; +typedef int pid_t ; +struct clone_args { +u64 clone_flags_high; + +u64 child_stack_base; +u64 child_stack_size; + +u64 parent_tid_ptr; +u64 child_tid_ptr; + +u32 nr_pids; + +u32 reserved0; +u64 reserved1; +}; +extern int clone_with_pids (int (*fn)(void *),void *child_stack, +unsigned long stack_size,unsigned long flags, +struct pid_set *target_pids,void *arg); + +extern pid_t getpgid (pid_t pid); +extern pid_t getsid (pid_t pid); + +static const char * procname; + +static void usage (const char *name) +{ +printf ("usage: %s [-h] [-c] [-mnuUip] [-P <pid-file>]" +"[command [arg ..]]\n" , name); +printf ("\n" ); +printf (" -h this message\n" ); +printf ("\n" ); +printf (" -z <pid> use clone_with_pids and specify chosen pid\n" ); +printf (" Note that -z and -p are not compatible\n" ); +printf (" -c use 'clone' rather than 'unshare' system call\n" ); +printf (" -g launch in new cgroup\n" ); +printf (" -m mount namespace\n" ); +printf (" -n network namespace\n" ); +printf (" -u utsname namespace\n" ); +printf (" -U userid namespace\n" ); +printf (" -i ipc namespace\n" ); +printf (" -P <pid-file> File in which to write global pid of cinit\n" ); +printf (" -p pid namespace\n" ); +printf (" -f <flag> extra clone flags\n" ); +printf ("\n" ); +printf ("(C) Copyright IBM Corp. 2006\n" ); +printf ("\n" ); +exit (1 ); +} + +static int string_to_ul (const char *str,unsigned long int *res) +{ +char *tail; +long long int r; + +if (!*str) +return -1 ; + + errno =0 ; + + r =strtol (str, &tail,16 ); + +/* +* according to strtol(3), if errno is set or tail does no point +* to the ending '\0', the conversion failed. +*/ +if (errno || *tail) +return -1 ; + + *res = r; +return 0 ; +} + +/* +* Copied following opentty() from Fedora's util-linux rpm +* I just changed the "FATAL" message below from syslog() +* to printf +*/ +static void +opentty (const char * tty) { +int i, fd, flags; + + fd =open (tty,O_RDWR |O_NONBLOCK ); +if (fd ==-1 ) { +printf ("FATAL: can't reopen tty: %s" ,strerror (errno)); +sleep (1 ); +exit (1 ); + } + + flags =fcntl (fd,F_GETFL ); + flags &= ~O_NONBLOCK ; +fcntl (fd,F_SETFL , flags); + +for (i =0 ; i < fd; i++) +close (i); +for (i =0 ; i <3 ; i++) +if (fd != i) +dup2 (fd, i); +if (fd >=3 ) +close (fd); +} +// Code copy end + +int do_newcgrp =0 ; + +int load_cgroup_dir (char *dest,int len) +{ +FILE *f =fopen ("/proc/mounts" ,"r" ); +char buf[200 ]; +char *name, *path, *fsname, *options, *p1, *p2, *s; +if (!f) +return 0 ; +while (fgets (buf,200 , f)) { + name =strtok_r (buf," " , &p1); + path =strtok_r (NULL ," " , &p1); + fsname =strtok_r (NULL ," " , &p1); + options =strtok_r (NULL ," " , &p1); +if (strcmp (fsname,"cgroup" ) !=0 ) +continue ; + +/* make sure the freezer is composed */ + s =strtok_r (options,"," , &p2); +while (s &&strcmp (s,"freezer" ) !=0 ) + s =strtok_r (NULL ,"," , &p2); +if (!s) +continue ; +strncpy (dest, path, len); +fclose (f); +return 1 ; + } +fclose (f); +printf ("Freezer not mounted\n" ); +return 0 ; +} + +int move_to_new_cgroup (int newcgroup) +{ +char cgroupname[150 ], cgroupbase[100 ], tasksfname[200 ]; +FILE *fout; +int ret; + +if (!load_cgroup_dir (cgroupbase,100 )) +return 0 ; + +snprintf (cgroupname,150 ,"%s/%d" , cgroupbase, newcgroup); + ret =mkdir (cgroupname,0755 ); +if (ret) +return 0 ; +snprintf (tasksfname,200 ,"%s/tasks" , cgroupname); + fout =fopen (tasksfname,"w" ); +if (!fout) +return 0 ; +fprintf (fout,"%d\n" ,getpid ()); +fclose (fout); +return 1 ; +} + +int pipefd[2 ]; + +/* gah. opentty will close the pipefd */ +int check_newcgrp (void ) +{ +int ret, newgroup; +char buf[20 ]; + +if (!do_newcgrp) +return 0 ; + +close (pipefd[1 ]); + ret =read (pipefd[0 ], buf,20 ); +close (pipefd[0 ]); +if (ret ==-1 ) { +perror ("read" ); +return 1 ; + } + newgroup =atoi (buf); +if (!move_to_new_cgroup (newgroup)) +return 1 ; + do_newcgrp =0 ; +return 0 ; +} + +int do_child (void *vargv) +{ +char **argv = (char **)vargv; + +if (check_newcgrp ()) +return 1 ; + +execve (argv[0 ], argv, __environ); +perror ("execve" ); +return 1 ; +} + +void write_pid (char *pid_file,int pid) +{ +FILE *fp; + +if (!pid_file) +return ; + + fp =fopen (pid_file,"w" ); +if (!fp) { +perror ("fopen, pid_file" ); +exit (1 ); + } +fprintf (fp,"%d" , pid); +fflush (fp); +fclose (fp); +} + +int main (int argc,char *argv[]) +{ +int c; +unsigned long flags =0 , eflags =0 ; +char ttyname[256 ]; +int status; +int ret, use_clone =0 ; +int pid; +char *pid_file =NULL ; +struct pid_set pid_set; +int chosen_pid =0 ; + + pid_set.num_pids =1 ; + pid_set.pids = &chosen_pid; + + procname =basename (argv[0 ]); + +memset (ttyname,'\0' ,sizeof (ttyname)); +readlink ("/proc/self/fd/0" , ttyname,sizeof (ttyname)); + +while ((c =getopt (argc, argv,"+mguUiphz:cnf:P:" )) !=EOF ) { +switch (c) { +case 'g' : do_newcgrp =getpid ();break ; +case 'm' : flags |=CLONE_NEWNS ;break ; +case 'c' : use_clone =1 ;break ; +case 'P' : pid_file = optarg;break ; +case 'u' : flags |=CLONE_NEWUTS ;break ; +case 'i' : flags |=CLONE_NEWIPC ;break ; +case 'U' : flags |=CLONE_NEWUSER ;break ; +case 'n' : flags |=CLONE_NEWNET ;break ; +case 'p' : flags |=CLONE_NEWNS |CLONE_NEWPID ;break ; +case 'z' : chosen_pid =atoi (optarg);break ; +case 'f' :if (!string_to_ul (optarg, &eflags)) { + flags |= eflags; +break ; + } +case 'h' : +default : +usage (procname); + } + }; + +if (chosen_pid) { + use_clone =1 ; +if (flags &CLONE_NEWPID ) { +printf ("Error: can't use CLONE_NEWPID and pick a pid\n" ); +exit (1 ); + } + } + argv = &argv[optind]; + argc = argc - optind; + +if (do_newcgrp) { + ret =pipe (pipefd); +if (ret) { +perror ("pipe" ); +return -1 ; + } + do_newcgrp = pipefd[0 ]; + } + +if (use_clone) { +int stacksize =4 *getpagesize (); +void *stack =malloc (stacksize); + +if (!stack) { +perror ("malloc" ); +return -1 ; + } + +printf ("about to clone with %lx\n" , flags); +if (chosen_pid) +printf ("Will choose pid %d\n" , chosen_pid); + flags |=SIGCHLD ; + pid =clone_with_pids (do_child, stack, stacksize, flags, + &pid_set, (void *)argv); +if (pid ==-1 ) { +perror ("clone" ); +return -1 ; + } + }else { +if ((pid =fork ()) ==0 ) { +// Child. +//print_my_info(procname, ttyname); + +if (check_newcgrp ()) +return 1 ; +opentty (ttyname); + +printf ("about to unshare with %lx\n" , flags); + ret =unshare (flags); +if (ret <0 ) { +perror ("unshare" ); +return 1 ; + } + +return do_child ((void *)argv); + } + + } +if (pid !=-1 && do_newcgrp) { +char buf[20 ]; +snprintf (buf,20 ,"%d" , pid); +close (pipefd[0 ]); +write (pipefd[1 ], buf,strlen (buf)+1 ); +close (pipefd[1 ]); + } + +write_pid (pid_file, pid); + +if ((ret =waitpid (pid, &status, __WALL)) <0 ) +printf ("waitpid() returns %d, errno %d\n" , ret, errno); + +exit (0 ); +}
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH user-cr 2/2] add nsexeccwp to test clone-with-pids
Quoted 4 lines · Oren Laadan
Ok, will add this to user-cr (v19-rc2).
BTW, where is the original nsexec source maintained ?
It isn't really 'maintained'. Used to be kept at lxc.sf.net, and right now a copy is in the cr_tests git tree.
-serge