[GHC] #9143: feature request: way to set actual program argv

#9143: feature request: way to set actual program argv ----------------------------------------------+---------------------------- Reporter: joeyhess | Owner: Type: feature request | simonmar Priority: normal | Status: new Component: Runtime System | Milestone: Keywords: | Version: 7.8.2 Architecture: Unknown/Multiple | Operating System: Linux Difficulty: Moderate (less than a day) | Type of failure: Blocked By: | None/Unknown Related Tickets: | Test Case: | Blocking: ----------------------------------------------+---------------------------- It's not currently possible to write a program with ghc that changes its argv, so as to change what the program name/parameters appear to be in ps. An example of a program that does this is sshd, which arranges for the process name to say which user it's serving. For example, "sshd: joey [priv]" I'd like to be able to write such programs using haskell too. Also, I have a haskell program that, due to the way it is executed, has a really horrible display in ps: /usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib64/ld-linux-x86-64.so.2 --library-path /usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib/x86_64-linux- gnu:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64-linux- gnu:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib64:/usr/local/propellor/docker /android-git-annex- builder.orca.kitenet.net.propellor.shim/etc/ld.so.conf.d:/usr/local/propellor/docker /android-git-annex-builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64 -linux-gnu/audit:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64-linux-gnu/gconv /usr/local/propellor/propellor That's enough motivation for me to dig into this. :) In rts/RtsMain.c, progargv is set to point to argv. However, it's static, so this cannot be (ab)used from the FFI to change argv. So, a minimal change would be to make progargv not be static, and perhaps give it a more formal name or minimal FFI binding. A GHC-specific library could then use this to modify argv. The haskell interface I'm considering would be: displayArgv :: [String] -> IO () It would need to truncate strings to fit within the available argv space. (This would not affect the argv used by System.Environment, which is a copy of argv.) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9143 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9143: feature request: way to set actual program argv -------------------------------------+------------------------------------- Reporter: joeyhess | Owner: simonmar Type: feature | Status: closed request | Milestone: Priority: normal | Version: 7.8.2 Component: Runtime | Keywords: System | Architecture: Unknown/Multiple Resolution: wontfix | Difficulty: Moderate (less Operating System: Linux | than a day) Type of failure: | Blocked By: None/Unknown | Related Tickets: Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Comment (by joeyhess): prctl can be used to set the name of a thread. This does not change the process name as displayed by ps. (The stackoverflow article is apparently wrong or out of date or something -- try the sample program there and you'll see it does not change the display in ps, although /proc/pid/task/tid/comm will be changed.) At least on linux when using libbsd's setproctitle, it requires setproctitle_init to first be called and passed the original argv. The libbsd-ctor arranges for this to be done at program startup. So, I was able to get that to work by passing these options to ghc: -lbsd-ctor -optl-u -optllibbsd_init_func -lbsd On linux, using a not very widely used libbsd, and needing linker options to make it work, seems like a rather long way around.. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9143#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9143: feature request: way to set actual program argv
-------------------------------------+-------------------------------------
Reporter: joeyhess | Owner:
Type: feature | Status: new
request | Milestone:
Priority: normal | Version: 7.8.2
Component: Runtime | Keywords:
System | Architecture: Unknown/Multiple
Resolution: | Difficulty: Moderate (less
Operating System: Linux | than a day)
Type of failure: | Blocked By:
None/Unknown | Related Tickets:
Test Case: |
Blocking: |
Differential Revisions: |
-------------------------------------+-------------------------------------
Changes (by thomie):
* owner: simonmar =>
* status: closed => new
* resolution: wontfix =>
Comment:
Or maybe that stackoverflow answer is not out of date, but it's just only
one half of the story? I added this comment: "This seems to only change
the name in the output of 'ps -A', 'ps -a' 'ps -d', 'ps -e' and maybe
others, whereas 'ps a', 'ps -ef', 'ps f' and probably others still show
the original command line arguments."
Changing argv[0] would then be the other half of the story, like you
suggested in the description. This C example shows "othername" when using
'ps -ef', but not when doing 'ps -e'. In `top` you can toggle between the
two by hitting the `c` key.
{{{
#include

#9143: feature request: way to set actual program argv -------------------------------------+------------------------------------- Reporter: joeyhess | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 7.8.2 Resolution: | Keywords: Operating System: Linux | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by joeyhess): This stackoverflow answer gives a way to do it without ghc changes and without needing unusual linker options: http://stackoverflow.com/questions/26657699/how-to-change-the-name-of-a -haskell-process-under-linux Still, that seems quite a hack, maybe linux/glibc-specific also? I continue to feel that, since changing the name displayed by ps for a program is easy to accomplish in most any other language, and many unix programs use the ability to do so, it should also be made reasonably easy to do in haskell. Often, a program will want to change argv[0] but not the thread name. For example, ssh will set the argv[0] to various things like "sshd: joey" but leaves the thread name "sshd". So, an interface to prctl should be kept separate from an interface to argv[0]. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9143#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9143: feature request: way to set actual program argv -------------------------------------+------------------------------------- Reporter: joeyhess | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 7.8.2 Resolution: | Keywords: Operating System: Linux | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Description changed by bgamari: Old description:
It's not currently possible to write a program with ghc that changes its argv, so as to change what the program name/parameters appear to be in ps.
An example of a program that does this is sshd, which arranges for the process name to say which user it's serving. For example, "sshd: joey [priv]"
I'd like to be able to write such programs using haskell too. Also, I have a haskell program that, due to the way it is executed, has a really horrible display in ps:
/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib64/ld-linux-x86-64.so.2 --library-path /usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib/x86_64-linux- gnu:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64-linux- gnu:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib64:/usr/local/propellor/docker /android-git-annex- builder.orca.kitenet.net.propellor.shim/etc/ld.so.conf.d:/usr/local/propellor/docker /android-git-annex-builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64 -linux-gnu/audit:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64-linux-gnu/gconv /usr/local/propellor/propellor
That's enough motivation for me to dig into this. :)
In rts/RtsMain.c, progargv is set to point to argv. However, it's static, so this cannot be (ab)used from the FFI to change argv.
So, a minimal change would be to make progargv not be static, and perhaps give it a more formal name or minimal FFI binding. A GHC-specific library could then use this to modify argv.
The haskell interface I'm considering would be:
displayArgv :: [String] -> IO ()
It would need to truncate strings to fit within the available argv space.
(This would not affect the argv used by System.Environment, which is a copy of argv.)
New description: It's not currently possible to write a program with ghc that changes its argv, so as to change what the program name/parameters appear to be in ps. An example of a program that does this is sshd, which arranges for the process name to say which user it's serving. For example, "sshd: joey [priv]" I'd like to be able to write such programs using haskell too. Also, I have a haskell program that, due to the way it is executed, has a really horrible display in ps: {{{ /usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib64/ld-linux-x86-64.so.2 --library-path /usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib/x86_64-linux- gnu:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64-linux- gnu:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/lib64:/usr/local/propellor/docker /android-git-annex- builder.orca.kitenet.net.propellor.shim/etc/ld.so.conf.d:/usr/local/propellor/docker /android-git-annex-builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64 -linux-gnu/audit:/usr/local/propellor/docker/android-git-annex- builder.orca.kitenet.net.propellor.shim/usr/lib/x86_64-linux-gnu/gconv /usr/local/propellor/propellor }}} That's enough motivation for me to dig into this. :) In rts/RtsMain.c, progargv is set to point to argv. However, it's static, so this cannot be (ab)used from the FFI to change argv. So, a minimal change would be to make progargv not be static, and perhaps give it a more formal name or minimal FFI binding. A GHC-specific library could then use this to modify argv. The haskell interface I'm considering would be: {{{ displayArgv :: [String] -> IO () }}} It would need to truncate strings to fit within the available argv space. (This would not affect the argv used by `System.Environment`, which is a copy of argv.) -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9143#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9143: feature request: way to set actual program argv -------------------------------------+------------------------------------- Reporter: joeyhess | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 7.8.2 Resolution: | Keywords: Operating System: Linux | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by bgamari): The cross-platform implications really worry me here. For instance, it's recommended that you do not modify `argv` at all on [[http://stackoverflow.com/questions/29158052/changing-argv0-with- winapi|Windows]]. Do we know that this is really safe (or will even accomplish the desired effect) on all of the platforms that GHC supports (FreeBSD, OpenBSD, Darwin, Linux, Solaris, and Windows, where it is not)? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9143#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9143: feature request: way to set actual program argv -------------------------------------+------------------------------------- Reporter: joeyhess | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Runtime System | Version: 7.8.2 Resolution: | Keywords: Operating System: Linux | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by pgj): As far as I know, on FreeBSD, the recommended method for changing the value of `argv[0]` is calling the [https://www.freebsd.org/cgi/man.cgi?query=setproctitle&sektion=3 setproctitle(3)] function. I am not sure if rest of `argv` could be changed safely. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9143#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC