Blame test/test-pipe-close-stdout-read-stdin.c

Packit b5b901
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#ifndef _WIN32
Packit b5b901
Packit b5b901
#include <stdlib.h>
Packit b5b901
#include <unistd.h>
Packit b5b901
#include <sys/wait.h>
Packit b5b901
#include <sys/types.h>
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "task.h"
Packit b5b901
Packit b5b901
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t* buf)
Packit b5b901
{
Packit b5b901
  static char buffer[1024];
Packit b5b901
Packit b5b901
  buf->base = buffer;
Packit b5b901
  buf->len = sizeof(buffer);
Packit b5b901
}
Packit b5b901
Packit b5b901
void read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t* buf)
Packit b5b901
{
Packit b5b901
  if (nread < 0) {
Packit b5b901
    uv_close((uv_handle_t*)stream, NULL);
Packit b5b901
    return;
Packit b5b901
  }
Packit b5b901
}
Packit b5b901
Packit b5b901
/*
Packit b5b901
 * This test is a reproduction of joyent/libuv#1419 .
Packit b5b901
 */
Packit b5b901
TEST_IMPL(pipe_close_stdout_read_stdin) {
Packit b5b901
  int r = -1;
Packit b5b901
  int pid;
Packit b5b901
  int fd[2];
Packit b5b901
  int status;
Packit b5b901
  char buf;
Packit b5b901
  uv_pipe_t stdin_pipe;
Packit b5b901
Packit b5b901
  r = pipe(fd);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
Packit b5b901
  if ((pid = fork()) == 0) {
Packit b5b901
    /*
Packit b5b901
     * Make the read side of the pipe our stdin.
Packit b5b901
     * The write side will be closed by the parent process.
Packit b5b901
    */
Packit b5b901
    close(fd[1]);
Packit b5b901
    /* block until write end of pipe is closed */
Packit b5b901
    r = read(fd[0], &buf, 1);
Packit b5b901
    ASSERT(-1 <= r && r <= 1);
Packit b5b901
    close(0);
Packit b5b901
    r = dup(fd[0]);
Packit b5b901
    ASSERT(r != -1);
Packit b5b901
Packit b5b901
    /* Create a stream that reads from the pipe. */
Packit b5b901
    r = uv_pipe_init(uv_default_loop(), (uv_pipe_t *)&stdin_pipe, 0);
Packit b5b901
    ASSERT(r == 0);
Packit b5b901
Packit b5b901
    r = uv_pipe_open((uv_pipe_t *)&stdin_pipe, 0);
Packit b5b901
    ASSERT(r == 0);
Packit b5b901
Packit b5b901
    r = uv_read_start((uv_stream_t *)&stdin_pipe, alloc_buffer, read_stdin);
Packit b5b901
    ASSERT(r == 0);
Packit b5b901
Packit b5b901
    /*
Packit b5b901
     * Because the other end of the pipe was closed, there should
Packit b5b901
     * be no event left to process after one run of the event loop.
Packit b5b901
     * Otherwise, it means that events were not processed correctly.
Packit b5b901
     */
Packit b5b901
    ASSERT(uv_run(uv_default_loop(), UV_RUN_NOWAIT) == 0);
Packit b5b901
  } else {
Packit b5b901
    /*
Packit b5b901
     * Close both ends of the pipe so that the child
Packit b5b901
     * get a POLLHUP event when it tries to read from
Packit b5b901
     * the other end.
Packit b5b901
     */
Packit b5b901
     close(fd[1]);
Packit b5b901
     close(fd[0]);
Packit b5b901
Packit b5b901
    waitpid(pid, &status, 0);
Packit b5b901
    ASSERT(WIFEXITED(status) && WEXITSTATUS(status) == 0);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  MAKE_VALGRIND_HAPPY();
Packit b5b901
  return 0;
Packit b5b901
}
Packit b5b901
Packit Service e08953
#else
Packit Service e08953
Packit Service e08953
typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
Packit Service e08953
Packit b5b901
#endif /* ifndef _WIN32 */