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