Skip to content

Commit ff619df

Browse files
committed
[Truffle] Implemented Process.getrlimit.
1 parent 9be6351 commit ff619df

File tree

4 files changed

+64
-38
lines changed

4 files changed

+64
-38
lines changed
Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,2 @@
1-
fails:Process.getrlimit returns a two-element Array of Integers
2-
fails:Process.getrlimit when passed an Object calls #to_int to convert to an Integer
3-
fails:Process.getrlimit when passed an Object raises a TypeError if #to_int does not return an Integer
4-
fails:Process.getrlimit when passed a Symbol coerces :AS into RLIMIT_AS
5-
fails:Process.getrlimit when passed a Symbol coerces :CORE into RLIMIT_CORE
6-
fails:Process.getrlimit when passed a Symbol coerces :CPU into RLIMIT_CPU
7-
fails:Process.getrlimit when passed a Symbol coerces :DATA into RLIMIT_DATA
8-
fails:Process.getrlimit when passed a Symbol coerces :FSIZE into RLIMIT_FSIZE
9-
fails:Process.getrlimit when passed a Symbol coerces :NOFILE into RLIMIT_NOFILE
10-
fails:Process.getrlimit when passed a Symbol coerces :STACK into RLIMIT_STACK
11-
fails:Process.getrlimit when passed a Symbol coerces :MEMLOCK into RLIMIT_MEMLOCK
12-
fails:Process.getrlimit when passed a Symbol coerces :NPROC into RLIMIT_NPROC
13-
fails:Process.getrlimit when passed a Symbol coerces :RSS into RLIMIT_RSS
14-
fails:Process.getrlimit when passed a Symbol raises ArgumentError when passed an unknown resource
15-
fails:Process.getrlimit when passed a String coerces 'AS' into RLIMIT_AS
16-
fails:Process.getrlimit when passed a String coerces 'CORE' into RLIMIT_CORE
17-
fails:Process.getrlimit when passed a String coerces 'CPU' into RLIMIT_CPU
18-
fails:Process.getrlimit when passed a String coerces 'DATA' into RLIMIT_DATA
19-
fails:Process.getrlimit when passed a String coerces 'FSIZE' into RLIMIT_FSIZE
20-
fails:Process.getrlimit when passed a String coerces 'NOFILE' into RLIMIT_NOFILE
21-
fails:Process.getrlimit when passed a String coerces 'STACK' into RLIMIT_STACK
22-
fails:Process.getrlimit when passed a String coerces 'MEMLOCK' into RLIMIT_MEMLOCK
23-
fails:Process.getrlimit when passed a String coerces 'NPROC' into RLIMIT_NPROC
24-
fails:Process.getrlimit when passed a String coerces 'RSS' into RLIMIT_RSS
25-
fails:Process.getrlimit when passed a String raises ArgumentError when passed an unknown resource
26-
fails:Process.getrlimit when passed on Object calls #to_str to convert to a String
27-
fails:Process.getrlimit when passed on Object calls #to_int if #to_str does not return a String
281
fails:Process.getrlimit when passed a Symbol coerces :SBSIZE into RLIMIT_SBSIZE
29-
fails:Process.getrlimit when passed a Symbol coerces :RTTIME into RLIMIT_RTTIME
30-
fails:Process.getrlimit when passed a Symbol coerces :MSGQUEUE into RLIMIT_MSGQUEUE
31-
fails:Process.getrlimit when passed a Symbol coerces :SIGPENDING into RLIMIT_SIGPENDING
32-
fails:Process.getrlimit when passed a Symbol coerces :RTPRIO into RLIMIT_RTPRIO
33-
fails:Process.getrlimit when passed a Symbol coerces :NICE into RLIMIT_NICE
342
fails:Process.getrlimit when passed a String coerces 'SBSIZE' into RLIMIT_SBSIZE
35-
fails:Process.getrlimit when passed a String coerces 'RTTIME' into RLIMIT_RTTIME
36-
fails:Process.getrlimit when passed a String coerces 'MSGQUEUE' into RLIMIT_MSGQUEUE
37-
fails:Process.getrlimit when passed a String coerces 'SIGPENDING' into RLIMIT_SIGPENDING
38-
fails:Process.getrlimit when passed a String coerces 'RTPRIO' into RLIMIT_RTPRIO
39-
fails:Process.getrlimit when passed a String coerces 'NICE' into RLIMIT_NICE

