summaryrefslogtreecommitdiff
path: root/test/nptl/tst-getpid3.c
blob: 01d1e46af4accd3ab573e9e1296747b64cf4625e (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "../test-skeleton.h"

#ifdef __ARCH_USE_MMU__

static pid_t pid;

static void *
pid_thread (void *arg)
{
  if (pid != getpid ())
    {
      printf ("pid wrong in thread: should be %d, is %d\n",
	      (int) pid, (int) getpid ());
      return (void *) 1L;
    }

  return NULL;
}

static int
do_test (void)
{
  pid = getpid ();

  pthread_t thr;
  int ret = pthread_create (&thr, NULL, pid_thread, NULL);
  if (ret)
    {
      printf ("pthread_create failed: %d\n", ret);
      return 1;
    }

  void *thr_ret;
  ret = pthread_join (thr, &thr_ret);
  if (ret)
    {
      printf ("pthread_create failed: %d\n", ret);
      return 1;
    }
  else if (thr_ret)
    {
      printf ("thread getpid failed\n");
      return 1;
    }

  pid_t child = fork ();
  if (child == -1)
    {
      printf ("fork failed: %m\n");
      return 1;
    }
  else if (child == 0)
    {
      if (pid == getpid ())
	{
	  puts ("pid did not change after fork");
	  exit (1);
	}

      pid = getpid ();
      ret = pthread_create (&thr, NULL, pid_thread, NULL);
      if (ret)
	{
	  printf ("pthread_create failed: %d\n", ret);
	  return 1;
	}

      ret = pthread_join (thr, &thr_ret);
      if (ret)
	{
	  printf ("pthread_create failed: %d\n", ret);
	  return 1;
	}
      else if (thr_ret)
	{
	  printf ("thread getpid failed\n");
	  return 1;
	}

      return 0;
    }

  int status;
  if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child)
    {
      puts ("waitpid failed");
      kill (child, SIGKILL);
      return 1;
    }

  if (!WIFEXITED (status))
    {
      if (WIFSIGNALED (status))
	printf ("died from signal %s\n", strsignal (WTERMSIG (status)));
      else
	puts ("did not terminate correctly");
      return 1;
    }
  if (WEXITSTATUS (status) != 0)
    {
      printf ("exit code %d\n", WEXITSTATUS (status));
      return 1;
    }

  return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"

#else

int main(void)
{
    printf("Skipping test on non-mmu host!\n");
    return 23;
}

#endif