File: spawnProc.c

package info (click to toggle)
ghc-cvs 20040725-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 68,484 kB
  • ctags: 19,658
  • sloc: haskell: 251,945; ansic: 109,709; asm: 24,961; sh: 12,825; perl: 5,786; makefile: 5,334; xml: 3,884; python: 682; yacc: 650; lisp: 477; cpp: 337; ml: 76; fortran: 24; csh: 18
file content (178 lines) | stat: -rw-r--r-- 4,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Spawning a Win32 child, redirecting its input and outputs
 * to standard file descriptors.
 *
 * Based on code snippets found in misc MSDN articles.
 *
 * This is the C support routine for Win32Spawn.hs
 */
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include "spawnProc.h"

/*
 * Function: mkAnonPipe
 *
 * Purpose:  create an anonymous pipe with read and write ends being
 *           optionally (non-)inheritable.
 */
static
BOOL
mkAnonPipe( HANDLE* pHandleIn,  BOOL isInheritableIn,
	    HANDLE* pHandleOut, BOOL isInheritableOut
	  )
{
  HANDLE hTemporaryIn  = NULL;
  HANDLE hTemporaryOut = NULL;
  BOOL status;
  SECURITY_ATTRIBUTES sec_attrs;

  /* Create inheritable security attributes */
  sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES); 
  sec_attrs.lpSecurityDescriptor = NULL; 
  sec_attrs.bInheritHandle = TRUE;

  /* Create the anon pipe with both ends inheritable */
  status = 
    CreatePipe( ((!isInheritableIn)  ? &hTemporaryIn  : pHandleIn),
		((!isInheritableOut) ? &hTemporaryOut : pHandleOut),
		&sec_attrs,
		0);

  if ( !status ) {
    /* Unable to create the pipe; just give up. */
    return FALSE;
  }
  
  if (!isInheritableIn) {
  /* Make the read end non-inheritable */
    status = DuplicateHandle( GetCurrentProcess(), hTemporaryIn,
			      GetCurrentProcess(), pHandleIn,
			      0,
			      FALSE, /* non-inheritable */
			      DUPLICATE_SAME_ACCESS);

    if ( !status ) {
      return FALSE;
    }
    CloseHandle(hTemporaryIn);
  }

  if (!isInheritableOut) {
    /* Make the write end non-inheritable */
    status = DuplicateHandle( GetCurrentProcess(), hTemporaryOut,
			      GetCurrentProcess(), pHandleOut,
			      0,
			      FALSE, /* non-inheritable */
			      DUPLICATE_SAME_ACCESS);

    if ( !status ) {
      return FALSE;
    }
    CloseHandle(hTemporaryOut);
  }
  
  return TRUE;
}

int
spawnProc ( LPTSTR cmd
	  , int* pFdInWrite
	  , int* pFdOutRead
	  , int* pFdErrRead
          )
{
  BOOL status;
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  HANDLE hStdinReadEnd,  hStdinWriteEnd;
  HANDLE hStdoutReadEnd, hStdoutWriteEnd;
  HANDLE hStderrReadEnd, hStderrWriteEnd;
  /*
  HANDLE hCurrentStdout, hCurrentStderr, hCurrentStdin;
  */

  /* Create the stdin anonymous pipe */
  if ( !mkAnonPipe( &hStdinReadEnd, TRUE,
		    &hStdinWriteEnd, FALSE) ) {
    /* Unable to create the pipe; just give up. */
    return -1;
  }
  
  /* Create the stdout anonymous pipe */
  if ( !mkAnonPipe( &hStdoutReadEnd, FALSE,
		    &hStdoutWriteEnd, TRUE) ) {
    /* Unable to create the pipe; just give up. */
    CloseHandle(hStdinReadEnd);
    CloseHandle(hStdinWriteEnd);
    return -1;
  }

  /* Create the stderr anonymous pipe */
  if ( !mkAnonPipe( &hStderrReadEnd, FALSE,
		    &hStderrWriteEnd, TRUE) ) {
    /* Unable to create the pipe; just give up. */
    CloseHandle(hStdinReadEnd);
    CloseHandle(hStdinWriteEnd);
    CloseHandle(hStdoutReadEnd);
    CloseHandle(hStdoutWriteEnd);
    return -1;
  }
  
  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  si.hStdInput  = hStdinReadEnd;
  si.hStdOutput = hStdoutWriteEnd;
  si.hStdError  = hStderrWriteEnd;
  si.dwFlags    = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  si.wShowWindow = SW_HIDE;
  
  /* Passing in the pipe handles via STARTUPINFO is slightly
     easier, so let's stick with that for now.
  hCurrentStdin  = GetStdHandle(STD_INPUT_HANDLE);
  hCurrentStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  hCurrentStderr = GetStdHandle(STD_ERROR_HANDLE);
  SetStdHandle(STD_INPUT_HANDLE, hStdinReadEnd);
  SetStdHandle(STD_OUTPUT_HANDLE, hStdoutWriteEnd);
  SetStdHandle(STD_ERROR_HANDLE, hStderrWriteEnd);
  */

  status = CreateProcess ( NULL,
			   cmd,
			   NULL,
			   NULL,
			   TRUE, /* inherit handles */
			   NORMAL_PRIORITY_CLASS,
			   NULL,
			   NULL,
			   &si,
			   &pi);
			   
  /*
  SetStdHandle(STD_INPUT_HANDLE,  hCurrentStdin);
  SetStdHandle(STD_OUTPUT_HANDLE, hCurrentStdout);
  SetStdHandle(STD_ERROR_HANDLE,  hCurrentStderr);
  */

  CloseHandle(hStdinReadEnd);
  CloseHandle(hStdoutWriteEnd);
  CloseHandle(hStderrWriteEnd);

  if (!status) {
    CloseHandle(hStdinWriteEnd);
    CloseHandle(hStdoutReadEnd);
    CloseHandle(hStderrReadEnd);
    return -1;
  }
  
  /* No need for these, close them. */
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  
  *pFdInWrite = (int)_open_osfhandle((long)hStdinWriteEnd, _O_TEXT);
  *pFdOutRead = (int)_open_osfhandle((long)hStdoutReadEnd, _O_TEXT);
  *pFdErrRead = (int)_open_osfhandle((long)hStderrReadEnd, _O_TEXT);

  return 0;
}