Blame src/wait.c

Packit Service 672cf4
/* wait.c
Packit Service 6c01f9
   Copyright (C) 2000 Werner Koch (dd9jn)
Packit Service 6c01f9
   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 g10 Code GmbH
Packit Service 6c01f9
Packit Service 6c01f9
   This file is part of GPGME.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is free software; you can redistribute it and/or modify it
Packit Service 6c01f9
   under the terms of the GNU Lesser General Public License as
Packit Service 6c01f9
   published by the Free Software Foundation; either version 2.1 of
Packit Service 6c01f9
   the License, or (at your option) any later version.
Packit Service 6c01f9
Packit Service 6c01f9
   GPGME is distributed in the hope that it will be useful, but
Packit Service 6c01f9
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6c01f9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6c01f9
   Lesser General Public License for more details.
Packit Service 6c01f9
Packit Service 6c01f9
   You should have received a copy of the GNU Lesser General Public
Packit Service 6c01f9
   License along with this program; if not, write to the Free Software
Packit Service 6c01f9
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
Packit Service 6c01f9
   02111-1307, USA.  */
Packit Service 672cf4
Packit Service 672cf4
#if HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
#include <assert.h>
Packit Service 672cf4
#include <errno.h>
Packit Service 672cf4
#ifdef HAVE_SYS_TYPES_H
Packit Service 672cf4
# include <sys/types.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include "util.h"
Packit Service 672cf4
#include "context.h"
Packit Service 672cf4
#include "ops.h"
Packit Service 672cf4
#include "wait.h"
Packit Service 672cf4
#include "sema.h"
Packit Service 672cf4
#include "priv-io.h"
Packit Service 672cf4
#include "engine.h"
Packit Service 672cf4
#include "debug.h"
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
void
Packit Service 672cf4
_gpgme_fd_table_init (fd_table_t fdt)
Packit Service 672cf4
{
Packit Service 672cf4
  fdt->fds = NULL;
Packit Service 672cf4
  fdt->size = 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
_gpgme_fd_table_deinit (fd_table_t fdt)
Packit Service 672cf4
{
Packit Service 672cf4
  if (fdt->fds)
Packit Service 672cf4
    free (fdt->fds);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* XXX We should keep a marker and roll over for speed.  */
Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
fd_table_put (fd_table_t fdt, int fd, int dir, void *opaque, int *idx)
Packit Service 672cf4
{
Packit Service 672cf4
  unsigned int i, j;
Packit Service 672cf4
  struct io_select_fd_s *new_fds;
Packit Service 672cf4
Packit Service 672cf4
  for (i = 0; i < fdt->size; i++)
Packit Service 672cf4
    {
Packit Service 672cf4
      if (fdt->fds[i].fd == -1)
Packit Service 672cf4
	break;
Packit Service 672cf4
    }
Packit Service 672cf4
  if (i == fdt->size)
Packit Service 672cf4
    {
Packit Service 672cf4
#define FDT_ALLOCSIZE 10
Packit Service 672cf4
      new_fds = realloc (fdt->fds, (fdt->size + FDT_ALLOCSIZE)
Packit Service 672cf4
			 * sizeof (*new_fds));
Packit Service 672cf4
      if (!new_fds)
Packit Service 672cf4
	return gpg_error_from_syserror ();
Packit Service 672cf4
Packit Service 672cf4
      fdt->fds = new_fds;
Packit Service 672cf4
      fdt->size += FDT_ALLOCSIZE;
Packit Service 672cf4
      for (j = 0; j < FDT_ALLOCSIZE; j++)
Packit Service 672cf4
	fdt->fds[i + j].fd = -1;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  fdt->fds[i].fd = fd;
Packit Service 672cf4
  fdt->fds[i].for_read = (dir == 1);
Packit Service 672cf4
  fdt->fds[i].for_write = (dir == 0);
Packit Service 672cf4
  fdt->fds[i].signaled = 0;
Packit Service 672cf4
  fdt->fds[i].opaque = opaque;
Packit Service 672cf4
  *idx = i;
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
/* Register the file descriptor FD with the handler FNC (which gets
Packit Service 672cf4
   FNC_DATA as its first argument) for the direction DIR.  DATA should
Packit Service 672cf4
   be the context for which the fd is added.  R_TAG will hold the tag
Packit Service 672cf4
   that can be used to remove the fd.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
_gpgme_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc,
Packit Service 672cf4
		  void *fnc_data, void **r_tag)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  gpgme_ctx_t ctx = (gpgme_ctx_t) data;
Packit Service 672cf4
  fd_table_t fdt;
Packit Service 672cf4
  struct wait_item_s *item;
Packit Service 672cf4
  struct tag *tag;
Packit Service 672cf4
Packit Service 672cf4
  assert (fnc);
Packit Service 672cf4
  assert (ctx);
Packit Service 672cf4
Packit Service 672cf4
  fdt = &ctx->fdt;
Packit Service 672cf4
  assert (fdt);
Packit Service 672cf4
Packit Service 672cf4
  tag = malloc (sizeof *tag);
Packit Service 672cf4
  if (!tag)
Packit Service 672cf4
    return gpg_error_from_syserror ();
Packit Service 672cf4
  tag->ctx = ctx;
Packit Service 672cf4
Packit Service 672cf4
  /* Allocate a structure to hold information about the handler.  */
Packit Service 672cf4
  item = calloc (1, sizeof *item);
Packit Service 672cf4
  if (!item)
Packit Service 672cf4
    {
Packit Service 672cf4
      free (tag);
Packit Service 672cf4
      return gpg_error_from_syserror ();
Packit Service 672cf4
    }
Packit Service 672cf4
  item->ctx = ctx;
Packit Service 672cf4
  item->dir = dir;
Packit Service 672cf4
  item->handler = fnc;
Packit Service 672cf4
  item->handler_value = fnc_data;
Packit Service 672cf4
Packit Service 672cf4
  err = fd_table_put (fdt, fd, dir, item, &tag->idx);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    {
Packit Service 672cf4
      free (tag);
Packit Service 672cf4
      free (item);
Packit Service 672cf4
      return err;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 6c01f9
  TRACE3 (DEBUG_CTX, "_gpgme_add_io_cb", ctx,
Packit Service 6c01f9
	  "fd %d, dir=%d -> tag=%p", fd, dir, tag);
Packit Service 672cf4
Packit Service 672cf4
  *r_tag = tag;
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
_gpgme_remove_io_cb (void *data)
Packit Service 672cf4
{
Packit Service 672cf4
  struct tag *tag = data;
Packit Service 672cf4
  gpgme_ctx_t ctx;
Packit Service 672cf4
  fd_table_t fdt;
Packit Service 672cf4
  int idx;
Packit Service 672cf4
Packit Service 672cf4
  assert (tag);
Packit Service 672cf4
  ctx = tag->ctx;
Packit Service 672cf4
  assert (ctx);
Packit Service 672cf4
  fdt = &ctx->fdt;
Packit Service 672cf4
  assert (fdt);
Packit Service 672cf4
  idx = tag->idx;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE2 (DEBUG_CTX, "_gpgme_remove_io_cb", data,
Packit Service 672cf4
	  "setting fd 0x%x (item=%p) done", fdt->fds[idx].fd,
Packit Service 672cf4
	  fdt->fds[idx].opaque);
Packit Service 672cf4
Packit Service 672cf4
  free (fdt->fds[idx].opaque);
Packit Service 672cf4
  free (tag);
Packit Service 672cf4
Packit Service 672cf4
  /* Free the table entry.  */
Packit Service 672cf4
  fdt->fds[idx].fd = -1;
Packit Service 672cf4
  fdt->fds[idx].for_read = 0;
Packit Service 672cf4
  fdt->fds[idx].for_write = 0;
Packit Service 672cf4
  fdt->fds[idx].opaque = NULL;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
/* This is slightly embarrassing.  The problem is that running an I/O
Packit Service 672cf4
   callback _may_ influence the status of other file descriptors.  Our
Packit Service 672cf4
   own event loops could compensate for that, but the external event
Packit Service 672cf4
   loops cannot.  FIXME: We may still want to optimize this a bit when
Packit Service 672cf4
   we are called from our own event loops.  So if CHECKED is 1, the
Packit Service 6c01f9
   check is skipped.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
_gpgme_run_io_cb (struct io_select_fd_s *an_fds, int checked,
Packit Service 672cf4
		  gpgme_error_t *op_err)
Packit Service 672cf4
{
Packit Service 672cf4
  struct wait_item_s *item;
Packit Service 672cf4
  struct io_cb_data iocb_data;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 672cf4
  item = (struct wait_item_s *) an_fds->opaque;
Packit Service 672cf4
  assert (item);
Packit Service 672cf4
Packit Service 672cf4
  if (!checked)
Packit Service 672cf4
    {
Packit Service 672cf4
      int nr;
Packit Service 672cf4
      struct io_select_fd_s fds;
Packit Service 672cf4
Packit Service 6c01f9
      TRACE0 (DEBUG_CTX, "_gpgme_run_io_cb", item, "need to check");
Packit Service 672cf4
      fds = *an_fds;
Packit Service 672cf4
      fds.signaled = 0;
Packit Service 672cf4
      /* Just give it a quick poll.  */
Packit Service 672cf4
      nr = _gpgme_io_select (&fds, 1, 1);
Packit Service 672cf4
      assert (nr <= 1);
Packit Service 672cf4
      if (nr < 0)
Packit Service 6c01f9
	return errno;
Packit Service 672cf4
      else if (nr == 0)
Packit Service 6c01f9
	/* The status changed in the meantime, there is nothing left
Packit Service 6c01f9
	   to do.  */
Packit Service 6c01f9
	return 0;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 6c01f9
  TRACE2 (DEBUG_CTX, "_gpgme_run_io_cb", item, "handler (%p, %d)",
Packit Service 672cf4
          item->handler_value, an_fds->fd);
Packit Service 672cf4
Packit Service 672cf4
  iocb_data.handler_value = item->handler_value;
Packit Service 672cf4
  iocb_data.op_err = 0;
Packit Service 672cf4
  err = item->handler (&iocb_data, an_fds->fd);
Packit Service 672cf4
Packit Service 672cf4
  *op_err = iocb_data.op_err;
Packit Service 672cf4
  return err;
Packit Service 672cf4
}