1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add Process.setproctitle().

* ruby.c (Process.setproctitle): New method to change the title of
  the running process that is shown in ps(1). [Feature #8696]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42425 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2013-08-07 14:12:04 +00:00
parent fe6273975e
commit 34bb945c40
4 changed files with 67 additions and 9 deletions

View file

@ -1,3 +1,8 @@
Wed Aug 7 23:05:55 2013 Akinori MUSHA <knu@iDaemons.org>
* ruby.c (Process.setproctitle): New method to change the title of
the running process that is shown in ps(1). [Feature #8696]
Wed Aug 7 20:05:38 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (rb_big_odd_p): Check the bignum length.

4
NEWS
View file

@ -48,6 +48,10 @@ with all sufficient information, see the ChangeLog file.
* misc
* Mutex#owned? is no longer experimental.
* Process
* New methods:
* Process.setproctitle() sets the process title without affecting $0.
* String
* New methods:
* String#scrub and String#scrub! verify and fix invalid byte sequence.

48
ruby.c
View file

@ -1780,21 +1780,49 @@ rb_load_file_str(VALUE fname_v)
return load_file(rb_parser_new(), fname_v, 0, cmdline_options_init(&opt));
}
/*
* call-seq:
* Process.argv0 -> frozen_string
*
* Returns the name of the script being executed. The value is not
* affected by assigning a new value to $0.
*/
static VALUE
proc_argv0(VALUE process)
{
return rb_orig_progname;
}
/*
* call-seq:
* Process.setproctitle(string) -> string
*
* Returns the process title that appears on the ps(1) command. Not
* necessarily effective on all platforms.
*
* Calling this method does not affect the value of $0.
*
* Process.setproctitle('myapp: worker #%d' % worker_id)
*/
static VALUE
proc_setproctitle(VALUE process, VALUE title)
{
StringValue(title);
setproctitle("%.*s", RSTRING_LENINT(title), RSTRING_PTR(title));
return title;
}
static void
set_arg0(VALUE val, ID id)
{
char *s;
long i;
if (origarg.argv == 0)
rb_raise(rb_eRuntimeError, "$0 not initialized");
StringValue(val);
s = RSTRING_PTR(val);
i = RSTRING_LEN(val);
setproctitle("%.*s", (int)i, s);
rb_progname = rb_obj_freeze(rb_external_str_new(s, i));
rb_progname = rb_str_new_frozen(proc_setproctitle(rb_mProcess, val));
}
/*! Sets the current script name to this value.
@ -1886,6 +1914,8 @@ ruby_prog_init(void)
rb_define_hooked_variable("$0", &rb_progname, 0, set_arg0);
rb_define_hooked_variable("$PROGRAM_NAME", &rb_progname, 0, set_arg0);
rb_define_module_function(rb_mProcess, "setproctitle", proc_setproctitle, 1);
/*
* ARGV contains the command line arguments used to run ruby with the
* first value containing the name of the executable.

View file

@ -477,6 +477,25 @@ class TestRubyOptions < Test::Unit::TestCase
end
end
def test_setproctitle
skip "platform dependent feature" if /linux|freebsd|netbsd|openbsd|darwin/ !~ RUBY_PLATFORM
with_tmpchdir do
write_file("test-script", "$_0 = $0.dup; Process.setproctitle('hello world'); $0 == $_0 or Process.setproctitle('$0 changed!'); sleep 60")
pid = spawn(EnvUtil.rubybin, "test-script")
ps = nil
10.times do
sleep 0.1
ps = `ps -p #{pid} -o command`
break if /hello world/ =~ ps
end
assert_match(/hello world/, ps)
Process.kill :KILL, pid
Process.wait(pid)
end
end
module SEGVTest
opts = {}
if /mswin|mingw/ =~ RUBY_PLATFORM