unregister-atfork.c 3.3 KB

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