Blame src/gl/fd-hook.c

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