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