-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.lua
More file actions
436 lines (398 loc) · 18.1 KB
/
process.lua
File metadata and controls
436 lines (398 loc) · 18.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
--- The process module allows querying various properties about the current
-- process, as well as creating, modifying, and searching other processes.
--
-- @module system.process
local expect = require "expect"
local util = require "util"
local process = {}
--- Returns the process ID of the current process.
-- @treturn number The process ID of the current process
function process.getpid()
return util.syscall.getpid()
end
--- Returns the process ID of the parent process, if available.
-- @treturn number The process ID of the parent process, if available
function process.getppid()
return util.syscall.getppid()
end
--- Returns the username the process is running under.
-- @treturn string The username the process is running under
function process.getuser()
return util.syscall.getuser()
end
--- Sets the user of the current process. This can only be run by root.
-- @tparam string user The user to switch to
function process.setuser(user)
expect(1, user, "string")
return util.syscall.setuser(user)
end
--- Returns the amount of time this process has executed. This may not be
-- entirely accurate due to a lack of precision in the system clock.
-- @treturn number The amount of time this process has executed
function process.clock()
return util.syscall.clock()
end
--- Returns the environment variable table for the current process.
-- @treturn table The environment variable table for the current process
function process.getenv()
return util.syscall.getenv()
end
--- Returns the environment table for the current process.
-- @treturn table The environment table for the current process
function process.getfenv()
return util.syscall.getfenv()
end
--- Returns the name of the current process.
-- @treturn string The name of the current process
function process.getname()
return util.syscall.getname()
end
--- Returns the working directory of the current process.
-- @treturn string The working directory of the current process
function process.getcwd()
return util.syscall.getcwd()
end
--- Sets the working directory of the current process.
-- @tparam string dir The new working directory, which must be absolute and existent.
function process.chdir(dir)
expect(1, dir, "string")
return util.syscall.chdir(dir)
end
--- Creates a new process running the specified function with arguments.
-- @tparam function func The function to run in the new process. This will be the
-- main function of the first thread, and will have its environment set to the
-- new process's environment.
-- @tparam string name? The name of the new process.
-- @tparam any ... Any arguments to pass to the function.
-- @treturn number The PID of the new process.
function process.fork(func, name, ...)
expect(1, func, "function")
expect(2, name, "string", "nil")
return util.syscall.fork(func, name, ...)
end
--- Creates a new process running the specified function with arguments. This
-- process will be placed in the background, meaning it has no stdin/out.
-- @tparam function func The function to run in the new process. This will be the
-- main function of the first thread, and will have its environment set to the
-- new process's environment.
-- @tparam string name? The name of the new process.
-- @tparam any ... Any arguments to pass to the function.
-- @treturn number The PID of the new process.
function process.forkbg(func, name, ...)
expect(1, func, "function")
expect(2, name, "string", "nil")
return util.syscall.fork(function(...)
util.syscall.stdin()
util.syscall.stdout()
util.syscall.stderr()
setfenv(func, _ENV)
return func(...)
end, name, ...)
end
--- Replaces the current process with the contents of the specified file.
-- This function does not return - it can only throw an error.
-- @tparam string path The path to the file to execute.
-- @tparam any ... Any arguments to pass to the file.
function process.exec(path, ...)
expect(1, path, "string")
return util.syscall.exec(path, ...)
end
--- Replaces the current process with the contents of the specified file or
-- command, searching the PATH environment variable if necessary.
-- This function does not return - it can only throw an error.
-- @tparam string command The command or file to execute.
-- @tparam any ... Any arguments to pass to the file.
function process.execp(command, ...)
expect(1, command, "string")
local path = util.syscall.getenv().PATH
if command:find "/" or type(path) ~= "string" then return util.syscall.exec(command, ...) end
for dir in path:gmatch "[^:]+" do
local p = util.syscall.combine(dir, command)
local s = util.syscall.stat(p)
if not s then
p = util.syscall.combine(dir, command .. ".lua")
s = util.syscall.stat(p)
end
if s and s.type ~= "directory" then return util.syscall.exec(p, ...) end
end
error(command .. ": No such file", 2)
end
--- Starts a new process from the specified path.
-- @tparam string path The path to the file to execute.
-- @tparam any ... Any arguments to pass to the file.
-- @treturn number The PID of the new process.
function process.start(path, ...)
expect(1, path, "string")
return util.syscall.fork(function(...) return coroutine.yield("syscall", "exec", ...) end, path, path, ...)
end
--- Starts a new process from the specified path. This process will be placed in
-- the background, meaning it has no stdin/out.
-- @tparam string path The path to the file to execute.
-- @tparam any ... Any arguments to pass to the file.
-- @treturn number The PID of the new process.
function process.startbg(path, ...)
expect(1, path, "string")
return util.syscall.fork(function(...)
util.syscall.stdin()
util.syscall.stdout()
util.syscall.stderr()
return coroutine.yield("syscall", "exec", ...)
end, path, path, ...)
end
--- Runs a program from the specified path in a new process, waiting until it completes.
-- @tparam string path The command or file to execute
-- @tparam any ... Any arguments to pass to the file
-- @treturn[1] true When the process succeeded
-- @treturn[1] any The return value from the process
-- @treturn[2] false When the process errored
-- @treturn[2] string The error message from the process
function process.run(command, ...)
expect(1, command, "string")
local PATH = util.syscall.getenv().PATH
local path
if command:find "/" or type(PATH) ~= "string" then path = command else
for dir in PATH:gmatch "[^:]+" do
local p = util.syscall.combine(dir, command)
local s = util.syscall.stat(p)
if not s then
p = util.syscall.combine(dir, command .. ".lua")
s = util.syscall.stat(p)
end
if s and s.type ~= "directory" then path = p break end
end
error(command .. ": No such file", 2)
end
local pid = util.syscall.fork(function(...) return coroutine.yield("syscall", "exec", ...) end, path, path, ...)
local event, param
repeat event, param = coroutine.yield() until event == "process_complete" and param.id == pid
return param.error == nil, param.error or param.value
end
--- Creates a new thread running the specified function with arguments.
-- Threads in the same process share the same environment, event queue, and
-- other properties.
-- @tparam function func The function to start
-- @tparam any ... Any arguments to pass to the function
-- @treturn number The ID of the new thread
function process.newthread(func, ...)
expect(1, func, "function")
return util.syscall.newthread(func, ...)
end
--- Ends the current process immediately, stopping all threads and sending the
-- specified return value to the parent. This function does not return.
-- @tparam number code? The value to return.
function process.exit(code)
return util.syscall.exit(code)
end
--- Runs a function when the program exists. This function will never get any
-- events, and is time-limited to 100 syscalls due to running in a different
-- context than normal threads - avoid passing long-running functions.
-- Functions added here cannot be removed later, so if the function may not be
-- needed after being added, use a variable check to disable it instead.
-- @tparam function fn The function to call at exit
function process.atexit(fn)
expect(1, fn, "function")
return util.syscall.atexit(fn)
end
--- Returns a list of all valid PIDs.
-- @treturn table A list of all valid PIDs
function process.getplist()
return util.syscall.getplist()
end
--- Returns a table with various information about the specified process.
-- @tparam number pid The process ID to query.
-- @treturn {id=number,name=string,user=string,parent?=number,dir=string,stdin?=number,stdout?=number,stderr?=number,cputime=number,systime=number,threads={[number]={id=number,name=string,status=string}}}|nil The process information, or nil if the process doesn't exist.
function process.getpinfo(pid)
expect(1, pid, "number")
return util.syscall.getpinfo(pid)
end
--- Sets the niceness level of the specified process, or the current one if left
-- unspecified. Nice values cause the process to run longer with a lower number
-- (requires root), or shorter with a higher number. Values range from -20 to 20.
-- @tparam number level The nice level to set to
-- @tparam[opt] number pid The process ID to modify (must be root or same user)
function process.nice(level, pid)
expect(1, level, "number")
expect.range(level, -20, 20)
expect(2, pid, "number", "nil")
return util.syscall.nice(level, pid)
end
--- Debugging subsystem
-- @section system.process.debug
process.debug = {}
--- Enables or disables debugging for the specified process. If `pid` is `nil`,
-- it will set whether other processes can debug this one. In other words,
-- calling `debug_enable(nil, false)` will disable any form of debugging on the
-- current program, even by root.
-- @tparam number pid The process ID to operate on (`nil` for this process)
-- @tparam boolean enabled Whether to enable debugging
function process.debug.enable(pid, enabled)
expect(1, pid, "number", "nil")
expect(2, enabled, "boolean")
return util.syscall.debug_enable(pid, enabled)
end
--- Pauses the specified thread, or all threads if none is specified, in the
-- target process. This will trigger a `debug_break` event in the calling
-- process for each thread that was paused as a result of this syscall.
--
-- If `pid` is `nil`, then this syscall operates differently: it will pause the
-- current thread (regardless of the `thread` parameter), and sends a
-- `debug_break` event to the last process that called `debug_enable` on this
-- process. If debugging is not enabled, then this syscall is a no-op, allowing
-- for programs to break to a debugger only if one is enabled.
-- @tparam number pid The process ID to pause, or `nil` to pause the current process
-- @tparam number thread The thread to pause, or `nil` to pause all threads
function process.debug.brk(pid, thread)
expect(1, pid, "number", "nil")
expect(2, thread, "number", "nil")
return util.syscall.debug_break(pid, thread)
end
--- Continues a paused thread, or all threads if none is specified.
-- @tparam number pid The process ID to unpause
-- @tparam number thread The thread to unpause, or `nil` to unpause all threads
function process.debug.continue(pid, thread)
expect(1, pid, "number")
expect(2, thread, "number", "nil")
return util.syscall.debug_continue(pid, thread)
end
--- Sets a breakpoint for the specified process, optionally filtering by thread.
-- When a breakpoint is hit in the target process, the thread (or all threads if
-- none is specified) is paused, and a `debug_break` event is queued in the
-- process that set the breakpoint.
--
-- The type can be one of these values:
-- - `call`: Break on a function call
-- - `return`: Break on a function return
-- - `line`: Break when execution changes lines
-- - `error`: Break when an error is thrown
-- - `resume`: Break when a coroutine is resumed
-- - `yield`: Break when a coroutine yields (not including preemption)
-- - `syscall`: Break when the process executes a system call
-- - For this case, the filter argument will only respect the `name` field
-- (for syscall name)
-- - Any number: Break after this number of VM instructions
--
-- The filter contains entries from a `debug.getinfo` table to match before
-- breaking. The breakpoint will only be triggered if all provided filters match.
-- @usage This example shows how to set a breakpoint on a specific line of a file, and then wait for the breakpoint to be hit:
--
-- local bp = syscall.debug_setbreakpoint(processID, nil, "line", {
-- source = "@/home/user/program.lua",
-- currentline = 11
-- })
-- repeat
-- local event, param = coroutine.yield()
-- until event == "debug_break" and param.breakpoint == bp
--
-- @tparam number pid The process ID to set the breakpoint on
-- @tparam number thread The thread to set the breakpoint on (or `nil` for any thread)
-- @tparam string|number type The type of breakpoint to set
-- @tparam[opt] table filter A filter to set on the breakpoint (see above)
-- @treturn number The ID of the new breakpoint
function process.debug.setbreakpoint(pid, thread, type, filter)
expect(1, pid, "number")
expect(2, thread, "number", "nil")
expect(3, type, "string", "number")
expect(4, filter, "table", "nil")
return util.syscall.debug_setbreakpoint(pid, thread, type, filter)
end
--- Unsets a previously set breakpoint.
-- @tparam number pid The process ID to operate on
-- @tparam number breakpoint The ID of the breakpoint to remove
function process.debug.unsetbreakpoint(pid, breakpoint)
expect(1, pid, "number")
expect(2, breakpoint, "number")
return util.syscall.debug_unsetbreakpoint(pid, breakpoint)
end
--- Returns a list of currently set breakpoints. Each entry has a `type` field,
-- as well as an optional `thread` field, and any filter items passed to
-- `debug_setbreakpoint`.
-- @tparam number pid The process ID to check
-- @treturn table[] A list of currently set breakpoints. This table may have holes in it if some breakpoints were unset!
function process.debug.listbreakpoints(pid)
expect(1, pid, "number")
return util.syscall.debug_listbreakpoints(pid)
end
--- Calls `debug.getinfo` on the specified thread in another process. Debugging
-- must be enabled for the target process, and the target thread must be paused.
-- @tparam number pid The process ID to operate on
-- @tparam number thread The thread ID to operate on
-- @tparam number level The level in the call stack to get info for
-- @tparam[opt] string what A string with the info to extract, or `nil` for all
-- @treturn table A table from `debug.getinfo`
function process.debug.getinfo(pid, thread, level, what)
expect(1, pid, "number")
expect(2, thread, "number")
expect(3, level, "number")
expect(4, what, "string", "nil")
return util.syscall.debug_getinfo(pid, thread, level, what)
end
--- Calls `debug.getlocal` on the specified thread in another process. Debugging
-- must be enabled for the target process, and the target thread must be paused.
-- @tparam number pid The process ID to operate on
-- @tparam number thread The thread ID to operate on
-- @tparam number level The level in the call stack to get info for
-- @tparam number n The index of the local to check
-- @treturn string|nil The local name
-- @treturn any The local value
function process.debug.getlocal(pid, thread, level, n)
expect(1, pid, "number")
expect(2, thread, "number")
expect(3, level, "number")
expect(4, n, "number")
return util.syscall.debug_getlocal(pid, thread, level, n)
end
--- Calls `debug.getupvalue` on the specified thread in another process. Debugging
-- must be enabled for the target process, and the target thread must be paused.
-- @tparam number pid The process ID to operate on
-- @tparam number thread The thread ID to operate on
-- @tparam number level The level in the call stack to get info for
-- @tparam number n The index of the upvalue to check
-- @treturn string|nil The upvalue name
-- @treturn any The upvalue value
function process.debug.getupvalue(pid, thread, level, n)
expect(1, pid, "number")
expect(2, thread, "number")
expect(3, level, "number")
expect(4, n, "number")
return util.syscall.debug_getupvalue(pid, thread, level, n)
end
--- Executes a function in the context of another process/thread. Debugging must
-- be enabled for the target process, and the target thread must be paused. The
-- environment for the function will be set to the environment of the process.
-- Note that the function runs under the hook environment, and thus will not be
-- preempted - avoid long-running tasks in this environment. (This may be fixed
-- in the future!)
-- @tparam number pid The process ID to operate on
-- @tparam number thread The thread ID to operate on
-- @tparam function fn The function to call
-- @return The values returned from the function
function process.debug.exec(pid, thread, fn)
expect(1, pid, "number")
expect(2, thread, "number")
expect(3, fn, "function")
util.syscall.debug_exec(pid, thread, fn)
while true do
local event, param = coroutine.yield()
if event == "debug_exec_result" and param.process == pid and param.thread == thread then
if param.ok then return table.unpack(param, 1, param.n)
else error(param.error, 0) end
end
end
end
--- Executes a function in the context of another process/thread asynchronously.
-- Debugging must be enabled for the target process, and the target thread must
-- be paused. The environment for the function will be set to the environment of
-- the process. The result of the function call will be passed in a
-- `debug_exec_result` event. Note that the function runs under the hook
-- environment, and thus will not be preempted - avoid long-running tasks in
-- this environment. (This may be fixed in the future!)
-- @tparam number pid The process ID to operate on
-- @tparam number thread The thread ID to operate on
-- @tparam function fn The function to call
function process.debug.execAsync(pid, thread, fn)
expect(1, pid, "number")
expect(2, thread, "number")
expect(3, fn, "function")
return util.syscall.debug_exec(pid, thread, fn)
end
return process