tst-setcontext6.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Check getcontext and setcontext on the context from makecontext.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <ucontext.h>
  18. #include <unistd.h>
  19. #include <stdatomic.h>
  20. static ucontext_t ctx[3];
  21. static atomic_int done;
  22. static void
  23. f1 (void)
  24. {
  25. printf ("start f1\n");
  26. if (!done)
  27. {
  28. if (getcontext (&ctx[2]) != 0)
  29. {
  30. printf ("%s: getcontext: %m\n", __FUNCTION__);
  31. exit (EXIT_FAILURE);
  32. }
  33. if (done)
  34. exit (EXIT_SUCCESS);
  35. }
  36. done++;
  37. if (setcontext (&ctx[2]) != 0)
  38. {
  39. printf ("%s: setcontext: %m\n", __FUNCTION__);
  40. exit (EXIT_FAILURE);
  41. }
  42. }
  43. static int
  44. do_test (void)
  45. {
  46. char st1[32768];
  47. puts ("making contexts");
  48. if (getcontext (&ctx[0]) != 0)
  49. {
  50. printf ("%s: getcontext: %m\n", __FUNCTION__);
  51. exit (EXIT_FAILURE);
  52. }
  53. if (getcontext (&ctx[1]) != 0)
  54. {
  55. printf ("%s: getcontext: %m\n", __FUNCTION__);
  56. exit (EXIT_FAILURE);
  57. }
  58. ctx[1].uc_stack.ss_sp = st1;
  59. ctx[1].uc_stack.ss_size = sizeof st1;
  60. ctx[1].uc_link = &ctx[0];
  61. makecontext (&ctx[1], (void (*) (void)) f1, 0);
  62. if (setcontext (&ctx[1]) != 0)
  63. {
  64. printf ("%s: setcontext: %m\n", __FUNCTION__);
  65. exit (EXIT_FAILURE);
  66. }
  67. exit (EXIT_FAILURE);
  68. }
  69. #include <support/test-driver.c>