Blame src/wait-user.c

Packit Service 672cf4
/* wait-user.c
Packit Service 6c01f9
   Copyright (C) 2000 Werner Koch (dd9jn)
Packit Service 6c01f9
   Copyright (C) 2001, 2002, 2003, 2004, 2005 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 <assert.h>
Packit Service 672cf4
Packit Service 672cf4
#include "gpgme.h"
Packit Service 672cf4
#include "context.h"
Packit Service 672cf4
#include "priv-io.h"
Packit Service 672cf4
#include "wait.h"
Packit Service 672cf4
#include "ops.h"
Packit Service 672cf4
#include "debug.h"
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
/* The user event loops are used for all asynchronous operations for
Packit Service 672cf4
   which a user callback is defined.  */
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
/* Internal I/O Callbacks.  */
Packit Service 672cf4
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
_gpgme_user_io_cb_handler (void *data, int fd)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err = 0;
Packit Service 672cf4
  gpgme_error_t op_err = 0;
Packit Service 672cf4
  struct tag *tag = (struct tag *) data;
Packit Service 672cf4
  gpgme_ctx_t ctx;
Packit Service 672cf4
Packit Service 672cf4
  (void)fd;
Packit Service 672cf4
Packit Service 672cf4
  assert (data);
Packit Service 672cf4
  ctx = tag->ctx;
Packit Service 672cf4
  assert (ctx);
Packit Service 672cf4
Packit Service 672cf4
  LOCK (ctx->lock);
Packit Service 672cf4
  if (ctx->canceled)
Packit Service 672cf4
    err = gpg_error (GPG_ERR_CANCELED);
Packit Service 672cf4
  UNLOCK (ctx->lock);
Packit Service 672cf4
Packit Service 672cf4
  if (! err)
Packit Service 672cf4
    err = _gpgme_run_io_cb (&ctx->fdt.fds[tag->idx], 0, &op_err);
Packit Service 672cf4
  if (err || op_err)
Packit Service 672cf4
    _gpgme_cancel_with_err (ctx, err, op_err);
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      unsigned int i;
Packit Service 672cf4
Packit Service 672cf4
      for (i = 0; i < ctx->fdt.size; i++)
Packit Service 672cf4
	if (ctx->fdt.fds[i].fd != -1)
Packit Service 672cf4
	  break;
Packit Service 672cf4
Packit Service 672cf4
      if (i == ctx->fdt.size)
Packit Service 672cf4
	{
Packit Service 672cf4
	  struct gpgme_io_event_done_data done_data;
Packit Service 672cf4
Packit Service 672cf4
	  done_data.err = 0;
Packit Service 672cf4
	  done_data.op_err = 0;
Packit Service 672cf4
	  _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &done_data);
Packit Service 672cf4
	}
Packit Service 672cf4
    }
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_wait_user_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_ctx_t ctx = (gpgme_ctx_t) data;
Packit Service 672cf4
  struct tag *tag;
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 672cf4
  assert (ctx);
Packit Service 672cf4
  err = _gpgme_add_io_cb (data, fd, dir, fnc, fnc_data, r_tag);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
  tag = *r_tag;
Packit Service 672cf4
  assert (tag);
Packit Service 672cf4
  err = (*ctx->io_cbs.add) (ctx->io_cbs.add_priv, fd, dir,
Packit Service 672cf4
			    _gpgme_user_io_cb_handler, *r_tag,
Packit Service 672cf4
			    &tag->user_tag);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    _gpgme_remove_io_cb (*r_tag);
Packit Service 672cf4
  return err;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
_gpgme_wait_user_remove_io_cb (void *data)
Packit Service 672cf4
{
Packit Service 672cf4
  struct tag *tag = (struct tag *) data;
Packit Service 672cf4
  gpgme_ctx_t ctx;
Packit Service 672cf4
Packit Service 672cf4
  assert (tag);
Packit Service 672cf4
  ctx = tag->ctx;
Packit Service 672cf4
Packit Service 672cf4
  (*ctx->io_cbs.remove) (tag->user_tag);
Packit Service 672cf4
  _gpgme_remove_io_cb (data);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
void
Packit Service 672cf4
_gpgme_wait_user_event_cb (void *data, gpgme_event_io_t type, void *type_data)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_ctx_t ctx = data;
Packit Service 672cf4
Packit Service 672cf4
  if (ctx->io_cbs.event)
Packit Service 672cf4
    (*ctx->io_cbs.event) (ctx->io_cbs.event_priv, type, type_data);
Packit Service 672cf4
}