caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Writing to argv[0]
@ 2008-10-09 15:26 Dave Benjamin
  2008-10-09 15:31 ` [Caml-list] " Basile STARYNKEVITCH
  0 siblings, 1 reply; 12+ messages in thread
From: Dave Benjamin @ 2008-10-09 15:26 UTC (permalink / raw)
  To: caml-list

Hi,

Is there any way for an OCaml program to change its name in the process 
table? Assigning to Sys.argv.(0) has no effect.

Thanks,
Dave


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-09 15:26 Writing to argv[0] Dave Benjamin
@ 2008-10-09 15:31 ` Basile STARYNKEVITCH
  2008-10-09 15:43   ` Savanni D'Gerinel
                     ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Basile STARYNKEVITCH @ 2008-10-09 15:31 UTC (permalink / raw)
  To: Dave Benjamin; +Cc: caml-list

Dave Benjamin wrote:
> Hi,
> 
> Is there any way for an OCaml program to change its name in the process 
> table? Assigning to Sys.argv.(0) has no effect.


Write a C wrapper for that. And this trick seems Linux specific (it has 
no sense in Posix) -maybe working on few other OSes.

Regards.

-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mines, sont seulement les miennes} ***


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-09 15:31 ` [Caml-list] " Basile STARYNKEVITCH
@ 2008-10-09 15:43   ` Savanni D'Gerinel
  2008-10-10  4:58     ` Dave Benjamin
  2008-10-10  4:53   ` Dave Benjamin
  2008-10-10 15:11   ` Richard Jones
  2 siblings, 1 reply; 12+ messages in thread
From: Savanni D'Gerinel @ 2008-10-09 15:43 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1649 bytes --]

One possibility is setproctitle.  It appears to be non-standard, but  
maybe a bit more standard than overwriting argv[0].  I know it works  
on Linux, and I see a FreeBSD man page for it.

"The setproctitle() function is implicitly non-standard.  Other  
methods of
causing the ps(1) command line to change, including copying over the
argv[0] string are also implicitly non-portable.  It is preferable to  
use
an operating system supplied setproctitle() if present."

I do not know whether there is an OCaml function to do this, and it  
might actually be complicated because of the variable length argument  
list.  I have not written enough ocaml -> C bindings to have any idea  
how to handle that.

--
Savanni

On Oct 9, 2008, at 10:31 AM, Basile STARYNKEVITCH wrote:

> Dave Benjamin wrote:
>> Hi,
>> Is there any way for an OCaml program to change its name in the  
>> process table? Assigning to Sys.argv.(0) has no effect.
>
>
> Write a C wrapper for that. And this trick seems Linux specific (it  
> has no sense in Posix) -maybe working on few other OSes.
>
> Regards.
>
> -- 
> Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
> email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
> 8, rue de la Faiencerie, 92340 Bourg La Reine, France
> *** opinions {are only mines, sont seulement les miennes} ***
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 188 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-09 15:31 ` [Caml-list] " Basile STARYNKEVITCH
  2008-10-09 15:43   ` Savanni D'Gerinel
