fd-hook.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Hook for making file descriptor functions close(), ioctl() extensible.
  2. Copyright (C) 2009-2024 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef FD_HOOK_H
  14. #define FD_HOOK_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /* Currently, this entire code is only needed for the handling of sockets
  19. on native Windows platforms. */
  20. #if WINDOWS_SOCKETS
  21. /* Type of function that closes FD. */
  22. typedef int (*gl_close_fn) (int fd);
  23. /* Type of function that applies a control request to FD. */
  24. typedef int (*gl_ioctl_fn) (int fd, int request, void *arg);
  25. /* An element of the list of file descriptor hooks.
  26. In CLOS (Common Lisp Object System) speak, it consists of an "around"
  27. method for the close() function and an "around" method for the ioctl()
  28. function.
  29. The fields of this structure are considered private. */
  30. struct fd_hook
  31. {
  32. /* Doubly linked list. */
  33. struct fd_hook *private_next;
  34. struct fd_hook *private_prev;
  35. /* Function that treats the types of FD that it knows about and calls
  36. execute_close_hooks (REMAINING_LIST, PRIMARY, FD) as a fallback. */
  37. int (*private_close_fn) (const struct fd_hook *remaining_list,
  38. gl_close_fn primary,
  39. int fd);
  40. /* Function that treats the types of FD that it knows about and calls
  41. execute_ioctl_hooks (REMAINING_LIST, PRIMARY, FD, REQUEST, ARG) as a
  42. fallback. */
  43. int (*private_ioctl_fn) (const struct fd_hook *remaining_list,
  44. gl_ioctl_fn primary,
  45. int fd, int request, void *arg);
  46. };
  47. /* This type of function closes FD, applying special knowledge for the FD
  48. types it knows about, and calls
  49. execute_close_hooks (REMAINING_LIST, PRIMARY, FD)
  50. for the other FD types.
  51. In CLOS speak, REMAINING_LIST is the remaining list of "around" methods,
  52. and PRIMARY is the "primary" method for close(). */
  53. typedef int (*close_hook_fn) (const struct fd_hook *remaining_list,
  54. gl_close_fn primary,
  55. int fd);
  56. /* Execute the close hooks in REMAINING_LIST, with PRIMARY as "primary" method.
  57. Return 0 or -1, like close() would do. */
  58. extern int execute_close_hooks (const struct fd_hook *remaining_list,
  59. gl_close_fn primary,
  60. int fd);
  61. /* Execute all close hooks, with PRIMARY as "primary" method.
  62. Return 0 or -1, like close() would do. */
  63. extern int execute_all_close_hooks (gl_close_fn primary, int fd);
  64. /* This type of function applies a control request to FD, applying special
  65. knowledge for the FD types it knows about, and calls
  66. execute_ioctl_hooks (REMAINING_LIST, PRIMARY, FD, REQUEST, ARG)
  67. for the other FD types.
  68. In CLOS speak, REMAINING_LIST is the remaining list of "around" methods,
  69. and PRIMARY is the "primary" method for ioctl(). */
  70. typedef int (*ioctl_hook_fn) (const struct fd_hook *remaining_list,
  71. gl_ioctl_fn primary,
  72. int fd, int request, void *arg);
  73. /* Execute the ioctl hooks in REMAINING_LIST, with PRIMARY as "primary" method.
  74. Return 0 or -1, like ioctl() would do. */
  75. extern int execute_ioctl_hooks (const struct fd_hook *remaining_list,
  76. gl_ioctl_fn primary,
  77. int fd, int request, void *arg);
  78. /* Execute all ioctl hooks, with PRIMARY as "primary" method.
  79. Return 0 or -1, like ioctl() would do. */
  80. extern int execute_all_ioctl_hooks (gl_ioctl_fn primary,
  81. int fd, int request, void *arg);
  82. /* Add a function pair to the list of file descriptor hooks.
  83. CLOSE_HOOK and IOCTL_HOOK may be NULL, indicating no change.
  84. The LINK variable points to a piece of memory which is guaranteed to be
  85. accessible until the corresponding call to unregister_fd_hook. */
  86. extern void register_fd_hook (close_hook_fn close_hook, ioctl_hook_fn ioctl_hook,
  87. struct fd_hook *link);
  88. /* Removes a hook from the list of file descriptor hooks. */
  89. extern void unregister_fd_hook (struct fd_hook *link);
  90. #endif
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif /* FD_HOOK_H */