Class: OsCtld::ContainerControl::Frontend
- Inherits:
-
Object
- Object
- OsCtld::ContainerControl::Frontend
- Defined in:
- lib/osctld/container_control/frontend.rb
Overview
Frontend is run from osctld in daemon mode, when it is running as root
Direct Known Subclasses
Commands::Exec::Frontend, Commands::Freeze::Frontend, Commands::GetHostname::Frontend, Commands::Mount::Frontend, Commands::Reboot::Frontend, Commands::Runscript::Frontend, Commands::State::Frontend, Commands::Stop::Frontend, Commands::StopByHalt::Frontend, Commands::StopRunit::Frontend, Commands::Syscmd::Frontend, Commands::Unfreeze::Frontend, Commands::Unmount::Frontend, Commands::VethName::Frontend, Commands::Wall::Frontend, Commands::WithMountns::Frontend, Commands::WithRootfs::Frontend
Constant Summary collapse
- FAILED_RUNNER_GRACE =
5
Instance Attribute Summary collapse
- #command_class ⇒ Class readonly
- #ct ⇒ Container readonly
Instance Method Summary collapse
- #cleanup_runner_cgroup(cgroup_path) ⇒ Object protected
-
#exec_runner(opts = {}) ⇒ ContainerControl::Result
protected
Fork&exec to the container user and invoke the runner.
-
#execute(*args, **kwargs) ⇒ Object
Implement this method.
-
#fork_runner(opts = {}) ⇒ ContainerControl::Result
protected
Fork to the container user and invoke the runner.
-
#initialize(command_class, ct) ⇒ Frontend
constructor
A new instance of Frontend.
-
#reap_failed_runner(pid) ⇒ Object
protected
Only our unreaped direct child is addressed here, so its numeric PID cannot be reused before waitpid.
- #runner_result(payload) ⇒ Object protected
- #write_runner_failure(io, error, stage: :setup) ⇒ Object protected
Constructor Details
#initialize(command_class, ct) ⇒ Frontend
Returns a new instance of Frontend.
17 18 19 20 |
# File 'lib/osctld/container_control/frontend.rb', line 17 def initialize(command_class, ct) @command_class = command_class @ct = ct end |
Instance Attribute Details
#command_class ⇒ Class (readonly)
10 11 12 |
# File 'lib/osctld/container_control/frontend.rb', line 10 def command_class @command_class end |
#ct ⇒ Container (readonly)
13 14 15 |
# File 'lib/osctld/container_control/frontend.rb', line 13 def ct @ct end |
Instance Method Details
#cleanup_runner_cgroup(cgroup_path) ⇒ Object (protected)
235 236 237 238 239 |
# File 'lib/osctld/container_control/frontend.rb', line 235 def cleanup_runner_cgroup(cgroup_path) CGroup.rmpath_all(cgroup_path) rescue SystemCallError => e ct.log(:warn, "Unable to remove runner cgroup #{cgroup_path}: #{e.}") end |
#exec_runner(opts = {}) ⇒ ContainerControl::Result (protected)
Fork&exec to the container user and invoke the runner.
#exec_runner forks from osctld and then execs into osctld-ct-runner. The runner then switches to the container's user and enters its cgroups. This runner is safe to use when you need to attach to the container, e.g. with LXC::Container#attach.
It is however more costly than #fork_runner as it makes the Ruby runtime to start all over again. Use #fork_runner when you don't need to attach to the container.
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 |
# File 'lib/osctld/container_control/frontend.rb', line 51 def exec_runner(opts = {}) transient_network = ContainerControl::TransientNetwork.new(ct) if opts[:transient_network] network_socket = transient_network&.runner_socket # Used to send command to the runner cmd_r, cmd_w = IO.pipe # Used to read return value ret_r, ret_w = IO.pipe # File descriptors to capture output/feed input stdin = opts[:stdin] stdout = opts.fetch(:stdout, $stdout) stderr = opts.fetch(:stderr, $stderr) # User configuration sysuser = ct.user.sysusername ugid = ct.user.ugid homedir = ct.user.homedir prlimits = ct.prlimits.export switch_extra_namespaces = opts.fetch(:switch_extra_namespaces, true) cgroup_path = opts.fetch(:cgroup_path) do if switch_extra_namespaces ct.entry_cgroup_path else # A finishing helper must not remove another helper's prepared leaf. # Keep this a sibling: other callers still attach processes directly # to ct.attach_cgroup_path, which must not become an internal v2 node. "#{ct.attach_cgroup_path}.#{SecureRandom.hex(16)}" end end cleanup_cgroup_path = !switch_extra_namespaces && !opts.has_key?(:cgroup_path) if switch_extra_namespaces # This path creates a boundary only for a new transient run. Running # attachment belongs to LXC's pidfd transition, never root setns of a # late numeric PID (or separate syslog/tracing namespace descriptors). raise ContainerControl::Error, 'container started before transient helper launch' if ct.init_pid syslogns_tag = ct.syslogns_tag(run_id: ct.run_conf&.run_id) end # Runner configuration runner_opts = { name: command_class.name, pool: ct.pool.name, id: ct.id, lxc_home: ct.lxc_home, user_home: ct.user.homedir, log_file: ct.log_path, args: opts.fetch(:args, []), kwargs: opts.fetch(:kwargs, {}), return: ret_w.fileno, network_socket: network_socket&.fileno, stdin: stdin && stdin.fileno, stdout: stdout.fileno, stderr: stderr.fileno } CGroup.mkpath_all( cgroup_path.split('/'), chown: ugid, delegate_existing: false ) # On cgroup v2, we must reset subtree control for lxc-execute to work. # The subtree control is configured by osctld when creating the entry_cgroup_path, # which is a bit unfortunate in this case. if opts.fetch(:reset_subtree_control, false) && CGroup.v2? CGroup.reset_subtree_control(ct.abs_cgroup_path(nil)) end pid = SwitchUser.fork( keep_fds: [ cmd_r, ret_w, stdin, stdout, stderr, network_socket ].compact ) do # Closed by SwitchUser.fork # cmd_w.close # ret_r.close $stdin.reopen(cmd_r) [cmd_r, ret_w, stdin, stdout, stderr, network_socket].compact.each do |io| io.close_on_exec = false end SwitchUser.apply_prlimits(Process.pid, prlimits) SwitchUser.switch_to( sysuser, ugid, homedir, cgroup_path, syslogns_tag: ) Process.exec(::OsCtld.bin('osctld-ct-runner')) rescue StandardError => e write_runner_failure(ret_w, e) exit(false) end stdin.close if stdin stdout.close if stdout != $stdout stderr.close if stderr != $stderr # The child blocks on its command pipe until this exact identity is # pinned. Numeric PID reuse therefore cannot select another helper. runner_identity = ProcessIdentity.open(pid) if transient_network network_socket&.close cmd_w.write(runner_opts.to_json) cmd_w.close ret_w.close if transient_network && !transient_network.serve(runner_identity) return ContainerControl::Result.new(false, message: 'transient network setup failed') end begin ret = JSON.parse(ret_r.readline, symbolize_names: true) runner_responded = true runner_result(ret) rescue EOFError ContainerControl::Result.new( false, message: 'helper exited without a response' ) end ensure transient_network&.close # Release a helper still waiting for its command when pinning or command # transport fails, then reap it just as on the successful result path. [cmd_r, cmd_w, ret_r, ret_w].compact.each { |io| io.close unless io.closed? } if pid begin if transient_network && !runner_responded reap_failed_runner(pid) else Process.wait(pid) end ensure cleanup_runner_cgroup(cgroup_path) if cleanup_cgroup_path runner_identity&.close end else cleanup_runner_cgroup(cgroup_path) if cleanup_cgroup_path runner_identity&.close end end |
#execute(*args, **kwargs) ⇒ Object
Implement this method
25 26 27 |
# File 'lib/osctld/container_control/frontend.rb', line 25 def execute(*args, **kwargs) raise NotImplementedError end |
#fork_runner(opts = {}) ⇒ ContainerControl::Result (protected)
Fork to the container user and invoke the runner.
#fork_runner can be used only when we do not need to enter the container itself. It does not attach to its cgroups, because a forked osctld can have a large memory footprint, which we do not want to charge to the container. It can be used only to interact with LXC from the outside.
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 |
# File 'lib/osctld/container_control/frontend.rb', line 258 def fork_runner(opts = {}) r, w = IO.pipe stdin = opts[:stdin] stdout = opts.fetch(:stdout, $stdout) stderr = opts.fetch(:stderr, $stderr) keep_fds = Array(opts[:keep_fds]) runner_opts = { id: ct.id, lxc_home: ct.lxc_home, user_home: ct.user.homedir, log_file: ct.log_path, stdin:, stdout:, stderr: } ctid = ct.ident args = opts.fetch(:args, []) kwargs = opts.fetch(:kwargs, {}) sysuser = ct.user.sysusername ugid = ct.user.ugid homedir = ct.user.homedir pid = SwitchUser.fork(keep_fds: ([w, stdin, stdout, stderr] + keep_fds).compact) do # Closed by SwitchUser.fork # r.close begin Process.setproctitle( "osctld: #{ctid} " \ "runner:#{command_class.name.split('::').last.downcase}" ) if opts.fetch(:switch_to_system, true) SwitchUser.switch_to_system(sysuser, ugid, ugid, homedir) end runner = command_class::Runner.new(**runner_opts) rescue StandardError => e write_runner_failure(w, e, stage: :setup) exit(false) end begin ret = runner.execute(*args, **kwargs) rescue StandardError => e write_runner_failure(w, e, stage: :execution) exit(false) end begin w.write("#{ret.to_json}\n") rescue StandardError => e write_runner_failure(w, e, stage: :response) exit(false) end end w.close begin ret = JSON.parse(r.readline, symbolize_names: true) Process.wait(pid) runner_result(ret) rescue EOFError Process.wait(pid) ContainerControl::Result.new( false, message: 'helper exited without a response' ) end ensure r&.close unless r&.closed? w&.close unless w&.closed? end |
#reap_failed_runner(pid) ⇒ Object (protected)
Only our unreaped direct child is addressed here, so its numeric PID cannot be reused before waitpid. Never impose this bound on a successfully started user payload: it applies only after startup/transport failure.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/osctld/container_control/frontend.rb', line 211 def reap_failed_runner(pid) deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + FAILED_RUNNER_GRACE loop do return if Process.waitpid(pid, Process::WNOHANG) break if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline sleep(0.05) end Process.kill('TERM', pid) deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + FAILED_RUNNER_GRACE loop do return if Process.waitpid(pid, Process::WNOHANG) break if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline sleep(0.05) end Process.kill('KILL', pid) Process.wait(pid) rescue Errno::ECHILD nil end |
#runner_result(payload) ⇒ Object (protected)
342 343 344 345 |
# File 'lib/osctld/container_control/frontend.rb', line 342 def runner_result(payload) ct.log(:warn, payload[:diagnostic]) if payload[:diagnostic] ContainerControl::Result.from_runner(payload) end |
#write_runner_failure(io, error, stage: :setup) ⇒ Object (protected)
336 337 338 339 340 |
# File 'lib/osctld/container_control/frontend.rb', line 336 def write_runner_failure(io, error, stage: :setup) io.write("#{ContainerControl::Result.failure_payload(error, stage:).to_json}\n") rescue SystemCallError, IOError nil end |