vivarium 0.4.2 → 0.5.1

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: 0a0f8b6dfc29af71d39ff5fe1ebceca9040cf62a2c6147dba5c2a074b392cdc1
4
- data.tar.gz: c9f10129e5b42fd51653dcda0a02ed0e7be8b4c2ad7daef4f48a9fa5c03dcf41
3
+ metadata.gz: 43539002d1068c28f470c91be68c64227c36312b72d4dc35a39ce89248cc0a97
4
+ data.tar.gz: 7f1d83e733fcedc3094897317b0273c26a8e5317428dda1917022facd58b02c6
5
5
  SHA512:
6
- metadata.gz: 9530187d826e63976bd7e1c86872345f5cad281608b2ee06f0dbe20e6211455d8d050a18013822f6baecf4c0af18e9e01b58fec7fe13174d9631181df8660569
7
- data.tar.gz: c3ee802cf168d5ca917c6d9de5cce25c9441ba46a35995bc5e5f78ce6c78055c1a4d8141bff7dd4e7763205d65c1a41344a789185dbea8fc65f4589ef8e90586
6
+ metadata.gz: b7ef0d3e7501bd259fd405a09870eb785e042c920b93bbe93f6efb1804fafba44a5538df9a20c809934732338c49ffc6c29a3fcff74b5395fe7e7b86449fd849
7
+ data.tar.gz: 250af21f03f00cb7cbec31b8bdced3b3b5effe618da48df1f0f83b1b5f42025085c35ec436506eef5ee60664f9bc7dfda6cb712e82fc18dfc9a3acd77893ba39
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2019 Uchio Kondo
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,214 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "socket"
5
+
6
+ module Vivarium
7
+ # In-memory, sequence-numbered log of raw event_t records (#{Vivarium::EVENT_STRUCT_SIZE} bytes
8
+ # each) fed by the daemon's ring buffer poller and consumed by /events streams.
9
+ class EventLog
10
+ def initialize(capacity: 50_000)
11
+ @capacity = capacity
12
+ @mutex = Mutex.new
13
+ @cond = ConditionVariable.new
14
+ @records = []
15
+ @seq = 0
16
+ end
17
+
18
+ def append(bytes)
19
+ @mutex.synchronize do
20
+ @seq += 1
21
+ @records << [@seq, bytes]
22
+ overflow = @records.size - @capacity
23
+ @records.shift(overflow) if overflow.positive?
24
+ @cond.broadcast
25
+ end
26
+ end
27
+
28
+ def tail_seq
29
+ @mutex.synchronize { @seq }
30
+ end
31
+
32
+ # Returns records with seq > cursor. Blocks up to timeout seconds when nothing newer
33
+ # is available so callers can long-poll.
34
+ def read_after(cursor, timeout: 1.0)
35
+ @mutex.synchronize do
36
+ if @records.empty? || @records.last[0] <= cursor
37
+ @cond.wait(@mutex, timeout)
38
+ end
39
+ @records.select { |seq, _| seq > cursor }
40
+ end
41
+ end
42
+ end
43
+
44
+ # Wraps the daemon's live BPF target maps so the API can (un)register PIDs.
45
+ class Registry
46
+ def initialize(config_root_targets, config_spawned_targets)
47
+ @config_root_targets = config_root_targets
48
+ @config_spawned_targets = config_spawned_targets
49
+ end
50
+
51
+ def register(pid)
52
+ @config_root_targets[pid] = 1
53
+ end
54
+
55
+ def unregister(pid)
56
+ @config_root_targets.delete(pid)
57
+ @config_spawned_targets.clear
58
+ rescue KeyError
59
+ nil
60
+ end
61
+ end
62
+
63
+ # Minimal HTTP/1.1 server over a Unix domain socket exposing the daemon control API.
64
+ class ApiServer
65
+ STREAM_POLL_TIMEOUT = 1.0
66
+
67
+ def initialize(socket_path:, event_log:, registry:, daemon_pid: Process.pid)
68
+ @socket_path = socket_path
69
+ @event_log = event_log
70
+ @registry = registry
71
+ @daemon_pid = daemon_pid
72
+ end
73
+
74
+ def start
75
+ FileUtils.mkdir_p(File.dirname(@socket_path))
76
+ File.unlink(@socket_path) if File.exist?(@socket_path)
77
+ @server = UNIXServer.new(@socket_path)
78
+ File.chmod(0o666, @socket_path)
79
+ @thread = Thread.new { accept_loop }
80
+ self
81
+ end
82
+
83
+ def stop
84
+ @server&.close
85
+ rescue StandardError
86
+ nil
87
+ ensure
88
+ File.unlink(@socket_path) if @socket_path && File.exist?(@socket_path)
89
+ end
90
+
91
+ private
92
+
93
+ def accept_loop
94
+ loop do
95
+ conn = @server.accept
96
+ Thread.new(conn) { |c| handle(c) }
97
+ end
98
+ rescue IOError, Errno::EBADF
99
+ # server closed during shutdown
100
+ end
101
+
102
+ def handle(conn)
103
+ request_line = conn.gets
104
+ return if request_line.nil?
105
+
106
+ method, target, = request_line.split(" ")
107
+ drain_headers(conn)
108
+
109
+ path, query = target.to_s.split("?", 2)
110
+ route(conn, method, path, query)
111
+ rescue Errno::EPIPE, IOError
112
+ nil
113
+ rescue StandardError => e
114
+ warn "[vivariumd api] #{e.class}: #{e.message}"
115
+ ensure
116
+ begin
117
+ conn.close
118
+ rescue StandardError
119
+ nil
120
+ end
121
+ end
122
+
123
+ def drain_headers(conn)
124
+ while (line = conn.gets)
125
+ break if line == "\r\n" || line == "\n"
126
+ end
127
+ end
128
+
129
+ def route(conn, method, path, query)
130
+ target_match = path.to_s.match(%r{\A/targets/(\d+)\z})
131
+
132
+ if method == "GET" && path == "/healthz"
133
+ respond_json(conn, 200, { status: "ok", pid: @daemon_pid })
134
+ elsif method == "GET" && path == "/events"
135
+ stream_events(conn, query)
136
+ elsif method == "PUT" && target_match
137
+ pid = Integer(target_match[1], 10)
138
+ @registry.register(pid)
139
+ respond_json(conn, 200, { status: "registered", pid: pid })
140
+ elsif method == "DELETE" && target_match
141
+ pid = Integer(target_match[1], 10)
142
+ @registry.unregister(pid)
143
+ respond_json(conn, 200, { status: "unregistered", pid: pid })
144
+ else
145
+ respond_json(conn, 404, { error: "not_found" })
146
+ end
147
+ end
148
+
149
+ def stream_events(conn, query)
150
+ since = parse_since(query)
151
+ conn.write("HTTP/1.1 200 OK\r\n")
152
+ conn.write("Content-Type: application/octet-stream\r\n")
153
+ conn.write("Transfer-Encoding: chunked\r\n")
154
+ conn.write("\r\n")
155
+
156
+ cursor = since || @event_log.tail_seq
157
+ loop do
158
+ records = @event_log.read_after(cursor, timeout: STREAM_POLL_TIMEOUT)
159
+ records.each do |seq, bytes|
160
+ conn.write(format("%x\r\n", bytes.bytesize))
161
+ conn.write(bytes)
162
+ conn.write("\r\n")
163
+ cursor = seq
164
+ end
165
+ end
166
+ rescue Errno::EPIPE, Errno::ECONNRESET, IOError
167
+ # client disconnected
168
+ ensure
169
+ begin
170
+ conn.write("0\r\n\r\n")
171
+ rescue StandardError
172
+ nil
173
+ end
174
+ end
175
+
176
+ def parse_since(query)
177
+ return nil if query.nil? || query.empty?
178
+
179
+ query.split("&").each do |pair|
180
+ key, value = pair.split("=", 2)
181
+ return Integer(value, 10) if key == "since" && value
182
+ end
183
+ nil
184
+ rescue ArgumentError
185
+ nil
186
+ end
187
+
188
+ def respond_json(conn, status, payload)
189
+ body = json_encode(payload)
190
+ conn.write("HTTP/1.1 #{status} #{status_text(status)}\r\n")
191
+ conn.write("Content-Type: application/json\r\n")
192
+ conn.write("Content-Length: #{body.bytesize}\r\n")
193
+ conn.write("Connection: close\r\n")
194
+ conn.write("\r\n")
195
+ conn.write(body)
196
+ end
197
+
198
+ def status_text(status)
199
+ case status
200
+ when 200 then "OK"
201
+ when 404 then "Not Found"
202
+ else "Status"
203
+ end
204
+ end
205
+
206
+ def json_encode(hash)
207
+ pairs = hash.map do |key, value|
208
+ encoded = value.is_a?(Integer) ? value.to_s : %("#{value}")
209
+ %("#{key}":#{encoded})
210
+ end
211
+ "{#{pairs.join(',')}}"
212
+ end
213
+ end
214
+ end
data/lib/vivarium/box.rb CHANGED
@@ -12,21 +12,12 @@ module Vivarium
12
12
  # result = box::MyClass.new.foo # automatically traced if Vivarium.observe is active
13
13
  #
14
14
  class Box < Module
15
- DEFAULT_FILTER = {
16
- include_events: %w[
17
- proc_fork proc_exec span_start span_stop
18
- sock_connect dns_req odd_socket
19
- ssl_write
20
- dlopen mmap_exec
21
- task_kill
22
- setid_change capable_check bprm_creds
23
- ]
24
- }
15
+ DEFAULT_FILTER = Vivarium::DEFAULT_FILTER
25
16
 
26
- def initialize(pin_dir: Vivarium.bpf_pin_dir, dest: $stdout, filter: DEFAULT_FILTER)
17
+ def initialize(socket_path: Vivarium.socket_path, dest: $stdout, filter: DEFAULT_FILTER)
27
18
  super()
28
19
  @inner_box = Ruby::Box.new
29
- @pin_dir = pin_dir
20
+ @socket_path = socket_path
30
21
  @dest = dest
31
22
  @filter = filter
32
23
  @session = nil
@@ -124,7 +115,7 @@ module Vivarium
124
115
 
125
116
  def start_vivarium_observation
126
117
  puts "[debug] Starting Vivarium observation for Box method calls"
127
- @session = Vivarium.top_observe(pin_dir: @pin_dir, dest: @dest, filter: @filter)
118
+ @session = Vivarium.top_observe(socket_path: @socket_path, dest: @dest, filter: @filter)
128
119
  end
129
120
 
130
121
  def stop_vivarium_observation
data/lib/vivarium/cli.rb CHANGED
@@ -5,7 +5,7 @@ require "optparse"
5
5
  module Vivarium
6
6
  module CLI
7
7
  def self.run!(argv = ARGV)
8
- options = { pin_dir: Vivarium.bpf_pin_dir, dest: $stdout }
8
+ options = { socket_path: Vivarium.socket_path, dest: $stdout }
9
9
  parser = OptionParser.new do |opts|
10
10
  opts.banner = "Usage: vivarium [options] <command> [args]"
11
11
  opts.separator ""
@@ -13,7 +13,7 @@ module Vivarium
13
13
  opts.separator " load <script> Load and observe a Ruby script"
14
14
  opts.separator ""
15
15
  opts.separator "Options:"
16
- opts.on("--pin-dir PATH", "Pinned map directory") { |v| options[:pin_dir] = v }
16
+ opts.on("--socket PATH", "vivariumd Unix domain socket path") { |v| options[:socket_path] = v }
17
17
  opts.on("-o", "--output PATH", "Log output file (default: stdout)") { |v| options[:dest] = File.open(v, "a") }
18
18
  end
19
19
  parser.order!(argv)
@@ -32,7 +32,8 @@ module Vivarium
32
32
  abort "Usage: vivarium load <script>" unless script
33
33
  abort "File not found: #{script}" unless File.exist?(script)
34
34
 
35
- Vivarium.observe(pin_dir: options[:pin_dir], dest: options[:dest]) do
35
+ Vivarium.observe(socket_path: options[:socket_path], dest: options[:dest],
36
+ filter: Vivarium::DEFAULT_FILTER) do
36
37
  Kernel.load(File.expand_path(script))
37
38
  end
38
39
  end
@@ -1,48 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rbbcc"
4
3
  require "time"
5
4
 
6
5
  module Vivarium
6
+ # Client-side consumer of the vivariumd event stream. Connects to the daemon's
7
+ # Unix domain socket, reads chunked raw event_t records, accumulates them, and
8
+ # renders a tree on stop. It never touches BPF maps or the ring buffer directly.
7
9
  class Correlator
8
10
  RawEvent = Struct.new(
9
11
  :ktime_ns, :pid, :tid, :event_name, :payload, :dropped_since_last,
10
12
  keyword_init: true
11
13
  )
12
14
 
13
- EVENT_C_TYPE = <<~C
14
- struct event_t {
15
- u64 ktime_ns;
16
- u32 pid;
17
- u32 tid;
18
- char event_name[16];
19
- char payload[256];
20
- u64 dropped_since_last;
21
- };
22
- C
23
-
24
- POLL_TIMEOUT_MS = 200
25
-
26
- def initialize(pin_dir:, observer_pid:, main_tid:, filter: nil, dest: $stdout)
27
- @pin_dir = pin_dir
15
+ # Grace period after stop to let trailing events drain through the stream.
16
+ DRAIN_SLEEP = 0.3
17
+
18
+ def initialize(socket_path: Vivarium.socket_path, observer_pid:, main_tid:,
19
+ filter: nil, dest: $stdout)
20
+ @socket_path = socket_path
28
21
  @observer_pid = observer_pid
29
22
  @main_tid = main_tid
30
23
  @filter = filter
31
24
  @dest = dest
32
25
 
26
+ @client = DaemonClient.new(socket_path: socket_path)
33
27
  @events = []
34
28
  @events_mutex = Mutex.new
35
29
  @stop_flag = false
36
30
  @started = false
37
-
38
- @ringbuf = RbBCC::RingBuf.from_pin(
39
- File.join(@pin_dir, "events"),
40
- EVENT_C_TYPE,
41
- Vivarium::EVENTS_RINGBUF_PAGES
42
- )
43
- @ringbuf.open_ring_buffer do |_ctx, data, size|
44
- capture_event(data, size)
45
- end
31
+ @stopped = false
46
32
  end
47
33
 
48
34
  def start
@@ -50,6 +36,7 @@ module Vivarium
50
36
 
51
37
  @session_start_iso = Time.now.utc.iso8601(3)
52
38
  @session_start_ktime = Vivarium.monotonic_ktime_ns
39
+ @sock = @client.open_event_stream
53
40
  @thread = Thread.new { run }
54
41
  @started = true
55
42
  end
@@ -58,13 +45,13 @@ module Vivarium
58
45
  return unless @started
59
46
  return if @stopped
60
47
 
48
+ sleep DRAIN_SLEEP
61
49
  @stop_flag = true
62
- @thread&.join(POLL_TIMEOUT_MS * 4 / 1000.0 + 1)
50
+ @sock&.close
51
+ @thread&.join(2)
63
52
  @session_stop_iso = Time.now.utc.iso8601(3)
64
53
  @session_stop_ktime = Vivarium.monotonic_ktime_ns
65
54
 
66
- 3.times { safe_poll(50) }
67
-
68
55
  events_snapshot = @events_mutex.synchronize { @events.dup }
69
56
  @stopped = true
70
57
 
@@ -85,18 +72,43 @@ module Vivarium
85
72
 
86
73
  def run
87
74
  until @stop_flag
88
- safe_poll(POLL_TIMEOUT_MS)
75
+ size = read_chunk_size(@sock)
76
+ break if size.nil? || size.zero?
77
+
78
+ bytes = read_exactly(@sock, size)
79
+ break if bytes.nil?
80
+
81
+ @sock.read(2) # trailing CRLF after chunk data
82
+ capture_event(bytes)
89
83
  end
84
+ rescue IOError, Errno::EBADF, Errno::ECONNRESET
85
+ # socket closed on stop
86
+ rescue StandardError => e
87
+ warn "[vivarium correlator] stream error: #{e.class}: #{e.message}"
90
88
  end
91
89
 
92
- def safe_poll(timeout_ms)
93
- @ringbuf.ring_buffer_poll(timeout_ms)
94
- rescue StandardError => e
95
- warn "[vivarium correlator] poll error: #{e.class}: #{e.message}"
90
+ def read_chunk_size(sock)
91
+ line = sock.gets
92
+ return nil if line.nil?
93
+
94
+ Integer(line.strip, 16)
95
+ rescue ArgumentError
96
+ nil
96
97
  end
97
98
 
98
- def capture_event(data, size)
99
- bytes = data[0, size].to_s.b
99
+ def read_exactly(sock, size)
100
+ buffer = +""
101
+ while buffer.bytesize < size
102
+ chunk = sock.read(size - buffer.bytesize)
103
+ return nil if chunk.nil?
104
+
105
+ buffer << chunk
106
+ end
107
+ buffer
108
+ end
109
+
110
+ def capture_event(bytes)
111
+ bytes = bytes.to_s.b
100
112
  bytes = bytes.ljust(Vivarium::EVENT_STRUCT_SIZE, "\x00") if bytes.bytesize < Vivarium::EVENT_STRUCT_SIZE
101
113
 
102
114
  ktime_ns = bytes[Vivarium::EVENT_TS_OFFSET, Vivarium::EVENT_TS_SIZE].unpack1("Q<")
@@ -119,6 +131,5 @@ module Vivarium
119
131
  rescue StandardError => e
120
132
  warn "[vivarium correlator] capture error: #{e.class}: #{e.message}"
121
133
  end
122
-
123
134
  end
124
135
  end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+
5
+ module Vivarium
6
+ # HTTP-over-Unix-domain-socket client for talking to vivariumd. The client side
7
+ # never touches BPF maps or the ring buffer directly; everything goes through here.
8
+ class DaemonClient
9
+ def initialize(socket_path: Vivarium.socket_path)
10
+ @socket_path = socket_path
11
+ end
12
+
13
+ def healthy?
14
+ status, = simple_request("GET", "/healthz")
15
+ status == 200
16
+ rescue Error
17
+ false
18
+ end
19
+
20
+ def register(pid)
21
+ simple_request("PUT", "/targets/#{pid}")
22
+ end
23
+
24
+ def unregister(pid)
25
+ simple_request("DELETE", "/targets/#{pid}")
26
+ end
27
+
28
+ # Opens a dedicated streaming connection to GET /events, consumes the response
29
+ # headers, and returns the still-open socket positioned at the start of the
30
+ # chunked body. The caller is responsible for reading chunks and closing it.
31
+ def open_event_stream(since: nil)
32
+ sock = connect
33
+ path = since ? "/events?since=#{since}" : "/events"
34
+ sock.write("GET #{path} HTTP/1.1\r\n")
35
+ sock.write("Host: vivarium\r\n")
36
+ sock.write("Accept: application/octet-stream\r\n")
37
+ sock.write("\r\n")
38
+ read_response_headers(sock)
39
+ sock
40
+ end
41
+
42
+ private
43
+
44
+ def connect
45
+ UNIXSocket.new(@socket_path)
46
+ rescue Errno::ENOENT, Errno::ECONNREFUSED => e
47
+ raise Error, "cannot connect to vivariumd at #{@socket_path}: #{e.message} " \
48
+ "(is vivariumd running?)"
49
+ end
50
+
51
+ def simple_request(method, path)
52
+ sock = connect
53
+ begin
54
+ sock.write("#{method} #{path} HTTP/1.1\r\n")
55
+ sock.write("Host: vivarium\r\n")
56
+ sock.write("Connection: close\r\n")
57
+ sock.write("\r\n")
58
+ status = read_status(sock)
59
+ body = sock.read
60
+ [status, body]
61
+ ensure
62
+ sock.close
63
+ end
64
+ end
65
+
66
+ def read_status(sock)
67
+ status_line = sock.gets
68
+ return nil if status_line.nil?
69
+
70
+ status_line.split(" ")[1].to_i
71
+ end
72
+
73
+ def read_response_headers(sock)
74
+ read_status(sock)
75
+ while (line = sock.gets)
76
+ break if line == "\r\n" || line == "\n"
77
+ end
78
+ end
79
+ end
80
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vivarium
4
- VERSION = "0.4.2"
4
+ VERSION = "0.5.1"
5
5
  end
data/lib/vivarium.rb CHANGED
@@ -6,6 +6,7 @@ require "net/http"
6
6
  require "optparse"
7
7
  require "pathname"
8
8
  require "rbbcc"
9
+ require "set"
9
10
  require "socket"
10
11
  if defined?(Ruby) && defined?(Ruby::Box) && Ruby::Box.enabled?
11
12
  Ruby::Box.root.require "vivarium_usdt"
@@ -25,6 +26,8 @@ module Vivarium
25
26
  CONFIG_TARGETS_PIN = CONFIG_ROOT_TARGETS_PIN
26
27
  EVENTS_PIN = File.join(PIN_DIR, "events")
27
28
 
29
+ SOCKET_PATH = ENV.fetch("VIVARIUM_SOCKET_PATH", "/run/vivarium/vivariumd.sock")
30
+
28
31
  EVENT_NAME_SIZE = 16
29
32
  EVENT_PAYLOAD_SIZE = 256
30
33
  EVENT_TS_SIZE = 8
@@ -85,14 +88,31 @@ module Vivarium
85
88
  TCPSocket,
86
89
  UDPSocket,
87
90
  UNIXSocket,
88
- File,
89
- Dir,
90
91
  Signal,
91
92
  Process,
92
93
  Process::UID,
93
94
  Process::GID,
94
95
  Net::HTTP,
95
96
  ]
