test/others: refactor loop process

There are several problems with the loop.sh script. First, the code is
duplicated across tests in the so-called 'othres' category. Second, we
need to run it with the 'setsid' utility to make sure that it runs in
a new session. Third, we have to redirect the standard file descriptors
and use the '&' operator to make it run in the background. Finally,
obtaining the PID of the 'loop.sh' process resulted in race condition.

In this patch we replace the loop.sh script with a program that would
address all problems mentioned above. The requirements for this program
are as follows.
- It must be reusable across tests
- It must start a process that is detached from the current shell
- It must wait for the process to start and output its PID

Signed-off-by: Radostin Stoyanov <rstoyanov@fedoraproject.org>
This commit is contained in:
Radostin Stoyanov 2021-06-09 08:13:00 +01:00 committed by Andrei Vagin
parent 2b78d95e6b
commit 2aa4185a6c
12 changed files with 74 additions and 20 deletions

1
test/others/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
loop

2
test/others/Makefile Normal file
View file

@ -0,0 +1,2 @@
loop:
gcc -Wall loop.c -o loop

View file

@ -1,4 +1,5 @@
run: clean
@make -C .. loop
./test.sh
clean:

View file

@ -1,4 +0,0 @@
#!/bin/bash
while :; do
sleep 1
done

View file

@ -8,8 +8,7 @@ source ../env.sh
images_list=""
function gen_imgs {
setsid ./loop.sh < /dev/null &> /dev/null &
PID=$!
PID=$(../loop)
if ! $CRIU dump -v4 -o dump.log -D ./ -t "$PID"; then
echo "Failed to checkpoint process $PID"
cat dump.log

View file

@ -1,4 +1,5 @@
run: clean
@make -C .. loop
./test.sh
clean:

View file

@ -1,4 +0,0 @@
#!/bin/bash
while :; do
sleep 1
done

View file

@ -1,8 +1,7 @@
source ../env.sh
function gen_imgs {
setsid ./loop.sh < /dev/null &> /dev/null &
PID=$!
PID=$(../loop)
if ! $CRIU dump -v4 -o dump.log -D ./ -t "$PID"; then
echo "Failed to checkpoint process $PID"
cat dump.log

63
test/others/loop.c Normal file
View file

@ -0,0 +1,63 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
pid_t pid;
pid_t sid;
int res = EXIT_FAILURE;
int start_pipe[2];
if (pipe(start_pipe)) {
perror("pipe failed!");
goto out;
}
pid = fork();
if (pid < 0) {
perror("fork failed!");
goto out;
}
if (pid == 0) {
close(start_pipe[0]);
sid = setsid();
if (sid < 0) {
perror("setsid failed!");
res = EXIT_FAILURE;
write(start_pipe[1], &res, sizeof(res));
close(start_pipe[1]);
exit(1);
}
// Create a file descriptor for "crit x ./ fds" test
open("/dev/null", O_RDONLY);
chdir("/");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
res = EXIT_SUCCESS;
write(start_pipe[1], &res, sizeof(res));
close(start_pipe[1]);
while(1) {
sleep(1);
}
}
close(start_pipe[1]);
read(start_pipe[0], &res, sizeof(res));
close(start_pipe[0]);
out:
if (res == EXIT_SUCCESS)
printf("%d\n", pid);
return res;
}

View file

@ -7,6 +7,7 @@ LDLIBS += -lprotobuf-c
PYTHON ?= python
run: all
@make -C .. loop
mkdir -p build
chmod a+rwx build
rm -f build/status

View file

@ -1,4 +0,0 @@
#!/bin/bash
while :; do
sleep 1
done

View file

@ -44,12 +44,11 @@ function test_py {
function test_restore_loop {
mkdir -p build/imgs_loop
title_print "Run loop.sh"
setsid ./loop.sh < /dev/null &> build/loop.log &
P=${!}
title_print "Run loop process"
P=$(../loop)
echo "pid ${P}"
title_print "Dump loop.sh"
title_print "Dump loop process"
# So theoretically '-j' (--shell-job) should not be necessary, but on alpine
# this test fails without it.
${CRIU} dump -j -v4 -o dump-loop.log -D build/imgs_loop -t ${P}