hrr_rb_lxns 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e3cc1b893875690893c0ac182dfcee29ffca9a81a57f045afd45cc14c1c6501
4
- data.tar.gz: 92354baeca2cad1967283dd4da71bd9e94df231ab5b9d8d2315b878dbeb7f9e6
3
+ metadata.gz: e8243b5243363dd383204313c4487478786834e46ff9a3c1be7f90b2c5f77052
4
+ data.tar.gz: daa322824dc8a8c75a80213d37739b44e223a09647d73502c5b448b014629df1
5
5
  SHA512:
6
- metadata.gz: 45dfd00ee670af333b10cd6600b98f021f4f6570c0f26ffe0def48f8fb384c3c24c65612d0f28feb15e0bb502a8ecd96d04a3592dbe5f0e5bbc34a2d64ac6632
7
- data.tar.gz: 6c1880e6ff91a7dad30b59396ae1273a814c88006e2c68d3bd4f96580fb84dd68631ac89400418468ba49c3af2e2d7910823063cad668eccad0efc76c1644e3c
6
+ metadata.gz: 948f4793805ee19b20cd5da1fc1f9acff00c065829708bc9f1fb26e947917214e480b6ad8c86d955863dcb0e10a5413f335fa9270dcdf2c816f6ba0d4d2e12a6
7
+ data.tar.gz: 3a74b885cff6c12dcd179b6ee697d4e0da73f078f6dab6e3de8358e913ef0caa556ad104d05ff188eec848b3e804862fcb818e9da68cd7b0d64c59697347825b
data/Gemfile CHANGED
@@ -2,6 +2,6 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem "rake", "~> 12.0"
5
+ gem "rake", ">= 12.0"
6
6
  gem "rake-compiler"
7
7
  gem "rspec", "~> 3.0"
data/README.md CHANGED
@@ -68,6 +68,15 @@ HrrRbLxns.setns HrrRbLxns::NEWUTS, pid # => 0
68
68
  File.readlink "/proc/self/ns/uts" # => uts:[yyy]