97
+
98
+ # File/Dir are deliberately NOT in SPAN_ALLOWCLASSES: tracing every method is
99
+ # far too noisy and read/query methods (exist?, basename, read, stat, ...) carry
100
+ # little security signal. Instead only the security-relevant methods below are
101
+ # turned into spans. Detection is done via tp.self (not tp.defined_class) so that
102
+ # e.g. File.open, whose method is owned by IO, is still matched. Kernel LSM events
103
+ # (path_open, file_chmod, file_rename, file_symlink, file_hardlink, file_getdents)
104
+ # already capture the underlying filesystem actions regardless of the Ruby method.
105
+ SPAN_FILE_METHODS = %i[
106
+ open new write binwrite
107
+ delete unlink rename truncate
108
+ chmod lchmod chown lchown
109
+ symlink link readlink
110
+ realpath realdirpath
111
+ mkfifo mknod utime
112
+ ].to_set.freeze
113
+ SPAN_DIR_METHODS = %i[
114
+ mkdir rmdir delete unlink chdir chroot glob
115
+ ].to_set.freeze
96
116
  SPAN_ALLOWLIST = [
97
117
  "Kernel#system",
98
118
  "Kernel#require",
@@ -121,6 +141,26 @@ module Vivarium
121
141
  dlopen
122
142
  ].freeze
