Setting argv[0] in Haskell?
Asked Answered
S

2

9

Is there a way to set argv[0] in a Haskell program (say, one compiled with ghc)?

I found the getProgName and withProgName functions in System.Environment, but it doesn't seem to change what ps reports (Ubuntu).

import System.Environment

main =
  do name <- getProgName
     putStrLn $ "Hello, my name is " ++ name
     withProgName "other" $ do
       newname <- getProgName
       putStrLn $ "Name now set to " ++ newname
       putStrLn "What is your name: "
       -- allow time to run ps
       ans <- getLine
       putStrLn $ "Pleased to meet you, " ++ ans
Staffordshire answered 29/2, 2012 at 16:46 Comment(1)
There is no reliable and portable way to change what ps reports, so I wouldn't be surprised if GHC does not support this.Blasto
B
16

There is no portable way of doing this, but on Linux 2.6.9 and up the process name can be changed with prctl() using the PR_SET_NAME operation, so we just need a little bit of FFI to use it from Haskell. (It's usually a good idea to check if there are any bindings on Hackage, but in this case I couldn't find any).

{-# LANGUAGE ForeignFunctionInterface #-}

import Foreign.C

foreign import ccall "sys/prctl.h prctl"
  prctl :: CInt -> CString -> CULong -> CULong -> CULong -> IO CInt

setProgName :: String -> IO ()
setProgName title =
  withCString title $ \title' -> do
    res <- prctl pr_set_name title' 0 0 0
    return ()
  where pr_set_name = 15

This seems to work fine for changing the name as seen by ps. However, the value returned by getProgName appears to be cached when the program starts, so you'll have to combine this with withProgName to see the change within your program.

Bronchoscope answered 29/2, 2012 at 17:49 Comment(1)
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.Redhanded
S
3

The program name is fixed at the time the program starts, so any mechanism to change the reported program name will be OS-specific. As far as I know, there's no way to do this with the standard libraries, and a quick search of Hackage doesn't show anything up. I'm not sure there's any way to accomplish this with Linux in the first place, other than re-executing the same program with a different argv[0].

Striped answered 29/2, 2012 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.