Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
spawn_sync.h
Go to the documentation of this file.
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef SRC_SPAWN_SYNC_H_
23 #define SRC_SPAWN_SYNC_H_
24 
25 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26 
27 #include "node.h"
28 #include "node_buffer.h"
29 
30 namespace node {
31 
32 using v8::Array;
33 using v8::Context;
34 using v8::FunctionCallbackInfo;
35 using v8::HandleScope;
36 using v8::Integer;
37 using v8::Isolate;
38 using v8::Local;
39 using v8::Null;
40 using v8::Number;
41 using v8::Object;
42 using v8::String;
43 using v8::Value;
44 
45 
46 class SyncProcessOutputBuffer;
47 class SyncProcessStdioPipe;
48 class SyncProcessRunner;
49 
50 
51 class SyncProcessOutputBuffer {
52  static const unsigned int kBufferSize = 65536;
53 
54  public:
55  inline SyncProcessOutputBuffer();
56 
57  inline void OnAlloc(size_t suggested_size, uv_buf_t* buf) const;
58  inline void OnRead(const uv_buf_t* buf, size_t nread);
59 
60  inline size_t Copy(char* dest) const;
61 
62  inline unsigned int available() const;
63  inline unsigned int used() const;
64 
65  inline SyncProcessOutputBuffer* next() const;
66  inline void set_next(SyncProcessOutputBuffer* next);
67 
68  private:
69  // Use unsigned int because that's what `uv_buf_init` takes.
70  mutable char data_[kBufferSize];
71  unsigned int used_;
72 
73  SyncProcessOutputBuffer* next_;
74 };
75 
76 
77 class SyncProcessStdioPipe {
78  enum Lifecycle {
79  kUninitialized = 0,
80  kInitialized,
81  kStarted,
82  kClosing,
83  kClosed
84  };
85 
86  public:
87  SyncProcessStdioPipe(SyncProcessRunner* process_handler,
88  bool readable,
89  bool writable,
90  uv_buf_t input_buffer);
91  ~SyncProcessStdioPipe();
92 
93  int Initialize(uv_loop_t* loop);
94  int Start();
95  void Close();
96 
97  Local<Object> GetOutputAsBuffer(Environment* env) const;
98 
99  inline bool readable() const;
100  inline bool writable() const;
101  inline uv_stdio_flags uv_flags() const;
102 
103  inline uv_pipe_t* uv_pipe() const;
104  inline uv_stream_t* uv_stream() const;
105  inline uv_handle_t* uv_handle() const;
106 
107  private:
108  inline size_t OutputLength() const;
109  inline void CopyOutput(char* dest) const;
110 
111  inline void OnAlloc(size_t suggested_size, uv_buf_t* buf);
112  inline void OnRead(const uv_buf_t* buf, ssize_t nread);
113  inline void OnWriteDone(int result);
114  inline void OnShutdownDone(int result);
115  inline void OnClose();
116 
117  inline void SetError(int error);
118 
119  static void AllocCallback(uv_handle_t* handle,
120  size_t suggested_size,
121  uv_buf_t* buf);
122  static void ReadCallback(uv_stream_t* stream,
123  ssize_t nread,
124  const uv_buf_t* buf);
125  static void WriteCallback(uv_write_t* req, int result);
126  static void ShutdownCallback(uv_shutdown_t* req, int result);
127  static void CloseCallback(uv_handle_t* handle);
128 
129  SyncProcessRunner* process_handler_;
130 
131  bool readable_;
132  bool writable_;
133  uv_buf_t input_buffer_;
134 
135  SyncProcessOutputBuffer* first_output_buffer_;
136  SyncProcessOutputBuffer* last_output_buffer_;
137 
138  mutable uv_pipe_t uv_pipe_;
139  uv_write_t write_req_;
140  uv_shutdown_t shutdown_req_;
141 
142  Lifecycle lifecycle_;
143 };
144 
145 
146 class SyncProcessRunner {
147  enum Lifecycle {
148  kUninitialized = 0,
149  kInitialized,
150  kHandlesClosed
151  };
152 
153  public:
154  static void Initialize(Local<Object> target,
155  Local<Value> unused,
156  Local<Context> context);
157  static void Spawn(const FunctionCallbackInfo<Value>& args);
158 
159  private:
160  friend class SyncProcessStdioPipe;
161 
162  explicit SyncProcessRunner(Environment* env_);
163  ~SyncProcessRunner();
164 
165  inline Environment* env() const;
166 
167  Local<Object> Run(Local<Value> options);
168  void TryInitializeAndRunLoop(Local<Value> options);
169  void CloseHandlesAndDeleteLoop();
170 
171  void CloseStdioPipes();
172  void CloseKillTimer();
173 
174  void Kill();
175  void IncrementBufferSizeAndCheckOverflow(ssize_t length);
176 
177  void OnExit(int64_t exit_status, int term_signal);
178  void OnKillTimerTimeout();
179 
180  int GetError();
181  void SetError(int error);
182  void SetPipeError(int pipe_error);
183 
184  Local<Object> BuildResultObject();
185  Local<Array> BuildOutputArray();
186 
187  int ParseOptions(Local<Value> js_value);
188  int ParseStdioOptions(Local<Value> js_value);
189  int ParseStdioOption(int child_fd, Local<Object> js_stdio_option);
190 
191  inline int AddStdioIgnore(uint32_t child_fd);
192  inline int AddStdioPipe(uint32_t child_fd,
193  bool readable,
194  bool writable,
195  uv_buf_t input_buffer);
196  inline int AddStdioInheritFD(uint32_t child_fd, int inherit_fd);
197 
198  static bool IsSet(Local<Value> value);
199  int CopyJsString(Local<Value> js_value, const char** target);
200  int CopyJsStringArray(Local<Value> js_value, char** target);
201 
202  static void ExitCallback(uv_process_t* handle,
203  int64_t exit_status,
204  int term_signal);
205  static void KillTimerCallback(uv_timer_t* handle);
206  static void KillTimerCloseCallback(uv_handle_t* handle);
207 
208  double max_buffer_;
209  uint64_t timeout_;
210  int kill_signal_;
211 
212  uv_loop_t* uv_loop_;
213 
214  uint32_t stdio_count_;
215  uv_stdio_container_t* uv_stdio_containers_;
216  SyncProcessStdioPipe** stdio_pipes_;
217  bool stdio_pipes_initialized_;
218 
219  uv_process_options_t uv_process_options_;
220  const char* file_buffer_;
221  char* args_buffer_;
222  char* env_buffer_;
223  const char* cwd_buffer_;
224 
225  uv_process_t uv_process_;
226  bool killed_;
227 
228  size_t buffered_output_size_;
229  int64_t exit_status_;
230  int term_signal_;
231 
232  uv_timer_t uv_timer_;
233  bool kill_timer_initialized_;
234 
235  // Errors that happen in one of the pipe handlers are stored in the
236  // `pipe_error` field. They are treated as "low-priority", only to be
237  // reported if no more serious errors happened.
238  int error_;
239  int pipe_error_;
240 
241  Lifecycle lifecycle_;
242 
243  Environment* env_;
244 };
245 
246 } // namespace node
247 
248 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
249 
250 #endif // SRC_SPAWN_SYNC_H_
unsigned char * buf
Definition: cares_wrap.cc:483
Environment *const env_
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
uv_fs_t req
Definition: node_file.cc:374
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536
MaybeLocal< Object > Copy(Isolate *isolate, const char *data, size_t length)
Definition: node_buffer.cc:320