Class: OsCtld::ContainerControl::Runner
- Inherits:
-
Object
- Object
- OsCtld::ContainerControl::Runner
- Defined in:
- lib/osctld/container_control/runner.rb
Overview
Runner is run in a forked&execed process and under the container's user
Direct Known Subclasses
Commands::Exec::Runner, Commands::Freeze::Runner, Commands::GetHostname::Runner, Commands::Mount::Runner, Commands::Reboot::Runner, Commands::Runscript::Runner, Commands::State::Runner, Commands::Stop::Runner, Commands::StopByHalt::Runner, Commands::StopRunit::Runner, Commands::Unfreeze::Runner, Commands::Unmount::Runner, Commands::VethName::Runner, Commands::Wall::Runner, Commands::WithMountns::Runner, Commands::WithRootfs::Runner
Constant Summary collapse
- LXC_ATTACH_FLAGS =
osctld uses ruby-lxc attach for in-process maintenance blocks, not for launching untrusted commands. Keep only personality setup: cgroup moves fail from the already-placed runner on cgroup v2, dropped capabilities break netns maintenance, and LSM exec transitions are irrelevant without execve().
LXC::LXC_ATTACH_SET_PERSONALITY
Instance Attribute Summary collapse
-
#ctid ⇒ Object
readonly
Returns the value of attribute ctid.
-
#log_file ⇒ Object
readonly
Returns the value of attribute log_file.
-
#lxc_home ⇒ Object
readonly
Returns the value of attribute lxc_home.
-
#network_socket ⇒ Object
readonly
protected
Returns the value of attribute network_socket.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#stderr ⇒ Object
readonly
protected
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
protected
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
protected
Returns the value of attribute stdout.
-
#user_home ⇒ Object
readonly
Returns the value of attribute user_home.
Instance Method Summary collapse
- #error(msg) ⇒ Object protected
-
#execute(*args, **kwargs) ⇒ Hash
Implement this method.
- #exitstatus(status) ⇒ Object protected
-
#initialize(**opts) ⇒ Runner
constructor
A new instance of Runner.
- #lxc_attach_command(cmd, stdout:, stderr:, stdin: nil) ⇒ Object protected
- #lxc_attach_wait(timeout: nil, **opts, &block) ⇒ Object protected
- #lxc_attach_wait_for_exit(attach_opts) ⇒ Object protected
- #lxc_ct ⇒ Object protected
- #ok(out = nil) ⇒ Object protected
- #reap_interrupted_lxc_attach(pid) ⇒ Object protected
- #setup_exec_env ⇒ Object protected
- #setup_exec_run_env ⇒ Object protected
- #system_path ⇒ Object protected
- #wait_for_lxc_attachable(timeout: 10) ⇒ Object protected
- #wait_for_lxc_stopped(timeout: 10) ⇒ Object protected
- #wait_for_path(path, timeout: 10, interval: 0.1) ⇒ Object protected
- #wait_for_process(pid, timeout:) ⇒ Object protected
- #wait_status_exitstatus(status) ⇒ Object protected
Constructor Details
#initialize(**opts) ⇒ Runner
Returns a new instance of Runner.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/osctld/container_control/runner.rb', line 24 def initialize(**opts) @pool = opts[:pool] @ctid = opts[:id] @lxc_home = opts[:lxc_home] @user_home = opts[:user_home] @log_file = opts[:log_file] @stdin = opts[:stdin] @stdout = opts[:stdout] @stderr = opts[:stderr] @network_socket = opts[:network_socket] end |
Instance Attribute Details
#ctid ⇒ Object (readonly)
Returns the value of attribute ctid.
13 14 15 |
# File 'lib/osctld/container_control/runner.rb', line 13 def ctid @ctid end |
#log_file ⇒ Object (readonly)
Returns the value of attribute log_file.
13 14 15 |
# File 'lib/osctld/container_control/runner.rb', line 13 def log_file @log_file end |
#lxc_home ⇒ Object (readonly)
Returns the value of attribute lxc_home.
13 14 15 |
# File 'lib/osctld/container_control/runner.rb', line 13 def lxc_home @lxc_home end |
#network_socket ⇒ Object (readonly, protected)
Returns the value of attribute network_socket.
46 47 48 |
# File 'lib/osctld/container_control/runner.rb', line 46 def network_socket @network_socket end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
13 14 15 |
# File 'lib/osctld/container_control/runner.rb', line 13 def pool @pool end |
#stderr ⇒ Object (readonly, protected)
Returns the value of attribute stderr.
46 47 48 |
# File 'lib/osctld/container_control/runner.rb', line 46 def stderr @stderr end |
#stdin ⇒ Object (readonly, protected)
Returns the value of attribute stdin.
46 47 48 |
# File 'lib/osctld/container_control/runner.rb', line 46 def stdin @stdin end |
#stdout ⇒ Object (readonly, protected)
Returns the value of attribute stdout.
46 47 48 |
# File 'lib/osctld/container_control/runner.rb', line 46 def stdout @stdout end |
#user_home ⇒ Object (readonly)
Returns the value of attribute user_home.
13 14 15 |
# File 'lib/osctld/container_control/runner.rb', line 13 def user_home @user_home end |
Instance Method Details
#error(msg) ⇒ Object (protected)
52 53 54 |
# File 'lib/osctld/container_control/runner.rb', line 52 def error(msg) { status: false, message: msg } end |
#execute(*args, **kwargs) ⇒ Hash
Implement this method
40 41 42 |
# File 'lib/osctld/container_control/runner.rb', line 40 def execute(*args, **kwargs) raise NotImplementedError end |
#exitstatus(status) ⇒ Object (protected)
217 218 219 220 221 222 223 |
# File 'lib/osctld/container_control/runner.rb', line 217 def exitstatus(status) return wait_status_exitstatus(status) if status.is_a?(Integer) return status.exitstatus if status.exited? return 128 + status.termsig if status.signaled? 1 end |
#lxc_attach_command(cmd, stdout:, stderr:, stdin: nil) ⇒ Object (protected)
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 |
# File 'lib/osctld/container_control/runner.rb', line 127 def lxc_attach_command(cmd, stdout:, stderr:, stdin: nil) pid = Process.fork do if stdin $stdin.reopen(stdin) else $stdin.close end $stdout.reopen(stdout) $stderr.reopen(stderr) setup_exec_env Process.exec( 'lxc-attach', '-P', lxc_home, '-n', ctid, '--elevated-privileges=CGROUP', '--clear-env', '--keep-var=TERM', "--set-var=PATH=#{system_path.join(':')}", '--set-var=HOME=/root', '--set-var=USER=root', '--', *Array(cmd) ) end _, status = Process.wait2(pid) exitstatus(status) end |
#lxc_attach_wait(timeout: nil, **opts, &block) ⇒ Object (protected)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/osctld/container_control/runner.rb', line 75 def lxc_attach_wait(timeout: nil, **opts, &block) attach_opts = { flags: LXC_ATTACH_FLAGS }.merge(opts).merge(wait: false) return lxc_attach_wait_for_exit(attach_opts, &block) unless timeout pid = Process.fork do Process.setpgrp exit!(lxc_attach_wait_for_exit(attach_opts, &block)) end begin Process.setpgid(pid, pid) rescue Errno::EACCES, Errno::ESRCH # The child either established its own group or exited first. end status = wait_for_process(pid, timeout:) status ? exitstatus(status) : 124 end |
#lxc_attach_wait_for_exit(attach_opts) ⇒ Object (protected)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/osctld/container_control/runner.rb', line 95 def lxc_attach_wait_for_exit(attach_opts, &) # ruby-lxc's wait mode uses a SIGKILL unblock callback. A Ruby interrupt # must not kill a healthy attached process just to wake the waiting VM. pid = lxc_ct.attach(attach_opts, &) return 1 unless pid > 0 _, status = Process.waitpid2(pid) # Preserve ruby-lxc's normalization of an attach setup failure. return 1 if status.exited? && status.exitstatus == 255 exitstatus(status) rescue Errno::ECHILD 1 ensure reap_interrupted_lxc_attach(pid) if pid && pid > 0 && status.nil? end |
#lxc_ct ⇒ Object (protected)
56 57 58 |
# File 'lib/osctld/container_control/runner.rb', line 56 def lxc_ct @lxc_ct ||= LXC::Container.new(ctid, lxc_home) end |
#ok(out = nil) ⇒ Object (protected)
48 49 50 |
# File 'lib/osctld/container_control/runner.rb', line 48 def ok(out = nil) { status: true, output: out } end |
#reap_interrupted_lxc_attach(pid) ⇒ Object (protected)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/osctld/container_control/runner.rb', line 112 def reap_interrupted_lxc_attach(pid) # An already-reaped PID is not ours to signal. A live, waitable child # cannot have its PID reused before this runner collects its status. return if Process.waitpid2(pid, Process::WNOHANG) begin Process.kill('KILL', pid) rescue Errno::ESRCH # Still collect the status if the child exited before the signal. end Process.waitpid2(pid) rescue Errno::ECHILD nil end |
#setup_exec_env ⇒ Object (protected)
64 65 66 67 68 |
# File 'lib/osctld/container_control/runner.rb', line 64 def setup_exec_env ENV.delete_if { |k, _| k != 'TERM' } ENV['PATH'] = system_path.join(':') ENV['HOME'] = user_home end |
#setup_exec_run_env ⇒ Object (protected)
70 71 72 73 |
# File 'lib/osctld/container_control/runner.rb', line 70 def setup_exec_run_env setup_exec_env ENV['PATH'] = ['/run/wrappers/bin', ENV.fetch('PATH', nil)].join(':') end |
#system_path ⇒ Object (protected)
60 61 62 |
# File 'lib/osctld/container_control/runner.rb', line 60 def system_path SwitchUser::SYSTEM_PATH end |
#wait_for_lxc_attachable(timeout: 10) ⇒ Object (protected)
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/osctld/container_control/runner.rb', line 165 def wait_for_lxc_attachable(timeout: 10) deadline = Time.now + timeout loop do init_pid = lxc_ct.init_pid return init_pid if lxc_ct.running? && init_pid && init_pid > 0 return false if Time.now >= deadline sleep(0.1) end end |
#wait_for_lxc_stopped(timeout: 10) ⇒ Object (protected)
159 160 161 162 163 |
# File 'lib/osctld/container_control/runner.rb', line 159 def wait_for_lxc_stopped(timeout: 10) deadline = Time.now + timeout sleep(0.1) while lxc_ct.running? && Time.now < deadline end |
#wait_for_path(path, timeout: 10, interval: 0.1) ⇒ Object (protected)
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/osctld/container_control/runner.rb', line 177 def wait_for_path(path, timeout: 10, interval: 0.1) deadline = Time.now + timeout loop do return true if File.exist?(path) return false if Time.now >= deadline sleep(interval) end end |
#wait_for_process(pid, timeout:) ⇒ Object (protected)
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 |
# File 'lib/osctld/container_control/runner.rb', line 188 def wait_for_process(pid, timeout:) deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout loop do waited_pid, status = Process.wait2(pid, Process::WNOHANG) return status if waited_pid remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) break if remaining <= 0 sleep([remaining, 0.05].min) end begin Process.kill('KILL', -pid) rescue Errno::ESRCH begin Process.kill('KILL', pid) rescue Errno::ESRCH nil end end Process.wait(pid) nil rescue Errno::ECHILD nil end |
#wait_status_exitstatus(status) ⇒ Object (protected)
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/osctld/container_control/runner.rb', line 225 def wait_status_exitstatus(status) return 1 if status < 0 term_sig = status & 0x7f if term_sig == 0 (status >> 8) & 0xff elsif term_sig == 0x7f 1 else 128 + term_sig end end |