2012-06-15 18:11:09 +00:00
|
|
|
{- lsof interface
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
|
|
|
module Utility.Lsof where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
|
|
|
|
import System.Posix.Types
|
|
|
|
|
2012-06-15 19:13:31 +00:00
|
|
|
data LsofOpenMode = OpenReadWrite | OpenReadOnly | OpenWriteOnly | OpenUnknown
|
2012-06-16 02:16:13 +00:00
|
|
|
deriving (Show, Eq)
|
2012-06-15 18:11:09 +00:00
|
|
|
|
|
|
|
type CmdLine = String
|
|
|
|
|
|
|
|
data ProcessInfo = ProcessInfo ProcessID CmdLine
|
2012-06-15 19:13:31 +00:00
|
|
|
deriving (Show)
|
2012-06-15 18:11:09 +00:00
|
|
|
|
2012-06-16 02:34:42 +00:00
|
|
|
{- Checks each of the files in a directory to find open files.
|
|
|
|
- Note that this will find hard links to files elsewhere that are open. -}
|
|
|
|
queryDir :: FilePath -> IO [(FilePath, LsofOpenMode, ProcessInfo)]
|
|
|
|
queryDir path = query ["+d", path]
|
|
|
|
|
2012-06-16 03:24:01 +00:00
|
|
|
{- Runs lsof with some parameters.
|
|
|
|
-
|
|
|
|
- Ignores nonzero exit code; lsof returns that when no files are open.
|
|
|
|
-
|
|
|
|
- Note: If lsof is not available, this always returns [] !
|
|
|
|
-}
|
2012-06-16 02:34:42 +00:00
|
|
|
query :: [String] -> IO [(FilePath, LsofOpenMode, ProcessInfo)]
|
2012-07-19 04:43:36 +00:00
|
|
|
query opts =
|
|
|
|
withHandle StdoutHandle (createProcessChecked checkSuccessProcess) p $ \h -> do
|
|
|
|
parse <$> hGetContentsStrict h
|
|
|
|
where
|
|
|
|
p = proc "lsof" ("-F0can" : opts)
|
2012-06-15 18:11:09 +00:00
|
|
|
|
|
|
|
{- Parsing null-delimited output like:
|
|
|
|
-
|
|
|
|
- pPID\0cCMDLINE\0
|
|
|
|
- aMODE\0nFILE\0
|
|
|
|
- aMODE\0nFILE\0
|
|
|
|
- pPID\0[...]
|
|
|
|
-
|
|
|
|
- Where each new process block is started by a pid, and a process can
|
|
|
|
- have multiple files open.
|
|
|
|
-}
|
2012-06-15 19:13:31 +00:00
|
|
|
parse :: String -> [(FilePath, LsofOpenMode, ProcessInfo)]
|
|
|
|
parse s = bundle $ go [] $ lines s
|
2012-06-15 18:11:09 +00:00
|
|
|
where
|
2012-06-15 19:13:31 +00:00
|
|
|
bundle = concatMap (\(fs, p) -> map (\(f, m) -> (f, m, p)) fs)
|
|
|
|
|
2012-06-15 18:11:09 +00:00
|
|
|
go c [] = c
|
2012-06-16 02:16:13 +00:00
|
|
|
go c ((t:r):ls)
|
2012-06-15 19:13:31 +00:00
|
|
|
| t == 'p' =
|
|
|
|
let (fs, ls') = parsefiles [] ls
|
|
|
|
in go ((fs, parseprocess r):c) ls'
|
2012-06-15 18:11:09 +00:00
|
|
|
| otherwise = parsefail
|
|
|
|
go _ _ = parsefail
|
|
|
|
|
|
|
|
parseprocess l =
|
|
|
|
case splitnull l of
|
2012-06-15 19:13:31 +00:00
|
|
|
[pid, 'c':cmdline, ""] ->
|
2012-06-15 18:11:09 +00:00
|
|
|
case readish pid of
|
|
|
|
(Just n) -> ProcessInfo n cmdline
|
|
|
|
Nothing -> parsefail
|
|
|
|
_ -> parsefail
|
|
|
|
|
2012-06-15 19:13:31 +00:00
|
|
|
parsefiles c [] = (c, [])
|
|
|
|
parsefiles c (l:ls) =
|
2012-06-15 18:11:09 +00:00
|
|
|
case splitnull l of
|
2012-06-15 19:13:31 +00:00
|
|
|
['a':mode, 'n':file, ""] ->
|
|
|
|
parsefiles ((file, parsemode mode):c) ls
|
|
|
|
(('p':_):_) -> (c, l:ls)
|
2012-06-15 18:11:09 +00:00
|
|
|
_ -> parsefail
|
|
|
|
|
2012-06-15 19:13:31 +00:00
|
|
|
parsemode ('r':_) = OpenReadOnly
|
|
|
|
parsemode ('w':_) = OpenWriteOnly
|
|
|
|
parsemode ('u':_) = OpenReadWrite
|
|
|
|
parsemode _ = OpenUnknown
|
2012-06-15 18:11:09 +00:00
|
|
|
|
|
|
|
splitnull = split "\0"
|
|
|
|
|
2012-06-15 19:13:31 +00:00
|
|
|
parsefail = error $ "failed to parse lsof output: " ++ show s
|