@ 2008-10-10  4:53   ` Dave Benjamin
  2008-10-10 13:26     ` Dave Benjamin
  2008-10-10 15:11   ` Richard Jones
  2 siblings, 1 reply; 12+ messages in thread
From: Dave Benjamin @ 2008-10-10  4:53 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: caml-list

Basile STARYNKEVITCH wrote:
> Dave Benjamin wrote:
>> Hi,
>>
>> Is there any way for an OCaml program to change its name in the 
>> process table? Assigning to Sys.argv.(0) has no effect.
> 
> 
> Write a C wrapper for that. And this trick seems Linux specific (it has 
> no sense in Posix) -maybe working on few other OSes.

It doesn't seem like it's possible, even if I drop to C. Writing to 
"caml_exe_name" does nothing, and "caml_main_argv" is declared static.

Oh well - judging from what I've read, it seems this technique has 
always been somewhat of a hack anyway...

Thanks,
Dave


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-09 15:43   ` Savanni D'Gerinel
@ 2008-10-10  4:58     ` Dave Benjamin
  0 siblings, 0 replies; 12+ messages in thread
From: Dave Benjamin @ 2008-10-10  4:58 UTC (permalink / raw)
  To: Savanni D'Gerinel; +Cc: caml-list

Savanni D'Gerinel wrote:
> One possibility is setproctitle.  It appears to be non-standard, but 
> maybe a bit more standard than overwriting argv[0].  I know it works on 
> Linux, and I see a FreeBSD man page for it.

Thanks, this was an interesting idea, though apparently setproctitle is 
not available on Linux, only BSD. There have been some attempts to 
expose it to Linux, including this one for Perl:

http://search.cpan.org/~opi/Sys-Proctitle-0.03/

You can see the gory details here:

http://search.cpan.org/src/OPI/Sys-Proctitle-0.03/libsetproctitle/setproctitle.c

It appears that it is possible to do this with OCaml, but it involves 
building and linking in a shared library that defines the obsolete _init 
method, fiddles around with memory relocating the environment and a 
bunch of other nasty stuff I don't even want to understand. If it was 
something simple like wrapping a libc function, I'd go for it, but this 
is all too complicated for such a simple, stupid hack. =)

Thanks,
Dave


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-10  4:53   ` Dave Benjamin
@ 2008-10-10 13:26     ` Dave Benjamin
  2008-10-10 18:48       ` Jason Noakes
  0 siblings, 1 reply; 12+ messages in thread
From: Dave Benjamin @ 2008-10-10 13:26 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: caml-list

Dave Benjamin wrote:
> Basile STARYNKEVITCH wrote:
>> Write a C wrapper for that. And this trick seems Linux specific (it 
>> has no sense in Posix) -maybe working on few other OSes.
> 
> It doesn't seem like it's possible, even if I drop to C. Writing to 
> "caml_exe_name" does nothing, and "caml_main_argv" is declared static.

Just as I was about to give up, I found this thread, which alerted me to 
GNU's "program_invocation_name" global:

http://groups.google.com/group/comp.unix.programmer/browse_thread/thread/99acb4dff3707e56/003e46ff836f5d9e

So, I finally got it to work in OCaml. This is GNU/Linux only, most likely.

proctitle.idl:

quote(c, "extern char * program_invocation_name;");

[string] char * getproctitle(void)
     quote(call, "_res = program_invocation_name;");

void setproctitle([string] char * proctitle)
     quote(call, "strcpy(program_invocation_name, proctitle);");

test.ml:

let () =
   Proctitle.setproctitle "testing setproctitle";
   print_endline (Proctitle.getproctitle ());
   Unix.sleep 10


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-09 15:31 ` [Caml-list] " Basile STARYNKEVITCH
  2008-10-09 15:43   ` Savanni D'Gerinel
  2008-10-10  4:53   ` Dave Benjamin
@ 2008-10-10 15:11   ` Richard Jones
  2008-10-10 16:33     ` Dave Benjamin
  2 siblings, 1 reply; 12+ messages in thread
From: Richard Jones @ 2008-10-10 15:11 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: Dave Benjamin, caml-list

On Thu, Oct 09, 2008 at 05:31:11PM +0200, Basile STARYNKEVITCH wrote:
> Dave Benjamin wrote:
> >Hi,
> >
> >Is there any way for an OCaml program to change its name in the process 
> >table? Assigning to Sys.argv.(0) has no effect.
> 
> 
> Write a C wrapper for that. And this trick seems Linux specific (it has 
> no sense in Posix) -maybe working on few other OSes.

http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/misc/ps_status.c?rev=1.33.2.1;content-type=text%2Fplain

PostgreSQL offers this code to change the proc title on many different
platforms.

Rich.

-- 
Richard Jones
Red Hat


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-10 15:11   ` Richard Jones
@ 2008-10-10 16:33     ` Dave Benjamin
  2008-10-10 16:59       ` Richard Jones
  0 siblings, 1 reply; 12+ messages in thread
