Skip to content

Commit

Permalink
[Truffle] Implemented Process.getrlimit.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed May 30, 2015
1 parent 9be6351 commit ff619df
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
37 changes: 0 additions & 37 deletions spec/truffle/tags/core/process/getrlimit_tags.txt
@@ -1,39 +1,2 @@
fails:Process.getrlimit returns a two-element Array of Integers
fails:Process.getrlimit when passed an Object calls #to_int to convert to an Integer
fails:Process.getrlimit when passed an Object raises a TypeError if #to_int does not return an Integer
fails:Process.getrlimit when passed a Symbol coerces :AS into RLIMIT_AS
fails:Process.getrlimit when passed a Symbol coerces :CORE into RLIMIT_CORE
fails:Process.getrlimit when passed a Symbol coerces :CPU into RLIMIT_CPU
fails:Process.getrlimit when passed a Symbol coerces :DATA into RLIMIT_DATA
fails:Process.getrlimit when passed a Symbol coerces :FSIZE into RLIMIT_FSIZE
fails:Process.getrlimit when passed a Symbol coerces :NOFILE into RLIMIT_NOFILE
fails:Process.getrlimit when passed a Symbol coerces :STACK into RLIMIT_STACK
fails:Process.getrlimit when passed a Symbol coerces :MEMLOCK into RLIMIT_MEMLOCK
fails:Process.getrlimit when passed a Symbol coerces :NPROC into RLIMIT_NPROC
fails:Process.getrlimit when passed a Symbol coerces :RSS into RLIMIT_RSS
fails:Process.getrlimit when passed a Symbol raises ArgumentError when passed an unknown resource
fails:Process.getrlimit when passed a String coerces 'AS' into RLIMIT_AS
fails:Process.getrlimit when passed a String coerces 'CORE' into RLIMIT_CORE
fails:Process.getrlimit when passed a String coerces 'CPU' into RLIMIT_CPU
fails:Process.getrlimit when passed a String coerces 'DATA' into RLIMIT_DATA
fails:Process.getrlimit when passed a String coerces 'FSIZE' into RLIMIT_FSIZE
fails:Process.getrlimit when passed a String coerces 'NOFILE' into RLIMIT_NOFILE
fails:Process.getrlimit when passed a String coerces 'STACK' into RLIMIT_STACK
fails:Process.getrlimit when passed a String coerces 'MEMLOCK' into RLIMIT_MEMLOCK
fails:Process.getrlimit when passed a String coerces 'NPROC' into RLIMIT_NPROC
fails:Process.getrlimit when passed a String coerces 'RSS' into RLIMIT_RSS
fails:Process.getrlimit when passed a String raises ArgumentError when passed an unknown resource
fails:Process.getrlimit when passed on Object calls #to_str to convert to a String
fails:Process.getrlimit when passed on Object calls #to_int if #to_str does not return a String
fails:Process.getrlimit when passed a Symbol coerces :SBSIZE into RLIMIT_SBSIZE
fails:Process.getrlimit when passed a Symbol coerces :RTTIME into RLIMIT_RTTIME
fails:Process.getrlimit when passed a Symbol coerces :MSGQUEUE into RLIMIT_MSGQUEUE
fails:Process.getrlimit when passed a Symbol coerces :SIGPENDING into RLIMIT_SIGPENDING
fails:Process.getrlimit when passed a Symbol coerces :RTPRIO into RLIMIT_RTPRIO
fails:Process.getrlimit when passed a Symbol coerces :NICE into RLIMIT_NICE
fails:Process.getrlimit when passed a String coerces 'SBSIZE' into RLIMIT_SBSIZE
fails:Process.getrlimit when passed a String coerces 'RTTIME' into RLIMIT_RTTIME
fails:Process.getrlimit when passed a String coerces 'MSGQUEUE' into RLIMIT_MSGQUEUE
fails:Process.getrlimit when passed a String coerces 'SIGPENDING' into RLIMIT_SIGPENDING
fails:Process.getrlimit when passed a String coerces 'RTPRIO' into RLIMIT_RTPRIO
fails:Process.getrlimit when passed a String coerces 'NICE' into RLIMIT_NICE
Expand Up @@ -280,6 +280,11 @@ public long getAtOffsetLong(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getLong(offset);
}

@Specialization(guards = "type == TYPE_ULONG")
public long getAtOffsetULong(RubyBasicObject pointer, int offset, int type) {
return getPointer(pointer).getLong(offset);
}

@Specialization(guards = "type == TYPE_STRING")
public RubyString getAtOffsetString(RubyBasicObject pointer, int offset, int type) {
return createString(getPointer(pointer).getString(offset));
Expand Down
Expand Up @@ -197,6 +197,27 @@ public int getGroups(int max, RubyBasicObject pointer) {

}

@CoreMethod(names = "getrlimit", isModuleFunction = true, required = 2)
public abstract static class GetRLimitNode extends CoreMethodArrayArgumentsNode {

public GetRLimitNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization
public int getrlimit(int resource, RubyBasicObject pointer) {
final int result = posix().getrlimit(resource, PointerPrimitiveNodes.getPointer(pointer));

if (result == -1) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().errnoError(posix().errno(), this));
}

return result;
}

}

@CoreMethod(names = "getuid", isModuleFunction = true)
public abstract static class GetUIDNode extends CoreMethodArrayArgumentsNode {

Expand Down
39 changes: 38 additions & 1 deletion truffle/src/main/ruby/core/rubinius/common/process.rb
Expand Up @@ -64,6 +64,21 @@ module Constants

FFI = Rubinius::FFI

class Rlimit < FFI::Struct
config "rbx.platform.rlimit", :rlim_cur, :rlim_max
end

def self.getrlimit(resource)
resource = coerce_rlimit_resource(resource)

lim_max = []
rlimit = Rlimit.new
ret = FFI::Platform::POSIX.getrlimit(resource, rlimit.pointer)
Errno.handle if ret == -1

[rlimit[:rlim_cur], rlimit[:rlim_max]]
end

def self.setsid
pgid = FFI::Platform::POSIX.setsid
Errno.handle if pgid == -1
Expand Down Expand Up @@ -293,7 +308,29 @@ def self.wait2(input_pid=-1, flags=nil)

[pid, status]
end


def self.coerce_rlimit_resource(resource)
case resource
when Integer
return resource
when Symbol, String
# do nothing
else
unless r = Rubinius::Type.check_convert_type(resource, String, :to_str)
return Rubinius::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
Expand Down

0 comments on commit ff619df

Please sign in to comment.