[xmonad] sh-less spawn?

Joachim Breitner mail at joachim-breitner.de
Mon Sep 20 16:06:40 EDT 2010


I just noticed this in my process list:

  │           ├─xmonad-x86_64-l
  │           │   └─sh -c gnome-terminal
  │           │       └─gnome-terminal

and the reason is here, in Core.hs:

-- | Like 'spawn', but returns the 'ProcessID' of the launched application
spawnPID :: MonadIO m => String -> m ProcessID
spawnPID x = io . forkProcess . finally nullStdin $ do
                executeFile "/bin/sh" False ["-c", x] Nothing
    nullStdin = do
        fd <- openFd "/dev/null" ReadOnly Nothing defaultFileFlags
        dupTo fd stdInput
        closeFd fd

Generally, this is of course useful. But I bet that most commands passed
to spawn are simple program names without arguments or any fancy
shell-related stuff. Executing a shell here seems overkill. Not much,
but still avoidable.

How about checking whether the argument x contains only alphanumerical
characters and, if that is the case, executing the program directly?
Such short-circuit logic is for example also used by perl’s sytem(), see
perldoc -f system:

               If there is only one scalar argument, the argument
               is checked for shell metacharacters, and if there are any, the entire
               argument is passed to the system's command shell for parsing (this is
               "/bin/sh -c" on Unix platforms, but varies on other platforms).  If
               there are no shell metacharacters in the argument, it is split into
               words and passed directly to "execvp", which is more efficient.


Joachim "nomeata" Breitner
  mail: mail at joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C
  JID: nomeata at joachim-breitner.de | http://www.joachim-breitner.de/
  Debian Developer: nomeata at debian.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/xmonad/attachments/20100920/1b96a823/attachment.bin

More information about the xmonad mailing list