Setting argv[0] in Haskell?

前端 未结 2 1296
轻奢々
轻奢々 2021-01-12 02:19

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

I found the getProgName and withProgName func

2条回答
  •  轮回少年
    2021-01-12 02:32

    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.

提交回复
热议问题