diff --git a/bindings/SigarWrapper.pm b/bindings/SigarWrapper.pm index 205185b9..dcc3f19a 100644 --- a/bindings/SigarWrapper.pm +++ b/bindings/SigarWrapper.pm @@ -2350,7 +2350,7 @@ sub generate_class { if ($func->{num_args} == 1) { my $arg_type; if ($func->{is_proc}) { - $arg_type = 'NUM2UINT'; + $arg_type = 'OBJ2PID'; } else { $arg_type = 'StringValuePtr'; diff --git a/bindings/ruby/examples/pargs.rb b/bindings/ruby/examples/pargs.rb index 8872ae74..92c7876b 100644 --- a/bindings/ruby/examples/pargs.rb +++ b/bindings/ruby/examples/pargs.rb @@ -14,5 +14,5 @@ end sigar = Sigar.new ARGV.each do |pid| - output(sigar, pid.to_i) + output(sigar, pid) end diff --git a/bindings/ruby/examples/penv.rb b/bindings/ruby/examples/penv.rb index 2fb4ef54..1ca054c8 100644 --- a/bindings/ruby/examples/penv.rb +++ b/bindings/ruby/examples/penv.rb @@ -10,5 +10,5 @@ end sigar = Sigar.new ARGV.each do |pid| - output(sigar, pid.to_i) + output(sigar, pid) end diff --git a/bindings/ruby/rbsigar.c b/bindings/ruby/rbsigar.c index f1372b1f..a9acb012 100644 --- a/bindings/ruby/rbsigar.c +++ b/bindings/ruby/rbsigar.c @@ -21,9 +21,11 @@ #include "sigar.h" #include "sigar_fileinfo.h" #include "sigar_format.h" +#include "sigar_ptql.h" -#define RB_SIGAR_CROAK rb_raise(rb_eArgError, "%s", sigar_strerror(sigar, status)) -#define NUM2PID NUM2UINT +#define RB_SIGAR_RAISE(msg) rb_raise(rb_eArgError, "%s", msg) +#define RB_SIGAR_CROAK RB_SIGAR_RAISE(sigar_strerror(sigar, status)) +#define OBJ2PID(pid) rb_sigar_pid_get(sigar, pid) #ifndef RSTRING_PTR #define RSTRING_PTR(s) RSTRING(s)->ptr @@ -40,6 +42,50 @@ static sigar_t *rb_sigar_get(VALUE obj) return sigar; } +#define sigar_isdigit(c) \ + (isdigit(((unsigned char)(c)))) + +static sigar_pid_t rb_sigar_pid_get(sigar_t *sigar, VALUE obj) +{ + if (TYPE(obj) == T_STRING) { + char *pid = StringValuePtr(obj); + + if (sigar_isdigit(*pid)) { + obj = rb_str2inum(obj, 10); + /* fallthru */ + } + else if ((RSTRING_LEN(obj) == 2) && + (*pid == '$') && (*(pid + 1) == '$')) + { + return sigar_pid_get(sigar); + } + else { + /* XXX cache queries */ + sigar_ptql_query_t *query; + sigar_ptql_error_t error; + int status = + sigar_ptql_query_create(&query, (char *)pid, &error); + + if (status == SIGAR_OK) { + sigar_pid_t qpid; + + status = sigar_ptql_query_find_process(sigar, query, &qpid); + sigar_ptql_query_destroy(query); + if (status == SIGAR_OK) { + return qpid; + } + else { + RB_SIGAR_RAISE(sigar_strerror(sigar, status)); + } + } + else { + RB_SIGAR_RAISE(error.message); + } + } + } + return NUM2UINT(obj); +} + static void rb_sigar_free(void *obj) { free(obj); @@ -359,7 +405,7 @@ static VALUE rb_sigar_proc_args(VALUE obj, VALUE pid) sigar_proc_args_t args; VALUE RETVAL; - status = sigar_proc_args_get(sigar, NUM2PID(pid), &args); + status = sigar_proc_args_get(sigar, OBJ2PID(pid), &args); if (status != SIGAR_OK) { RB_SIGAR_CROAK; @@ -393,7 +439,7 @@ static VALUE rb_sigar_proc_env(VALUE obj, VALUE pid) procenv.env_getter = rb_sigar_env_getall; procenv.data = &RETVAL; - status = sigar_proc_env_get(sigar, NUM2PID(pid), &procenv); + status = sigar_proc_env_get(sigar, OBJ2PID(pid), &procenv); if (status != SIGAR_OK) { RB_SIGAR_CROAK; }