Blame src/spawn.c

Packit Service 672cf4
/* spawn.c - Run an arbitrary command with callbacks.
Packit Service 6c01f9
   Copyright (C) 2014 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
Packit Service 672cf4
#include "gpgme.h"
Packit Service 672cf4
#include "debug.h"
Packit Service 672cf4
#include "context.h"
Packit Service 672cf4
#include "util.h"
Packit Service 672cf4
#include "ops.h"
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
spawn_start (gpgme_ctx_t ctx, int synchronous,
Packit Service 672cf4
             const char *file, const char *argv[],
Packit Service 672cf4
             gpgme_data_t datain,
Packit Service 672cf4
             gpgme_data_t dataout, gpgme_data_t dataerr,
Packit Service 672cf4
             unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
  const char *tmp_argv[2];
Packit Service 672cf4
Packit Service 672cf4
  if (ctx->protocol != GPGME_PROTOCOL_SPAWN)
Packit Service 672cf4
    return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_reset (ctx, synchronous);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  if (!argv)
Packit Service 672cf4
    {
Packit Service 672cf4
      tmp_argv[0] = _gpgme_get_basename (file);
Packit Service 672cf4
      tmp_argv[1] = NULL;
Packit Service 672cf4
      argv = tmp_argv;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  return _gpgme_engine_op_spawn (ctx->engine, file, argv,
Packit Service 672cf4
                                 datain, dataout, dataerr, flags);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Run the command FILE with the arguments in ARGV.  Connect stdin to
Packit Service 672cf4
   DATAIN, stdout to DATAOUT, and STDERR to DATAERR.  If one the data
Packit Service 672cf4
   streams is NULL, connect to /dev/null instead.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_spawn_start (gpgme_ctx_t ctx, const char *file, const char *argv[],
Packit Service 672cf4
                      gpgme_data_t datain,
Packit Service 672cf4
                      gpgme_data_t dataout, gpgme_data_t dataerr,
Packit Service 672cf4
                      unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_spawn_start", ctx, "file=(%s) flaggs=%x",
Packit Service 672cf4
              file, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  err = spawn_start (ctx, 0, file, argv, datain, dataout, dataerr, flags);
Packit Service 672cf4
  return err;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Run the command FILE with the arguments in ARGV.  Connect stdin to
Packit Service 672cf4
   DATAIN, stdout to DATAOUT, and STDERR to DATAERR.  If one the data
Packit Service 672cf4
   streams is NULL, connect to /dev/null instead.  Synchronous
Packit Service 672cf4
   variant. */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_spawn (gpgme_ctx_t ctx, const char *file, const char *argv[],
Packit Service 672cf4
	        gpgme_data_t datain,
Packit Service 672cf4
                gpgme_data_t dataout, gpgme_data_t dataerr,
Packit Service 672cf4
                unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 6c01f9
  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_spawn", ctx, "file=(%s) flags=%x",
Packit Service 672cf4
              file, flags);
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  err = spawn_start (ctx, 1, file, argv, datain, dataout, dataerr, flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}