git-annex/Utility/Lsof.hs

125 lines
3.3 KiB
Haskell
Raw Normal View History

2012-06-15 18:11:09 +00:00
{- lsof interface
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- License: BSD-2-clause
2012-06-15 18:11:09 +00:00
-}
2014-04-26 23:25:05 +00:00
{-# LANGUAGE CPP #-}
2012-06-15 18:11:09 +00:00
module Utility.Lsof where
import Common
import Build.SysConfig as SysConfig
2013-05-11 22:23:41 +00:00
import Utility.Env
2012-06-15 18:11:09 +00:00
import System.Posix.Types
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
deriving (Show)
2012-06-15 18:11:09 +00:00
{- lsof is not in PATH on all systems, so SysConfig may have the absolute
- path where the program was found. Make sure at runtime that lsof is
- available, and if it's not in PATH, adjust PATH to contain it. -}
setup :: IO ()
setup = do
let cmd = fromMaybe "lsof" SysConfig.lsof
when (isAbsolute cmd) $ do
path <- getSearchPath
let path' = takeDirectory cmd : path
setEnv "PATH" (intercalate [searchPathSeparator] path') True
{- 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]
{- 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 [] !
-}
query :: [String] -> IO [(FilePath, LsofOpenMode, ProcessInfo)]
query opts =
withHandle StdoutHandle (createProcessChecked checkSuccessProcess) p $ \h -> do
fileEncoding h
parse <$> hGetContentsStrict h
2012-12-13 04:24:19 +00:00
where
p = proc "lsof" ("-F0can" : opts)
2012-06-15 18:11:09 +00:00
2013-02-11 21:24:12 +00:00
type LsofParser = String -> [(FilePath, LsofOpenMode, ProcessInfo)]
parse :: LsofParser
#ifdef __ANDROID__
2013-02-11 21:24:12 +00:00
parse = parseDefault
#else
parse = parseFormatted
#endif
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.
-}
2013-02-11 21:24:12 +00:00
parseFormatted :: LsofParser
parseFormatted s = bundle $ go [] $ lines s
2012-12-13 04:24:19 +00:00
where
bundle = concatMap (\(fs, p) -> map (\(f, m) -> (f, m, p)) fs)
go c [] = c
go c ((t:r):ls)
| t == 'p' =
let (fs, ls') = parsefiles [] ls
in go ((fs, parseprocess r):c) ls'
| otherwise = parsefail
go _ _ = parsefail
parseprocess l = case splitnull l of
[pid, 'c':cmdline, ""] ->
case readish pid of
(Just n) -> ProcessInfo n cmdline
Nothing -> parsefail
_ -> parsefail
parsefiles c [] = (c, [])
parsefiles c (l:ls) = parsefiles' c (splitnull l) l ls
parsefiles' c ['a':mode, 'n':file, ""] _ ls =
parsefiles ((file, parsemode mode):c) ls
parsefiles' c (('p':_):_) l ls = (c, l:ls)
-- Some buggy versions of lsof emit a f field
-- that was not requested, so ignore it.
parsefiles' c (('f':_):rest) l ls = parsefiles' c rest l ls
parsefiles' _ _ _ _ = parsefail
2012-12-13 04:24:19 +00:00
parsemode ('r':_) = OpenReadOnly
parsemode ('w':_) = OpenWriteOnly
parsemode ('u':_) = OpenReadWrite
parsemode _ = OpenUnknown
splitnull = split "\0"
parsefail = error $ "failed to parse lsof output: " ++ show s
2013-02-11 21:24:12 +00:00
{- Parses lsof's default output format. -}
parseDefault :: LsofParser
2014-04-26 23:25:05 +00:00
parseDefault = mapMaybe parseline . drop 1 . lines
2013-02-11 21:24:12 +00:00
where
2013-02-15 17:05:39 +00:00
parseline l = case words l of
2013-02-11 21:24:12 +00:00
(command : spid : _user : _fd : _type : _device : _size : _node : rest) ->
case readish spid of
Nothing -> Nothing
Just pid -> Just (unwords rest, OpenUnknown, ProcessInfo pid command)
_ -> Nothing