Process.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package asys;
  2. import asys.uv.UVError;
  3. import haxe.NoData;
  4. import haxe.async.*;
  5. import haxe.io.*;
  6. import asys.net.Socket;
  7. import asys.io.*;
  8. import asys.uv.UVProcessSpawnFlags;
  9. /**
  10. Options for spawning a process. See `Process.spawn`.
  11. **/
  12. typedef ProcessSpawnOptions = {
  13. ?cwd:FilePath,
  14. ?env:Map<String, String>,
  15. ?argv0:String,
  16. ?stdio:Array<ProcessIO>,
  17. ?detached:Bool,
  18. ?uid:Int,
  19. ?gid:Int,
  20. // ?shell:?,
  21. ?windowsVerbatimArguments:Bool,
  22. ?windowsHide:Bool
  23. };
  24. /**
  25. Class representing a spawned process.
  26. **/
  27. class Process {
  28. /**
  29. Execute the given `command` with `args` (none by default). `options` can be
  30. specified to change the way the process is spawned.
  31. `options.stdio` is an optional array of `ProcessIO` specifications which
  32. can be used to define the file descriptors for the new process:
  33. - `Ignore` - skip the current position. No stream or pipe will be open for
  34. this index.
  35. - `Inherit` - inherit the corresponding file descriptor from the current
  36. process. Shares standard input, standard output, and standard error in
  37. index 0, 1, and 2, respectively. In index 3 or higher, `Inherit` has the
  38. same effect as `Ignore`.
  39. - `Pipe(readable, writable, ?pipe)` - create or use a pipe. `readable` and
  40. `writable` specify whether the pipe will be readable and writable from
  41. the point of view of the spawned process. If `pipe` is given, it is used
  42. directly, otherwise a new pipe is created.
  43. - `Ipc` - create an IPC (inter-process communication) pipe. Only one may be
  44. specified in `options.stdio`. This special pipe will not have an entry in
  45. the `stdio` array of the resulting process; instead, messages can be sent
  46. using the `send` method, and received over `messageSignal`. IPC pipes
  47. allow sending and receiving structured Haxe data, as well as connected
  48. sockets and pipes.
  49. Pipes are made available in the `stdio` array after the process is
  50. spawned. Standard file descriptors have their own variables:
  51. - `stdin` - set to point to a pipe in index 0, if it exists and is
  52. read-only for the spawned process.
  53. - `stdout` - set to point to a pipe in index 1, if it exists and is
  54. write-only for the spawned process.
  55. - `stderr` - set to point to a pipe in index 2, if it exists and is
  56. write-only for the spawned process.
  57. If `options.stdio` is not given,
  58. `[Pipe(true, false), Pipe(false, true), Pipe(false, true)]` is used as a
  59. default.
  60. @param options.cwd Path to the working directory. Defaults to the current
  61. working directory if not given.
  62. @param options.env Environment variables. Defaults to the environment
  63. variables of the current process if not given.
  64. @param options.argv0 First entry in the `argv` array for the spawned
  65. process. Defaults to `command` if not given.
  66. @param options.stdio Array of `ProcessIO` specifications, see above.
  67. @param options.detached When `true`, creates a detached process which can
  68. continue running after the current process exits. Note that `unref` must
  69. be called on the spawned process otherwise the event loop of the current
  70. process is kept allive.
  71. @param options.uid User identifier.
  72. @param options.gid Group identifier.
  73. @param options.windowsVerbatimArguments (Windows only.) Do not perform
  74. automatic quoting or escaping of arguments.
  75. @param options.windowsHide (Windows only.) Automatically hide the window of
  76. the spawned process.
  77. **/
  78. extern public static function spawn(command:String, ?args:Array<String>, ?options:ProcessSpawnOptions):Process;
  79. /**
  80. Emitted when `this` process and all of its pipes are closed.
  81. **/
  82. public final closeSignal:Signal<NoData> = new ArraySignal();
  83. // public final disconnectSignal:Signal<NoData> = new ArraySignal(); // IPC
  84. /**
  85. Emitted when an error occurs during communication with `this` process.
  86. **/
  87. public final errorSignal:Signal<UVError> = new ArraySignal();
  88. /**
  89. Emitted when `this` process exits, potentially due to a signal.
  90. **/
  91. public final exitSignal:Signal<ProcessExit> = new ArraySignal();
  92. /**
  93. Emitted when a message is received over IPC. The process must be created
  94. with an `Ipc` entry in `options.stdio`; see `Process.spawn`.
  95. **/
  96. public var messageSignal(default, null):Signal<IpcMessage>;
  97. public var connected(default, null):Bool = false;
  98. public var killed:Bool;
  99. extern private function get_pid():Int;
  100. /**
  101. Process identifier of `this` process. A PID uniquely identifies a process
  102. on its host machine for the duration of its lifetime.
  103. **/
  104. public var pid(get, never):Int;
  105. /**
  106. Standard input. May be `null` - see `options.stdio` in `spawn`.
  107. **/
  108. public var stdin:IWritable;
  109. /**
  110. Standard output. May be `null` - see `options.stdio` in `spawn`.
  111. **/
  112. public var stdout:IReadable;
  113. /**
  114. Standard error. May be `null` - see `options.stdio` in `spawn`.
  115. **/
  116. public var stderr:IReadable;
  117. /**
  118. Pipes created between the current (host) process and `this` (spawned)
  119. process. The order corresponds to the `ProcessIO` specifiers in
  120. `options.stdio` in `spawn`. This array can be used to access non-standard
  121. pipes, i.e. file descriptors 3 and higher, as well as file descriptors 0-2
  122. with non-standard read/write access.
  123. **/
  124. public var stdio:Array<Socket>;
  125. var ipc:Socket;
  126. var ipcOut:asys.io.IpcSerializer;
  127. var ipcIn:asys.io.IpcUnserializer;
  128. // public function disconnect():Void; // IPC
  129. /**
  130. Send a signal to `this` process.
  131. **/
  132. extern public function kill(?signal:Int = 7):Void;
  133. /**
  134. Close `this` process handle and all pipes in `stdio`.
  135. **/
  136. extern public function close(?cb:Callback<NoData>):Void;
  137. /**
  138. Send `data` to the process over the IPC channel. The process must be
  139. created with an `Ipc` entry in `options.stdio`; see `Process.spawn`.
  140. **/
  141. public function send(message:IpcMessage):Void {
  142. if (!connected)
  143. throw "IPC not connected";
  144. ipcOut.write(message);
  145. }
  146. extern public function ref():Void;
  147. extern public function unref():Void;
  148. private function new() {}
  149. }