-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathprocess.rb
922 lines (767 loc) · 23.4 KB
/
process.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
# frozen_string_literal: true
# Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 2.0, or
# GNU General Public License version 2, or
# GNU Lesser General Public License version 2.1.
# Copyright (c) 2007-2015, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
module Process
module Constants
EXIT_SUCCESS = Truffle::Config['platform.process.EXIT_SUCCESS'] || 0
EXIT_FAILURE = Truffle::Config['platform.process.EXIT_FAILURE'] || 1
PRIO_PGRP = Truffle::Config['platform.process.PRIO_PGRP']
PRIO_PROCESS = Truffle::Config['platform.process.PRIO_PROCESS']
PRIO_USER = Truffle::Config['platform.process.PRIO_USER']
RLIM_INFINITY = Truffle::Config['platform.process.RLIM_INFINITY']
RLIM_SAVED_MAX = Truffle::Config['platform.process.RLIM_SAVED_MAX']
RLIM_SAVED_CUR = Truffle::Config['platform.process.RLIM_SAVED_CUR']
RLIMIT_AS = Truffle::Config['platform.process.RLIMIT_AS']
RLIMIT_CORE = Truffle::Config['platform.process.RLIMIT_CORE']
RLIMIT_CPU = Truffle::Config['platform.process.RLIMIT_CPU']
RLIMIT_DATA = Truffle::Config['platform.process.RLIMIT_DATA']
RLIMIT_FSIZE = Truffle::Config['platform.process.RLIMIT_FSIZE']
RLIMIT_NOFILE = Truffle::Config['platform.process.RLIMIT_NOFILE']
RLIMIT_STACK = Truffle::Config['platform.process.RLIMIT_STACK']
%i[RLIMIT_MEMLOCK RLIMIT_NPROC RLIMIT_RSS RLIMIT_SBSIZE RLIMIT_RTPRIO
RLIMIT_RTTIME RLIMIT_SIGPENDING RLIMIT_MSGQUEUE RLIMIT_NICE].each do |limit|
if value = Truffle::Config.lookup("platform.process.#{limit}")
const_set limit, value
end
end
WNOHANG = Truffle::Config['platform.process.WNOHANG']
WUNTRACED = Truffle::Config['platform.process.WUNTRACED']
end
include Constants
FFI = Truffle::FFI
# Terminate with given status code.
#
def self.exit(code=0)
case code
when true
code = 0
when false
code = 1
else
code = Truffle::Type.coerce_to code, Integer, :to_int
end
raise SystemExit, code
end
def self.exit!(code=1)
case code
when true
code = 0
when false
code = 1
else
code = Truffle::Type.coerce_to code, Integer, :to_int
end
Primitive.vm_exit code
end
section = 'platform.clocks.'
Truffle::Config.section(section) do |key, value|
const_set(Primitive.string_substring(key, section.size, key.length), value)
end
def self.clock_getres(id, unit=:float_second)
res = case id
when :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC,
CLOCK_MONOTONIC
1
when :GETTIMEOFDAY_BASED_CLOCK_REALTIME,
:GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID,
:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
1_000
when CLOCK_REALTIME
# https://bugs.openjdk.java.net/browse/JDK-8068730
1000
when :TIMES_BASED_CLOCK_MONOTONIC,
:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
10_000_000
when :TIME_BASED_CLOCK_REALTIME
1_000_000_000
when Symbol
raise Errno::EINVAL
else
res_for_id = Truffle::POSIX.truffleposix_clock_getres(id)
if res_for_id == 0
Errno.handle
else
res_for_id
end
end
if :hertz == unit
1.0 / nanoseconds_to_unit(res,:float_second)
else
nanoseconds_to_unit(res,unit)
end
end
def self.clock_gettime(id, unit=:float_second)
if Primitive.object_kind_of?(id, Symbol)
id = case id
when :GETTIMEOFDAY_BASED_CLOCK_REALTIME,
:TIME_BASED_CLOCK_REALTIME
CLOCK_REALTIME
when :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC,
:TIMES_BASED_CLOCK_MONOTONIC
CLOCK_MONOTONIC
when :GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID,
:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID,
:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
CLOCK_THREAD_CPUTIME_ID
else
raise Errno::EINVAL
end
end
case id
when CLOCK_MONOTONIC # most common clock id
time = Primitive.process_time_nanotime
when CLOCK_REALTIME
time = Primitive.process_time_instant
else
time = Truffle::POSIX.truffleposix_clock_gettime(id)
Errno.handle if time == 0
end
nanoseconds_to_unit(time, unit)
end
def self.nanoseconds_to_unit(nanoseconds, unit)
case unit # ordered by expected frequency
when :float_second, nil
nanoseconds / 1e9
when :nanosecond
nanoseconds
when :microsecond
nanoseconds / 1_000
when :float_microsecond
nanoseconds / 1e3
when :float_millisecond
nanoseconds / 1e6
when :second
nanoseconds / 1_000_000_000
when :millisecond
nanoseconds / 1_000_000
else
raise ArgumentError, "unexpected unit: #{unit}"
end
end
private_class_method :nanoseconds_to_unit
##
# Sets the process title. Calling this method does not affect the value of
# `$0` as per MRI behaviour. This method returns the title set.
#
# @param [String] title
# @return [Title]
#
def self.setproctitle(title)
title = Truffle::Type.coerce_to(title, String, :to_str)
argv = Primitive.vm_native_argv
# Not run from ruby launcher, we cannot set process title
return title if argv == 0
max_length = Primitive.vm_native_argv_length
new_title =
if title.bytesize > max_length
title.byteslice(0, max_length)
else
title + "\x00" * (max_length - title.bytesize)
end
argv_ptr = FFI::Pointer.new(:char, argv)
argv0_ptr = argv_ptr.read_pointer.slice(0, max_length)
argv0_ptr.write_bytes(new_title)
title
end
def self.setrlimit(resource, cur_limit, max_limit=undefined)
resource = coerce_rlimit_resource(resource)
cur_limit = Truffle::Type.coerce_to cur_limit, Integer, :to_int
if Primitive.undefined? max_limit
max_limit = cur_limit
else
max_limit = Truffle::Type.coerce_to max_limit, Integer, :to_int
end
rlim_t = Truffle::Config['platform.typedef.rlim_t']
raise rlim_t unless rlim_t == 'ulong' or rlim_t == 'ulong_long'
Truffle::FFI::MemoryPointer.new(:rlim_t, 2) do |ptr|
ptr[0].write_ulong cur_limit
ptr[1].write_ulong max_limit
ret = Truffle::POSIX.setrlimit(resource, ptr)
Errno.handle if ret == -1
end
nil
end
def self.getrlimit(resource)
resource = coerce_rlimit_resource(resource)
rlim_t = Truffle::Config['platform.typedef.rlim_t']
raise rlim_t unless rlim_t == 'ulong' or rlim_t == 'ulong_long'
Truffle::FFI::MemoryPointer.new(:rlim_t, 2) do |ptr|
ret = Truffle::POSIX.getrlimit(resource, ptr)
Errno.handle if ret == -1
cur = ptr[0].read_ulong
max = ptr[1].read_ulong
[cur, max]
end
end
def self.setsid
pgid = Truffle::POSIX.setsid
Errno.handle if pgid == -1
pgid
end
def self.fork
raise NotImplementedError, 'fork is not available'
end
Primitive.method_unimplement method(:fork)
def self.times
Truffle::FFI::MemoryPointer.new(:double, 4) do |ptr|
ret = Truffle::POSIX.truffleposix_getrusage(ptr)
Errno.handle if ret == -1
Process::Tms.new(*ptr.read_array_of_double(4))
end
end
def self.kill(signal, *pids)
raise ArgumentError, 'PID argument required' if pids.length == 0
use_process_group = false
signal = signal.to_s if Primitive.object_kind_of?(signal, Symbol)
if Primitive.object_kind_of?(signal, String)
if signal.start_with? '-'
signal = signal[1..-1]
use_process_group = true
end
if signal.start_with? 'SIG'
signal = signal[3..-1]
end
signal = Signal::Names[signal]
end
raise ArgumentError unless Primitive.object_kind_of?(signal, Integer)
if signal < 0
signal = -signal
use_process_group = true
end
pids.each do |pid|
pid = Truffle::Type.coerce_to pid, Integer, :to_int
if pid == Process.pid && signal != 0
signal_name = Signal::Numbers[signal].to_sym
result = Primitive.process_kill_raise signal_name
if result == -1 # Try kill() if the java Signal.raise() failed
result = Truffle::POSIX.kill(pid, signal)
Errno.handle if result == -1
end
else
pid = -pid if use_process_group
result = Truffle::POSIX.kill(pid, signal)
Errno.handle if result == -1
end
end
pids.length
end
def self.abort(msg=nil)
if msg
msg = StringValue(msg)
$stderr.puts(msg)
end
raise SystemExit.new(1, msg)
end
def self.getpgid(pid)
pid = Truffle::Type.coerce_to pid, Integer, :to_int
ret = Truffle::POSIX.getpgid(pid)
Errno.handle if ret == -1
ret
end
def self.setpgid(pid, int)
pid = Truffle::Type.coerce_to pid, Integer, :to_int
int = Truffle::Type.coerce_to int, Integer, :to_int
ret = Truffle::POSIX.setpgid(pid, int)
Errno.handle if ret == -1
ret
end
@maxgroups = 32
class << self
attr_accessor :maxgroups
end
def self.setpgrp
setpgid(0, 0)
end
def self.getpgrp
ret = Truffle::POSIX.getpgrp
Errno.handle if ret == -1
ret
end
def self.pid
Truffle::POSIX.getpid
end
def self.ppid
Truffle::POSIX.getppid
end
def self.uid=(uid)
# the 4 rescue clauses below are needed
# until respond_to? can be used to query the implementation of methods attached via FFI
# atm respond_to returns true if a method is attached but not implemented on the platform
uid = Truffle::Type.coerce_to uid, Integer, :to_int
begin
ret = Truffle::POSIX.setresuid(uid, -1, -1)
rescue NotImplementedError
begin
ret = Truffle::POSIX.setreuid(uid, -1)
rescue NotImplementedError
begin
ret = Truffle::POSIX.setruid(uid)
rescue NotImplementedError
if Process.euid == uid
ret = Truffle::POSIX.setuid(uid)
else
raise NotImplementedError
end
end
end
end
Errno.handle if ret == -1
uid
end
def self.gid=(gid)
gid = Truffle::Type.coerce_to gid, Integer, :to_int
Process::Sys.setgid gid
end
def self.euid=(uid)
# the 4 rescue clauses below are needed
# until respond_to? can be used to query the implementation of methods attached via FFI
# atm respond_to returns true if a method is attached but not implemented on the platform
uid = Truffle::Type.coerce_to uid, Integer, :to_int
begin
ret = Truffle::POSIX.setresuid(-1, uid, -1)
rescue NotImplementedError
begin
ret = Truffle::POSIX.setreuid(-1, uid)
rescue NotImplementedError
begin
ret = Truffle::POSIX.seteuid(uid)
rescue NotImplementedError
if Process.uid == uid
ret = Truffle::POSIX.setuid(uid)
else
raise NotImplementedError
end
end
end
end
Errno.handle if ret == -1
uid
end
def self.egid=(gid)
gid = Truffle::Type.coerce_to gid, Integer, :to_int
Process::Sys.setegid gid
end
def self.uid
Truffle::POSIX.getuid
end
def self.gid
Truffle::POSIX.getgid
end
def self.euid
Truffle::POSIX.geteuid
end
def self.egid
Truffle::POSIX.getegid
end
def self.getpriority(kind, id)
kind = Truffle::Type.coerce_to kind, Integer, :to_int
id = Truffle::Type.coerce_to id, Integer, :to_int
ret = Truffle::POSIX.truffleposix_getpriority(kind, id)
if ret <= -100
raise SystemCallError.new(nil, -(ret + 100))
end
ret
end
def self.setpriority(kind, id, priority)
kind = Truffle::Type.coerce_to kind, Integer, :to_int
id = Truffle::Type.coerce_to id, Integer, :to_int
priority = Truffle::Type.coerce_to priority, Integer, :to_int
ret = Truffle::POSIX.setpriority(kind, id, priority)
Errno.handle if ret == -1
ret
end
def self.groups
ngroups = Truffle::POSIX.getgroups(0, FFI::Pointer::NULL)
Errno.handle if ngroups == -1
gid_t = Truffle::Config['platform.typedef.gid_t']
raise gid_t unless gid_t == 'uint'
FFI::MemoryPointer.new(:gid_t, ngroups) do |ptr|
ret = Truffle::POSIX.getgroups(ngroups, ptr)
Errno.handle if ret == -1
ptr.read_array_of_uint(ngroups)
end
end
def self.groups=(groups)
gid_t = Truffle::Config['platform.typedef.gid_t']
raise gid_t unless gid_t == 'uint'
@maxgroups = groups.size if groups.size > @maxgroups
FFI::MemoryPointer.new(:gid_t, groups.size) do |ptr|
ptr.write_array_of_uint(groups)
r = Truffle::POSIX.setgroups(groups.size, ptr)
Errno.handle if r == -1
end
groups
end
def self.initgroups(username, gid)
username = StringValue(username)
gid = Truffle::Type.coerce_to gid, Integer, :to_int
if Truffle::POSIX.initgroups(username, gid) == -1
Errno.handle
end
Process.groups
end
def self.last_status
$?
end
#
# Wait for the given process to exit.
#
# The pid may be the specific pid of some previously forked
# process, or -1 to indicate to watch for *any* child process
# exiting. Other options, such as process groups, may be available
# depending on the system.
#
# With no arguments the default is to block waiting for any
# child processes (pid -1.)
#
# The flag may be Process::WNOHANG, which indicates that
# the child should only be quickly checked. If it has not
# exited yet, nil is returned immediately instead.
#
# The return value is the exited pid or nil if Process::WNOHANG
# was used and the child had not yet exited.
#
# If the pid has exited, the global $? is set to a Process::Status
# object representing the exit status (and possibly other info) of
# the child.
#
# If there exists no such pid (e.g. never forked or already
# waited for), or no children at all, Errno::ECHILD is raised.
#
# TODO: Support other options such as WUNTRACED? --rue
#
def self.wait2(input_pid=-1, flags=nil)
status = Truffle::ProcessOperations.wait(input_pid, flags, true, true)
[status.pid, status]
end
#
# Wait for all child processes.
#
# Blocks until all child processes have exited, and returns
# an Array of [pid, Process::Status] results, one for each
# child.
#
# Be mindful of the effects of creating new processes while
# .waitall has been called (usually in a different thread.)
# The .waitall call does not in any way check that it is only
# waiting for children that existed at the time it was called.
#
def self.waitall
statuses = []
begin
while status = Process.wait2
statuses << status
end
rescue Errno::ECHILD
nil # No child processes
end
statuses
end
def self.wait(pid=-1, flags=nil)
Truffle::ProcessOperations.wait(pid, flags, true, true)&.pid
end
class << self
alias_method :waitpid, :wait
alias_method :waitpid2, :wait2
end
def self.daemon(*args)
raise NotImplementedError, 'Process.daemon is not available'
end
Primitive.method_unimplement method(:daemon)
def self.exec(*args)
Truffle::ProcessOperations.exec(*args)
end
def self.spawn(*args)
Truffle::ProcessOperations.spawn(*args)
end
# TODO: Should an error be raised on ECHILD? --rue
#
# TODO: This operates on the assumption that waiting on
# the event consumes very little resources. If this
# is not the case, the check should be made WNOHANG
# and called periodically.
#
def self.detach(pid)
raise ArgumentError, 'Only positive pids may be detached' unless pid > 0
thread = Thread.new { Process.wait pid; $? }
thread[:pid] = pid
def thread.pid; self[:pid] end
thread
end
def self.coerce_rlimit_resource(resource)
case resource
when Integer
return resource
when Symbol, String
# do nothing
else
unless r = Truffle::Type.rb_check_convert_type(resource, String, :to_str)
return Truffle::Type.coerce_to resource, Integer, :to_int
end
resource = r
end
constant = "RLIMIT_#{resource}"
unless const_defined? constant
raise ArgumentError, "invalid resource name: #{constant}"
end
const_get constant
end
private_class_method :coerce_rlimit_resource
#--
# TODO: Most of the fields aren't implemented yet.
# TODO: Also, these objects should only need to be constructed by
# Process.wait and family.
#++
class Status
attr_reader :exitstatus, :termsig, :stopsig
def initialize(pid=nil, exitstatus=nil, termsig=nil, stopsig=nil, raw_status=nil)
@pid = pid
@exitstatus = exitstatus
@termsig = termsig
@stopsig = stopsig
@raw_status = raw_status
end
def to_i
@raw_status
end
def &(num)
@raw_status & num
end
def >>(num)
@raw_status >> num
end
def ==(other)
other = other.to_i if Primitive.object_kind_of?(other, Process::Status)
@raw_status == other
end
def coredump?
false
end
def exited?
@exitstatus != nil
end
def pid
@pid
end
def signaled?
@termsig != nil
end
def stopped?
@stopsig != nil
end
def success?
if exited?
@exitstatus == 0
else
nil
end
end
def to_s
"pid #{@pid.inspect} exit #{@exitstatus.inspect}"
end
def inspect
"#<Process::Status: #{self}>"
end
def self.wait(pid=-1, flags=nil)
Truffle::ProcessOperations.wait(pid, flags, false, false)
end
end
module Sys
class << self
def getegid
Truffle::POSIX.getegid
end
def geteuid
Truffle::POSIX.geteuid
end
def getgid
Truffle::POSIX.getgid
end
def getuid
Truffle::POSIX.getuid
end
def issetugid
raise 'not implemented'
end
def setgid(gid)
gid = Truffle::Type.coerce_to gid, Integer, :to_int
ret = Truffle::POSIX.setgid gid
Errno.handle if ret == -1
nil
end
def setuid(uid)
uid = Truffle::Type.coerce_to uid, Integer, :to_int
ret = Truffle::POSIX.setuid uid
Errno.handle if ret == -1
nil
end
def setegid(egid)
egid = Truffle::Type.coerce_to egid, Integer, :to_int
ret = Truffle::POSIX.setegid egid
Errno.handle if ret == -1
nil
end
def seteuid(euid)
euid = Truffle::Type.coerce_to euid, Integer, :to_int
ret = Truffle::POSIX.seteuid euid
Errno.handle if ret == -1
nil
end
def setrgid(rgid)
setregid(rgid, -1)
end
def setruid(ruid)
setreuid(ruid, -1)
end
def setregid(rid, eid)
rid = Truffle::Type.coerce_to rid, Integer, :to_int
eid = Truffle::Type.coerce_to eid, Integer, :to_int
ret = Truffle::POSIX.setregid rid, eid
Errno.handle if ret == -1
nil
end
def setreuid(rid, eid)
rid = Truffle::Type.coerce_to rid, Integer, :to_int
eid = Truffle::Type.coerce_to eid, Integer, :to_int
ret = Truffle::POSIX.setreuid rid, eid
Errno.handle if ret == -1
nil
end
def setresgid(rid, eid, sid)
rid = Truffle::Type.coerce_to rid, Integer, :to_int
eid = Truffle::Type.coerce_to eid, Integer, :to_int
sid = Truffle::Type.coerce_to sid, Integer, :to_int
ret = Truffle::POSIX.setresgid rid, eid, sid
Errno.handle if ret == -1
nil
end
def setresuid(rid, eid, sid)
rid = Truffle::Type.coerce_to rid, Integer, :to_int
eid = Truffle::Type.coerce_to eid, Integer, :to_int
sid = Truffle::Type.coerce_to sid, Integer, :to_int
ret = Truffle::POSIX.setresuid rid, eid, sid
Errno.handle if ret == -1
nil
end
end
end
module UID
class << self
def change_privilege(uid)
uid = Truffle::Type.coerce_to uid, Integer, :to_int
ret = Truffle::POSIX.setreuid(uid, uid)
Errno.handle if ret == -1
uid
end
def eid
Truffle::POSIX.geteuid
end
def eid=(uid)
uid = Truffle::Type.coerce_to uid, Integer, :to_int
ret = Truffle::POSIX.seteuid(uid)
Errno.handle if ret == -1
uid
end
alias_method :grant_privilege, :eid=
def re_exchange
real = Truffle::POSIX.getuid
eff = Truffle::POSIX.geteuid
ret = Truffle::POSIX.setreuid(eff, real)
Errno.handle if ret == -1
eff
end
def rid
Truffle::POSIX.getuid
end
end
end
module GID
class << self
def change_privilege(gid)
gid = Truffle::Type.coerce_to gid, Integer, :to_int
ret = Truffle::POSIX.setregid(gid, gid)
Errno.handle if ret == -1
gid
end
def eid
Truffle::POSIX.getegid
end
def eid=(gid)
gid = Truffle::Type.coerce_to gid, Integer, :to_int
ret = Truffle::POSIX.setegid(gid)
Errno.handle if ret == -1
gid
end
alias_method :grant_privilege, :eid=
def re_exchange
real = Truffle::POSIX.getgid
eff = Truffle::POSIX.getegid
ret = Truffle::POSIX.setregid(eff, real)
Errno.handle if ret == -1
eff
end
def rid
Truffle::POSIX.getgid
end
end
end
xid = Module.new do
def re_exchangeable?
true
end
def sid_available?
true
end
def switch
eff = re_exchange
if block_given?
ret = yield
re_exchange
ret
else
eff
end
end
end
UID.extend xid
GID.extend xid
end
Truffle::KernelOperations.define_hooked_variable(
:'$0',
-> { Primitive.global_variable_get :'$0' },
-> v {
v = StringValue(v)
Process.setproctitle(v)
Primitive.global_variable_set :'$0', v
})
alias $PROGRAM_NAME $0