restorer: Block signals early

Otherwise threads can handle them before calling sigreturn with wrong (not
belonging to crtools, but to the target task) handers:

__export_restore_task				| __export_restore_thread

restore_finish_stage(CR_STATE_RESTORE);		| restore_finish_stage(CR_STATE_RESTORE);
sys_sigaction(SIGCHLD, &args->sigchld_act, ...) |
restore_signals()				|
	ksigfillset(&to_block);			|
	sys_rt_sigqueueinfo(sys_getpid(), ...); |
						| execute signal handler() <------ BUG !!!
						| restore_signals()
						|	ksigfillset(&to_block);

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
service: change a few "puts" with "pr_perror"

Signed-off-by: Ruslan Kuprieiev <kupruser@gmail.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
service: move worker's code in a separate function

Original-patch-by: Andrey Vagin <avagin@openvz.org>
Original-patch-by: Ruslan Kuprieiev <kupruser@gmail.com>

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: allocate pstree items from a shared pool (v2)

A pstree item will contain pid and state. Both these properties will be
determined after creating processes and will be used from the
controlling proces.

v2: fix the error path
Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: get real pid for each task (v2)

For the root task the clone syscall returns the pid in criu's pidns,
but for other processes the clone syscall returns PID in the restored
namespace.

The /proc/self link contains the PID value of the current process, so if
we want to determing the PID in a criu's pidns, we should use criu's
/proc.

v2: readlink() does not append a null byte to buf, so we must do that
Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parasite: stop task on exit from a specified syscall

This patch adds nothing new, just splits the existant function.

Currently a parasite stopped on sigreturn for unmaping a parasite blob.
The same scheme will be used for restorer blob and this function will be
used to stop on exit from the munmap syscall.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parasite: allow to wait more than one process on the exit from syscall

All processes must be started by PTRACE_SYSCALL. The function calls wait
in a loop and if a process on the exit from the required syscall, it
is stopped, otherwise it will be reexecuted by PTRACE_SYSCALL.

The function doesn't know, which processes should be trapped, so
you should care, that wait() will not catch someone else.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parasite: prep_ctl doesn't search place for syscall if vma_list is NULL

This feature will be used for unmaping restorer blobs. All reqired code
will be in the blob.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parasite: add a function for unmaping bootstrap blob

The munmap syscall must be executed from a process memory. The code can
be injected in memory and then removed. But we can avoid all these
actions, if the code will be in the blob and a process will be trapped
on the exit from the munmap syscall.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parse_proc: move parse_threads from cr-dump.c

It will be used in cr-restore.c for stopping threads on the exit from
sigreturn.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
proc: allow parse_thread to use an existent buffer

parse_thread allocated a buffer for threads and then it initialized read
pid for each thread.

Now we want to use it on restore and in this moment we already have
a buffer with initialized virt pid-s, so we need to initialize read
pid-s only.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
pstree: initialize real pid to -1 for threads on restore

We are going to check is it valid or not.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: save a task state on pstree_item

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: catch task on the exit from sigreturn (v4)

A task is stopped here for unmaping restorer blob and restoring a state.

The method is the same as for parasite. CRIU attaches to processes via
ptrace and start to trace all syscalls.

v2: don't use a software breakpoint
v3: stop all thread on the exit from sigreturn
v4: attach to each thread
Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restorer: unmap itself (v2)

This patch adds a function for removing the restorer blob. This function
never returns and the process must be trapped on the exit from the
munmap syscall.

v2: * release parasite_ctl sturcture and use the new interface of
      parasite_prep_ctl

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parasite: unmap itself (v2)

This patch adds a new parasite command, which unmaps the parasite blob.
This command never returns and the criu process traps the target process
on the exit from the munmap syscall.

v2: rename the function for unmaping a parasite blob to not intersects
with criu's functions.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restorer: rework unmaping old VMA-s (v3)

All process VMA-s are in "premmaped area". All restorer stuff are in
bootstap "area", so we have two areas.

So we don't need to unmap extra VMA-s one by one. We can call munmap
three times for the region before the first area, for the hole between
areas and for the region after the second area.

The old scheme didn't work, because the list of VMA-s can be changed
after collecting. It can be due to memory allocations by libc or due to
increased stack.

v2: improve readability at the expense of beautiness
v3: print return code of munmap in error messages
Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: check that a process has the same set of VMS-s after restore

Some VMA-s can be merged on restore. For example, If a process maps
VMA1, VMA2 and then VMA3 between the previous ones.
|VMA1|VMA3|VMA2|
The VMA3 will be merged only with VMA1, but all three VMA-s will be
merged on restore, because they are mmaped in a correct order VMA1,
VMA3, VMA2.

Due to this issue, we have a small script for merging continuous VMA-s.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: check that pending signals are handled after resuming processes

This patch detects the race, when a signal hanler could be executed
during restore.
More details are in: 5d18eca restorer: Block signals early

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
arm: mark the "[vectors]" vma as VMA_AREA_VSYSCALL

This vma looks like VSYSCALL on x86. We don't need to dump and restore it.

Currently this vma is dumped and restored as a private vma, but it is not
remmaped in a correct place:
Restore
 --- dump/pipe00/6392/1/dump.maps	2013-09-23 12:49:19.436816192 +0000
 +++ dump/pipe00/6392/1/restore.maps	2013-09-23 12:49:20.276766356 +0000
 @@ -6,5 +6,6 @@ e05000-e26000
  4009d000-4009f000
  400a0000-400aa000
  400b8000-401e7000
 +b6d6f000-b6d70000
  be838000-be859000
  ffff0000-ffff1000
ERROR: Sets of mappings differ:

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>