Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 581251e62f34
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4e60ecd8092c
Choose a head ref
  • 4 commits
  • 4 files changed
  • 1 contributor

Commits on Dec 2, 2016

  1. Copy the full SHA
    fef8836 View commit details
  2. Copy the full SHA
    d59ef3d View commit details
  3. [Truffle] Cache the address of argv[0] as MRI tests change the proces…

    …s title for every test.
    eregon committed Dec 2, 2016
    Copy the full SHA
    e4187e7 View commit details
  4. Merge pull request #4354 from eregon/truffle_argv0_linux

    [Truffle] Implement changing argv[0] on Linux
    eregon authored Dec 2, 2016
    Copy the full SHA
    4e60ecd View commit details
1 change: 0 additions & 1 deletion spec/truffle/tags/language/predefined_tags.txt
Original file line number Diff line number Diff line change
@@ -3,6 +3,5 @@ slow:The predefined global constant STDIN retains the encoding set by #set_encod
slow:The predefined global constant STDIN has the encodings set by #set_encoding
slow:The predefined global constant STDOUT has the encodings set by #set_encoding
slow:The predefined global constant ARGV contains Strings encoded in locale Encoding
linux:Global variable $0 actually sets the program name
slow:Global variable $0 is the path given as the main script and the same as __FILE__
slow:Global variable $? is thread-local
4 changes: 4 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/core/CoreLibrary.java
Original file line number Diff line number Diff line change
@@ -1357,6 +1357,10 @@ public DynamicObject getProcClass() {
return procClass;
}

public DynamicObject getProcessModule() {
return processModule;
}

public DynamicObject getRangeClass() {
return rangeClass;
}
Original file line number Diff line number Diff line change
@@ -1765,8 +1765,10 @@ public RubyNode visitGlobalAsgnNode(GlobalAsgnParseNode node) {
final RubyNode translated;

if (name.equals("$0")) {
translated = VMPrimitiveNodesFactory.VMSetProcessTitleNodeFactory.create(
new RubyNode[]{ writeGlobalVariableNode });
// Call Process.setproctitle
RubyNode processClass = new ObjectLiteralNode(context, fullSourceSection, context.getCoreLibrary().getProcessModule());
translated = new RubyCallNode(new RubyCallNodeParameters(context, fullSourceSection, processClass, "setproctitle", null,
new RubyNode[]{writeGlobalVariableNode}, false, false));
} else {
translated = writeGlobalVariableNode;
}
54 changes: 53 additions & 1 deletion truffle/src/main/ruby/core/process.rb
Original file line number Diff line number Diff line change
@@ -129,8 +129,60 @@ def self.cpu_times
#
def self.setproctitle(title)
val = Rubinius::Type.coerce_to(title, String, :to_str)
Truffle.invoke_primitive(:vm_set_process_title, val)
if RbConfig::CONFIG["host_os"] == "linux" and File.readable?("/proc/self/maps")
setproctitle_linux_from_proc_maps(val)
else
Truffle.invoke_primitive(:vm_set_process_title, val)
end
end

# Very hacky implementation to pass the specs, since the JVM doesn't give us argv[0]
# Overwrite *argv inplace because finding the argv pointer itself is harder.
# Truncates title if title would cover our org.jruby (main class) marker.
def self.setproctitle_linux_from_proc_maps(title)
@_argv0_address ||= begin
command = File.binread("/proc/self/cmdline")

stack = File.readlines("/proc/self/maps").grep(/\[stack\]/)
raise stack.to_s unless stack.size == 1

from, to = stack[0].split[0].split('-').map { |addr| Integer(addr, 16) }
raise unless from < to

original_argv = Truffle::Boot.original_argv
args_length = original_argv.reduce(0) { |bytes,arg| bytes + 1 + arg.bytesize }
page = 4096
size = 2 * page
size += args_length
base = to - size
base_ptr = FFI::Pointer.new(FFI::Type::CHAR, base)
haystack = base_ptr.read_string(size)

main_index = command.index("\x00org.jruby")
raise "Did not find the main class in args" unless main_index
needle = command[0...main_index]
i = haystack.index("\x00\x00#{needle}")
raise "argv[0] not found" unless i
i += 2

@_argv0_max_length = needle.bytesize
base + i
end

if title.bytesize > @_argv0_max_length
title = title.byteslice(0, @_argv0_max_length)
end
new_title = title + "\x00" * (@_argv0_max_length - title.bytesize)

argv0_ptr = FFI::Pointer.new(FFI::Type::CHAR, @_argv0_address)
argv0_ptr.write_string(new_title)

new_command = File.binread("/proc/self/cmdline")
raise "failed" unless new_command.start_with?(new_title)

title
end
private_class_method :setproctitle_linux_from_proc_maps

def self.setrlimit(resource, cur_limit, max_limit=undefined)
resource = coerce_rlimit_resource(resource)