Import upstream version 1.27
[debian/tar] / gnu / fd-hook.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Hook for making making file descriptor functions close(), ioctl() extensible.
4    Copyright (C) 2009-2013 Free Software Foundation, Inc.
5    Written by Bruno Haible <bruno@clisp.org>, 2009.
6
7    This program is free software: you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published
9    by the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include "fd-hook.h"
24
25 #include <stdlib.h>
26
27 /* Currently, this entire code is only needed for the handling of sockets
28    on native Windows platforms.  */
29 #if WINDOWS_SOCKETS
30
31 /* The first and last link in the doubly linked list.
32    Initially the list is empty.  */
33 static struct fd_hook anchor = { &anchor, &anchor, NULL, NULL };
34
35 int
36 execute_close_hooks (const struct fd_hook *remaining_list, gl_close_fn primary,
37                      int fd)
38 {
39   if (remaining_list == &anchor)
40     /* End of list reached.  */
41     return primary (fd);
42   else
43     return remaining_list->private_close_fn (remaining_list->private_next,
44                                              primary, fd);
45 }
46
47 int
48 execute_all_close_hooks (gl_close_fn primary, int fd)
49 {
50   return execute_close_hooks (anchor.private_next, primary, fd);
51 }
52
53 int
54 execute_ioctl_hooks (const struct fd_hook *remaining_list, gl_ioctl_fn primary,
55                      int fd, int request, void *arg)
56 {
57   if (remaining_list == &anchor)
58     /* End of list reached.  */
59     return primary (fd, request, arg);
60   else
61     return remaining_list->private_ioctl_fn (remaining_list->private_next,
62                                              primary, fd, request, arg);
63 }
64
65 int
66 execute_all_ioctl_hooks (gl_ioctl_fn primary,
67                          int fd, int request, void *arg)
68 {
69   return execute_ioctl_hooks (anchor.private_next, primary, fd, request, arg);
70 }
71
72 void
73 register_fd_hook (close_hook_fn close_hook, ioctl_hook_fn ioctl_hook, struct fd_hook *link)
74 {
75   if (close_hook == NULL)
76     close_hook = execute_close_hooks;
77   if (ioctl_hook == NULL)
78     ioctl_hook = execute_ioctl_hooks;
79
80   if (link->private_next == NULL && link->private_prev == NULL)
81     {
82       /* Add the link to the doubly linked list.  */
83       link->private_next = anchor.private_next;
84       link->private_prev = &anchor;
85       link->private_close_fn = close_hook;
86       link->private_ioctl_fn = ioctl_hook;
87       anchor.private_next->private_prev = link;
88       anchor.private_next = link;
89     }
90   else
91     {
92       /* The link is already in use.  */
93       if (link->private_close_fn != close_hook
94           || link->private_ioctl_fn != ioctl_hook)
95         abort ();
96     }
97 }
98
99 void
100 unregister_fd_hook (struct fd_hook *link)
101 {
102   struct fd_hook *next = link->private_next;
103   struct fd_hook *prev = link->private_prev;
104
105   if (next != NULL && prev != NULL)
106     {
107       /* The link is in use.  Remove it from the doubly linked list.  */
108       prev->private_next = next;
109       next->private_prev = prev;
110       /* Clear the link, to mark it unused.  */
111       link->private_next = NULL;
112       link->private_prev = NULL;
113       link->private_close_fn = NULL;
114       link->private_ioctl_fn = NULL;
115     }
116 }
117
118 #endif