Index: subversion/libsvn_repos/hooks.c =================================================================== --- subversion/libsvn_repos/hooks.c (revision 19130) +++ subversion/libsvn_repos/hooks.c (working copy) @@ -25,6 +25,9 @@ #ifdef AS400 #include #include +#include +#include +#include #endif #include "svn_error.h" @@ -171,11 +174,12 @@ return err; } #else /* Run hooks with spawn() on OS400. */ +#define AS400_BUFFER_SIZE 128 { const char *script_stderr_utf8 = ""; const char **native_args; - char buffer[20]; - int rc, fd_map[3], stderr_pipe[2], stdin_pipe[2], exitcode; + char stdin_buffer[AS400_BUFFER_SIZE]; + int fd_map[3], stderr_pipe[2], stdin_pipe[2], exitcode; svn_stringbuf_t *script_output = svn_stringbuf_create("", pool); pid_t child_pid, wait_rv; apr_size_t args_arr_size = 0, i; @@ -186,6 +190,11 @@ char *xmp_envp[2] = {"QIBM_USE_DESCRIPTOR_STDIO=Y", NULL}; const char *dev_null_ebcdic = SVN_NULL_DEVICE_NAME; #pragma convert(1208) + svn_boolean_t more_stderr = read_errstream; + svn_boolean_t more_stdin = stdin_handle ? TRUE : FALSE; + svn_boolean_t last_stdin_write_succeeded = TRUE; + apr_size_t bytes_last_read; + struct pollfd pfds[2]; /* Find number of elements in args array. */ while (args[args_arr_size] != NULL) @@ -222,6 +231,14 @@ cmd); } fd_map[0] = stdin_pipe[0]; + + /* Prevent blocking on write end of stdin pipe. */ + if (fcntl(stdin_pipe[1], F_SETFL, O_NONBLOCK) == -1) + { + return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, + "Can't set flag for stdin pipe for " + "hook '%s'", cmd); + } } else { @@ -273,38 +290,6 @@ cmd); } - /* If there is "APR stdin", read it and then write it to the hook's - * stdin pipe. */ - if (stdin_handle) - { - while (1) - { - apr_size_t bytes_read = sizeof(buffer); - int wc; - svn_error_t *err = svn_io_file_read(stdin_handle, buffer, - &bytes_read, pool); - if (err && APR_STATUS_IS_EOF(err->apr_err)) - break; - - if (err) - return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, - "Error piping stdin to hook " - "script '%s'", cmd); - - wc = write(stdin_pipe[1], buffer, bytes_read); - if (wc == -1) - return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, - "Error piping stdin to hook " - "script '%s'", cmd); - } - - /* Close the write end of the stdin pipe. */ - if (close(stdin_pipe[1]) == -1) - return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, - "Error closing write end of stdin pipe " - "to hook script '%s'", cmd); - } - /* Close the stdout file descriptor. */ if (close(fd_map[1]) == -1) return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, @@ -317,13 +302,49 @@ return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, "Error closing write end of stderr pipe to " "hook script '%s'", cmd); + + /* Close read end of stdin pipe so we don't hang on the write. */ + if (close(fd_map[0]) == -1) + return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, + "Error closing read end of stdin pipe to " + "hook script '%s'", cmd); - /* Read the hook's stderr if we care about that. */ - if (read_errstream) + /* Specify the events we want to poll: + * + * 1) Can read from the script's stderr pipe + * 2) Can write to the script's stdin pipe + */ + pfds[0].fd = read_errstream ? stderr_pipe[0] : -1; + pfds[0].events = POLLIN; + pfds[1].fd = stdin_handle != NULL ? stdin_pipe[1] : -1; + pfds[1].events = POLLOUT; + + while (more_stderr || more_stdin) { - while (1) + apr_size_t bytes_read = sizeof(stdin_buffer); + int wc; + svn_error_t *err; + + /* Blocking poll until we are ready to write some stdin to the script + * or read it's stderr. */ + if (poll(pfds, 2, -1) == -1) { - rc = read(stderr_pipe[0], buffer, sizeof(buffer)); + return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, + "Error polling stderr/stdin of hook " + "script '%s'", cmd); + } + + if (read_errstream && more_stderr && pfds[0].revents & POLLIN) + { + int rc; + + svn_stringbuf_ensure(script_output, + script_output->len + AS400_BUFFER_SIZE + 1); + + rc = read(stderr_pipe[0], + &(script_output->data[script_output->len]), + AS400_BUFFER_SIZE); + if (rc == -1) { return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, @@ -331,21 +352,84 @@ "script '%s'", cmd); } - svn_stringbuf_appendbytes(script_output, buffer, rc); + script_output->len += rc; - /* If read() returned 0 then EOF was found. */ if (rc == 0) { - /* Close the read end of the stderr pipe. */ - if (close(stderr_pipe[0]) == -1) + /* If read() returned 0 then EOF was found and we are done + * reading stderr. */ + more_stderr = FALSE; + } + } + + if (more_stdin && pfds[1].revents & POLLOUT) + { + /* Don't lose any stdin: Use what we last read into the buffer + * if the previous write to stdin pipe failed. */ + if (last_stdin_write_succeeded) + { + err = svn_io_file_read(stdin_handle, stdin_buffer, + &bytes_read, pool); + bytes_last_read = bytes_read; + } + if (err) + { + if (APR_STATUS_IS_EOF(err->apr_err)) + { + /* Done writing stdin to the script. */ + more_stdin = FALSE; + + /* Don't poll stdin pipe anymore. */ + pfds[1].fd = -1; + + /* Close the write end of the stderr pipe. */ + if (close(stdin_pipe[1]) == -1) + return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, + "Error closing write end of " + "stdin pipe to hook script " + "'%s'", cmd); + } + else return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, - "Error closing read end of stderr " - "pipe to hook script '%s'", cmd); - break; + "Error piping stdin to hook " + "script '%s'", cmd); } + else + { + wc = write(stdin_pipe[1], stdin_buffer, bytes_last_read); + if (wc == -1) + { + if (errno == EWOULDBLOCK) + { + last_stdin_write_succeeded = FALSE; + } + else if (errno == EPIPE) + { + /* If the script exits without ever trying to read + * stdin we'll get a broken pipe error. */ + more_stdin = FALSE; + } + else + { + return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, + NULL, "Error piping stdin " + "to hook script '%s'", cmd); + } + } + else + { + last_stdin_write_succeeded = TRUE; + } + } } - } + } /* while (more_stderr || more_stdin) */ + /* Close the read end of the stderr pipe. */ + if (read_errstream && close(stderr_pipe[0]) == -1) + return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, + "Error closing read end of stderr " + "pipe to hook script '%s'", cmd); + /* Wait for the child process to complete. */ wait_rv = waitpid(child_pid, &exitcode, 0); if (wait_rv == -1) @@ -355,12 +439,6 @@ "hook script '%s'", cmd); } - /* Close read end of stdin pipe if it exists. */ - if (close(fd_map[0]) == -1) - return svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, - "Error closing read end of stdin pipe to hook " - "script '%s'", cmd); - if (!svn_stringbuf_isempty(script_output)) { /* OS400 scripts produce EBCDIC stderr, so convert it. */