Blame src/spawn.c

Packit d7e8d0
/* spawn.c - Run an arbitrary command with callbacks.
Packit Service 30b792
 * Copyright (C) 2014 Code GmbH
Packit Service 30b792
 *
Packit Service 30b792
 * This file is part of GPGME.
Packit Service 30b792
 *
Packit Service 30b792
 * GPGME is free software; you can redistribute it and/or modify it
Packit Service 30b792
 * under the terms of the GNU Lesser General Public License as
Packit Service 30b792
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 30b792
 * the License, or (at your option) any later version.
Packit Service 30b792
 *
Packit Service 30b792
 * GPGME is distributed in the hope that it will be useful, but
Packit Service 30b792
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 30b792
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 30b792
 * Lesser General Public License for more details.
Packit Service 30b792
 *
Packit Service 30b792
 * You should have received a copy of the GNU Lesser General Public
Packit Service 30b792
 * License along with this program; if not, see <https://gnu.org/licenses/>.
Packit Service 30b792
 * SPDX-License-Identifier: LGPL-2.1-or-later
Packit Service 30b792
 */
Packit d7e8d0
Packit d7e8d0
#if HAVE_CONFIG_H
Packit d7e8d0
#include <config.h>
Packit d7e8d0
#endif
Packit d7e8d0
#include <stdlib.h>
Packit d7e8d0
Packit d7e8d0
#include "gpgme.h"
Packit d7e8d0
#include "debug.h"
Packit d7e8d0
#include "context.h"
Packit d7e8d0
#include "util.h"
Packit d7e8d0
#include "ops.h"
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
static gpgme_error_t
Packit d7e8d0
spawn_start (gpgme_ctx_t ctx, int synchronous,
Packit d7e8d0
             const char *file, const char *argv[],
Packit d7e8d0
             gpgme_data_t datain,
Packit d7e8d0
             gpgme_data_t dataout, gpgme_data_t dataerr,
Packit d7e8d0
             unsigned int flags)
Packit d7e8d0
{
Packit d7e8d0
  gpgme_error_t err;
Packit d7e8d0
  const char *tmp_argv[2];
Packit d7e8d0
Packit d7e8d0
  if (ctx->protocol != GPGME_PROTOCOL_SPAWN)
Packit d7e8d0
    return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
Packit d7e8d0
Packit d7e8d0
  err = _gpgme_op_reset (ctx, synchronous);
Packit d7e8d0
  if (err)
Packit d7e8d0
    return err;
Packit d7e8d0
Packit d7e8d0
  if (!argv)
Packit d7e8d0
    {
Packit d7e8d0
      tmp_argv[0] = _gpgme_get_basename (file);
Packit d7e8d0
      tmp_argv[1] = NULL;
Packit d7e8d0
      argv = tmp_argv;
Packit d7e8d0
    }
Packit d7e8d0
Packit d7e8d0
  return _gpgme_engine_op_spawn (ctx->engine, file, argv,
Packit d7e8d0
                                 datain, dataout, dataerr, flags);
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
/* Run the command FILE with the arguments in ARGV.  Connect stdin to
Packit d7e8d0
   DATAIN, stdout to DATAOUT, and STDERR to DATAERR.  If one the data
Packit d7e8d0
   streams is NULL, connect to /dev/null instead.  */
Packit d7e8d0
gpgme_error_t
Packit d7e8d0
gpgme_op_spawn_start (gpgme_ctx_t ctx, const char *file, const char *argv[],
Packit d7e8d0
                      gpgme_data_t datain,
Packit d7e8d0
                      gpgme_data_t dataout, gpgme_data_t dataerr,
Packit d7e8d0
                      unsigned int flags)
Packit d7e8d0
{
Packit d7e8d0
  gpgme_error_t err;
Packit d7e8d0
Packit Service 30b792
  TRACE_BEG  (DEBUG_CTX, "gpgme_op_spawn_start", ctx, "file=(%s) flaggs=%x",
Packit d7e8d0
              file, flags);
Packit d7e8d0
Packit d7e8d0
  if (!ctx)
Packit d7e8d0
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit d7e8d0
Packit d7e8d0
  err = spawn_start (ctx, 0, file, argv, datain, dataout, dataerr, flags);
Packit d7e8d0
  return err;
Packit d7e8d0
}
Packit d7e8d0
Packit d7e8d0
Packit d7e8d0
/* Run the command FILE with the arguments in ARGV.  Connect stdin to
Packit d7e8d0
   DATAIN, stdout to DATAOUT, and STDERR to DATAERR.  If one the data
Packit d7e8d0
   streams is NULL, connect to /dev/null instead.  Synchronous
Packit d7e8d0
   variant. */
Packit d7e8d0
gpgme_error_t
Packit d7e8d0
gpgme_op_spawn (gpgme_ctx_t ctx, const char *file, const char *argv[],
Packit d7e8d0
	        gpgme_data_t datain,
Packit d7e8d0
                gpgme_data_t dataout, gpgme_data_t dataerr,
Packit d7e8d0
                unsigned int flags)
Packit d7e8d0
{
Packit d7e8d0
  gpgme_error_t err;
Packit d7e8d0
Packit Service 30b792
  TRACE_BEG  (DEBUG_CTX, "gpgme_op_spawn", ctx, "file=(%s) flags=%x",
Packit d7e8d0
              file, flags);
Packit d7e8d0
  if (!ctx)
Packit d7e8d0
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit d7e8d0
Packit d7e8d0
  err = spawn_start (ctx, 1, file, argv, datain, dataout, dataerr, flags);
Packit d7e8d0
Packit d7e8d0
  if (!err)
Packit d7e8d0
    err = _gpgme_wait_one (ctx);
Packit d7e8d0
  return TRACE_ERR (err);
Packit d7e8d0
}