123
143
 
144
+ # Default display filter applied by both `vivarium load` (CLI) and Vivarium::Box.
145
+ # path_open fires on every file open and is far too noisy to show in full, so it
146
+ # is restricted to opens under /etc and /proc (config/state that matters for
147
+ # security review). render_event_payload renders the path via String#inspect,
148
+ # so the matched target text looks like "/etc/passwd" (leading quote included).
149
+ DEFAULT_FILTER = {
150
+ include_events: %w[
151
+ proc_fork proc_exec span_start span_stop
152
+ path_open
153
+ sock_connect dns_req odd_socket
154
+ ssl_write
155
+ dlopen mmap_exec
156
+ task_kill
157
+ setid_change capable_check bprm_creds
158
+ ],
159
+ payload: {
160
+ "path_open" => %r{\A"?/(?:home|root|etc|proc)(?:/|"|\z)}
161
+ }
162
+ }.freeze
163
+
124
164
  CAPABILITY_NAMES = {
125
165
  0 => "CAP_CHOWN",
126
166
  1 => "CAP_DAC_OVERRIDE",
@@ -160,13 +200,18 @@ module Vivarium
160
200
  }.freeze
161
201
 
162
202
  @bpf_pin_dir = PIN_DIR
203
+ @socket_path = SOCKET_PATH
163
204
 
