4
0

close-hook.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Hook for making the close() function extensible.
  2. Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2009.
  4. This program is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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 General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "close-hook.h"
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #undef close
  20. /* Currently, this entire code is only needed for the handling of sockets
  21. on native Windows platforms. */
  22. #if WINDOWS_SOCKETS
  23. /* The first and last link in the doubly linked list.
  24. Initially the list is empty. */
  25. static struct close_hook anchor = { &anchor, &anchor, NULL };
  26. int
  27. execute_close_hooks (int fd, const struct close_hook *remaining_list)
  28. {
  29. if (remaining_list == &anchor)
  30. /* End of list reached. */
  31. return close (fd);
  32. else
  33. return remaining_list->private_fn (fd, remaining_list->private_next);
  34. }
  35. int
  36. execute_all_close_hooks (int fd)
  37. {
  38. return execute_close_hooks (fd, anchor.private_next);
  39. }
  40. void
  41. register_close_hook (close_hook_fn hook, struct close_hook *link)
  42. {
  43. if (link->private_next == NULL && link->private_prev == NULL)
  44. {
  45. /* Add the link to the doubly linked list. */
  46. link->private_next = anchor.private_next;
  47. link->private_prev = &anchor;
  48. link->private_fn = hook;
  49. anchor.private_next->private_prev = link;
  50. anchor.private_next = link;
  51. }
  52. else
  53. {
  54. /* The link is already in use. */
  55. if (link->private_fn != hook)
  56. abort ();
  57. }
  58. }
  59. void
  60. unregister_close_hook (struct close_hook *link)
  61. {
  62. struct close_hook *next = link->private_next;
  63. struct close_hook *prev = link->private_prev;
  64. if (next != NULL && prev != NULL)
  65. {
  66. /* The link is in use. Remove it from the doubly linked list. */
  67. prev->private_next = next;
  68. next->private_prev = prev;
  69. /* Clear the link, to mark it unused. */
  70. link->private_next = NULL;
  71. link->private_prev = NULL;
  72. link->private_fn = NULL;
  73. }
  74. }
  75. #endif