summaryrefslogtreecommitdiffstats
path: root/Tests/RunCMake/CTestTimeout/TestTimeout.c
blob: da397f8c885377227b9a6f335e667bc5729e06f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OpenBSD__)
/* NOLINTNEXTLINE(bugprone-reserved-identifier) */
#  define _XOPEN_SOURCE 600
#endif

#if defined(_WIN32)
#  include <windows.h>
#else
#  include <sched.h>
#  include <unistd.h>
#endif

#include <stdio.h>

#ifdef SIGNAL
#  include <errno.h>
#  include <signal.h>
#  include <string.h>

static unsigned int signal_count;
static void signal_handler(int signum)
{
  (void)signum;
  ++signal_count;
}
#endif

int main(void)
{
#ifdef FORK
  pid_t pid = fork();
  if (pid != 0) {
    return 0;
  }
#endif

#ifdef SIGNAL
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = signal_handler;
  while ((sigaction(SIGUSR1, &sa, NULL) < 0) && (errno == EINTR))
    ;
#endif

#if defined(_WIN32)
  Sleep((TIMEOUT + 4) * 1000);
#elif defined(SIGNAL_IGNORE)
#  if defined(__CYGWIN__) || defined(__sun__) || defined(__GNU__)
#    define ERRNO_IS_EINTR (errno == EINTR || errno == 0)
#  else
#    define ERRNO_IS_EINTR (errno == EINTR)
#  endif
  {
    unsigned int timeLeft = (TIMEOUT + 4 + SIGNAL_IGNORE);
    while ((timeLeft = sleep(timeLeft), timeLeft > 0 && ERRNO_IS_EINTR)) {
      printf("EINTR: timeLeft=%u\n", timeLeft);
      fflush(stdout);
    }
  }
#else
  sleep((TIMEOUT + 4));
#endif

#ifdef SIGNAL
  if (signal_count > 0) {
    printf("SIGUSR1: count=%u\n", signal_count);
    fflush(stdout);
  }
#endif

  return 0;
}