summaryrefslogtreecommitdiff
path: root/ace/Process.cpp
blob: 071d8eabcbb25426361269e6b9b71e5068f6bb20 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#define ACE_BUILD_DLL
// $Id$

#include "ace/Process.h"
#include "ace/ARGV.h"
#include "ace/SString.h"

#if !defined (__ACE_INLINE__)
#include "ace/Process.i"
#endif /* __ACE_INLINE__ */

int
ACE_Process::wait (void)
{
#if defined (ACE_WIN32)
  return ::WaitForSingleObject (process_info_.hProcess, INFINITE);
#else /* ACE_WIN32 */
  return ACE_OS::waitpid (this->child_id_, 0, 0);
#endif /* ACE_WIN32 */
}

ACE_Process::ACE_Process (void)
#if defined (ACE_WIN32)
  : set_handles_called_ (0)
#else /* ACE_WIN32 */
  : stdin_ (ACE_INVALID_HANDLE),
    stdout_ (ACE_INVALID_HANDLE),
    stderr_ (ACE_INVALID_HANDLE)
#endif /* ACE_WIN32 */
{
#if defined (ACE_WIN32)
  ACE_OS::memset ((void *) &this->startup_info_, 
		  0, sizeof this->startup_info_);
  ACE_OS::memset ((void *) &this->process_info_, 
		  0, sizeof this->process_info_);
  this->startup_info_.cb = sizeof this->startup_info_;
#endif /* ACE_WIN32 */
  this->cwd_[0] = '\0';
}

ACE_Process::~ACE_Process (void)
{
#if defined (ACE_WIN32)
  // Just in case <start> wasn't called.
  if (this->set_handles_called_)
    {
      ::CloseHandle (this->startup_info_.hStdInput);
      ::CloseHandle (this->startup_info_.hStdOutput);
      ::CloseHandle (this->startup_info_.hStdOutput);
      this->set_handles_called_ = 0;
    }

  // Free resources allocated in kernel.
  ACE_OS::close (this->process_info_.hThread);
  ACE_OS::close (this->process_info_.hProcess);
#endif /* ACE_WIN32 */
}

int
ACE_Process::set_handles (ACE_HANDLE std_in,
			  ACE_HANDLE std_out,
			  ACE_HANDLE std_err)
{
#if defined (ACE_WIN32)
  this->set_handles_called_ = 1;

  // Tell the new process to use our std handles.
  this->startup_info_.dwFlags = STARTF_USESTDHANDLES;

  if (std_in == ACE_INVALID_HANDLE)
    std_in = ACE_STDIN;
  if (std_out == ACE_INVALID_HANDLE)
    std_out = ACE_STDOUT;
  if (std_err == ACE_INVALID_HANDLE)
    std_err = ACE_STDERR;

  if (!::DuplicateHandle (::GetCurrentProcess(),
			  std_in,
			  ::GetCurrentProcess(),
			  &this->startup_info_.hStdInput,
			  NULL,
			  TRUE,
			  DUPLICATE_SAME_ACCESS))
    return -1;

  if (!::DuplicateHandle (::GetCurrentProcess(),
			  std_out,
			  ::GetCurrentProcess(),
			  &this->startup_info_.hStdOutput,
			  NULL,
			  TRUE,
			  DUPLICATE_SAME_ACCESS))
    return -1;

  if (!::DuplicateHandle (::GetCurrentProcess(),
			  std_err,
			  ::GetCurrentProcess(),
			  &this->startup_info_.hStdError,
			  NULL,
			  TRUE,
			  DUPLICATE_SAME_ACCESS))
    return -1;
#else /* ACE_WIN32 */
  this->stdin_ = std_in;
  this->stdout_ = std_out;
  this->stderr_ = std_err;
#endif /* ACE_WIN32 */

  return 0; // Success.
}

int
ACE_Process::set_cwd (const TCHAR *cwd)
{
  ACE_OS::strncpy (this->cwd_, cwd, MAXPATHLEN);
  // This is for paranoia...
  this->cwd_[MAXPATHLEN] = '\0';
  return 0;
}

pid_t
ACE_Process::start (char *argv[], char *envp[])
{
#if defined (ACE_WIN32)
  ACE_ARGV argv_buf (argv);

  LPTSTR buf = (LPTSTR) ACE_WIDE_STRING (argv_buf.buf ());

  if (buf == 0)
    return -1;

  BOOL fork_result = 
    ::CreateProcess (NULL,
		     buf,
		     NULL, // No process attributes.
		     NULL, // No thread attributes.
		     TRUE, // Allow handle inheritance.
		     NULL, // CREATE_NEW_CONSOLE, // Create a new console window.
		     envp, // Environment.
		     this->cwd_, // Current directory to start in.
		     &this->startup_info_,
		     &this->process_info_);

  if (set_handles_called_)
    {
      ::CloseHandle (this->startup_info_.hStdInput);
      ::CloseHandle (this->startup_info_.hStdOutput);
      ::CloseHandle (this->startup_info_.hStdOutput);
      this->set_handles_called_ = 0;
    }

  if (fork_result) // If success.
    return 0;
  else
    // CreateProcess failed.
    return -1;
#else /* ACE_WIN32 */
  // Fork the new process.
  this->child_id_ = ACE_OS::fork (argv == 0 ? "child" : argv[1]);

  switch (this->child_id_)
    {
    case -1:
      // Error.
      return -1;
    case 0:
      if (stdin_ != ACE_INVALID_HANDLE
	  && ACE_OS::dup2 (stdin_, ACE_STDIN) == -1)
	return -1;
      else if (stdout_ != ACE_INVALID_HANDLE
	       && ACE_OS::dup2 (stdout_, ACE_STDOUT) == -1)
	return -1;
      else if (stderr_ != ACE_INVALID_HANDLE
	       && ACE_OS::dup2 (stderr_, ACE_STDERR) == -1)
	return -1;

      // If we must, set the working directory for the child process.
      if (this->cwd_[0] != '\0')
	::chdir (cwd_);

      if (argv != 0)
	{
	  // Child process executes the command.
	  int result;
      
	  if (envp == 0)
	    result = ACE_OS::execv (argv[0], argv);
	  else
	    result = ACE_OS::execve (argv[0], argv, envp);

	  if (result == -1)
	    // If the execv fails, this child needs to exit.
	    ACE_OS::exit (errno);
	}
      return 0;
      /* NOTREACHED */
    default:
      // Server process.  The fork succeeded.
      return this->child_id_;
    }
#endif /* ACE_WIN32 */
}

ACE_Process::ACE_Process (char *argv[],
			  ACE_HANDLE std_in,
			  ACE_HANDLE std_out,
			  ACE_HANDLE std_err,
			  char *envp[])
#if defined (ACE_WIN32)
  : set_handles_called_ (0)
#else /* ACE_WIN32 */
  : stdin_ (ACE_INVALID_HANDLE),
    stdout_ (ACE_INVALID_HANDLE),
    stderr_ (ACE_INVALID_HANDLE)
#endif /* ACE_WIN32 */
{
#if defined (ACE_WIN32)
  ACE_OS::memset ((void *) &this->startup_info_, 
		  0, 
		  sizeof this->startup_info_);
  ACE_OS::memset ((void *) &this->process_info_, 
		  0, 
		  sizeof this->process_info_);
  this->startup_info_.cb = sizeof this->startup_info_;
#endif /* ACE_WIN32 */
  this->cwd_[0] = '\0';

  if (this->set_handles (std_in, std_out, std_err) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "set_handles"));
  else if (this->start (argv, envp) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "start"));
}