Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting argv[0] in Haskell?

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
like image 569
ErikR Avatar asked Feb 29 '12 16:02

ErikR


People also ask

How do I get arguments from the command line in Haskell?

Let's start by looking at the command line argument processing code. The basic way to get arguments in a Haskell program is provided by the System.Environment library. We can use the getArgs function: Which is empty, since we didn't provide any arguments!

What is the best way to use TAC in Haskell?

It's also reasonably careful about setting exit status on finishing, using the functions from System.Exit. The actual core algorithm for 'tac' is a nice pure Haskell function, and really all the hard work is done processing the command line args. $ ./tac -h Usage: tac [-vh] [file ..]

How to test getArgs with no command line ARGs?

you can test it with no command line args like this: and then give it a few test command line args like this: As I show in the comments that I added, getArgs has the type IO [String], and progName has the type IO String.

Can I start writing Unix scripts in Haskell?

Lesson: you can start writing your unix scripts in Haskell right now. They'll be flexible, clean, and easy to maintain. And most of all, fun to write!


1 Answers

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.

like image 199
hammar Avatar answered Nov 12 '22 01:11

hammar