From: "Brian F. Feldman" Date: 2001-09-01T14:05:54+09:00 Subject: [ruby-talk:20655] Re: Changing a program's name at run time via $0... Sean Chittenden wrote: > Anyone have any ideas how to change a program's name while it's > being executed (specifically on FreeBSD)? > > #!/usr/local/bin/ruby -w > $0 = 'other_name' > sleep(60) > > (quickly dash to another keyboard and type ps (auxwww|-ef)) > Doesn't seem to work... <:~( Anyone else have any luck trying this or > thoughts on how to get it to work? -sc Ruby needs to be updated to use setproctitle(3) on platforms which have it. E.g. I get: irb(main):012:0> puts `ps p #{$$}`.split(/\n/)[1] 20276 p5 S+ 0:00.98 ruby: irb (ruby) nil irb(main):013:0> $0 = "proctitle!" "proctitle!" irb(main):014:0> puts `ps p #{$$}`.split(/\n/)[1] 20276 p5 S+ 0:01.07 ruby: proctitle! (ruby) nil if I apply this change: --- ruby.c 2001/07/14 15:17:19 1.49 +++ ruby.c 2001/09/01 05:05:28 @@ -885,7 +885,7 @@ static int len; if (origargv == 0) rb_raise(rb_eRuntimeError, "$0 not initialized"); -#ifndef __hpux +#if !defined(__hpux) && !defined(__FreeBSD__) && !defined(__NetBSD__) if (len == 0) { s = origargv[0]; s += strlen(s); @@ -900,20 +900,7 @@ StringValue(val); s = RSTRING(val)->ptr; i = RSTRING(val)->len; -#ifndef __hpux - if (i < len) { - memcpy(origargv[0], s, i); - origargv[0][i] = '\0'; - } - else { - memcpy(origargv[0], s, i); - s = origargv[0]+i; - *s++ = '\0'; - while (++i < len) - *s++ = ' '; - } - rb_progname = rb_tainted_str_new2(origargv[0]); -#else +#ifdef __hpux if (i >= PST_CLEN) { union pstun j; j.pst_command = s; @@ -928,6 +915,22 @@ pstat(PSTAT_SETCMD, j, i, 0, 0); } rb_progname = rb_tainted_str_new(s, i); +#elif defined(__FreeBSD__) || defined(__NetBSD__) + setproctitle("%.*s", i, s); + rb_progname = rb_tainted_str_new(s, i); +#else + if (i < len) { + memcpy(origargv[0], s, i); + origargv[0][i] = '\0'; + } + else { + memcpy(origargv[0], s, i); + s = origargv[0]+i; + *s++ = '\0'; + while (++i < len) + *s++ = ' '; + } + rb_progname = rb_tainted_str_new2(origargv[0]); #endif } -- Brian Fundakowski Feldman \ FreeBSD: The Power to Serve! / green@FreeBSD.org `------------------------------'