Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

childthread.c 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Test exit of a child of a TCB_EXITING child where the toplevel process starts
  2. * waiting on it. The middle one gets detached and strace must update the
  3. * toplevel process'es number of attached children to 0.
  4. *
  5. * gcc -o test/childthread test/childthread.c -Wall -ggdb2 -pthread;./strace -f ./test/childthread
  6. * It must print: write(1, "OK\n", ...
  7. */
  8. #include <pthread.h>
  9. #include <assert.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <sys/wait.h>
  14. static void *start0(void *arg)
  15. {
  16. pause();
  17. /* NOTREACHED */
  18. assert(0);
  19. }
  20. int main(int argc, char *argv[])
  21. {
  22. pthread_t thread0;
  23. pid_t child, got_pid;
  24. int status;
  25. int i;
  26. child = fork();
  27. switch (child) {
  28. case -1:
  29. assert(0);
  30. case 0:
  31. i = pthread_create(&thread0, NULL, start0, NULL);
  32. assert(i == 0);
  33. /* The thread must be initialized, it becomes thread-child of this
  34. process-child (child of a child of the toplevel process). */
  35. sleep(1);
  36. /* Here the child TCB cannot be deallocated as there still exist
  37. * children (the thread child in START0). */
  38. exit(42);
  39. /* NOTREACHED */
  40. assert(0);
  41. default:
  42. /* We must not be waiting in WAITPID when the child double-exits. */
  43. sleep(2);
  44. /* PID must be -1. */
  45. got_pid = waitpid(-1, &status, 0);
  46. assert(got_pid == child);
  47. assert(WIFEXITED(status));
  48. assert(WEXITSTATUS(status) == 42);
  49. puts("OK");
  50. exit(0);
  51. }
  52. /* NOTREACHED */
  53. assert(0);
  54. }