Class: OsCtl::ExportFS::Operations::Server::Exec
- Defined in:
- lib/osctl/exportfs/operations/server/exec.rb
Overview
Attach to the server's namespaces and run an arbitrary code block
A new process is forked and attached to namespaces of the server's init process. The code block is run in the forked process.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
protected
Returns the value of attribute block.
-
#cgroup ⇒ Object
readonly
protected
Returns the value of attribute cgroup.
-
#server ⇒ Object
readonly
protected
Returns the value of attribute server.
-
#sys ⇒ Object
readonly
protected
Returns the value of attribute sys.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(server, &block) ⇒ Exec
constructor
A new instance of Exec.
Methods inherited from Base
Constructor Details
Instance Attribute Details
#block ⇒ Object (readonly, protected)
Returns the value of attribute block.
53 54 55 |
# File 'lib/osctl/exportfs/operations/server/exec.rb', line 53 def block @block end |
#cgroup ⇒ Object (readonly, protected)
Returns the value of attribute cgroup.
53 54 55 |
# File 'lib/osctl/exportfs/operations/server/exec.rb', line 53 def cgroup @cgroup end |
#server ⇒ Object (readonly, protected)
Returns the value of attribute server.
53 54 55 |
# File 'lib/osctl/exportfs/operations/server/exec.rb', line 53 def server @server end |
#sys ⇒ Object (readonly, protected)
Returns the value of attribute sys.
53 54 55 |
# File 'lib/osctl/exportfs/operations/server/exec.rb', line 53 def sys @sys end |
Instance Method Details
#execute ⇒ Object
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 |
# File 'lib/osctl/exportfs/operations/server/exec.rb', line 18 def execute unless server.running? fail 'the server is not running' end pid = server.read_pid namespaces = { 'mnt' => OsCtl::Lib::Sys::CLONE_NEWNS, 'net' => OsCtl::Lib::Sys::CLONE_NEWNET, 'uts' => OsCtl::Lib::Sys::CLONE_NEWUTS, 'ipc' => OsCtl::Lib::Sys::CLONE_NEWIPC, 'pid' => OsCtl::Lib::Sys::CLONE_NEWPID, } ios = {} main = Process.fork do cgroup.enter_payload namespaces.each do |ns, type| ios[ns] = File.open(File.join('/proc', pid.to_s, 'ns', ns), 'r') end namespaces.each do |ns, type| sys.setns_io(ios[ns], type) ios[ns].close end pid = Process.fork { block.call } Process.wait(pid) end Process.wait(main) end |