* login/login_tty.c (login_tty): The Linux kernel can return EBUSY
[glibc.git] / nptl / tst-getpid3.c
blobf1e77f6b1080c4e7fcc0567696f661d4d7edf4e8
1 #include <errno.h>
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/wait.h>
10 static pid_t pid;
12 static void *
13 pid_thread (void *arg)
15 if (pid != getpid ())
17 printf ("pid wrong in thread: should be %d, is %d\n",
18 (int) pid, (int) getpid ());
19 return (void *) 1L;
22 return NULL;
25 static int
26 do_test (void)
28 pid = getpid ();
30 pthread_t thr;
31 int ret = pthread_create (&thr, NULL, pid_thread, NULL);
32 if (ret)
34 printf ("pthread_create failed: %d\n", ret);
35 return 1;
38 void *thr_ret;
39 ret = pthread_join (thr, &thr_ret);
40 if (ret)
42 printf ("pthread_create failed: %d\n", ret);
43 return 1;
45 else if (thr_ret)
47 printf ("thread getpid failed\n");
48 return 1;
51 pid_t child = fork ();
52 if (child == -1)
54 printf ("fork failed: %m\n");
55 return 1;
57 else if (child == 0)
59 if (pid == getpid ())
61 puts ("pid did not change after fork");
62 exit (1);
65 pid = getpid ();
66 ret = pthread_create (&thr, NULL, pid_thread, NULL);
67 if (ret)
69 printf ("pthread_create failed: %d\n", ret);
70 return 1;
73 ret = pthread_join (thr, &thr_ret);
74 if (ret)
76 printf ("pthread_create failed: %d\n", ret);
77 return 1;
79 else if (thr_ret)
81 printf ("thread getpid failed\n");
82 return 1;
85 return 0;
88 int status;
89 if (TEMP_FAILURE_RETRY (waitpid (child, &status, 0)) != child)
91 puts ("waitpid failed");
92 kill (child, SIGKILL);
93 return 1;
96 if (!WIFEXITED (status))
98 if (WIFSIGNALED (status))
99 printf ("died from signal %s\n", strsignal (WTERMSIG (status)));
100 else
101 puts ("did not terminate correctly");
102 return 1;
104 if (WEXITSTATUS (status) != 0)
106 printf ("exit code %d\n", WEXITSTATUS (status));
107 return 1;
110 return 0;
113 #define TEST_FUNCTION do_test ()
114 #include "../test-skeleton.c"