Search code examples
linuxhaskellroot

Checking root permissions from Haskell


My program requires elevated permissions and hence it crashes when run without these.

Is there a way of checking whether a Haskell program has been run as the root on Linux?

I.e., differentiating between

sudo ./myProgram

and

./myprogram

Solution

  • From the unix package, System.Posix.User:

    getRealUserID :: IO UserID
    getEffectiveUserID :: IO UserID
    

    When run under sudo, I think they should both report 0 (root).

    When run as a setuid, only the second one should report 0 (if I remember correctly).

    The man pages for getuid / geteuid should clarify, I hope.