Blame lib/fd-hook.c

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