Blame lib/fd-hook.c

Packit 709fb3
/* Hook for making file descriptor functions close(), ioctl() extensible.
Packit 709fb3
   Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
   Written by Bruno Haible <bruno@clisp.org>, 2009.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify it
Packit 709fb3
   under the terms of the GNU General Public License as published
Packit 709fb3
   by the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 709fb3
   General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
/* Specification.  */
Packit 709fb3
#include "fd-hook.h"
Packit 709fb3
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
Packit 709fb3
/* Currently, this entire code is only needed for the handling of sockets
Packit 709fb3
   on native Windows platforms.  */
Packit 709fb3
#if WINDOWS_SOCKETS
Packit 709fb3
Packit 709fb3
/* The first and last link in the doubly linked list.
Packit 709fb3
   Initially the list is empty.  */
Packit 709fb3
static struct fd_hook anchor = { &anchor, &anchor, NULL, NULL };
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
execute_close_hooks (const struct fd_hook *remaining_list, gl_close_fn primary,
Packit 709fb3
                     int fd)
Packit 709fb3
{
Packit 709fb3
  if (remaining_list == &anchor)
Packit 709fb3
    /* End of list reached.  */
Packit 709fb3
    return primary (fd);
Packit 709fb3
  else
Packit 709fb3
    return remaining_list->private_close_fn (remaining_list->private_next,
Packit 709fb3
                                             primary, fd);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
execute_all_close_hooks (gl_close_fn primary, int fd)
Packit 709fb3
{
Packit 709fb3
  return execute_close_hooks (anchor.private_next, primary, fd);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
execute_ioctl_hooks (const struct fd_hook *remaining_list, gl_ioctl_fn primary,
Packit 709fb3
                     int fd, int request, void *arg)
Packit 709fb3
{
Packit 709fb3
  if (remaining_list == &anchor)
Packit 709fb3
    /* End of list reached.  */
Packit 709fb3
    return primary (fd, request, arg);
Packit 709fb3
  else
Packit 709fb3
    return remaining_list->private_ioctl_fn (remaining_list->private_next,
Packit 709fb3
                                             primary, fd, request, arg);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
int
Packit 709fb3
execute_all_ioctl_hooks (gl_ioctl_fn primary,
Packit 709fb3
                         int fd, int request, void *arg)
Packit 709fb3
{
Packit 709fb3
  return execute_ioctl_hooks (anchor.private_next, primary, fd, request, arg);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
void
Packit 709fb3
register_fd_hook (close_hook_fn close_hook, ioctl_hook_fn ioctl_hook, struct fd_hook *link)
Packit 709fb3
{
Packit 709fb3
  if (close_hook == NULL)
Packit 709fb3
    close_hook = execute_close_hooks;
Packit 709fb3
  if (ioctl_hook == NULL)
Packit 709fb3
    ioctl_hook = execute_ioctl_hooks;
Packit 709fb3
Packit 709fb3
  if (link->private_next == NULL && link->private_prev == NULL)
Packit 709fb3
    {
Packit 709fb3
      /* Add the link to the doubly linked list.  */
Packit 709fb3
      link->private_next = anchor.private_next;
Packit 709fb3
      link->private_prev = &anchor;
Packit 709fb3
      link->private_close_fn = close_hook;
Packit 709fb3
      link->private_ioctl_fn = ioctl_hook;
Packit 709fb3
      anchor.private_next->private_prev = link;
Packit 709fb3
      anchor.private_next = link;
Packit 709fb3
    }
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
      /* The link is already in use.  */
Packit 709fb3
      if (link->private_close_fn != close_hook
Packit 709fb3
          || link->private_ioctl_fn != ioctl_hook)
Packit 709fb3
        abort ();
Packit 709fb3
    }
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
void
Packit 709fb3
unregister_fd_hook (struct fd_hook *link)
Packit 709fb3
{
Packit 709fb3
  struct fd_hook *next = link->private_next;
Packit 709fb3
  struct fd_hook *prev = link->private_prev;
Packit 709fb3
Packit 709fb3
  if (next != NULL && prev != NULL)
Packit 709fb3
    {
Packit 709fb3
      /* The link is in use.  Remove it from the doubly linked list.  */
Packit 709fb3
      prev->private_next = next;
Packit 709fb3
      next->private_prev = prev;
Packit 709fb3
      /* Clear the link, to mark it unused.  */
Packit 709fb3
      link->private_next = NULL;
Packit 709fb3
      link->private_prev = NULL;
Packit 709fb3
      link->private_close_fn = NULL;
Packit 709fb3
      link->private_ioctl_fn = NULL;
Packit 709fb3
    }
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#endif