crtools: add a separate type for atomic variables

It's a struct to prevent using usual arithmetic operations.

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

This code is copied from the Linux kernel.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
restore: auto-unlink for ghost files (v2)

A ghost file is used for restoring descriptors of an unlinked file.
It is created, opened and deleted.

Currently ghost files are collected in root task and then removed
by crtools when everybody is restored. This scheme doesn't work,
ghost_file_list is not shared, plus tasks may live in different mount
namespace.

It was broken by the following commit:
bd4e5d2f restore: prepare shared objects after initializing namespaces

We can't just move clear_ghost_files(), because we need to wait, until
all processes have not opened a ghost file.
We can add one more global barrier or move clear_ghost_files() in
a restore code bellow an existent barrier.

Here is a better sollution, a gost file is deleted by the last user.

v2: Use the type atomic_t and fix a commit message.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: open an one unlinked files two times

A few test check restoring of file descriptors to a one file struct.
Here is better to check, that a few file struct can be linked to one unlinked file.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
parasite: Initialize fd in parasite_get_proc_fd

Otherwise I get

 | parasite.c: In function ‘parasite_service’:
 | parasite.c:383:11: error: ‘fd’ may be used uninitialized in this function [-Werror=uninitialized]
 | cc1: all warnings being treated as errors

Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
fd: Move ps fd list selection to separate routine

Just for cleaner (from my pov) code.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
fd: Move rst fd list iteration into helper

Same again -- for code to be cleaner.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
fd: Fast path post open

Check for post_open callback presense before any other checks.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
fd: Rename tmp to ret in open_fd

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
fd: Sanitize open_fd with send_fd_to_self helper

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: fix compilation in test_msg()

cc -g -O2 -Wall -Werror -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0   -c -o msg.o msg.c
msg.c: In function ‘test_msg’:
msg.c:40:6: error: variable ‘len’ set but not used [-Werror=unused-but-set-variable]

Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: fix comilation for non-constant PAGE_SIZE

On some systems PAGE_SIZE is declared as sysconf(_SC_PAGESIZE) in <sys/user.h>
this is non-constant expression, so it cannot be used in type declarations.

cc -g -O2 -Wall -Werror -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0   -c -o ns.o ns.c
ns.c:124:7: error: variably modified ‘stack’ at file scope

Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: fix struct tpacket_req3 redeclaration

Linux has it since v3.2

cc -g -O2 -Wall -Werror -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 -I../../lib   packet_sock.c ../../lib/libzdtmtst.a   -o packet_sock
packet_sock.c:70:8: error: redefinition of ‘struct tpacket_req3’
In file included from packet_sock.c:23:0:
/usr/include/linux/if_packet.h:245:8: note: originally defined here

Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Acked-by: Andrew Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm.sh: fix bashisms

fix obvious bashisms to make script more portable

Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Acked-by: Andrey Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: fix bashisms

`< $FILE` isn't posix compatible and doesn't work in dash

Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Acked-by: Andrey Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: don't start state-tests on global stop

$ make cleanout stop
rm -f -r *.pid *.out* *.test* *.state
./conntracks --statefile=conntracks.state --outfile=conntracks.out start
make: *** [conntracks.state] Error 1

Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Acked-by: Andrey Vagin <avagin@parallels.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
sk-unix: add more information messages

* A message about external sockets is printed before any action.
* Add a message, when a peer is added in a list of sockets.

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
sk-unix: don't add a socket in a second time

A socket can be added in a list two times, if two other sockets
are connected to it.

This patch fixes a problem:
    Error (sk-unix.c:395): BUG at sk-unix.c:395

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
zdtm: make several connections to a file dgram socket

Before we checked only one connection.
With this patch the test reproduces a bug:

Error (sk-unix.c:395): BUG at sk-unix.c:395

Signed-off-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
sk-unix: add a peer only if it's not in a list yet

I tried to fix that by:
commit bc2ac71c1abddbe76893c0dc1f0421311786070c
Author: Andrey Vagin <avagin@openvz.org>
Date:   Wed Sep 12 15:16:39 2012 +0400

    sk-unix: don't add a socket in a second time

but made a mistake and now a peer would be added only if it's in a list.

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