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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file process.lua
--
-- define module: process
local process = process or {}
local _subprocess = _subprocess or {}
-- load modules
local io = require("base/io")
local path = require("base/path")
local utils = require("base/utils")
local string = require("base/string")
local coroutine = require("base/coroutine")
local scheduler = require("base/scheduler")
-- save original interfaces
process._open = process._open or process.open
process._openv = process._openv or process.openv
process._wait = process._wait or process.wait
process._kill = process._kill or process.kill
process._close = process._close or process.close
process.wait = nil
process.kill = nil
process.close = nil
process._subprocess = _subprocess
-- new an subprocess
function _subprocess.new(program, proc)
local subprocess = table.inherit(_subprocess)
subprocess._PROGRAM = program
subprocess._PROC = proc
setmetatable(subprocess, _subprocess)
return subprocess
end
-- get the process name
function _subprocess:name()
if not self._NAME then
self._NAME = path.filename(self:program())
end
return self._NAME
end
-- get the process program
function _subprocess:program()
return self._PROGRAM
end
-- get cdata of process
function _subprocess:cdata()
return self._PROC
end
-- get poller object type, poller.OT_PROC
function _subprocess:otype()
return 3
end
-- wait subprocess
--
-- @param timeout the timeout
--
-- @return ok, status
--
function _subprocess:wait(timeout)
-- ensure opened
local ok, errors = self:_ensure_opened()
if not ok then
return -1, errors
end
-- wait events
local result = -1
local status_or_errors = nil
if scheduler:co_running() then
result, status_or_errors = scheduler:poller_waitproc(self, timeout or -1)
else
result, status_or_errors = process._wait(self:cdata(), timeout or -1)
end
if result < 0 and status_or_errors then
status_or_errors = string.format("%s: %s", self, status_or_errors)
end
return result, status_or_errors
end
-- kill subprocess
function _subprocess:kill()
-- ensure opened
local ok, errors = self:_ensure_opened()
if not ok then
return false, errors
end
-- kill process
process._kill(self:cdata())
return true
end
-- close subprocess
function _subprocess:close()
-- ensure opened
local ok, errors = self:_ensure_opened()
if not ok then
return false, errors
end
-- cancel pipe events from the scheduler
if scheduler:co_running() then
ok, errors = scheduler:poller_cancel(self)
if not ok then
return false, errors
end
end
-- close process
ok = process._close(self:cdata())
if ok then
self._PROC = nil
end
return ok
end
-- ensure the process is opened
function _subprocess:_ensure_opened()
if not self:cdata() then
return false, string.format("%s: has been closed!", self)
end
return true
end
-- tostring(subprocess)
function _subprocess:__tostring()
return "<subprocess: " .. self:name() .. ">"
end
-- gc(subprocess)
function _subprocess:__gc()
if self._PROC and process._close(self._PROC) then
self._PROC = nil
end
end
-- open a subprocess
--
-- @param command the process command
-- @param opt the option arguments, e.g. {stdin = filepath/file/pipe, stdout = filepath/file/pipe, stderr = filepath/file/pipe, envs = {"PATH=xxx", "XXX=yyy"}})
--
-- @return the subprocess
--
function process.open(command, opt)
-- get stdin and pass to subprocess
opt = opt or {}
local stdin = opt.stdin
if type(stdin) == "string" then
opt.inpath = stdin
elseif type(stdin) == "table" then
if stdin.otype and stdin:otype() == 2 then
opt.inpipe = stdin:cdata()
else
opt.infile = stdin:cdata()
end
end
-- get stdout and pass to subprocess
local stdout = opt.stdout
if type(stdout) == "string" then
opt.outpath = stdout
elseif type(stdout) == "table" then
if stdout.otype and stdout:otype() == 2 then
opt.outpipe = stdout:cdata()
else
opt.outfile = stdout:cdata()
end
end
-- get stderr and pass to subprocess
local stderr = opt.stderr
if type(stderr) == "string" then
opt.errpath = stderr
elseif type(stderr) == "table" then
if stderr.otype and stderr:otype() == 2 then
opt.errpipe = stderr:cdata()
else
opt.errfile = stderr:cdata()
end
end
-- open subprocess
local proc = process._open(command, opt)
if proc then
return _subprocess.new(command:split(' ', {plain = true})[1], proc)
else
return nil, string.format("open process(%s) failed!", command)
end
end
-- open a subprocess with the arguments list
--
-- @param program the program
-- @param argv the arguments list
-- @param opt the option arguments, e.g. {stdin = filepath/file/pipe, stdout = filepath/file/pipe, stderr = filepath/file/pipe, envs = {"PATH=xxx", "XXX=yyy"}})
--
-- @return the subprocess
--
function process.openv(program, argv, opt)
-- get stdin and pass to subprocess
opt = opt or {}
local stdin = opt.stdin
if type(stdin) == "string" then
opt.inpath = stdin
elseif type(stdin) == "table" then
if stdin.otype and stdin:otype() == 2 then
opt.inpipe = stdin:cdata()
else
opt.infile = stdin:cdata()
end
end
-- get stdout and pass to subprocess
local stdout = opt.stdout
if type(stdout) == "string" then
opt.outpath = stdout
elseif type(stdout) == "table" then
if stdout.otype and stdout:otype() == 2 then
opt.outpipe = stdout:cdata()
else
opt.outfile = stdout:cdata()
end
end
-- get stderr and pass to subprocess
local stderr = opt.stderr
if type(stderr) == "string" then
opt.errpath = stderr
elseif type(stderr) == "table" then
if stderr.otype and stderr:otype() == 2 then
opt.errpipe = stderr:cdata()
else
opt.errfile = stderr:cdata()
end
end
-- open subprocess
local proc = process._openv(program, argv, opt)
if proc then
return _subprocess.new(program, proc)
else
return nil, string.format("openv process(%s, %s) failed!", program, os.args(argv))
end
end
-- get missing dlls
function process._get_missing_dlls(program)
local missing = {}
if not os.isexec(program) then
return missing
end
-- get paths
local pathenv = os.getenv("PATH") or ""
local paths = path.splitenv(pathenv)
table.insert(paths, 1, path.directory(program))
-- find missing dlls
local sandbox_module = require("sandbox/modules/import/core/sandbox/module")
local get_depend_libraries = sandbox_module.import("utils.binary.deplibs", {anonymous = true})
local imports = get_depend_libraries(program, {recursive = true}) or {}
for i, dll in ipairs(imports) do
imports[i] = path.filename(dll)
end
for _, dll in ipairs(imports) do
local found = false
for _, p in ipairs(paths) do
if os.isfile(path.join(p, dll)) then
found = true
break
end
end
if not found then
table.insert(missing, dll)
end
end
return missing
end
-- get process exit errors
function process.get_exit_errors(program, exitcode)
local errors
if os.is_host("windows") then
-- DLL is missing, 0xC0000135
if exitcode == -1073741515 then
local missing_dlls = process._get_missing_dlls(program)
if #missing_dlls > 0 then
errors = string.format("system error 0xC0000135 (STATUS_DLL_NOT_FOUND).\nThe application failed to start because the following DLLs were not found:\n - %s\nPlease check your PATH environment variable or copy the missing DLLs to the executable directory.", table.concat(missing_dlls, "\n - "))
else
errors = string.format("system error 0xC0000135 (STATUS_DLL_NOT_FOUND).\nThe application failed to start because a dependent DLL was not found.\nPlease check your PATH environment variable or copy the missing DLL to the executable directory.")
end
end
end
return errors
end
-- return module: process
return process
|