69
69
  ```
70
70
 
71
+ HrrRbLxns.setns can associate namespaces using files which specify namespaces, instead of specifying pid. The files are specified in an options hash. The keys which are available in the hash for each namespace are `:mount`, `:uts`, `:ipc`, `:network`, `:pid`, `:user`, `:cgroup`, and `:time`.
72
+
73
+ ```ruby
74
+ # Create a network namespace using the ip netns command. The command generates "/run/netns/ns0" file, which is a bind-mounted namespace file.
75
+ system "ip netns add ns0"
76
+ # Then associate with the network namespace with specifying the file instead of pid.
77
+ HrrRbLxns.setns HrrRbLxns::NETNET, nil, {network: "/run/netns/ns0"}
78
+ ```
79
+
71
80
  ## Note
72
81
 
73
82
  Some of the namespace operations are not multi-thread friendly. The library expects that only main thread is running before unshare or setns operation.
data/lib/hrr_rb_lxns.rb CHANGED
@@ -58,7 +58,7 @@ module HrrRbLxns
58
58
  # HrrRbLxns.setns HrrRbLxns::NEWUTS, pid # => 0
59
59
  # File.readlink "/proc/self/ns/uts" # => uts:[yyy]
60
60
  #
61
- # @param flags [Integer] An integer value that represents namespaces to disassociate.
61
+ # @param flags [Integer] An integer value that represents namespaces to associate.
62
62
  # @param flags [String] A string that represents namespaces. The mapping of charactors and flags are: <br>
63
63
  # "i" : NEWIPC <br>
64
64
  # "m" : NEWNS <br>
@@ -68,21 +68,31 @@ module HrrRbLxns
68
68
  # "U" : NEWUSER <br>
69
69
  # "C" : NEWCGROUP <br>
70
70
  # "T" : NEWTIME <br>
71
- # @param options [Hash] For future use.
71
+ # @param pid [Integer] Specifies a target process('s namespace) which the caller is to associate with. The paths specifying namespaces specified by pid are: <br>
72
+ # /proc/pid/ns/mnt : mount namespace <br>
73
+ # /proc/pid/ns/uts : uts namespace <br>
74
+ # /proc/pid/ns/ipc : ipc namespace <br>
75
+ # /proc/pid/ns/net : network namespace <br>
76
+ # /proc/pid/ns/pid : pid namespace <br>
77
+ # /proc/pid/ns/user : user namespace <br>
78
+ # /proc/pid/ns/cgroup : cgroup namespace <br>
79
+ # /proc/pid/ns/time : time namespace <br>
80
+ # @param options [Hash] Optional arguments.
81
+ # @option options [String] :mount A file which specifies the mount namespace to associate with.
82
+ # @option options [String] :uts A file which specifies the uts namespace to associate with.
83
+ # @option options [String] :ipc A file which specifies the ipc namespace to associate with.
84
+ # @option options [String] :network A file which specifies the network namespace to associate with.
85
+ # @option options [String] :pid A file which specifies the pid namespace to associate with.
86
+ # @option options [String] :user A file which specifies the user namespace to associate with.
87
+ # @option options [String] :cgroup A file which specifies the cgroup namespace to associate with.
88
+ # @option options [String] :time A file which specifies the time namespace to associate with.
72
89
  # @return [Integer] 0.
73
- # @raise [ArgumentError] When given flags argument is not appropriate.
90
+ # @raise [ArgumentError] When given flags argument is not appropriate or when given pid and/or options are not appropriate for the given flags.
74
91
  # @raise [Errno::EXXX] In case setns(2) system call failed.
75
92
  def self.setns flags, pid, options={}
76
93
  _flags = interpret_flags flags
77
- fds = get_fds _flags, pid
78
- fds.each do |path, nstype|
79
- begin
80
- fd = File.open(path, File::RDONLY)
81
- __setns__ fd.fileno, nstype
82
- ensure
83
- fd.close rescue nil
84
- end
85
- end
94
+ nstype_file_h = get_nstype_file_h _flags, pid, options
95
+ do_setns nstype_file_h
86
96
  end
87
97
 
88
98
  private
@@ -110,27 +120,41 @@ module HrrRbLxns
110
120
  end
111
121
  end
112
122
 
113
- def self.get_fds flags, pid
123
+ def self.do_setns nstype_file_h
124
+ nstype_file_h.each do |nstype, file|
125
+ File.open(file, File::RDONLY) do |f|
126
+ __setns__ f.fileno, nstype
127
+ end
128
+ end
129
+ end
130
+
131
+ def self.get_nstype_file_h flags, pid, options
114
132
  list = Array.new
115
- list.push ["ipc", NEWIPC ] if const_defined?(:NEWIPC)
116
- list.push ["mnt", NEWNS ] if const_defined?(:NEWNS)
117
- list.push ["net", NEWNET ] if const_defined?(:NEWNET)
118
- list.push ["pid", NEWPID ] if const_defined?(:NEWPID)
119
- list.push ["uts", NEWUTS ] if const_defined?(:NEWUTS)
120
- list.push ["user", NEWUSER ] if const_defined?(:NEWUSER)
121
- list.push ["cgroup", NEWCGROUP] if const_defined?(:NEWCGROUP)
122
- list.push ["time", NEWTIME ] if const_defined?(:NEWTIME)
123
- fds = Array.new
124
- list.each do |name, flag|
125
- fd = get_fd name, (flags & flag), pid
126
- fds.push [fd, flag] if fd
133
+ list.push ["ipc", NEWIPC, :ipc ] if const_defined?(:NEWIPC)
134
+ list.push ["mnt", NEWNS, :mount ] if const_defined?(:NEWNS)
135
+ list.push ["net", NEWNET, :network] if const_defined?(:NEWNET)
136
+ list.push ["pid", NEWPID, :pid ] if const_defined?(:NEWPID)
137
+ list.push ["uts", NEWUTS, :uts ] if const_defined?(:NEWUTS)
138
+ list.push ["user", NEWUSER, :user ] if const_defined?(:NEWUSER)
139
+ list.push ["cgroup", NEWCGROUP, :cgroup ] if const_defined?(:NEWCGROUP)
140
+ list.push ["time", NEWTIME, :time ] if const_defined?(:NEWTIME)
141
+ nstype_file_h = Hash.new
142
+ list.each do |name, flag, key|
143
+ file = get_file name, (flags & flag), pid, key, options[key]
144
+ nstype_file_h[flag] = file if file
127
145
  end
128
- fds
146
+ nstype_file_h
129
147
  end
130
148
 
131
- def self.get_fd name, flag, pid
132
- if flag.zero?.! && pid
133
- "/proc/#{pid}/ns/#{name}"
149
+ def self.get_file name, flag, pid, key, option
150
+ if flag.zero?.!
151
+ if option
152
+ option
153
+ elsif pid
154
+ "/proc/#{pid}/ns/#{name}"
155
+ else
156
+ raise ArgumentError, "neither pid nor options[:#{key}] specified for #{key} namespace"
157
+ end
134
158
  else
135
159
  nil
136
160
  end
@@ -1,3 +1,3 @@
1
1
  module HrrRbLxns
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hrr_rb_lxns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hirura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-20 00:00:00.000000000 Z
11
+ date: 2020-04-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Utilities working with Linux namespaces for CRuby.
14
14
  email: