criu/test/zdtm/lib/lock.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

85 lines
1.8 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <time.h>
#include "zdtmtst.h"
#define TASK_WAITER_INITIAL 0x0fffff
static long sys_gettid(void)
{
return syscall(__NR_gettid);
}
void task_waiter_init(task_waiter_t *t)
{
datagen((void *)&t->seed, sizeof(t->seed), NULL);
t->seed = t->seed % TASK_WAITER_INITIAL;
if (pipe(t->pipes)) {
pr_perror("task_waiter_init failed");
exit(1);
}
}
void task_waiter_fini(task_waiter_t *t)
{
close(t->pipes[0]);
close(t->pipes[1]);
}
void task_waiter_wait4(task_waiter_t *t, unsigned int lockid)
{
struct timespec req = { .tv_nsec = TASK_WAITER_INITIAL, };
struct timespec rem = { };
unsigned int v;
for (;;) {
if (read(t->pipes[0], &v, sizeof(v)) != sizeof(v))
goto err;
/*
* If we read a value not intended for us, say parent
* waits for specified child to complete among set of
* children, or we just have completed and wait for
* another lockid from a parent -- we need to write
* the value back and wait for some time before
* next attempt.
*/
if (v != lockid) {
if (write(t->pipes[1], &v, sizeof(v)) != sizeof(v))
goto err;
/*
* If we get a collision in access, lets sleep
* semi-random time magnitude to decrease probability
* of a new collision.
*/
nanosleep(&req, &rem);
req.tv_nsec += t->seed;
} else
break;
}
return;
err:
pr_perror("task_waiter_wait4 failed");
exit(errno);
}
void task_waiter_complete(task_waiter_t *t, unsigned int lockid)
{
if (write(t->pipes[1], &lockid, sizeof(lockid)) != sizeof(lockid)) {
pr_perror("task_waiter_complete failed");
exit(1);
}
}
void task_waiter_complete_current(task_waiter_t *t)
{
return task_waiter_complete(t, (int)sys_gettid());
}