Blame gl/fd-hook.c

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