summaryrefslogtreecommitdiff
path: root/ruby.c
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-08-07 14:12:04 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-08-07 14:12:04 +0000
commit34bb945c40beb9998e3e33ed603a5841826128c5 (patch)
tree94512da7bd44ad0ba489c205882ee9a03eb9a368 /ruby.c
parentfe6273975eb7a8b8bbc48ef62dc74b7a1a8c10e8 (diff)
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
Diffstat (limited to 'ruby.c')
-rw-r--r--ruby.c48
1 files changed, 39 insertions, 9 deletions
diff --git a/ruby.c b/ruby.c
index df3f08926..6c9061c 100644
--- a/ruby.c
+++ b/ruby.c
@@ -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.