/
run.cr
283 lines (241 loc) · 7.61 KB
/
run.cr
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require "c/unistd"
class Process
# The standard io configuration of a process:
#
# * `nil`: use a pipe
# * `false`: no IO (`/dev/null`)
# * `true`: inherit from parent
# * `IO`: use the given IO
alias Stdio = Nil | Bool | IO
alias Env = Nil | Hash(String, Nil) | Hash(String, String?) | Hash(String, String)
# Executes a process and waits for it to complete.
#
# By default the process is configured without input, output or error.
def self.run(cmd : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = false, input : Stdio = false, output : Stdio = false, error : Stdio = false, chdir : String? = nil) : Process::Status
status = new(cmd, args, env, clear_env, shell, input, output, error, chdir).wait
$? = status
status
end
# Executes a process, yields the block, and then waits for it to finish.
#
# By default the process is configured to use pipes for input, output and error. These
# will be closed automatically at the end of the block.
#
# Returns the block's value.
def self.run(cmd : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = false, input : Stdio = nil, output : Stdio = nil, error : Stdio = nil, chdir : String? = nil)
process = new(cmd, args, env, clear_env, shell, input, output, error, chdir)
begin
value = yield process
$? = process.wait
value
rescue ex
process.kill
raise ex
end
end
getter pid : Int32
# A pipe to this process's input. Raises if a pipe wasn't asked when creating the process.
getter! input : IO::FileDescriptor
# A pipe to this process's output. Raises if a pipe wasn't asked when creating the process.
getter! output : IO::FileDescriptor
# A pipe to this process's error. Raises if a pipe wasn't asked when creating the process.
getter! error : IO::FileDescriptor
@waitpid_future : Concurrent::Future(Process::Status)
# Creates a process, executes it, but doesn't wait for it to complete.
#
# To wait for it to finish, invoke `wait`.
#
# By default the process is configured without input, output or error.
def initialize(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = false, input : Stdio = false, output : Stdio = false, error : Stdio = false, chdir : String? = nil)
if shell
command = %(#{command} "${@}") unless command.includes?(' ')
shell_args = ["-c", command, "--"]
if args
unless command.includes?(%("${@}"))
raise ArgumentError.new(%(can't specify arguments in both, command and args without including "${@}" into your command))
end
ifdef freebsd
shell_args << ""
end
shell_args.concat(args)
end
command = "/bin/sh"
args = shell_args
end
argv = [command.to_unsafe]
args.try &.each do |arg|
argv << arg.to_unsafe
end
argv << Pointer(UInt8).null
@wait_count = 0
if needs_pipe?(input)
fork_input, process_input = IO.pipe(read_blocking: true)
if input
@wait_count += 1
spawn { copy_io(input, process_input, channel, close_dst: true) }
else
@input = process_input
end
end
if needs_pipe?(output)
process_output, fork_output = IO.pipe(write_blocking: true)
if output
@wait_count += 1
spawn { copy_io(process_output, output, channel, close_src: true) }
else
@output = process_output
end
end
if needs_pipe?(error)
process_error, fork_error = IO.pipe(write_blocking: true)
if error
@wait_count += 1
spawn { copy_io(process_error, error, channel, close_src: true) }
else
@error = process_error
end
end
@pid = Process.fork_internal(run_hooks: false) do
begin
# File.umask(umask) if umask
reopen_io(fork_input || input, STDIN, "r")
reopen_io(fork_output || output, STDOUT, "w")
reopen_io(fork_error || error, STDERR, "w")
ENV.clear if clear_env
env.try &.each do |key, val|
if val
ENV[key] = val
else
ENV.delete key
end
end
Dir.cd(chdir) if chdir
LibC.execvp(command, argv)
rescue ex
ex.inspect_with_backtrace STDERR
ensure
LibC._exit 127
end
end
@waitpid_future = Event::SignalChildHandler.instance.waitpid(pid)
fork_input.try &.close
fork_output.try &.close
fork_error.try &.close
end
protected def initialize(@pid)
@waitpid_future = Event::SignalChildHandler.instance.waitpid(pid)
@wait_count = 0
end
# See Process.kill
def kill(sig = Signal::TERM)
Process.kill sig, @pid
end
# Waits for this process to complete and closes any pipes.
def wait : Process::Status
close_io @input # only closed when a pipe was created but not managed by copy_io
@wait_count.times do
ex = channel.receive
raise ex if ex
end
@wait_count = 0
@waitpid_future.get
ensure
close
end
# Closes any pipes to the child process.
def close
close_io @input
close_io @output
close_io @error
end
private def channel
@channel ||= Channel(Exception?).new
end
private def needs_pipe?(io)
io.nil? || (io.is_a?(IO) && !io.is_a?(IO::FileDescriptor))
end
private def copy_io(src, dst, channel, close_src = false, close_dst = false)
return unless src.is_a?(IO) && dst.is_a?(IO)
begin
IO.copy(src, dst)
# close is called here to trigger exceptions
# close must be called before channel.send or the process may deadlock
src.close if close_src
close_src = false
dst.close if close_dst
close_dst = false
channel.send nil
rescue ex
channel.send ex
ensure
# any exceptions are silently ignored because of spawn
src.close if close_src
dst.close if close_dst
end
end
private def reopen_io(src_io, dst_io, mode)
case src_io
when IO::FileDescriptor
src_io.blocking = true
dst_io.reopen(src_io)
when true
# use same io as parent
dst_io.blocking = true
when false
File.open("/dev/null", mode) do |file|
dst_io.reopen(file)
end
else
raise "unknown object type #{src_io}"
end
dst_io.close_on_exec = false
end
private def close_io(io)
io.close if io
end
end
# Executes the given command in a subshell.
# Standard input, output and error are inherited.
# Returns `true` if the command gives zero exit code, `false` otherwise.
# The special `$?` variable is set to a `Process::Status` associated with this execution.
#
# If *command* contains no spaces and *args* is given, it will become
# its argument list.
#
# If *command* contains spaces and *args* is given, *command* must include
# `"${@}"` (including the quotes) to receive the argument list.
#
# No shell interpretation is done in *args*.
#
# Example:
#
# ```
# system("echo *")
# ```
#
# Produces:
#
# ```text
# LICENSE shard.yml Readme.md spec src
# ```
def system(command : String, args = nil) : Bool
status = Process.run(command, args, shell: true, input: true, output: true, error: true)
$? = status
status.success?
end
# Returns the standard output of executing *command* in a subshell.
# Standard input, and error are inherited.
# The special `$?` variable is set to a `Process::Status` associated with this execution.
#
# Example:
#
# ```
# `echo *` # => "LICENSE shard.yml Readme.md spec src\n"
# ```
def `(command) : String
process = Process.new(command, shell: true, input: true, output: nil, error: true)
output = process.output.gets_to_end
status = process.wait
$? = status
output
end