truffle/src/main/java/org/jruby/truffle/nodes/rubinius/PointerPrimitiveNodes.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ public long getAtOffsetLong(RubyBasicObject pointer, int offset, int type) {
280280
return getPointer(pointer).getLong(offset);
281281
}
282282

283+
@Specialization(guards = "type == TYPE_ULONG")
284+
public long getAtOffsetULong(RubyBasicObject pointer, int offset, int type) {
285+
return getPointer(pointer).getLong(offset);
286+
}
287+
283288
@Specialization(guards = "type == TYPE_STRING")
284289
public RubyString getAtOffsetString(RubyBasicObject pointer, int offset, int type) {
285290
return createString(getPointer(pointer).getString(offset));

truffle/src/main/java/org/jruby/truffle/nodes/rubinius/PosixNodes.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ public int getGroups(int max, RubyBasicObject pointer) {
197197

198198
}
199199

200+
@CoreMethod(names = "getrlimit", isModuleFunction = true, required = 2)
201+
public abstract static class GetRLimitNode extends CoreMethodArrayArgumentsNode {
202+
203+
public GetRLimitNode(RubyContext context, SourceSection sourceSection) {
204+
super(context, sourceSection);
205+
}
206+
207+
@Specialization
208+
public int getrlimit(int resource, RubyBasicObject pointer) {
209+
final int result = posix().getrlimit(resource, PointerPrimitiveNodes.getPointer(pointer));
210+
211+
if (result == -1) {
212+
CompilerDirectives.transferToInterpreter();
213+
throw new RaiseException(getContext().getCoreLibrary().errnoError(posix().errno(), this));
214+
}
215+
216+
return result;
217+
}
218+
219+
}
220+
200221
@CoreMethod(names = "getuid", isModuleFunction = true)
201222
public abstract static class GetUIDNode extends CoreMethodArrayArgumentsNode {
202223

truffle/src/main/ruby/core/rubinius/common/process.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ module Constants
6464

6565
FFI = Rubinius::FFI
6666

67+
class Rlimit < FFI::Struct
68+
config "rbx.platform.rlimit", :rlim_cur, :rlim_max
69+
end
70+
71+
def self.getrlimit(resource)
72+
resource = coerce_rlimit_resource(resource)
73+
74+
lim_max = []
75+
rlimit = Rlimit.new
76+
ret = FFI::Platform::POSIX.getrlimit(resource, rlimit.pointer)
77+
Errno.handle if ret == -1
78+
79+
[rlimit[:rlim_cur], rlimit[:rlim_max]]
80+
end
81+
6782
def self.setsid
6883
pgid = FFI::Platform::POSIX.setsid
6984
Errno.handle if pgid == -1
@@ -293,7 +308,29 @@ def self.wait2(input_pid=-1, flags=nil)
293308

294309
[pid, status]
295310
end
296-
311+
312+
def self.coerce_rlimit_resource(resource)
313+
case resource
314+
when Integer
315+
return resource
316+
when Symbol, String
317+
# do nothing
318+
else
319+
unless r = Rubinius::Type.check_convert_type(resource, String, :to_str)
320+
return Rubinius::Type.coerce_to resource, Integer, :to_int
321+
end
322+
323+
resource = r
324+
end
325+
326+
constant = "RLIMIT_#{resource}"
327+
unless const_defined? constant
328+
raise ArgumentError, "invalid resource name: #{constant}"
329+
end
330+
const_get constant
331+
end
332+
private_class_method :coerce_rlimit_resource
333+
297334
#--
298335
# TODO: Most of the fields aren't implemented yet.
299336
# TODO: Also, these objects should only need to be constructed by

0 commit comments

Comments
 (0)