unregister-atfork.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <errno.h>
  16. #include <stdlib.h>
  17. #include <fork.h>
  18. #include <atomic.h>
  19. #include <tls.h>
  20. #ifdef __ARCH_USE_MMU__
  21. void
  22. __unregister_atfork (
  23. void *dso_handle)
  24. {
  25. /* Check whether there is any entry in the list which we have to
  26. remove. It is likely that this is not the case so don't bother
  27. getting the lock.
  28. We do not worry about other threads adding entries for this DSO
  29. right this moment. If this happens this is a race and we can do
  30. whatever we please. The program will crash anyway seen. */
  31. struct fork_handler *runp = __fork_handlers;
  32. struct fork_handler *lastp = NULL;
  33. while (runp != NULL)
  34. if (runp->dso_handle == dso_handle)
  35. break;
  36. else
  37. {
  38. lastp = runp;
  39. runp = runp->next;
  40. }
  41. if (runp == NULL)
  42. /* Nothing to do. */
  43. return;
  44. /* Get the lock to not conflict with additions or deletions. Note
  45. that there couldn't have been another thread deleting something.
  46. The __unregister_atfork function is only called from the
  47. dlclose() code which itself serializes the operations. */
  48. lll_lock (__fork_lock, LLL_PRIVATE);
  49. /* We have to create a new list with all the entries we don't remove. */
  50. struct deleted_handler
  51. {
  52. struct fork_handler *handler;
  53. struct deleted_handler *next;
  54. } *deleted = NULL;
  55. /* Remove the entries for the DSO which is unloaded from the list.
  56. It's a single linked list so readers are. */
  57. do
  58. {
  59. again:
  60. if (runp->dso_handle == dso_handle)
  61. {
  62. if (lastp == NULL)
  63. {
  64. /* We have to use an atomic operation here because
  65. __linkin_atfork also uses one. */
  66. if (catomic_compare_and_exchange_bool_acq (&__fork_handlers,
  67. runp->next, runp)
  68. != 0)
  69. {
  70. runp = __fork_handlers;
  71. goto again;
  72. }
  73. }
  74. else
  75. lastp->next = runp->next;
  76. /* We cannot overwrite the ->next element now. Put the deleted
  77. entries in a separate list. */
  78. struct deleted_handler *newp = alloca (sizeof (*newp));
  79. newp->handler = runp;
  80. newp->next = deleted;
  81. deleted = newp;
  82. }
  83. else
  84. lastp = runp;
  85. runp = runp->next;
  86. }
  87. while (runp != NULL);
  88. /* Release the lock. */
  89. lll_unlock (__fork_lock, LLL_PRIVATE);
  90. /* Walk the list of all entries which have to be deleted. */
  91. while (deleted != NULL)
  92. {
  93. /* We need to be informed by possible current users. */
  94. deleted->handler->need_signal = 1;
  95. /* Make sure this gets written out first. */
  96. atomic_write_barrier ();
  97. /* Decrement the reference counter. If it does not reach zero
  98. wait for the last user. */
  99. atomic_decrement (&deleted->handler->refcntr);
  100. unsigned int val;
  101. while ((val = deleted->handler->refcntr) != 0)
  102. lll_futex_wait (&deleted->handler->refcntr, val, LLL_PRIVATE);
  103. deleted = deleted->next;
  104. }
  105. }
  106. #else
  107. void
  108. __unregister_atfork (
  109. void *dso_handle)
  110. {
  111. /* Nothing to do. */
  112. }
  113. #endif