- if (mode_write) {
-// Save the handle to the current STDIN.
-
- hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);
-
-// Create a pipe for the child process's STDIN.
-
- if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
- ErrorExit("Stdin pipe creation failed\n");
- goto cleanup;
- }
-
-// Set a read handle to the pipe to be STDIN.
-
- if (! SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd)) {
- ErrorExit("Redirecting Stdin failed");
- goto cleanup;
- }
-
-// Duplicate the write handle to the pipe so it is not inherited.
-
- fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
- GetCurrentProcess(), &hChildStdinWrDup, 0,
- FALSE, // not inherited
- DUPLICATE_SAME_ACCESS);
- if (!fSuccess) {
- ErrorExit("DuplicateHandle failed");
- goto cleanup;
- }
-
- CloseHandle(hChildStdinWr);
+ // Create a pipe for the child process's STDOUT.
+ if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
+ ErrorExit("Stdout pipe creation failed\n");
+ goto cleanup;
+ }
+ // Create noninheritable read handle and close the inheritable read
+ // handle.
+
+ fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
+ GetCurrentProcess(), &hChildStdoutRdDup , 0,
+ FALSE,
+ DUPLICATE_SAME_ACCESS);
+ if ( !fSuccess ) {
+ ErrorExit("DuplicateHandle failed");
+ goto cleanup;
+ }
+
+ CloseHandle(hChildStdoutRd);
+ hChildStdoutRd = INVALID_HANDLE_VALUE;
+ }
+
+ if (mode_write) {
+
+ // Create a pipe for the child process's STDIN.
+
+ if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
+ ErrorExit("Stdin pipe creation failed\n");
+ goto cleanup;
+ }
+
+ // Duplicate the write handle to the pipe so it is not inherited.
+ fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
+ GetCurrentProcess(), &hChildStdinWrDup,
+ 0,
+ FALSE, // not inherited
+ DUPLICATE_SAME_ACCESS);
+ if (!fSuccess) {
+ ErrorExit("DuplicateHandle failed");
+ goto cleanup;
+ }
+
+ CloseHandle(hChildStdinWr);
+ hChildStdinWr = INVALID_HANDLE_VALUE;
+ }
+ // spawn program with redirected handles as appropriate
+ bpipe->worker_pid = (pid_t)
+ CreateChildProcess(prog, // commandline
+ hChildStdinRd, // stdin HANDLE
+ hChildStdoutWr, // stdout HANDLE
+ hChildStdoutWr); // stderr HANDLE
+
+ if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
+ goto cleanup;
+
+ bpipe->wait = wait;
+ bpipe->worker_stime = time(NULL);
+
+ if (mode_read) {
+ CloseHandle(hChildStdoutWr); // close our write side so when
+ // process terminates we can
+ // detect eof.
+ // ugly but convert WIN32 HANDLE to FILE*
+ int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
+ if (rfd >= 0) {
+ bpipe->rfd = _fdopen(rfd, "r");
+ }
+ }
+ if (mode_write) {
+ CloseHandle(hChildStdinRd); // close our read side so as not
+ // to interfre with child's copy
+ // ugly but convert WIN32 HANDLE to FILE*
+ int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
+ if (wfd >= 0) {
+ bpipe->wfd = _fdopen(wfd, "w");
+ }
+ }
+
+ if (wait > 0) {
+ bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
+ }
+
+ return bpipe;
+
+cleanup:
+
+ CloseIfValid(hChildStdoutRd);
+ CloseIfValid(hChildStdoutRdDup);
+ CloseIfValid(hChildStdinWr);
+ CloseIfValid(hChildStdinWrDup);
+
+ free((void *) bpipe);
+ errno = b_errno_win32; /* do GetLastError() for error code */
+ return NULL;
+}
+
+#endif //HAVE_MINGW
+
+int
+kill(int pid, int signal)
+{
+ int rval = 0;
+ if (!TerminateProcess((HANDLE)pid, (UINT) signal)) {
+ rval = -1;
+ errno = b_errno_win32;