tst-setcontext7.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Check 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[5];
  21. static atomic_int done;
  22. static void
  23. f1 (void)
  24. {
  25. puts ("start f1");
  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. {
  35. puts ("set context in f1");
  36. if (setcontext (&ctx[3]) != 0)
  37. {
  38. printf ("%s: setcontext: %m\n", __FUNCTION__);
  39. exit (EXIT_FAILURE);
  40. }
  41. }
  42. }
  43. done++;
  44. puts ("swap contexts in f1");
  45. if (swapcontext (&ctx[4], &ctx[2]) != 0)
  46. {
  47. printf ("%s: setcontext: %m\n", __FUNCTION__);
  48. exit (EXIT_FAILURE);
  49. }
  50. puts ("end f1");
  51. exit (done == 2 ? EXIT_SUCCESS : EXIT_FAILURE);
  52. }
  53. static int
  54. do_test (void)
  55. {
  56. char st1[32768];
  57. puts ("making contexts");
  58. if (getcontext (&ctx[0]) != 0)
  59. {
  60. printf ("%s: getcontext: %m\n", __FUNCTION__);
  61. exit (EXIT_FAILURE);
  62. }
  63. if (getcontext (&ctx[1]) != 0)
  64. {
  65. printf ("%s: getcontext: %m\n", __FUNCTION__);
  66. exit (EXIT_FAILURE);
  67. }
  68. ctx[1].uc_stack.ss_sp = st1;
  69. ctx[1].uc_stack.ss_size = sizeof st1;
  70. ctx[1].uc_link = &ctx[0];
  71. makecontext (&ctx[1], (void (*) (void)) f1, 0);
  72. puts ("swap contexts");
  73. if (swapcontext (&ctx[3], &ctx[1]) != 0)
  74. {
  75. printf ("%s: setcontext: %m\n", __FUNCTION__);
  76. exit (EXIT_FAILURE);
  77. }
  78. if (done != 1)
  79. exit (EXIT_FAILURE);
  80. done++;
  81. puts ("set context");
  82. if (setcontext (&ctx[4]) != 0)
  83. {
  84. printf ("%s: setcontext: %m\n", __FUNCTION__);
  85. exit (EXIT_FAILURE);
  86. }
  87. exit (EXIT_FAILURE);
  88. }
  89. #include <support/test-driver.c>