164
205
  class << self
165
- attr_writer :bpf_pin_dir
206
+ attr_writer :bpf_pin_dir, :socket_path
166
207
 
167
208
  def bpf_pin_dir
168
209
  @bpf_pin_dir || PIN_DIR
169
210
  end
211
+
212
+ def socket_path
213
+ @socket_path || SOCKET_PATH
214
+ end
170
215
  end
171
216
 
172
217
  def self.c_string(bytes)
@@ -544,39 +589,6 @@ module Vivarium
544
589
  bytes[0, nul]
545
590
  end
546
591
 
547
- class MapStore
548
- def initialize(pin_dir: Vivarium.bpf_pin_dir)
549
- @pin_dir = pin_dir
550
- @config_root_targets = RbBCC::HashTable.from_pin(
551
- File.join(@pin_dir, "config_root_targets"),
552
- "unsigned int",
553
- "unsigned char",
554
- keysize: 4,
555
- leafsize: 1
556
- )
557
- @config_spawned_targets = RbBCC::HashTable.from_pin(
558
- File.join(@pin_dir, "config_spawned_targets"),
559
- "unsigned int",
560
- "unsigned char",
561
- keysize: 4,
562
- leafsize: 1
563
- )
564
- rescue StandardError => e
565
- raise Error, "failed to open pinned maps under #{@pin_dir}: #{e.class}: #{e.message}"
566
- end
567
-
568
- def register_pid(pid)
569
- @config_root_targets[pid] = 1
570
- end
571
-
572
- def unregister_pid(pid)
573
- @config_root_targets.delete(pid)
574
- @config_spawned_targets.clear
575
- rescue KeyError
576
- nil
577
- end
578
- end
579
-
580
592
  class Daemon
