cvisor 0.1.0-x86_64-linux-musl → 0.2.0-x86_64-linux-musl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e983fc78cc4f7cdb9d04456f9f2060fd9cbd6f2c1be9116cdd056ba0ce25bf6
4
- data.tar.gz: 0f96222560cfcea664a21c59ce7222a27576fddfe118dd9659c09e9f256398a8
3
+ metadata.gz: 7349bc76588bd03fd93a0e5f745fef82700fa1bae7e6531280947c7c87573bba
4
+ data.tar.gz: 4103d75dcb922bd6599e986fd700697d1803b309f3db0539cd04ec05db828e8e
5
5
  SHA512:
6
- metadata.gz: ba864664965840e7212c1661617a254a5dc7b4b0d8c7177fa4fc1a186d675c94c5103a94024eb50d4d524678a26daadb87cae834f187c2c87a7d158697e2cba4
7
- data.tar.gz: b136f6e692c27db9e8e6861f954eecfcd3f80672fde8ad208ba4a57d3d501aadf8016e7f23ba9fbef2751a557cd69b2ddc118288c5b76bfb6fa49d349e7009ea
6
+ metadata.gz: ad2ede5fe2200c27f45794d82cce9b535b018fea4946e39b6ec4d3ea8970aaf1b59924961cd4f8189453b084c6f73228617d62647709ea2ebf504fa19deb7490
7
+ data.tar.gz: 00db936505362b486b340e8a3c54ed7d94188cc2977b4e0f9467e85516d1587585c0806fbbc381c8654fe93bf52cc7d305ae43ae189f877e6efdae959bf7b863
data/README.md CHANGED
@@ -9,13 +9,29 @@ require "cvisor"
9
9
 
10
10
  sb = Cvisor::Sandbox.new
11
11
  out = sb.run("echo hello")
12
- puts out.stdout # "hello\n"
13
- puts out.stderr # ""
12
+ puts out.stdout # "hello\n"
13
+ puts out.stderr # ""
14
+ puts out.exit_code # 0
14
15
  ```
15
16
 
16
- `Sandbox#run(cmd)` blocks until the sandboxed command exits and returns an
17
- `Output` with `#stdout` / `#stderr` (String) and `#stdout_bytes` /
18
- `#stderr_bytes`.
17
+ `Sandbox#run(cmd, timeout_ms: nil)` blocks until the sandboxed command exits and
18
+ returns an `Output` with `#stdout` / `#stderr` (String), `#stdout_bytes` /
19
+ `#stderr_bytes`, and `#exit_code` (Integer, shell convention: status, or
20
+ 128+signo when killed by a signal).
21
+
22
+ Pass a positive `timeout_ms:` to SIGKILL the guest after that many
23
+ milliseconds; a timed-out run reports exit code 137:
24
+
25
+ ```ruby
26
+ sb.run("sleep 30", timeout_ms: 300).exit_code # 137
27
+ ```
28
+
29
+ `Sandbox#set_allow_network(allow)` toggles outbound INET/INET6 networking for
30
+ the sandbox (allowed by default):
31
+
32
+ ```ruby
33
+ sb.set_allow_network(false) # deny outbound networking
34
+ ```
19
35
 
20
36
  ## Interactive console
21
37
 
data/lib/cvisor.rb CHANGED
@@ -27,8 +27,11 @@ module Cvisor
27
27
  extern "void* cvisor_sandbox_new(void)"
28
28
  extern "void cvisor_sandbox_free(void*)"
29
29
  extern "void cvisor_sandbox_set_log_level(void*, int)"
30
+ extern "void cvisor_sandbox_set_allow_network(void*, int)"
30
31
  extern "void* cvisor_run(void*, const char*)"
32
+ extern "void* cvisor_run_timeout(void*, const char*, unsigned long long)"
31
33
  extern "void cvisor_output_free(void*)"
34
+ extern "int cvisor_output_exit_code(void*)"
32
35
  extern "void* cvisor_output_stdout(void*, void*)"
33
36
  extern "void* cvisor_output_stderr(void*, void*)"
34
37
  extern "void cvisor_bytes_free(void*, size_t)"
@@ -36,11 +39,12 @@ module Cvisor
36
39
 
37
40
  # Captured output of one run.
38
41
  class Output
39
- attr_reader :stdout_bytes, :stderr_bytes
42
+ attr_reader :stdout_bytes, :stderr_bytes, :exit_code
40
43
 
41
- def initialize(stdout_bytes, stderr_bytes)
44
+ def initialize(stdout_bytes, stderr_bytes, exit_code)
42
45
  @stdout_bytes = stdout_bytes
43
46
  @stderr_bytes = stderr_bytes
47
+ @exit_code = exit_code
44
48
  end
45
49
 
46
50
  def stdout = @stdout_bytes.force_encoding("UTF-8")
@@ -57,12 +61,21 @@ module Cvisor
57
61
  Native.cvisor_sandbox_set_log_level(@ptr, level == "DEBUG" ? 1 : 0)
58
62
  end
59
63
 
60
- def run(command)
61
- out = Native.cvisor_run(@ptr, command)
64
+ def set_allow_network(allow)
65
+ Native.cvisor_sandbox_set_allow_network(@ptr, allow ? 1 : 0)
66
+ end
67
+
68
+ def run(command, timeout_ms: nil)
69
+ out = if timeout_ms.is_a?(Integer) && timeout_ms.positive?
70
+ Native.cvisor_run_timeout(@ptr, command, timeout_ms)
71
+ else
72
+ Native.cvisor_run(@ptr, command)
73
+ end
62
74
  raise "sandbox run failed" if out.null?
63
75
  begin
64
76
  Output.new(read_output(out, :cvisor_output_stdout),
65
- read_output(out, :cvisor_output_stderr))
77
+ read_output(out, :cvisor_output_stderr),
78
+ Native.cvisor_output_exit_code(out))
66
79
  ensure
67
80
  Native.cvisor_output_free(out)
68
81
  end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cvisor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: x86_64-linux-musl
6
6
  authors:
7
7
  - butter.dev