(tests): Add tst-endian.
[glibc/history.git] / nptl / tst-getpid1.c
blob253ebf2e159af7f2637786ca1381d604a013e03a
1 #include <sched.h>
2 #include <signal.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <stackinfo.h>
10 #ifndef TEST_CLONE_FLAGS
11 #define TEST_CLONE_FLAGS 0
12 #endif
14 static int sig;
16 static int
17 f (void *a)
19 puts ("in f");
20 union sigval sival;
21 sival.sival_int = getpid ();
22 printf ("pid = %d\n", sival.sival_int);
23 if (sigqueue (getppid (), sig, sival) != 0)
24 return 1;
25 return 0;
29 static int
30 do_test (void)
32 int mypid = getpid ();
34 sig = SIGRTMIN;
35 sigset_t ss;
36 sigemptyset (&ss);
37 sigaddset (&ss, sig);
38 if (sigprocmask (SIG_BLOCK, &ss, NULL) != 0)
40 printf ("sigprocmask failed: %m\n");
41 return 1;
44 #ifdef __ia64__
45 extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base,
46 size_t __child_stack_size, int __flags,
47 void *__arg, ...);
48 char st[256 * 1024] __attribute__ ((aligned));
49 pid_t p = __clone2 (f, st, sizeof (st), TEST_CLONE_FLAGS, 0);
50 #else
51 char st[128 * 1024] __attribute__ ((aligned));
52 # if _STACK_GROWS_DOWN
53 pid_t p = clone (f, st + sizeof (st), TEST_CLONE_FLAGS, 0);
54 # elif _STACK_GROWS_UP
55 pid_t p = clone (f, st, TEST_CLONE_FLAGS, 0);
56 # else
57 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
58 # endif
59 #endif
60 if (p == -1)
62 printf("clone failed: %m\n");
63 return 1;
65 printf ("new thread: %d\n", (int) p);
67 siginfo_t si;
69 if (sigwaitinfo (&ss, &si) < 0)
71 printf("sigwaitinfo failed: %m\n");
72 kill (p, SIGKILL);
73 return 1;
75 while (si.si_signo != sig || si.si_code != SI_QUEUE);
77 int e;
78 if (waitpid (p, &e, __WCLONE) != p)
80 puts ("waitpid failed");
81 kill (p, SIGKILL);
82 return 1;
84 if (!WIFEXITED (e))
86 if (WIFSIGNALED (e))
87 printf ("died from signal %s\n", strsignal (WTERMSIG (e)));
88 else
89 puts ("did not terminate correctly");
90 return 1;
92 if (WEXITSTATUS (e) != 0)
94 printf ("exit code %d\n", WEXITSTATUS (e));
95 return 1;
98 if (si.si_int != (int) p)
100 printf ("expected PID %d, got si_int %d\n", (int) p, si.si_int);
101 kill (p, SIGKILL);
102 return 1;
105 if (si.si_pid != p)
107 printf ("expected PID %d, got si_pid %d\n", (int) p, (int) si.si_pid);
108 kill (p, SIGKILL);
109 return 1;
112 if (getpid () != mypid)
114 puts ("my PID changed");
115 return 1;
118 return 0;
121 #define TEST_FUNCTION do_test ()
122 #include "../test-skeleton.c"