581
593
  BPF_PROGRAM_TEMPLATE = <<~CLANG
582
594
  #include <linux/socket.h>
@@ -1459,60 +1471,6 @@ module Vivarium
1459
1471
  return 0;
1460
1472
  }
1461
1473
 
1462
- int on_span_start(struct pt_regs *ctx)
1463
- {
1464
- u64 pid_tgid = bpf_get_current_pid_tgid();
1465
- u32 pid = pid_tgid >> 32;
1466
- u32 tid = (u32)pid_tgid;
1467
-
1468
- if (!target_enabled(pid, tid)) {
1469
- return 0;
1470
- }
1471
-
1472
- u64 method_str_ptr = 0;
1473
- u64 file_str_ptr = 0;
1474
- s64 lineno = 0;
1475
- bpf_usdt_readarg(1, ctx, &method_str_ptr);
1476
- bpf_usdt_readarg(2, ctx, &file_str_ptr);
1477
- bpf_usdt_readarg(3, ctx, &lineno);
1478
-
1479
- struct event_t ev = {};
1480
- ev.pid = pid;
1481
- __builtin_memcpy(ev.event_name, "span_start", 11);
1482
- bpf_probe_read_user_str(&ev.payload[0], #{SPAN_METHOD_SIZE}, (void*)method_str_ptr);
1483
- bpf_probe_read_user_str(&ev.payload[#{SPAN_METHOD_SIZE}], #{SPAN_FILE_SIZE}, (void*)file_str_ptr);
1484
- __builtin_memcpy(&ev.payload[#{SPAN_LINENO_OFFSET}], &lineno, sizeof(lineno));
1485
- submit_event(&ev);
1486
- return 0;
1487
- }
1488
-
1489
- int on_span_stop(struct pt_regs *ctx)
1490
- {
1491
- u64 pid_tgid = bpf_get_current_pid_tgid();
1492
- u32 pid = pid_tgid >> 32;
1493
- u32 tid = (u32)pid_tgid;
1494
-
1495
- if (!target_enabled(pid, tid)) {
1496
- return 0;
1497
- }
1498
-
1499
- u64 method_str_ptr = 0;
1500
- u64 file_str_ptr = 0;
1501
- s64 lineno = 0;
1502
- bpf_usdt_readarg(1, ctx, &method_str_ptr);
1503
- bpf_usdt_readarg(2, ctx, &file_str_ptr);
1504
- bpf_usdt_readarg(3, ctx, &lineno);
1505
-
1506
- struct event_t ev = {};
1507
- ev.pid = pid;
1508
- __builtin_memcpy(ev.event_name, "span_stop", 10);
1509
- bpf_probe_read_user_str(&ev.payload[0], #{SPAN_METHOD_SIZE}, (void*)method_str_ptr);
1510
- bpf_probe_read_user_str(&ev.payload[#{SPAN_METHOD_SIZE}], #{SPAN_FILE_SIZE}, (void*)file_str_ptr);
1511
- __builtin_memcpy(&ev.payload[#{SPAN_LINENO_OFFSET}], &lineno, sizeof(lineno));
1512
- submit_event(&ev);
1513
- return 0;
1514
- }
1515
-
1516
1474
  int on_ssl_write(struct pt_regs *ctx)
1517
1475
  {
1518
1476
  u64 pid_tgid = bpf_get_current_pid_tgid();
@@ -1648,8 +1606,67 @@ module Vivarium
1648
1606
  submit_env_event(pid, "clearenv", 8, 0);
1649
1607
  return 0;
1650
1608
  }
1609
+ CLANG
1651
1610
 
1652
- int on_span_raise(struct pt_regs *ctx)
1611
+ # USDT span handlers are generated per attached .so so each gets a unique
1612
+ # fn_name. BCC emits _bpf_readarg_<fn_name>_<n> per USDT context, so sharing
1613
+ # one fn_name across contexts triggers a redefinition error.
1614
+ SPAN_PROBE_TEMPLATE = <<~CLANG
1615
+ int on_span_start__SUFFIX__(struct pt_regs *ctx)
1616
+ {
1617
+ u64 pid_tgid = bpf_get_current_pid_tgid();
1618
+ u32 pid = pid_tgid >> 32;
1619
+ u32 tid = (u32)pid_tgid;
1620
+
1621
+ if (!target_enabled(pid, tid)) {
1622
+ return 0;
1623
+ }
1624
+
1625
+ u64 method_str_ptr = 0;
1626
+ u64 file_str_ptr = 0;
1627
+ s64 lineno = 0;
1628
+ bpf_usdt_readarg(1, ctx, &method_str_ptr);
1629
+ bpf_usdt_readarg(2, ctx, &file_str_ptr);
1630
+ bpf_usdt_readarg(3, ctx, &lineno);
1631
+
1632
+ struct event_t ev = {};
1633
+ ev.pid = pid;
1634
+ __builtin_memcpy(ev.event_name, "span_start", 11);
1635
+ bpf_probe_read_user_str(&ev.payload[0], #{SPAN_METHOD_SIZE}, (void*)method_str_ptr);
1636
+ bpf_probe_read_user_str(&ev.payload[#{SPAN_METHOD_SIZE}], #{SPAN_FILE_SIZE}, (void*)file_str_ptr);
1637
+ __builtin_memcpy(&ev.payload[#{SPAN_LINENO_OFFSET}], &lineno, sizeof(lineno));
1638
+ submit_event(&ev);
1639
+ return 0;
1640
+ }
1641
+
1642
+ int on_span_stop__SUFFIX__(struct pt_regs *ctx)
1643
+ {
1644
+ u64 pid_tgid = bpf_get_current_pid_tgid();
1645
+ u32 pid = pid_tgid >> 32;
1646
+ u32 tid = (u32)pid_tgid;
1647
+
1648
+ if (!target_enabled(pid, tid)) {
1649
+ return 0;
1650
+ }
1651
+
1652
+ u64 method_str_ptr = 0;
1653
+ u64 file_str_ptr = 0;
1654
+ s64 lineno = 0;
1655
+ bpf_usdt_readarg(1, ctx, &method_str_ptr);
1656
+ bpf_usdt_readarg(2, ctx, &file_str_ptr);
1657
+ bpf_usdt_readarg(3, ctx, &lineno);
1658
+
1659
+ struct event_t ev = {};
1660
+ ev.pid = pid;
1661
+ __builtin_memcpy(ev.event_name, "span_stop", 10);
1662
+ bpf_probe_read_user_str(&ev.payload[0], #{SPAN_METHOD_SIZE}, (void*)method_str_ptr);
1663
+ bpf_probe_read_user_str(&ev.payload[#{SPAN_METHOD_SIZE}], #{SPAN_FILE_SIZE}, (void*)file_str_ptr);
1664
+ __builtin_memcpy(&ev.payload[#{SPAN_LINENO_OFFSET}], &lineno, sizeof(lineno));
1665
+ submit_event(&ev);
1666
+ return 0;
1667
+ }
1668
+
1669
+ int on_span_raise__SUFFIX__(struct pt_regs *ctx)
1653
1670
  {
1654
1671
  u64 pid_tgid = bpf_get_current_pid_tgid();
1655
1672
  u32 pid = pid_tgid >> 32;
@@ -1680,14 +1697,18 @@ module Vivarium
1680
1697
  }
1681
1698
  CLANG
1682
1699
 
1683
- def initialize(pin_dir: Vivarium.bpf_pin_dir, ssl_trace: true, libssl_path: nil,
1684
- dlopen_trace: true, env_trace: true, libc_path: nil)
1685
- @pin_dir = pin_dir
1686
- @ssl_trace = ssl_trace
1687
- @libssl_path = libssl_path
1688
- @dlopen_trace = dlopen_trace
1689
- @env_trace = env_trace
1690
- @libc_path = libc_path
1700
+ def initialize(pin_dir: Vivarium.bpf_pin_dir, socket_path: Vivarium.socket_path,
1701
+ ssl_trace: true, libssl_path: nil,
1702
+ dlopen_trace: true, env_trace: true, libc_path: nil,
1703
+ usdt_so_paths: nil)
1704
+ @pin_dir = pin_dir
1705
+ @socket_path = socket_path
1706
+ @ssl_trace = ssl_trace
1707
+ @libssl_path = libssl_path
1708
+ @dlopen_trace = dlopen_trace
1709
+ @env_trace = env_trace
1710
+ @libc_path = libc_path
1711
+ @usdt_so_paths = usdt_so_paths
1691
1712
  end
1692
1713
 
1693
1714
  def run
@@ -1700,13 +1721,11 @@ module Vivarium
1700
1721
  .gsub("__VIVARIUM_F_PATH_OFFSET__", f_path_offset.to_s)
1701
1722
  .gsub("__VIVARIUM_DENTRY_D_NAME_OFFSET__", d_name_offset.to_s)
1702
1723
 
1703
- usdt_so_path = ENV.fetch("VIVARIUM_USDT_SO_PATH") { Vivarium.locate_vivarium_usdt_so }
1704
- usdt = RbBCC::USDT.new(path: usdt_so_path)
1705
- usdt.enable_probe(probe: "start_probe", fn_name: "on_span_start")
1706
- usdt.enable_probe(probe: "stop_probe", fn_name: "on_span_stop")
1707
- usdt.enable_probe(probe: "raise_probe", fn_name: "on_span_raise")
1724
+ usdt_so_paths = resolve_usdt_so_paths
1725
+ usdt_contexts = build_usdt_contexts(usdt_so_paths)
1726
+ program += build_span_probe_sources(usdt_contexts)
1708
1727
 
1709
- bpf = RbBCC::BCC.new(text: program, usdt_contexts: [usdt])
1728
+ bpf = RbBCC::BCC.new(text: program, usdt_contexts: usdt_contexts.map(&:last))
1710
1729
 
1711
1730
  attach_ssl_write_uprobe(bpf) if @ssl_trace
1712
1731
  attach_dlopen_uprobe(bpf) if @dlopen_trace
@@ -1722,20 +1741,117 @@ module Vivarium
1722
1741
  pin_map(config_spawned_targets, File.join(@pin_dir, "config_spawned_targets"))
1723
1742
  pin_map(events_ringbuf, File.join(@pin_dir, "events"))
1724
1743
 
1744
+ event_log = EventLog.new
1745
+ registry = Registry.new(config_root_targets, config_spawned_targets)
1746
+ start_ringbuf_poller(bpf, events_ringbuf, event_log)
1747
+
1748
+ @api_server = ApiServer.new(
1749
+ socket_path: @socket_path,
1750
+ event_log: event_log,
1751
+ registry: registry,
1752
+ daemon_pid: Process.pid
1753
+ )
1754
+ @api_server.start
1755
+
1725
1756
  puts "[vivariumd] started"
1726
1757
  puts "[vivariumd] pinned maps in #{@pin_dir}"
1727
1758
  puts "[vivariumd] watching LSM file_open (f_path offset=#{f_path_offset})"
1728
- puts "[vivariumd] USDT attached via #{usdt_so_path}"
1759
+ puts "[vivariumd] API listening on unix:#{@socket_path}"
1729
1760
 
1730
1761
  loop do
1731
1762
  sleep 1
1732
1763
  end
1733
1764
  rescue Interrupt
1734
1765
  puts "\n[vivariumd] stopping"
1766
+ ensure
1767
+ @api_server&.stop
1735
1768
  end
1736
1769
 
1737
1770
  private
1738
1771
 
1772
+ USDT_PROBES = [
1773
+ ["start_probe", "on_span_start"],
1774
+ ["stop_probe", "on_span_stop"],
1775
+ ["raise_probe", "on_span_raise"]
1776
+ ].freeze
1777
+
1778
+ def resolve_usdt_so_paths
1779
+ raw =
1780
+ if @usdt_so_paths && !@usdt_so_paths.empty?
1781
+ @usdt_so_paths
1782
+ else
1783
+ env = ENV["VIVARIUM_USDT_SO_PATH"]
1784
+ if env && !env.empty?
1785
+ env.split(File::PATH_SEPARATOR)
1786
+ else
1787
+ [Vivarium.locate_vivarium_usdt_so]
1788
+ end
1789
+ end
1790
+
1791
+ paths = raw.map(&:strip).reject(&:empty?).uniq
1792
+ existing = paths.select do |p|
1793
+ if File.exist?(p)
1794
+ true
1795
+ else
1796
+ warn "[vivariumd] USDT .so not found, skipping: #{p}"
1797
+ false
1798
+ end
1799
+ end
1800
+
1801
+ if existing.empty?
1802
+ raise Error, "no USDT .so files to attach " \
1803
+ "(set --usdt-so PATH or VIVARIUM_USDT_SO_PATH)"
1804
+ end
1805
+
1806
+ existing
1807
+ end
1808
+
1809
+ # Returns an array of [suffix, usdt] pairs. Each context gets a unique suffix
1810
+ # so its enabled probes map to distinct BPF fn_names, and build_span_probe_sources
1811
+ # emits matching handler definitions for exactly these suffixes.
1812
+ def build_usdt_contexts(paths)
1813
+ contexts = paths.each_with_index.filter_map do |path, idx|
1814
+ usdt = RbBCC::USDT.new(path: path)
1815
+ USDT_PROBES.each do |probe, fn_name|
1816
+ usdt.enable_probe(probe: probe, fn_name: "#{fn_name}_#{idx}")
1817
+ end
1818
+ puts "[vivariumd] USDT context attached via #{path}"
1819
+ [idx, usdt]
1820
+ rescue StandardError => e
1821
+ warn "[vivariumd] USDT attach failed for #{path}: #{e.class}: #{e.message}"
1822
+ nil
1823
+ end
1824
+
1825
+ if contexts.empty?
1826
+ raise Error, "failed to attach any USDT contexts"
1827
+ end
1828
+
1829
+ contexts
1830
+ end
1831
+
1832
+ def build_span_probe_sources(contexts)
1833
+ contexts.map do |idx, _usdt|
1834
+ SPAN_PROBE_TEMPLATE.gsub("__SUFFIX__", "_#{idx}")
1835
+ end.join("\n")
1836
+ end
1837
+
1838
+ def start_ringbuf_poller(bpf, events_ringbuf, event_log)
1839
+ events_ringbuf.open_ring_buffer do |_ctx, data, size|
1840
+ bytes = data[0, size].to_s.b
1841
+ bytes = bytes.ljust(EVENT_STRUCT_SIZE, "\x00") if bytes.bytesize < EVENT_STRUCT_SIZE
1842
+ event_log.append(bytes)
1843
+ 0
1844
+ end
1845
+
1846
+ @ringbuf_thread = Thread.new do
1847
+ loop do
1848
+ bpf.ring_buffer_poll(50)
1849
+ rescue StandardError => e
1850
+ warn "[vivariumd] ringbuf poll error: #{e.class}: #{e.message}"
1851
+ end
1852
+ end
1853
+ end
1854
+
1739
1855
  def attach_ssl_write_uprobe(bpf)
1740
1856
  path = resolve_libssl_path
1741
1857
  unless path
@@ -1949,8 +2065,8 @@ module Vivarium
1949
2065
  end
1950
2066
 
1951
2067
  class ObservationSession
1952
- def initialize(store:, pid:, tracer:, correlator:)
1953
- @store = store
2068
+ def initialize(client:, pid:, tracer:, correlator:)
2069
+ @client = client
1954
2070
  @pid = pid
1955
2071
  @tracer = tracer
1956
2072
  @correlator = correlator
@@ -1962,58 +2078,58 @@ module Vivarium
1962
2078
 
1963
2079
  @stopped = true
1964
2080
  @tracer.disable
1965
- @store.unregister_pid(@pid)
2081
+ @client.unregister(@pid)
1966
2082
  @correlator.stop
1967
2083
  end
1968
2084
  end
1969
2085
 
1970
- def self.observe(pin_dir: bpf_pin_dir, dest: $stdout, filter: nil, &block)
1971
- return scoped_observe(pin_dir: pin_dir, dest: dest, filter: filter, &block) if block_given?
2086
+ def self.observe(socket_path: self.socket_path, dest: $stdout, filter: nil, &block)
2087
+ if block_given?
2088
+ return scoped_observe(socket_path: socket_path, dest: dest, filter: filter, &block)
2089
+ end
1972
2090
 
1973
- top_observe(pin_dir: pin_dir, dest: dest, filter: filter)
2091
+ top_observe(socket_path: socket_path, dest: dest, filter: filter)
1974
2092
  end
1975
2093
 
1976
- def self.top_observe(pin_dir: bpf_pin_dir, dest: $stdout, filter: nil)
1977
- store = MapStore.new(pin_dir: pin_dir)
2094
+ def self.top_observe(socket_path: self.socket_path, dest: $stdout, filter: nil)
2095
+ client = DaemonClient.new(socket_path: socket_path)
1978
2096
  pid = Process.pid
1979
- store.register_pid(pid)
1980
-
1981
2097
  main_tid = gettid
1982
2098
 
1983
2099
  correlator = Correlator.new(
1984
- pin_dir: pin_dir,
2100
+ socket_path: socket_path,
1985
2101
  observer_pid: pid,
1986
2102
  main_tid: main_tid,
1987
2103
  filter: filter,
1988
2104
  dest: dest
1989
2105
  )
1990
2106
  correlator.start
2107
+ client.register(pid)
1991
2108
 
1992
2109
  tracer = build_observe_tracepoint
1993
2110
  tracer.enable
1994
2111
 
1995
2112
  session = ObservationSession.new(
1996
- store: store, pid: pid, tracer: tracer, correlator: correlator
2113
+ client: client, pid: pid, tracer: tracer, correlator: correlator
1997
2114
  )
1998
2115
  at_exit { session.stop }
1999
2116
  session
2000
2117
  end
2001
2118
 
2002
- def self.scoped_observe(pin_dir:, dest:, filter: nil)
2003
- store = MapStore.new(pin_dir: pin_dir)
2119
+ def self.scoped_observe(socket_path: self.socket_path, dest:, filter: nil)
2120
+ client = DaemonClient.new(socket_path: socket_path)
2004
2121
  pid = Process.pid
2005
- store.register_pid(pid)
2006
-
2007
2122
  main_tid = gettid
2008
2123
 
2009
2124
  correlator = Correlator.new(
2010
- pin_dir: pin_dir,
2125
+ socket_path: socket_path,
2011
2126
  observer_pid: pid,
2012
2127
  main_tid: main_tid,
2013
2128
  filter: filter,
2014
2129
  dest: dest
2015
2130
  )
2016
2131
  correlator.start
2132
+ client.register(pid)
2017
2133
 
2018
2134
  tracer = build_observe_tracepoint
2019
2135
  tracer.enable
@@ -2021,7 +2137,7 @@ module Vivarium
2021
2137
  yield
2022
2138
  ensure
2023
2139
  tracer&.disable
2024
- store&.unregister_pid(pid)
2140
+ client&.unregister(pid)
2025
2141
  correlator&.stop
2026
2142
  end
2027
2143
 
@@ -2048,13 +2164,24 @@ module Vivarium
2048
2164
  else
2049
2165
  "#{tp.defined_class}##{tp.method_id}"
2050
2166
  end
2051
- is_target = allowlist.include?(signature) || \
2167
+
2168
+ recv = tp.self
2169
+ mid = tp.method_id
2170
+ file_dir_name =
2171
+ if (recv.is_a?(Class) ? recv <= File : recv.is_a?(File)) && SPAN_FILE_METHODS.include?(mid)
2172
+ "File"
2173
+ elsif (recv.is_a?(Class) ? recv <= Dir : recv.is_a?(Dir)) && SPAN_DIR_METHODS.include?(mid)
2174
+ "Dir"
2175
+ end
2176
+
2177
+ is_target = !file_dir_name.nil? || \
2178
+ allowlist.include?(signature) || \
2052
2179
  allow_classes.any? { |klass| tp.defined_class == klass } || \
2053
2180
  allow_classes.any? { |klass| tp.defined_class == klass.singleton_class }
2054
2181
  next unless is_target
2055
2182
 
2056
2183
  file_arg = tail_fit_string(tp.path, SPAN_FILE_ARG_MAX)
2057
- span_class_name = tp.self.equal?(ENV) ? "ENV" : tp.defined_class.to_s
2184
+ span_class_name = tp.self.equal?(ENV) ? "ENV" : (file_dir_name || tp.defined_class.to_s)
2058
2185
  case tp.event
2059
2186
  when :call, :c_call
2060
2187
  Vivarium::Usdt.start(span_class_name, tp.method_id.to_s, file: file_arg, lineno: tp.lineno)
@@ -2089,13 +2216,18 @@ module Vivarium
2089
2216
  end
2090
2217
 
2091
2218
  def self.run_daemon!(argv = ARGV)
2092
- options = { pin_dir: bpf_pin_dir, ssl_trace: true, libssl_path: nil,
2219
+ options = { pin_dir: bpf_pin_dir, socket_path: socket_path, ssl_trace: true, libssl_path: nil,
2093
2220
  env_trace: true,
2094
- dlopen_trace: true, libc_path: nil }
2221
+ dlopen_trace: true, libc_path: nil, usdt_so_paths: [] }
2095
2222
  OptionParser.new do |opts|
2096
- opts.banner = "Usage: vivariumd [--pin-dir PATH] [--no-ssl-trace] [--libssl PATH] " \
2097
- "[--no-dlopen-trace] [--no-env-trace] [--libc PATH]"
2223
+ opts.banner = "Usage: vivariumd [--pin-dir PATH] [--socket PATH] [--no-ssl-trace] [--libssl PATH] " \
2224
+ "[--no-dlopen-trace] [--no-env-trace] [--libc PATH] [--usdt-so PATH ...]"
2225
+ opts.on("--usdt-so PATH", "USDT .so to attach (repeatable; " \
2226
+ "overrides VIVARIUM_USDT_SO_PATH)") do |v|
2227
+ options[:usdt_so_paths] << v
2228
+ end
2098
2229
  opts.on("--pin-dir PATH", "Pinned map directory") { |v| options[:pin_dir] = v }
2230
+ opts.on("--socket PATH", "Unix domain socket path for the HTTP API") { |v| options[:socket_path] = v }
2099
2231
  opts.on("--[no-]ssl-trace", "Attach OpenSSL SSL_write uprobe (default: enabled)") do |v|
2100
2232
  options[:ssl_trace] = v
2101
2233
  end
@@ -2115,15 +2247,19 @@ module Vivarium
2115
2247
 
2116
2248
  Daemon.new(
2117
2249
  pin_dir: options[:pin_dir],
2250
+ socket_path: options[:socket_path],
2118
2251
  ssl_trace: options[:ssl_trace],
2119
2252
  libssl_path: options[:libssl_path],
2120
2253
  dlopen_trace: options[:dlopen_trace],
2121
2254
  env_trace: options[:env_trace],
2122
- libc_path: options[:libc_path]
2255
+ libc_path: options[:libc_path],
2256
+ usdt_so_paths: options[:usdt_so_paths]
2123
2257
  ).run
2124
2258
  end
2125
2259
  end
2126
2260
 
2261
+ require_relative "vivarium/daemon_client"
2262
+ require_relative "vivarium/api_server"
2127
2263
  require_relative "vivarium/correlator"
2128
2264
  require_relative "vivarium/display_filter"
2129
2265
  require_relative "vivarium/tree_renderer"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vivarium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio Kondo
@@ -62,6 +62,7 @@ extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
64
  - CONTEXT.md
65
+ - LICENSE
65
66
  - README.md
66
67
  - Rakefile
67
68
  - examples/box_demo.rb
@@ -81,9 +82,11 @@ files:
81
82
  - exe/vivariumd
82
83
  - image.png
83
84
  - lib/vivarium.rb
85
+ - lib/vivarium/api_server.rb
84
86
  - lib/vivarium/box.rb
85
87
  - lib/vivarium/cli.rb
86
88
  - lib/vivarium/correlator.rb
89
+ - lib/vivarium/daemon_client.rb
87
90
  - lib/vivarium/display_filter.rb
88
91
  - lib/vivarium/http_decoder.rb
89
92
  - lib/vivarium/tree_renderer.rb
@@ -91,7 +94,8 @@ files:
91
94
  - logo-simple.png
92
95
  - sig/vivarium.rbs
93
96
  homepage: https://github.com/udzura/vivarium
94
- licenses: []
97
+ licenses:
98
+ - Apache-2.0
95
99
  metadata:
96
100
  homepage_uri: https://github.com/udzura/vivarium
97
101
  source_code_uri: https://github.com/udzura/vivarium