1 # depends on: module.rb class.rb hash.rb struct.rb
7 PRIO_PROCESS = Rubinius::RUBY_CONFIG['rbx.platform.process.PRIO_PROCESS']
8 PRIO_PGRP = Rubinius::RUBY_CONFIG['rbx.platform.process.PRIO_PGRP']
9 PRIO_USER = Rubinius::RUBY_CONFIG['rbx.platform.process.PRIO_USER']
10 RLIMIT_CPU = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_CPU']
11 RLIMIT_FSIZE = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_FSIZE']
12 RLIMIT_DATA = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_DATA']
13 RLIMIT_STACK = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_STACK']
14 RLIMIT_CORE = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_CORE']
15 RLIMIT_RSS = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_RSS']
16 RLIMIT_NPROC = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_NPROC']
17 RLIMIT_NOFILE = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_NOFILE']
18 RLIMIT_MEMLOCK = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_MEMLOCK']
19 RLIMIT_AS = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_AS']
20 RLIM_INFINITY = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIM_INFINITY']
21 RLIM_SAVED_MAX = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIM_SAVED_MAX']
22 RLIM_SAVED_CUR = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIM_SAVED_CUR']
23 RLIMIT_SBSIZE = Rubinius::RUBY_CONFIG['rbx.platform.process.RLIMIT_SBSIZE']
27 class Rlimit < FFI::Struct
28 config "rbx.platform.rlimit", :rlim_cur, :rlim_max
31 def self.setrlimit(resource, cur_limit, max_limit=Undefined)
33 rlimit[:rlim_cur] = cur_limit
34 rlimit[:rlim_max] = max_limit.equal?(Undefined) ? cur_limit : max_limit
35 Errno.handle if -1 == Platform::POSIX.setrlimit(resource, rlimit.pointer)
39 def self.getrlimit(resource)
42 Errno.handle if -1 == Platform::POSIX.getrlimit(resource, rlimit.pointer)
43 lim_max = [rlimit[:rlim_cur], rlimit[:rlim_max]]
48 pgid = Platform::POSIX.setsid
49 Errno.handle if -1 == pgid
56 if block_given? and pid.nil?
64 micro_sleep(sec * 1_000_000)
68 micro_sleep(sec * 1_000)
73 Struct::Tms.new(now - $STARTUP_TIME, 0.0, 0.0, 0.0)
76 def self.kill(sig, pid)
77 use_process_group = false
78 if sig.kind_of?(String)
81 use_process_group = true
86 number = Signal::Names[sig]
91 use_process_group = true
94 pid = -pid if use_process_group
95 raise ArgumentError unless number
96 ret = Platform::POSIX.kill(pid, number)
105 def self.abort(msg=nil)
106 $stderr.puts(msg) if(msg)
109 def self.exit!(code=0)
113 def self.getpgid(pid)
114 ret = Platform::POSIX.getpgid(pid)
115 Errno.handle if ret == -1
119 def self.setpgid(pid, int)
120 ret = Platform::POSIX.setpgid(pid, int)
121 Errno.handle if ret == -1
127 attr_reader :maxgroups
137 ret = Platform::POSIX.getpgrp
138 Errno.handle if ret == -1
143 ret = Platform::POSIX.getpid
144 Errno.handle if ret == -1
149 ret = Platform::POSIX.getppid
150 Errno.handle if ret == -1
155 Process::Sys.setuid uid
159 Process::Sys.setgid gid
163 Process::Sys.seteuid uid
167 Process::Sys.setegid gid
171 ret = Platform::POSIX.getuid
172 Errno.handle if ret == -1
177 ret = Platform::POSIX.getgid
178 Errno.handle if ret == -1
183 ret = Platform::POSIX.geteuid
184 Errno.handle if ret == -1
189 ret = Platform::POSIX.getegid
190 Errno.handle if ret == -1
194 def self.getpriority(kind, id)
195 Platform::POSIX.errno = 0
196 ret = Platform::POSIX.getpriority(kind, id)
201 def self.setpriority(kind, id, priority)
202 ret = Platform::POSIX.setpriority(kind, id, priority)
203 Errno.handle if ret == -1
209 MemoryPointer.new(:int, @maxgroups) { |p|
210 num_groups = Platform::POSIX.getgroups(@maxgroups, p)
211 Errno.handle if num_groups == -1
212 g = p.read_array_of_int(num_groups)
218 @maxgroups = g.length if g.length > @maxgroups
219 MemoryPointer.new(:int, @maxgroups) { |p|
220 p.write_array_of_int(g)
221 Errno.handle if -1 == Platform::POSIX.setgroups(g.length, p)
226 def self.initgroups(username, gid)
227 Errno.handle if -1 == Platform::POSIX.initgroups(username, gid)
231 def self.wait(pid=-1, flags=0)
233 Scheduler.send_on_stopped(chan, pid, flags)
234 pid, status = chan.receive
241 $? = Process::Status.new pid, status
248 statuses << [Process.wait, $?] while true
253 def self.wait2(pid=-1, flags=0)
254 pid = Process.wait(pid, flags)
255 pid ? [pid, $?] : nil
259 alias_method :waitpid, :wait
260 alias_method :waitpid2, :wait2
266 break if Process.wait(pid, Process::WNOHANG)
273 # TODO: Most of the fields aren't implemented yet.
274 # TODO: Also, these objects should only need to be constructed by
275 # Process.wait and family.
279 def initialize(pid, status)
297 other = other.to_i if other.kind_of? Process::Status
345 ret = Platform::POSIX.getegid
346 Errno.handle if ret == -1
350 ret = Platform::POSIX.geteuid
351 Errno.handle if ret == -1
355 ret = Platform::POSIX.getgid
356 Errno.handle if ret == -1
360 ret = Platform::POSIX.getuid
361 Errno.handle if ret == -1
365 raise "not implemented"
368 Platform::POSIX.setgid gid
369 Errno.handle if ret == -1
373 Platform::POSIX.setuid uid
374 Errno.handle if ret == -1
378 ret = Platform::POSIX.setegid egid
379 Errno.handle if ret == -1
383 Platform::POSIX.seteuid euid
384 Errno.handle if ret == -1
393 def setregid(rid, eid)
394 Platform::POSIX.setregid rid, eid
395 Errno.handle if ret == -1
399 Platform::POSIX.setreuid rid
400 Errno.handle if ret == -1
403 def setresgid(rid, eid, sid)
404 Platform::POSIX.setresgid rid, eid, sid
405 Errno.handle if ret == -1
408 def setresuid(rid, eig, sid)
409 Platform::POSIX.setresuid rid, eid, sid
410 Errno.handle if ret == -1
418 def change_privilege(uid)
419 Platform::POSIX.setreuid(uid, uid)
424 ret = Platform::POSIX.geteuid
425 Errno.handle if ret == -1
430 ret = Platform::POSIX.seteuid(uid)
431 Errno.handle if ret == -1
434 alias_method :grant_privilege, :eid=
437 real = Platform::POSIX.getuid
438 Errno.handle if real == -1
439 eff = Platform::POSIX.geteuid
440 Errno.handle if eff == -1
441 ret = Platform::POSIX.setreuid(eff, real)
442 Errno.handle if ret == -1
451 ret = Platform::POSIX.getuid
452 Errno.handle if ret == -1
476 def change_privilege(gid)
477 ret = Platform::POSIX.setregid(gid, gid)
478 Errno.handle if ret == -1
483 ret = Platform::POSIX.getegid
484 Errno.handle if ret == -1
489 ret = Platform::POSIX.setegid(gid)
490 Errno.handle if ret == -1
493 alias_method :grant_privilege, :eid=
496 real = Platform::POSIX.getgid
497 Errno.handle if real == -1
498 eff = Platform::POSIX.getegid
499 Errno.handle if eff == -1
500 ret = Platform::POSIX.setregid(eff, real)
501 Errno.handle if ret == -1
510 ret = Platform::POSIX.getgid
511 Errno.handle if ret == -1
540 module_function :fork
542 def system(prog, *args)
548 exec(prog, *args) rescue exit! 1
551 module_function :system
554 if args.empty? and cmd.kind_of? String
555 raise SystemCallError if cmd.empty?
556 if /([*?{}\[\]<>()~&|$;'`"\n\s]|[^\w])/.match(cmd)
557 Process.replace "/bin/sh", ["sh", "-c", cmd]
559 Process.replace cmd, [cmd]
562 if cmd.kind_of? Array
574 Process.replace prog, argv
577 module_function :exec
580 str = StringValue(str)
581 read, write = IO.pipe
587 buf = String.buffer 50
589 Scheduler.send_on_readable chan, read, buf, 50
601 Process.replace "/bin/sh", ["sh", "-c", str]
605 module_function :` # `
609 def self.after_loaded
610 Struct.new 'Tms', :utime, :stime, :cutime, :cstime