Skip to content

Commit 5cedd64

Browse files
committed
[GR-18163] Process.euid= should accept String (#2651)
PullRequest: truffleruby/3344
2 parents 0d7b24d + 92636d8 commit 5cedd64

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Compatibility:
2525
* Implement `rb_imemo_tmpbuf` allocation for `ripper` (@aardvark179).
2626
* Implement `inherit` argument for `Module#class_variables` (#2653, @bjfish).
2727
* Fix `Float#/` when dividing by `Rational` (@bjfish).
28+
* `Process.euid=` should accept String (#2615, @ngtban).
2829

2930
Performance:
3031

spec/ruby/core/process/euid_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
-> { Process.euid = Object.new }.should raise_error(TypeError)
2222
end
2323

24+
it "sets the effective user id to its own uid if given the username corresponding to its own uid" do
25+
raise unless Process.uid == Process.euid
26+
27+
require "etc"
28+
user = Etc.getpwuid(Process.uid).name
29+
30+
Process.euid = user
31+
Process.euid.should == Process.uid
32+
end
33+
2434
as_user do
2535
it "raises Errno::ERPERM if run by a non superuser trying to set the superuser id" do
2636
-> { (Process.euid = 0)}.should raise_error(Errno::EPERM)

src/main/ruby/truffleruby/core/process.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,14 @@ def self.euid=(uid)
403403
# the 4 rescue clauses below are needed
404404
# until respond_to? can be used to query the implementation of methods attached via FFI
405405
# atm respond_to returns true if a method is attached but not implemented on the platform
406-
uid = Truffle::Type.coerce_to uid, Integer, :to_int
406+
uid =
407+
if uid.kind_of?(String)
408+
require 'etc'
409+
Etc.getpwnam(uid).uid
410+
else
411+
Truffle::Type.coerce_to uid, Integer, :to_int
412+
end
413+
407414
begin
408415
ret = Truffle::POSIX.setresuid(-1, uid, -1)
409416
rescue NotImplementedError

0 commit comments

Comments
 (0)