Blame gio/tests/test-pipe-unix.c

Packit ae235b
/* Unix pipe-to-self. This is a utility module for tests, not a test.
Packit ae235b
 *
Packit ae235b
 * Copyright © 2008-2010 Red Hat, Inc.
Packit ae235b
 * Copyright © 2011 Nokia Corporation
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <errno.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
Packit ae235b
#include "test-io-stream.h"
Packit ae235b
#include "test-pipe-unix.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#   include <gio/gunixinputstream.h>
Packit ae235b
#   include <gio/gunixoutputstream.h>
Packit ae235b
#else
Packit ae235b
#   error This module only exists on Unix
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * test_pipe:
Packit ae235b
 * @is: (out) (optional): used to return a #GInputStream
Packit ae235b
 * @os: (out) (optional): used to return a #GOutputStream
Packit ae235b
 * @error: used to raise an error
Packit ae235b
 *
Packit ae235b
 * Return a "pipe to self" connecting @is to @os. This can be used
Packit ae235b
 * as a unidirectional pipe to or from a child process, for instance.
Packit ae235b
 *
Packit ae235b
 * See test_bidi_pipe() if you want to emulate a bidirectional pipe
Packit ae235b
 * via a pair of unidirectional pipes.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
test_pipe (GInputStream  **is,
Packit ae235b
           GOutputStream **os,
Packit ae235b
           GError        **error)
Packit ae235b
{
Packit ae235b
  int pipefd[2];
Packit ae235b
  int ret;
Packit ae235b
Packit ae235b
  ret = pipe (pipefd);
Packit ae235b
Packit ae235b
  if (ret != 0)
Packit ae235b
    {
Packit ae235b
      int e = errno;
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (e),
Packit ae235b
                   "%s", g_strerror (e));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (is != NULL)
Packit ae235b
    *is = g_unix_input_stream_new (pipefd[0], TRUE);
Packit ae235b
  else
Packit ae235b
    close (pipefd[0]);
Packit ae235b
Packit ae235b
  if (os != NULL)
Packit ae235b
    *os = g_unix_output_stream_new (pipefd[1], TRUE);
Packit ae235b
  else
Packit ae235b
    close (pipefd[1]);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * test_bidi_pipe:
Packit ae235b
 * @left: (out) (optional): used to return one #GIOStream
Packit ae235b
 * @right: (out) (optional): used to return the other #GIOStream
Packit ae235b
 * @error: used to raise an error
Packit ae235b
 *
Packit ae235b
 * Return two #GIOStreams connected to each other with pipes.
Packit ae235b
 * The "left" input stream is connected by a unidirectional pipe
Packit ae235b
 * to the "right" output stream, and vice versa. This can be used
Packit ae235b
 * as a bidirectional pipe to a child process, for instance.
Packit ae235b
 *
Packit ae235b
 * See test_pipe() if you only need a one-way pipe.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
test_bidi_pipe (GIOStream **left,
Packit ae235b
                GIOStream **right,
Packit ae235b
                GError    **error)
Packit ae235b
{
Packit ae235b
  GInputStream *left_in = NULL;
Packit ae235b
  GOutputStream *left_out = NULL;
Packit ae235b
  GInputStream *right_in = NULL;
Packit ae235b
  GOutputStream *right_out = NULL;
Packit ae235b
  gboolean ret = FALSE;
Packit ae235b
Packit ae235b
  if (!test_pipe (&left_in, &right_out, error))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  if (!test_pipe (&right_in, &left_out, error))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  if (left != NULL)
Packit ae235b
    *left = test_io_stream_new (left_in, left_out);
Packit ae235b
Packit ae235b
  if (right != NULL)
Packit ae235b
    *right = test_io_stream_new (right_in, right_out);
Packit ae235b
Packit ae235b
  ret = TRUE;
Packit ae235b
Packit ae235b
out:
Packit ae235b
  g_clear_object (&left_in);
Packit ae235b
  g_clear_object (&left_out);
Packit ae235b
  g_clear_object (&right_in);
Packit ae235b
  g_clear_object (&right_out);
Packit ae235b
  return ret;
Packit ae235b
}