Class: OsCtl::Lib::Sys

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/sys.rb

Defined Under Namespace

Modules: Int

Constant Summary collapse

CLONE_NEWNS =
0x00020000
CLONE_NEWUTS =
0x04000000
CLONE_NEWUSER =
0x10000000
CLONE_NEWPID =
0x20000000
CLONE_NEWNET =
0x40000000
CLONE_NEWIPC =
0x08000000
MS_MGC_VAL =
0xc0ed0000
MS_NOSUID =
2
MS_NODEV =
4
MS_NOEXEC =
8
MS_BIND =
4096
MS_MOVE =
8192
MS_REC =
16_384
MS_SLAVE =
1 << 19
MS_SHARED =
1 << 20
SYSLOGNS_MAX_TAG_BYTESIZE =
12

Instance Method Summary collapse

Instance Method Details

#attach_syslogns(pid) ⇒ Object

Parameters:

  • pid (Integer)

    attach to syslogns of PID



182
183
184
# File 'lib/libosctl/sys.rb', line 182

def attach_syslogns(pid)
  setns_path(File.join('/proc', pid.to_s, 'ns/syslog'), 0)
end

#bind_mount(src, dst) ⇒ Object

Raises:

  • (SystemCallError)


66
67
68
69
70
71
# File 'lib/libosctl/sys.rb', line 66

def bind_mount(src, dst)
  ret = Int.mount(src, dst, 0, MS_MGC_VAL | MS_BIND, 0)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#chroot(path) ⇒ Object

Raises:

  • (SystemCallError)


159
160
161
162
163
164
# File 'lib/libosctl/sys.rb', line 159

def chroot(path)
  ret = Int.chroot(path)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#create_syslogns(tag) ⇒ Object

Parameters:

  • tag (String)

    syslogns tag prepended to all messages

Raises:

  • (SystemCallError)


167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/libosctl/sys.rb', line 167

def create_syslogns(tag)
  if tag.bytesize > SYSLOGNS_MAX_TAG_BYTESIZE
    raise ArgumentError, "tag can have at most #{SYSLOGNS_MAX_TAG_BYTESIZE} bytes"
  end

  klogctl_ret = Int.klogctl(11, tag, tag.bytesize)
  raise SystemCallError, Fiddle.last_error if klogctl_ret != 0

  unshare_ret = Int.unshare(0)
  raise SystemCallError, Fiddle.last_error if unshare_ret != 0

  0
end

#make_rshared(dst) ⇒ Object

Raises:

  • (SystemCallError)


101
102
103
104
105
106
# File 'lib/libosctl/sys.rb', line 101

def make_rshared(dst)
  ret = Int.mount('none', dst, 0, MS_REC | MS_SHARED, 0)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#make_rslave(dst) ⇒ Object

Raises:

  • (SystemCallError)


115
116
117
118
119
120
# File 'lib/libosctl/sys.rb', line 115

def make_rslave(dst)
  ret = Int.mount('none', dst, 0, MS_REC | MS_SLAVE, 0)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#make_shared(dst) ⇒ Object

Raises:

  • (SystemCallError)


94
95
96
97
98
99
# File 'lib/libosctl/sys.rb', line 94

def make_shared(dst)
  ret = Int.mount('none', dst, 0, MS_SHARED, 0)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#make_slave(dst) ⇒ Object

Raises:

  • (SystemCallError)


108
109
110
111
112
113
# File 'lib/libosctl/sys.rb', line 108

def make_slave(dst)
  ret = Int.mount('none', dst, 0, MS_SLAVE, 0)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#mount_proc(dst) ⇒ Object

Raises:

  • (SystemCallError)


87
88
89
90
91
92
# File 'lib/libosctl/sys.rb', line 87

def mount_proc(dst)
  ret = Int.mount('none', dst, 'proc', MS_MGC_VAL, 0)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#mount_tmpfs(dst, name: 'none', flags: MS_MGC_VAL, options: 0) ⇒ Object

Raises:

  • (SystemCallError)


80
81
82
83
84
85
# File 'lib/libosctl/sys.rb', line 80

def mount_tmpfs(dst, name: 'none', flags: MS_MGC_VAL, options: 0)
  ret = Int.mount(name, dst, 'tmpfs', flags, options)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#move_mount(src, dst) ⇒ Object

Raises:

  • (SystemCallError)


59
60
61
62
63
64
# File 'lib/libosctl/sys.rb', line 59

def move_mount(src, dst)
  ret = Int.mount(src, dst, 0, MS_MGC_VAL | MS_MOVE, 0)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#rbind_mount(src, dst) ⇒ Object

Raises:

  • (SystemCallError)


73
74
75
76
77
78
# File 'lib/libosctl/sys.rb', line 73

def rbind_mount(src, dst)
  ret = Int.mount(src, dst, 0, MS_MGC_VAL | MS_BIND | MS_REC, 0)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#setns_io(io, nstype) ⇒ Object

Raises:

  • (SystemCallError)


145
146
147
148
149
150
# File 'lib/libosctl/sys.rb', line 145

def setns_io(io, nstype)
  ret = Int.setns(io.fileno, nstype)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#setns_path(path, nstype) ⇒ Object

Raises:

  • (SystemCallError)


136
137
138
139
140
141
142
143
# File 'lib/libosctl/sys.rb', line 136

def setns_path(path, nstype)
  f = File.open(path)
  ret = Int.setns(f.fileno, nstype)
  f.close
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#setresgid(rgid, egid, sgid) ⇒ Object

Raises:

  • (SystemCallError)


52
53
54
55
56
57
# File 'lib/libosctl/sys.rb', line 52

def setresgid(rgid, egid, sgid)
  ret = Int.setresgid(rgid, egid, sgid)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#setresuid(ruid, euid, suid) ⇒ Object

Raises:

  • (SystemCallError)


45
46
47
48
49
50
# File 'lib/libosctl/sys.rb', line 45

def setresuid(ruid, euid, suid)
  ret = Int.setresuid(ruid, euid, suid)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#syncfs(path) ⇒ Object

Parameters:

  • path (String)

    filesystem directory



187
188
189
190
191
192
193
194
195
196
# File 'lib/libosctl/sys.rb', line 187

def syncfs(path)
  f = Tempfile.new('.syncfs', path)
  ret = Int.syncfs(f.fileno)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
ensure
  f.close
  f.unlink
end

#unmount(mountpoint) ⇒ Object

Raises:

  • (SystemCallError)


122
123
124
125
126
127
# File 'lib/libosctl/sys.rb', line 122

def unmount(mountpoint)
  ret = Int.umount2(mountpoint, 0) # force unmount returns EACCESS
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#unmount_lazy(mountpoint) ⇒ Object

Raises:

  • (SystemCallError)


129
130
131
132
133
134
# File 'lib/libosctl/sys.rb', line 129

def unmount_lazy(mountpoint)
  ret = Int.umount2(mountpoint, Int::MNT_DETACH)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end

#unshare_ns(type) ⇒ Object

Raises:

  • (SystemCallError)


152
153
154
155
156
157
# File 'lib/libosctl/sys.rb', line 152

def unshare_ns(type)
  ret = Int.unshare(type)
  raise SystemCallError, Fiddle.last_error if ret != 0

  ret
end