summaryrefslogtreecommitdiff
path: root/nptl/tst-getpid3.c
blob: f1e77f6b1080c4e7fcc0567696f661d4d7edf4e8 (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
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>


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"