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