From: Dave Benjamin @ 2008-10-10 16:33 UTC (permalink / raw)
  To: Richard Jones; +Cc: Basile STARYNKEVITCH, caml-list

On Fri, 10 Oct 2008, Richard Jones wrote:

> On Thu, Oct 09, 2008 at 05:31:11PM +0200, Basile STARYNKEVITCH wrote:
>> Dave Benjamin wrote:
>>> Is there any way for an OCaml program to change its name in the process
>>> table? Assigning to Sys.argv.(0) has no effect.
>>
>> Write a C wrapper for that. And this trick seems Linux specific (it has
>> no sense in Posix) -maybe working on few other OSes.
>
> http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/misc/ps_status.c?rev=1.33.2.1;content-type=text%2Fplain
>
> PostgreSQL offers this code to change the proc title on many different
> platforms.

Thanks, Rich - this looks very comprehensive - they even go out of their 
way to support Windows! =) However, unless I'm mistaken, I still have the 
problem of not being able to access "argc" and "argv" to pass into 
save_ps_display_args(). I suppose I could embed OCaml in a C program...

Dave


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-10 16:33     ` Dave Benjamin
@ 2008-10-10 16:59       ` Richard Jones
  2008-10-10 20:19         ` Dave Benjamin
  0 siblings, 1 reply; 12+ messages in thread
From: Richard Jones @ 2008-10-10 16:59 UTC (permalink / raw)
  To: Dave Benjamin; +Cc: Basile STARYNKEVITCH, caml-list

On Fri, Oct 10, 2008 at 09:33:17AM -0700, Dave Benjamin wrote:
> On Fri, 10 Oct 2008, Richard Jones wrote:
> 
> >On Thu, Oct 09, 2008 at 05:31:11PM +0200, Basile STARYNKEVITCH wrote:
> >>Dave Benjamin wrote:
> >>>Is there any way for an OCaml program to change its name in the process
> >>>table? Assigning to Sys.argv.(0) has no effect.
> >>
> >>Write a C wrapper for that. And this trick seems Linux specific (it has
> >>no sense in Posix) -maybe working on few other OSes.
> >
> >http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/misc/ps_status.c?rev=1.33.2.1;content-type=text%2Fplain
> >
> >PostgreSQL offers this code to change the proc title on many different
> >platforms.
> 
> Thanks, Rich - this looks very comprehensive - they even go out of their 
> way to support Windows! =) However, unless I'm mistaken, I still have the 
> problem of not being able to access "argc" and "argv" to pass into 
> save_ps_display_args(). I suppose I could embed OCaml in a C program...

Yes, it was intended more as a joke rather than a serious suggestion :-)

I was also talking to Jim Meyering about the possibility of adding
this code to gnulib[1] which would make it more widely available to
all programs.

Rich.

[1] http://www.gnu.org/software/gnulib/

-- 
Richard Jones
Red Hat


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-10 13:26     ` Dave Benjamin
@ 2008-10-10 18:48       ` Jason Noakes
  2008-10-10 20:11         ` Dave Benjamin
  0 siblings, 1 reply; 12+ messages in thread
From: Jason Noakes @ 2008-10-10 18:48 UTC (permalink / raw)
  To: caml-list

On Fri, October 10, 2008 9:26 am, Dave Benjamin wrote:
> So, I finally got it to work in OCaml. This is GNU/Linux only, most
> likely.
>
> proctitle.idl:
>
> quote(c, "extern char * program_invocation_name;");
>
> [string] char * getproctitle(void)
>      quote(call, "_res = program_invocation_name;");
>
> void setproctitle([string] char * proctitle)
>      quote(call, "strcpy(program_invocation_name, proctitle);");
>
> test.ml:
>
> let () =
>    Proctitle.setproctitle "testing setproctitle";
>    print_endline (Proctitle.getproctitle ());
>    Unix.sleep 10

