unregister-atfork.c 3.6 KB

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