2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-28 21:07:43 +00:00

namespaces: parametrized namespace option introduced

v2: strlen() check removed from parse_ns_string()

Now '-n' option must be followed by namespaces tags, separated by commas.
Currently, only "uts" namespace is supported.

Signed-off-by: Stanislav Kinsbursky <skinsbursky@parallels.com>
Acked-by: Pavel Emelyanov <xemul@parallels.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
This commit is contained in:
Stanislav Kinsbursky 2012-01-31 22:28:22 +04:00 committed by Cyrill Gorcunov
parent 1e694235b4
commit 0213d3ec64
7 changed files with 38 additions and 22 deletions

View File

@ -1329,8 +1329,8 @@ int cr_dump_tasks(pid_t pid, struct cr_options *opts)
if (collect_pstree(pid, &pstree_list)) if (collect_pstree(pid, &pstree_list))
goto err; goto err;
if (opts->with_namespaces) { if (opts->namespaces_flags) {
ret = dump_namespaces(pid); ret = dump_namespaces(pid, opts->namespaces_flags);
if (ret < 0) if (ret < 0)
goto err; goto err;
} }

View File

@ -1371,7 +1371,6 @@ static int restore_root_task(int fd, struct cr_options *opts)
struct pstree_entry e; struct pstree_entry e;
int ret, i; int ret, i;
struct sigaction act, old_act; struct sigaction act, old_act;
unsigned long ns_clone_flags;
ret = read(fd, &e, sizeof(e)); ret = read(fd, &e, sizeof(e));
if (ret != sizeof(e)) { if (ret != sizeof(e)) {
@ -1402,12 +1401,7 @@ static int restore_root_task(int fd, struct cr_options *opts)
* this later. * this later.
*/ */
if (opts->with_namespaces) ret = fork_with_pid(e.pid, opts->namespaces_flags);
ns_clone_flags = CLONE_NEWUTS;
else
ns_clone_flags = 0;
ret = fork_with_pid(e.pid, ns_clone_flags);
if (ret < 0) if (ret < 0)
return -1; return -1;

View File

@ -18,6 +18,7 @@
#include "util.h" #include "util.h"
#include "log.h" #include "log.h"
#include "sockets.h" #include "sockets.h"
#include "syscall.h"
static struct cr_options opts; static struct cr_options opts;
struct page_entry zero_page_entry; struct page_entry zero_page_entry;
@ -252,6 +253,26 @@ err:
return NULL; return NULL;
} }
static int parse_ns_string(const char *ptr, unsigned int *flags)
{
const char *end = ptr + strlen(ptr);
do {
if (ptr[3] != ',' && ptr[3] != '\0')
goto bad_ns;
if (!strncmp(ptr, "uts", 3))
opts.namespaces_flags |= CLONE_NEWUTS;
else
goto bad_ns;
ptr += 4;
} while (ptr < end);
return 0;
bad_ns:
pr_err("Unknown namespace '%s'\n", ptr);
return -1;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
pid_t pid = 0; pid_t pid = 0;
@ -260,7 +281,7 @@ int main(int argc, char *argv[])
int action = -1; int action = -1;
int log_inited = 0; int log_inited = 0;
static const char short_opts[] = "df:p:t:hcD:o:n"; static const char short_opts[] = "df:p:t:hcD:o:n:";
BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE); BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
@ -307,7 +328,8 @@ int main(int argc, char *argv[])
log_inited = 1; log_inited = 1;
break; break;
case 'n': case 'n':
opts.with_namespaces = true; if (parse_ns_string(optarg, &opts.namespaces_flags))
return -1;
break; break;
case 'h': case 'h':
default: default:

View File

@ -53,7 +53,7 @@ struct cr_options {
bool leader_only; bool leader_only;
bool show_pages_content; bool show_pages_content;
bool restore_detach; bool restore_detach;
bool with_namespaces; unsigned int namespaces_flags;
}; };
/* file descriptors template */ /* file descriptors template */

View File

@ -1,6 +1,6 @@
#ifndef __CR_NS_H__ #ifndef __CR_NS_H__
#define __CR_NS_H__ #define __CR_NS_H__
int dump_namespaces(int pid); int dump_namespaces(int pid, unsigned int ns_flags);
int prepare_namespace(int pid, unsigned long clone_flags); int prepare_namespace(int pid, unsigned long clone_flags);
int try_show_namespaces(int pid); int try_show_namespaces(int pid);
int switch_ns(int pid, int type, char *ns); int switch_ns(int pid, int type, char *ns);

View File

@ -26,7 +26,7 @@ out:
return ret; return ret;
} }
static int do_dump_namespaces(int ns_pid) static int do_dump_namespaces(int ns_pid, unsigned int ns_flags)
{ {
struct cr_fdset *fdset; struct cr_fdset *fdset;
int ret; int ret;
@ -42,7 +42,7 @@ static int do_dump_namespaces(int ns_pid)
} }
int dump_namespaces(int ns_pid) int dump_namespaces(int ns_pid, unsigned int ns_flags)
{ {
int pid, ret, status; int pid, ret, status;
@ -66,7 +66,7 @@ int dump_namespaces(int ns_pid)
} }
if (pid == 0) { if (pid == 0) {
ret = do_dump_namespaces(ns_pid); ret = do_dump_namespaces(ns_pid, ns_flags);
exit(ret); exit(ret);
} }

View File

@ -30,7 +30,7 @@ $ZP/static/caps00
$ZP/static/cmdlinenv00 $ZP/static/cmdlinenv00
$ZP/static/socket_listen" $ZP/static/socket_listen"
NS_TEST_LIST="\ UTS_TEST_LIST="\
$ZP/static/utsname" $ZP/static/utsname"
CRTOOLS=`pwd`/`dirname $0`/../crtools CRTOOLS=`pwd`/`dirname $0`/../crtools
@ -72,15 +72,15 @@ if [ $# -eq 0 ]; then
for t in $TEST_LIST; do for t in $TEST_LIST; do
run_test $t "" || exit 1 run_test $t "" || exit 1
done done
for t in $NS_TEST_LIST; do for t in $UTS_TEST_LIST; do
run_test $t "-n" || exit 1 run_test $t "-n uts" || exit 1
done done
elif [ "$1" == "-l" ]; then elif [ "$1" == "-l" ]; then
echo $TEST_LIST | sed -e "s#$ZP/##g" -e 's/ /\n/g' echo $TEST_LIST | sed -e "s#$ZP/##g" -e 's/ /\n/g'
echo $NS_TEST_LIST | sed -e "s#$ZP/##g" -e 's/ /\n/g' echo $UTS_TEST_LIST | sed -e "s#$ZP/##g" -e 's/ /\n/g'
else else
if echo "$NS_TEST_LIST" | fgrep -q "$1" ; then if echo "$UTS_TEST_LIST" | fgrep -q "$1" ; then
run_test "$ZP/$1" "-n" && exit 0 run_test "$ZP/$1" "-n uts" && exit 0
else else
run_test "$ZP/$1" && exit 0 run_test "$ZP/$1" && exit 0
fi fi