crtools: bump up the getopt return values to outside the ascii range

The return values were getting dangerously close to the range of meaningful
values, in particular the next candidate 63 is equal to '?' which is the
typical return value in case of error.

The return values for long options may be any integer, so bump them up to
outside the ascii range, start above 1000. For ease of review this patch, keep
the existing range (41-62) and increment each value by 1000.

Tested:
- Ran "criu --help", works fine.
- Manual dump and restore with some of the options, worked fine.
- Ran the zdtm test suite, tests passed.

Signed-off-by: Filipe Brandenburger <filbranden@google.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
cgroups: don't take into account a leading / for the "/" parent

Without this patch, we dump somethin like this:
{
	cnames: "hugetlb"
	dirs: 	{
		dir_name: ""
		children: 		{
			dir_name: "ewroot"
			children: <empty>
			properties: <empty>
		}

		properties: <empty>
	}

}

It's obvious, that dir_name should be newroot.

The problem is reproduced, if a task leaves in "/" and has a subgroup.
This issue was caught by a chance. The cgroup02 test doesn't clean up
controllers and leaves the "newroot" there. So when we executed a cgroup
test after cgroup02, we could find many directories like "ewroot",
"wroot", etc. This patch fixes this issue.

Signed-off-by: Andrew Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
cgroup: call fin_cgroup() on error paths

fini_cgroup umounts a cgyard directory, which is mounted
in prepare_cgroup().

Reported-by: Mr Jenkins
Signed-off-by: Andrew Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: fix compile errors of zdtm_ct

When "make test" is executed, CFLAGS is exported from the root Makefile.
These flags define _GNU_SOURCE, so we don't need to define it in the
souce file.

In addition unistd.h isn't included, so a few functions are shown as undeclared.

make zdtm_ns
make[3]: Entering directory `/root/criu/test'
gcc  -O2 -Wall -Werror -DCONFIG_X86_64 -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE    zdtm_ct.c   -o zdtm_ct
zdtm_ct.c:1:0: error: "_GNU_SOURCE" redefined [-Werror]
 #define _GNU_SOURCE
 ^
<command-line>:0:0: note: this is the location of the previous definition
zdtm_ct.c: In function ‘main’:
zdtm_ct.c:21:2: error: implicit declaration of function ‘fork’ [-Werror=implicit-function-declaration]
  pid = fork();
  ^
zdtm_ct.c:23:3: error: implicit declaration of function ‘setsid’ [-Werror=implicit-function-declaration]
   if (setsid() == -1) {
   ^
zdtm_ct.c:49:3: error: implicit declaration of function ‘execv’ [-Werror=implicit-function-declaration]
   execv(argv[1], argv + 1);
   ^
zdtm_ct.c:62:3: error: implicit declaration of function ‘getpid’ [-Werror=implicit-function-declaration]
   kill(getpid(), WTERMSIG(status));
   ^
cc1: all warnings being treated as errors

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Tested-by: Ruslan Kuprieiev <kupruser@gmail.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
test/cgroup02: Clean newroot dir from all controllers in hooks

From avagin@:
  And here is one more problem. the newroot directory is created for all
  controllers, but currently test cleans up it only for the zdtmtst
  controller. We need to find a way to clean up all other conntrollers.
   Tests are executed on a node, which is rebooted only for updating
  kernel, so if we will not clean up all other controllers, we can eat all
  memory.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
Tested-by: Andrew Vagin <avagin@openvz.org>
test: replace use of pcre grep with awk

Unfortunately, grep -P is not ubiquitous, so use awk with two regexps to
simulate the negative forward lookup in the grep -P expression.

Using awk doesn't really make it too unreadable, as using boolean operators
such as && and || might actually make it more intuitive than the extended
regexp.

Tested:
- sudo make -C test zdtm_ns
- sudo make -C test zdtm_nons

Signed-off-by: Filipe Brandenburger <filbranden@google.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
test: enhance library lookup when building mntns root

Use a single awk script to parse the ldd output. Filter out other cases that
are clearly not libraries, such as static builds ("not a dynamic executable")
and linux-gate.so. Make the grep for vdso more specific into linux-vdso.so.

Tested:
- sudo test/zdtm.sh '^ns/.*'

Signed-off-by: Filipe Brandenburger <filbranden@google.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: sort .gitignore entries

Signed-off-by: Filipe Brandenburger <filbranden@google.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: add missing entries to zdtm .gitignore

Signed-off-by: Filipe Brandenburger <filbranden@google.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
jenkins: exclude static/cgroups02 from the criu-dump job (v2)

In this job tests are dumped and resumed. The cgroup02 test checks,
that it is moved in another set of cgroups, but this is done on restore.

Output file: test/zdtm/live/static/cgroup02.out>
------------------------------------------------------------------------------
14:35:55.127:    85: found cgroup at cgroup02.test/zdtmtst>
14:35:55.127:    85: found cgroup at cgroup02.test/defaultroot>
14:35:55.127:    85: FAIL: cgroup02.c:132: oldroot not rewritten to zdtmtstroot!

v2: typo fix
Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: don't hide errors in cgroup02.hook

rmdir is executed for non-existent directories, so we don't check
an exit code of this operation.

This patch executs rmdir only for existent directories and check
an exit code of rmdir.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Acked-by: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
Use --root instead of --aufs-root

When dumping Docker containers using the AUFS graph driver, we can
use the --root option instead of --aufs-root for specifying the
container's root.  This patch obviates the need for --aufs-root
and makes dump CLI more consistent with restore CLI.

Signed-off-by: Saied Kazemi <saied@google.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: fallback to mknod(/dev/ptmx) when CONFIG_DEVPTS_MULTIPLE_INSTANCES is not set

A kernel without that option configured does not have /dev/pts/ptmx, so
fallback to the previous way of creating it using mknod instead.

The previous code was trying to bind mount ptmx on top of a symlink, which does
not actually work... Keep only the symlink call and use a relative symlink
instead. Adjust the error message of the symlink case to mention symlink()
instead of mknod() and also /dev/ptmx instead of /dev/pts.

Tested:
- zdtm test suite runs on ^ns/static/.* before and after the change.
- Same on a kernel with CONFIG_DEVPTS_MULTIPLE_INSTANCES unset.

Signed-off-by: Filipe Brandenburger <filbranden@google.com>
Acked-by: Andrew Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: die if init fails in --restore-detached mode

When in --restore-detached (i.e. root_as_sibling) mode, we ptrace(PTRACE_SEIZE)
the root task to receive its SIGCHLD in case one of its child tasks dies.
However, we don't receive a SIGCHLD if the root task itself dies, so we must
explicitly abort.

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: don't restore cg props if task restore fails

Once the task restore has failed, we can just abort, no need to restore the cg
props.

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: test that UNIX socket owner/group don't change

In container c/r(OVZ) UNIX socket uid and gid restored from
connected peer credentials instead of inode once.

https://bugzilla.openvz.org/show_bug.cgi?id=2969

changes:
v2 - cleanup headers, close socket, use err instead fail,
print id's on error.
v3 - wrong patch
v4 - move to sockets00, correct patch-message
v5 - minor cleanup

Signed-off-by: Pavel Tikhomirov <ptikhomirov@parallels.com>
Acked-by: Andrey Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: die properly if restore_one_task fails

This is really just the last bit of c32046c9; if restore_one_task() fails, we
need to do the same futex wakeup we do everywhere else in this function.

v2: use err instead of err_fini_mnt after mount has been finalized normally

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Acked-by: Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>