tst-mallocfork.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Derived from the test case in
  2. https://sourceware.org/bugzilla/show_bug.cgi?id=838. */
  3. #include <assert.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <libc-diag.h>
  11. static void
  12. sig_handler (int signum)
  13. {
  14. pid_t child = fork ();
  15. if (child == 0)
  16. exit (0);
  17. TEMP_FAILURE_RETRY (waitpid (child, NULL, 0));
  18. }
  19. static int
  20. do_test (void)
  21. {
  22. pid_t parent = getpid ();
  23. struct sigaction action = { .sa_handler = sig_handler };
  24. sigemptyset (&action.sa_mask);
  25. DIAG_PUSH_NEEDS_COMMENT;
  26. DIAG_IGNORE_NEEDS_COMMENT (10, "-Wunused-result");
  27. /* The result of malloc is deliberately ignored, so do not warn
  28. about that. */
  29. malloc (sizeof (int));
  30. DIAG_POP_NEEDS_COMMENT;
  31. if (sigaction (SIGALRM, &action, NULL) != 0)
  32. {
  33. puts ("sigaction failed");
  34. return 1;
  35. }
  36. /* Create a child that sends the signal to be caught. */
  37. pid_t child = fork ();
  38. if (child == 0)
  39. {
  40. if (kill (parent, SIGALRM) == -1)
  41. perror ("kill");
  42. exit (0);
  43. }
  44. TEMP_FAILURE_RETRY (waitpid (child, NULL, 0));
  45. return 0;
  46. }
  47. #define TEST_FUNCTION do_test ()
  48. #include "../test-skeleton.c"