Writing to argv[0]

View: New views
12 Messages — Rating Filter:   Alert me  

Writing to argv[0]

by Dave Benjamin-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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

Re: Writing to argv[0]

by Basile Starynkevitch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: Writing to argv[0]

by Savanninalkavir D'Gerinel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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


_______________________________________________
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

PGP.sig (195 bytes) Download Attachment

Re: Writing to argv[0]

by Dave Benjamin-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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

Re: Writing to argv[0]

by Dave Benjamin-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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

Re: Writing to argv[0]

by Dave Benjamin-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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

Re: Writing to argv[0]

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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

Re: Writing to argv[0]

by Dave Benjamin-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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

Re: Writing to argv[0]

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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

Re: Writing to argv[0]

by jjnoakes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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  >>


_______________________________________________
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

Re: Writing to argv[0]

by Dave Benjamin-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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

Re: Writing to argv[0]

by Dave Benjamin-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
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
LightInTheBox - Buy quality products at wholesale price!