hrr_rb_lxns 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e3cc1b893875690893c0ac182dfcee29ffca9a81a57f045afd45cc14c1c6501
4
+ data.tar.gz: 92354baeca2cad1967283dd4da71bd9e94df231ab5b9d8d2315b878dbeb7f9e6
5
+ SHA512:
6
+ metadata.gz: 45dfd00ee670af333b10cd6600b98f021f4f6570c0f26ffe0def48f8fb384c3c24c65612d0f28feb15e0bb502a8ecd96d04a3592dbe5f0e5bbc34a2d64ac6632
7
+ data.tar.gz: 6c1880e6ff91a7dad30b59396ae1273a814c88006e2c68d3bd4f96580fb84dd68631ac89400418468ba49c3af2e2d7910823063cad668eccad0efc76c1644e3c
@@ -0,0 +1,20 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ mkmf.log
14
+
15
+ # rspec failure tracking
16
+ .rspec_status
17
+
18
+ /.ruby-version
19
+ /Gemfile.lock
20
+ /vendor
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,29 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2
8
+ - 2.3
9
+ - 2.4
10
+ - 2.5
11
+ - 2.6
12
+ - 2.7
13
+ - ruby-head
14
+ jobs:
15
+ allow_failures:
16
+ - rvm: ruby-head
17
+ before_install:
18
+ - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
19
+ - gem install bundler -v '< 2'
20
+ - yes | gem update --system --force
21
+ - gem update bundler
22
+ install:
23
+ - bundle install
24
+ before_script:
25
+ - sudo env RBENV_VERSION="${RBENV_VERSION}" PATH="${RBENV_ROOT}/shims:${RBENV_ROOT}/bin:${PATH}" bundle exec rake clean
26
+ script:
27
+ - sudo env RBENV_VERSION="${RBENV_VERSION}" PATH="${RBENV_ROOT}/shims:${RBENV_ROOT}/bin:${PATH}" bundle exec rake compile spec
28
+ after_script:
29
+ - sudo env RBENV_VERSION="${RBENV_VERSION}" PATH="${RBENV_ROOT}/shims:${RBENV_ROOT}/bin:${PATH}" bundle exec rake clean
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hirura@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "rake-compiler"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 hirura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,102 @@
1
+ # HrrRbLxns
2
+
3
+ [![Build Status](https://travis-ci.com/hirura/hrr_rb_lxns.svg?branch=master)](https://travis-ci.com/hirura/hrr_rb_lxns)
4
+ [![Gem Version](https://badge.fury.io/rb/hrr_rb_lxns.svg)](https://badge.fury.io/rb/hrr_rb_lxns)
5
+
6
+ hrr_rb_lxns implements utilities working with Linux namespaces for CRuby.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'hrr_rb_lxns'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install hrr_rb_lxns
23
+
24
+ ## Usage
25
+
26
+ hrr_rb_lxns provides unshare and setns wrappers.
27
+
28
+ ### Unshare
29
+
30
+ HrrRbLxns.unshare method wraps around unshare(2) system call. The system call disassociates the caller process's namespace.
31
+
32
+ ```ruby
33
+ require "hrr_rb_lxns"
34
+
35
+ # Disassociates uts namespace
36
+ File.readlink "/proc/self/ns/uts" # => uts:[aaa]
37
+ HrrRbLxns.unshare HrrRbLxns::NEWUTS # => 0
38
+ File.readlink "/proc/self/ns/uts" # => uts:[xxx]
39
+
40
+ # Disassociates uts and mount namespaces
41
+ File.readlink "/proc/self/ns/uts" # => uts:[aaa]
42
+ File.readlink "/proc/self/ns/mnt" # => mnt:[bbb]
43
+ HrrRbLxns.unshare "um" # => 0
44
+ File.readlink "/proc/self/ns/uts" # => uts:[xxx]
45
+ File.readlink "/proc/self/ns/mnt" # => mnt:[yyy]
46
+ ```
47
+
48
+ ### Setns
49
+
50
+ HrrRbLxns.setns method wraps around setns(2) system call. The system call associate the caller process's namespace to an existing one, which is disassociated by some other process.
51
+
52
+ ```ruby
53
+ # Before doing setns, prepare a disassociated namespace with using unshare.
54
+ # The unshare(2) system call disassociate the caller process's namespace, so
55
+ # do fork the process and unshare in the child process.
56
+ # To keep the disassociated namespase, do sleep at last in the child.
57
+ pid = fork do
58
+ # Disassociates uts namespace
59
+ File.readlink "/proc/self/ns/uts" # => uts:[xxx]
60
+ HrrRbLxns.unshare HrrRbLxns::NEWUTS # => 0
61
+ File.readlink "/proc/self/ns/uts" # => uts:[yyy]
62
+ sleep
63
+ end
64
+
65
+ # Aassociates uts namespace
66
+ File.readlink "/proc/self/ns/uts" # => uts:[xxx]
67
+ HrrRbLxns.setns HrrRbLxns::NEWUTS, pid # => 0
68
+ File.readlink "/proc/self/ns/uts" # => uts:[yyy]
69
+ ```
70
+
71
+ ## Note
72
+
73
+ Some of the namespace operations are not multi-thread friendly. The library expects that only main thread is running before unshare or setns operation.
74
+
75
+ In particular, note that there are some limitations on the use of the library with Ruby version 2.5.x or earlier. This is because of the background timer thread of Ruby.
76
+
77
+ - Unshare user namespace (with NEWUSER flag) on Ruby 2.5.x or earlier fails.
78
+ - Unshare pid namespace (with NEWPID flag) then Kernel.#fork on Ruby 2.5.x or earlier gets a timer thread related warning.
79
+ - Unshare pid namespace (with NEWPID flag) then Kernel.#fork on Ruby 2.2.x or earlier fails.
80
+ - Setns user namespace (with NEWUSER flag) on Ruby 2.5.x or earlier fails.
81
+ - Setns pid namespace (with NEWPID flag) then Kernel.#fork on Ruby 2.5.x or earlier gets a timer thread related warning.
82
+ - Setns pid namespace (with NEWPID flag) then Kernel.#fork on Ruby 2.2.x or earlier fails.
83
+ - Setns mount namespace (with NEWNS flag) on Ruby 2.5.x or earlier fails.
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hirura/hrr_rb_lxns. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hirura/hrr_rb_lxns/blob/master/CODE_OF_CONDUCT.md).
94
+
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
99
+
100
+ ## Code of Conduct
101
+
102
+ Everyone interacting in the HrrRbLxns project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hirura/hrr_rb_lxns/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require "rake/extensiontask"
7
+
8
+ task :build => :compile
9
+
10
+ Rake::ExtensionTask.new("hrr_rb_lxns") do |ext|
11
+ ext.lib_dir = "lib/hrr_rb_lxns"
12
+ end
13
+
14
+ task :default => [:clobber, :compile, :spec]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hrr_rb_lxns"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("hrr_rb_lxns/hrr_rb_lxns")
@@ -0,0 +1,114 @@
1
+ #include "hrr_rb_lxns.h"
2
+ #define _GNU_SOURCE 1
3
+ #include <sched.h>
4
+
5
+ VALUE rb_mHrrRbLxns;
6
+ VALUE rb_mHrrRbLxnsConst;
7
+
8
+ /*
9
+ * A primitive wrapper around unshare(2) system call.
10
+ * Disassociates parts of the caller process's execution context.
11
+ *
12
+ * @example
13
+ * # Disassociates uts namespace
14
+ * File.readlink "/proc/self/ns/uts" # => uts:[aaa]
15
+ * HrrRbLxns.__unshare__ HrrRbLxns::NEWUTS # => 0
16
+ * File.readlink "/proc/self/ns/uts" # => uts:[xxx]
17
+ *
18
+ * # Disassociates uts and mount namespaces
19
+ * File.readlink "/proc/self/ns/uts" # => uts:[aaa]
20
+ * File.readlink "/proc/self/ns/mnt" # => mnt:[bbb]
21
+ * HrrRbLxns.__unshare__ HrrRbLxns::NEWUTS | HrrRbLxns::NEWNS # => 0
22
+ * File.readlink "/proc/self/ns/uts" # => uts:[xxx]
23
+ * File.readlink "/proc/self/ns/mnt" # => mnt:[yyy]
24
+ *
25
+ * @param flags [Integer] Represents the namespaces to disassociate.
26
+ * @return [Integer] 0.
27
+ * @raise [TypeError] In case the given flags cannot be converted to integer.
28
+ * @raise [Errno::EXXX] In case unshare(2) system call failed.
29
+ */
30
+ VALUE
31
+ hrr_rb_lxns_unshare(VALUE self, VALUE flags)
32
+ {
33
+ if (unshare(NUM2INT(flags)) < 0)
34
+ rb_sys_fail("unshare");
35
+
36
+ return INT2FIX(0);
37
+ }
38
+
39
+ /*
40
+ * A primitive wrapper around setns(2) system call.
41
+ * Associates the caller with the corresponding namespace of the given fd.
42
+ *
43
+ * @example
44
+ * pid = fork do
45
+ * File.readlink "/proc/self/ns/uts" # => uts:[xxx]
46
+ * HrrRbLxns.__unshare__ HrrRbLxns::NEWUTS # => 0
47
+ * File.readlink "/proc/self/ns/uts" # => uts:[yyy]
48
+ * sleep
49
+ * end
50
+ * File.readlink "/proc/self/ns/uts" # => uts:[xxx]
51
+ * fd = File.open "/proc/#{pid}/ns/uts", File::RDONLY
52
+ * HrrRbLxns.__setns__ fd.fileno, HrrRbLxns::NEWUTS # => 0
53
+ * fd.close
54
+ * File.readlink "/proc/self/ns/uts" # => uts:[yyy]
55
+ *
56
+ * @param fd [Integer] The file descriptor number to associate.
57
+ * @param nstype [Integer] Represents the namespace to associate.
58
+ * @return [Integer] 0.
59
+ * @raise [TypeError] In case the given fd cannot be converted to integer or the given nstype cannot be converted to integer.
60
+ * @raise [Errno::EXXX] In case setns(2) system call failed.
61
+ */
62
+ VALUE
63
+ hrr_rb_lxns_setns(VALUE self, VALUE fd, VALUE nstype)
64
+ {
65
+ if (setns(NUM2INT(fd), NUM2INT(nstype)) < 0)
66
+ rb_sys_fail("setns");
67
+
68
+ return INT2FIX(0);
69
+ }
70
+
71
+ void
72
+ Init_hrr_rb_lxns(void)
73
+ {
74
+ rb_mHrrRbLxns = rb_define_module("HrrRbLxns");
75
+
76
+ rb_define_singleton_method(rb_mHrrRbLxns, "__unshare__", hrr_rb_lxns_unshare, 1);
77
+ rb_define_singleton_method(rb_mHrrRbLxns, "__setns__", hrr_rb_lxns_setns, 2);
78
+
79
+ rb_mHrrRbLxnsConst = rb_define_module_under(rb_mHrrRbLxns, "Constants");
80
+ rb_include_module(rb_mHrrRbLxns, rb_mHrrRbLxnsConst);
81
+
82
+ #ifdef CLONE_NEWIPC
83
+ /* Represents ipc namespace. */
84
+ rb_define_const(rb_mHrrRbLxnsConst, "NEWIPC", INT2FIX(CLONE_NEWIPC));
85
+ #endif
86
+ #ifdef CLONE_NEWNS
87
+ /* Represents mount namespace. */
88
+ rb_define_const(rb_mHrrRbLxnsConst, "NEWNS", INT2FIX(CLONE_NEWNS));
89
+ #endif
90
+ #ifdef CLONE_NEWNET
91
+ /* Represents network namespace. */
92
+ rb_define_const(rb_mHrrRbLxnsConst, "NEWNET", INT2FIX(CLONE_NEWNET));
93
+ #endif
94
+ #ifdef CLONE_NEWPID
95
+ /* Represents pid namespace. */
96
+ rb_define_const(rb_mHrrRbLxnsConst, "NEWPID", INT2FIX(CLONE_NEWPID));
97
+ #endif
98
+ #ifdef CLONE_NEWUTS
99
+ /* Represents uts namespace. */
100
+ rb_define_const(rb_mHrrRbLxnsConst, "NEWUTS", INT2FIX(CLONE_NEWUTS));
101
+ #endif
102
+ #ifdef CLONE_NEWUSER
103
+ /* Represents user namespace. */
104
+ rb_define_const(rb_mHrrRbLxnsConst, "NEWUSER", INT2FIX(CLONE_NEWUSER));
105
+ #endif
106
+ #ifdef CLONE_NEWCGROUP
107
+ /* Represents cgroup namespace. */
108
+ rb_define_const(rb_mHrrRbLxnsConst, "NEWCGROUP", INT2FIX(CLONE_NEWCGROUP));
109
+ #endif
110
+ #ifdef CLONE_NEWTIME
111
+ /* Represents time namespace. */
112
+ rb_define_const(rb_mHrrRbLxnsConst, "NEWTIME", INT2FIX(CLONE_NEWTIME));
113
+ #endif
114
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef HRR_RB_LXNS_H
2
+ #define HRR_RB_LXNS_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* HRR_RB_LXNS_H */
@@ -0,0 +1,24 @@
1
+ require_relative 'lib/hrr_rb_lxns/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "hrr_rb_lxns"
5
+ spec.version = HrrRbLxns::VERSION
6
+ spec.authors = ["hirura"]
7
+ spec.email = ["hirura@gmail.com"]
8
+
9
+ spec.summary = %q{Utilities working with Linux namespaces for CRuby.}
10
+ spec.description = %q{Utilities working with Linux namespaces for CRuby.}
11
+ spec.homepage = "https://github.com/hirura/hrr_rb_lxns"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ #spec.metadata["source_code_uri"] = spec.homepage
17
+ #spec.metadata["changelog_uri"] = spec.homepage
18
+
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.require_paths = ["lib"]
23
+ spec.extensions = ["ext/hrr_rb_lxns/extconf.rb"]
24
+ end
@@ -0,0 +1,138 @@
1
+ require "hrr_rb_lxns/version"
2
+ require "hrr_rb_lxns/hrr_rb_lxns"
3
+
4
+ # Utilities working with Linux namespaces for CRuby.
5
+ module HrrRbLxns
6
+
7
+ # Constants that represent the flags for Linux namespaces operations.
8
+ module Constants
9
+ end
10
+
11
+ # A wrapper around unshare(2) system call.
12
+ #
13
+ # @example
14
+ # # Disassociates uts namespace
15
+ # File.readlink "/proc/self/ns/uts" # => uts:[aaa]
16
+ # HrrRbLxns.unshare HrrRbLxns::NEWUTS # => 0
17
+ # File.readlink "/proc/self/ns/uts" # => uts:[xxx]
18
+ #
19
+ # # Disassociates uts and mount namespaces
20
+ # File.readlink "/proc/self/ns/uts" # => uts:[aaa]
21
+ # File.readlink "/proc/self/ns/mnt" # => mnt:[bbb]
22
+ # HrrRbLxns.unshare "um" # => 0
23
+ # File.readlink "/proc/self/ns/uts" # => uts:[xxx]
24
+ # File.readlink "/proc/self/ns/mnt" # => mnt:[yyy]
25
+ #
26
+ # @param flags [Integer] An integer value that represents namespaces to disassociate.
27
+ # @param flags [String] A string that represents namespaces. The mapping of charactors and flags are: <br>
28
+ # "i" : NEWIPC <br>
29
+ # "m" : NEWNS <br>
30
+ # "n" : NEWNET <br>
31
+ # "p" : NEWPID <br>
32
+ # "u" : NEWUTS <br>
33
+ # "U" : NEWUSER <br>
34
+ # "C" : NEWCGROUP <br>
35
+ # "T" : NEWTIME <br>
36
+ # @param options [Hash] For future use.
37
+ # @return [Integer] 0.
38
+ # @raise [ArgumentError] When given flags argument is not appropriate.
39
+ # @raise [Errno::EXXX] In case unshare(2) system call failed.
40
+
41
+ def self.unshare flags, options={}
42
+ _flags = interpret_flags flags
43
+ __unshare__ _flags
44
+ end
45
+
46
+ # A wrapper around setns(2) system call.
47
+ #
48
+ # @example
49
+ # pid = fork do
50
+ # # Disassociates uts namespace
51
+ # File.readlink "/proc/self/ns/uts" # => uts:[xxx]
52
+ # HrrRbLxns.unshare HrrRbLxns::NEWUTS # => 0
53
+ # File.readlink "/proc/self/ns/uts" # => uts:[yyy]
54
+ # sleep
55
+ # end
56
+ # # Aassociates uts namespace
57
+ # File.readlink "/proc/self/ns/uts" # => uts:[xxx]
58
+ # HrrRbLxns.setns HrrRbLxns::NEWUTS, pid # => 0
59
+ # File.readlink "/proc/self/ns/uts" # => uts:[yyy]
60
+ #
61
+ # @param flags [Integer] An integer value that represents namespaces to disassociate.
62
+ # @param flags [String] A string that represents namespaces. The mapping of charactors and flags are: <br>
63
+ # "i" : NEWIPC <br>
64
+ # "m" : NEWNS <br>
65
+ # "n" : NEWNET <br>
66
+ # "p" : NEWPID <br>
67
+ # "u" : NEWUTS <br>
68
+ # "U" : NEWUSER <br>
69
+ # "C" : NEWCGROUP <br>
70
+ # "T" : NEWTIME <br>
71
+ # @param options [Hash] For future use.
72
+ # @return [Integer] 0.
73
+ # @raise [ArgumentError] When given flags argument is not appropriate.
74
+ # @raise [Errno::EXXX] In case setns(2) system call failed.
75
+ def self.setns flags, pid, options={}
76
+ _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
86
+ end
87
+
88
+ private
89
+
90
+ def self.interpret_flags arg
91
+ case arg
92
+ when Integer then arg
93
+ when String then chars_to_flags arg
94
+ else raise TypeError, "unsupported flags: #{arg.inspect}"
95
+ end
96
+ end
97
+
98
+ def self.chars_to_flags chars
99
+ chars.each_char.inject(0) do |f, c|
100
+ if c == "i" && const_defined?(:NEWIPC) then f | NEWIPC
101
+ elsif c == "m" && const_defined?(:NEWNS) then f | NEWNS
102
+ elsif c == "n" && const_defined?(:NEWNET) then f | NEWNET
103
+ elsif c == "p" && const_defined?(:NEWPID) then f | NEWPID
104
+ elsif c == "u" && const_defined?(:NEWUTS) then f | NEWUTS
105
+ elsif c == "U" && const_defined?(:NEWUSER) then f | NEWUSER
106
+ elsif c == "C" && const_defined?(:NEWCGROUP) then f | NEWCGROUP
107
+ elsif c == "T" && const_defined?(:NEWTIME) then f | NEWTIME
108
+ else raise ArgumentError, "unsupported flag charactor: #{c.inspect}"
109
+ end
110
+ end
111
+ end
112
+
113
+ def self.get_fds flags, pid
114
+ 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
127
+ end
128
+ fds
129
+ end
130
+
131
+ def self.get_fd name, flag, pid
132
+ if flag.zero?.! && pid
133
+ "/proc/#{pid}/ns/#{name}"
134
+ else
135
+ nil
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,3 @@
1
+ module HrrRbLxns
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hrr_rb_lxns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hirura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Utilities working with Linux namespaces for CRuby.
14
+ email:
15
+ - hirura@gmail.com
16
+ executables: []
17
+ extensions:
18
+ - ext/hrr_rb_lxns/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - ext/hrr_rb_lxns/extconf.rb
32
+ - ext/hrr_rb_lxns/hrr_rb_lxns.c
33
+ - ext/hrr_rb_lxns/hrr_rb_lxns.h
34
+ - hrr_rb_lxns.gemspec
35
+ - lib/hrr_rb_lxns.rb
36
+ - lib/hrr_rb_lxns/version.rb
37
+ homepage: https://github.com/hirura/hrr_rb_lxns
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/hirura/hrr_rb_lxns
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.0.3
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Utilities working with Linux namespaces for CRuby.
61
+ test_files: []