criu/test/zdtm/static/mntns_shared_bind.c
Dmitry Safonov 3bdaf5a9d8 zdtm: rely on -D_GNU_SOURCE passed from Makefiles
After the commit
  02c763939c10 ("test/zdtm: unify common code")

CFLAGS with -D_GNU_SOURCE defined in the top Makefile
are being passed to tests Makefiles.
As _GNU_SOURCE is also defined by tests, that resulted in
zdtm tests build failures:

  make[2]: Entering directory `/home/criu/test/zdtm/lib'
   CC        test.o
  test.c:1:0: error: "_GNU_SOURCE" redefined [-Werror]
   #define _GNU_SOURCE
   ^
  <command-line>:0:0: note: this is the location of the previous definition
  cc1: all warnings being treated as errors
  make[2]: *** [test.o] Error 1

However, we didn't catch this in time by Travis-CI, as zdtm.py doesn't
do `make zdtm`, rather it does `make -C test/zdtm/{lib,static,transition}`.
By calling middle makefile this way, it doesn't have _GNU_SOURCE in
CFLAGS from top-Makefile.

I think the right thing to do here - is following CRIU's way:
rely on definition of _GNU_SOURCE by Makefiles.

This patch is almost fully generated with
  find test/zdtm/ -name '*.c' -type f					\
     -exec sed -i '/define _GNU_SOURCE/{n;/^$/d;}' '{}' \;		\
     -exec sed -i '/define _GNU_SOURCE/d' '{}' \;

With an exception for adding -D_GNU_SOURCE in tests Makefile.inc for
keeping the same behaviour for zdtm.py.
Also changed utsname.c to use utsname::domainname, rather private
utsname::__domainname, as now it's uncovered (from sys/utsname.h):
> struct utsname
>  {
...
> # ifdef __USE_GNU
>     char domainname[_UTSNAME_DOMAIN_LENGTH];
> # else
>     char __domainname[_UTSNAME_DOMAIN_LENGTH];
> # endif

Reported-by: Adrian Reber <areber@redhat.com>
Cc: Kir Kolyshkin <kir@openvz.org>
Signed-off-by: Dmitry Safonov <dsafonov@virtuozzo.com>
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
2017-04-11 09:01:08 +03:00

127 lines
2.3 KiB
C

#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sched.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <limits.h>
#include "zdtmtst.h"
#ifndef CLONE_NEWNS
#define CLONE_NEWNS 0x00020000
#endif
const char *test_doc = "Check shared non-root bind-mounts";
const char *test_author = "Andrew Vagin <avagin@gmail.com>";
char *dirname;
TEST_OPTION(dirname, string, "directory name", 1);
int main(int argc, char **argv)
{
char path[PATH_MAX], bpath[PATH_MAX], spath[PATH_MAX];
pid_t pid;
int status;
task_waiter_t t;
test_init(argc, argv);
task_waiter_init(&t);
snprintf(path, sizeof(path), "%s/test", dirname);
snprintf(bpath, sizeof(bpath), "%s/test.bind", dirname);
snprintf(spath, sizeof(spath), "%s/test/sub", dirname);
if (mkdir(dirname, 0700)) {
pr_perror("mkdir");
return 1;
}
if (mount(NULL, "/", NULL, MS_SHARED, NULL)) {
pr_perror("mount");
return 1;
}
#ifdef SHARED_BIND02
/* */
if (mount(dirname, dirname, "tmpfs", 0, NULL) ||
mount(NULL, dirname, NULL, MS_SHARED, NULL)) {
pr_perror("mount");
return 1;
}
#endif
if (mkdir(path, 0700) ||
mkdir(spath, 0700) ||
mkdir(bpath, 0700)) {
pr_perror("mkdir");
return 1;
}
pid = fork();
if (pid < 0) {
pr_perror("fork");
return 1;
}
if (pid == 0) {
unshare(CLONE_NEWNS);
if (mount(path, bpath, NULL, MS_BIND, NULL)) {
pr_perror("mount");
return 1;
}
task_waiter_complete(&t, 1);
task_waiter_wait4(&t, 2);
if (umount(spath)) {
task_waiter_complete(&t, 2);
fail("umount");
return 1;
}
task_waiter_complete(&t, 3);
task_waiter_wait4(&t, 4);
return 0;
}
task_waiter_wait4(&t, 1);
if (mount("test", spath, "tmpfs", 0, NULL)) {
pr_perror("mount");
return 1;
}
test_daemon();
test_waitsig();
task_waiter_complete(&t, 2);
task_waiter_wait4(&t, 3);
if (umount(bpath)) {
task_waiter_complete(&t, 2);
fail("umount");
return 1;
}
task_waiter_complete(&t, 4);
if (waitpid(pid, &status, 0) != pid) {
pr_perror("waitpid %d", pid);
return 1;
}
if (status) {
pr_perror("%d/%d/%d/%d", WIFEXITED(status), WEXITSTATUS(status), WIFSIGNALED(status), WTERMSIG(status));
return 1;
}
pass();
return 0;
}