Blame gettext-tools/libgettextpo/fd-hook.c

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