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

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