Just be careful with that. You are copying over memory and you also aren't
doing any bounds checking. A long enough "Proctitle.setproctitle" argument
will corrupt your process.

-JJ

--

<<  Jason J. Noakes  >>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-10 18:48       ` Jason Noakes
@ 2008-10-10 20:11         ` Dave Benjamin
  0 siblings, 0 replies; 12+ messages in thread
From: Dave Benjamin @ 2008-10-10 20:11 UTC (permalink / raw)
  To: Jason Noakes; +Cc: caml-list

On Fri, 10 Oct 2008, Jason Noakes wrote:

> Just be careful with that. You are copying over memory and you also 
> aren't doing any bounds checking. A long enough "Proctitle.setproctitle" 
> argument will corrupt your process.

Good point. Even a moderately long argument will corrupt the environment:

# Proctitle.setproctitle "Mary had a little lamb, whose fleece was white 
as snow.";;
- : unit = ()
# Unix.environment ();;
- : string array =
[|"little lamb, whose fleece was white as snow.";
   ", whose fleece was white as snow."; " was white as snow."; "w.";
   ...

That is why you not only need bounds checking but relocation of the 
environment to do this safely.

Thanks,
Dave


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Writing to argv[0]
  2008-10-10 16:59       ` Richard Jones
@ 2008-10-10 20:19         ` Dave Benjamin
  0 siblings, 0 replies; 12+ messages in thread
From: Dave Benjamin @ 2008-10-10 20:19 UTC (permalink / raw)
  To: Richard Jones; +Cc: Basile STARYNKEVITCH, caml-list

On Fri, 10 Oct 2008, Richard Jones wrote:

> On Fri, Oct 10, 2008 at 09:33:17AM -0700, Dave Benjamin wrote:
>>
>> Thanks, Rich - this looks very comprehensive - they even go out of their
>> way to support Windows! =) However, unless I'm mistaken, I still have the
>> problem of not being able to access "argc" and "argv" to pass into
>> save_ps_display_args(). I suppose I could embed OCaml in a C program...
>
> Yes, it was intended more as a joke rather than a serious suggestion :-)

I'm starting to wonder if this all isn't one big joke that went over my 
head. =)

> I was also talking to Jim Meyering about the possibility of adding
> this code to gnulib[1] which would make it more widely available to
> all programs.

Yeah, it would be really nice to get a safe version of this feature 
packaged up somewhere that could be installed easily. If you search the 
web for "libproctitle" you'll find a library by Dmitry V. Levin which it 
looks like he was trying to get into Debian earlier this year. I couldn't 
find it in unstable, though. There are RPMs available here:

http://sisyphus.ru/srpm/Sisyphus/setproctitle/get

That Perl module also has the low-level stuff broken out into a C library, 
though it doesn't appear like any attempt was made to release it 
independently:

http://search.cpan.org/src/OPI/Sys-Proctitle-0.03/libsetproctitle/

Hmm. Maybe it's time to replace "ps" with something more modern... this is 
all so dirty, yet it seems like monitoring process status is still a 
useful thing to be able to do, especially with the proliferation of 
multi-process servers.

Dave


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2008-10-10 20:19 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-09 15:26 Writing to argv[0] Dave Benjamin
2008-10-09 15:31 ` [Caml-list] " Basile STARYNKEVITCH
2008-10-09 15:43   ` Savanni D'Gerinel
2008-10-10  4:58     ` Dave Benjamin
2008-10-10  4:53   ` Dave Benjamin
2008-10-10 13:26     ` Dave Benjamin
2008-10-10 18:48       ` Jason Noakes
2008-10-10 20:11         ` Dave Benjamin
2008-10-10 15:11   ` Richard Jones
2008-10-10 16:33     ` Dave Benjamin
2008-10-10 16:59       ` Richard Jones
2008-10-10 20:19         ` Dave Benjamin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).