2012-06-15 18:11:09 +00:00
|
|
|
{- lsof interface
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-06-15 18:11:09 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-06-15 18:11:09 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.Lsof where
|
|
|
|
|
|
|
|
import Common
|
2017-12-14 16:46:57 +00:00
|
|
|
import BuildInfo
|
2017-12-31 20:08:31 +00:00
|
|
|
import Utility.Env.Set
|
2012-06-15 18:11:09 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-12-14 16:46:57 +00:00
|
|
|
{- lsof is not in PATH on all systems, so BuildInfo may have the absolute
|
2012-12-14 19:52:44 +00:00
|
|
|
- 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. -}
|
2013-10-03 20:57:21 +00:00
|
|
|
setup :: IO ()
|
|
|
|
setup = do
|
2017-12-14 16:46:57 +00:00
|
|
|
let cmd = fromMaybe "lsof" BuildInfo.lsof
|
2012-12-14 19:52:44 +00:00
|
|
|
when (isAbsolute cmd) $ do
|
|
|
|
path <- getSearchPath
|
|
|
|
let path' = takeDirectory cmd : path
|
2014-10-16 00:33:52 +00:00
|
|
|
setEnv "PATH" (intercalate [searchPathSeparator] path') True
|
2012-12-14 19:52:44 +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 =
|
2016-12-24 18:46:31 +00:00
|
|
|
withHandle StdoutHandle (createProcessChecked checkSuccessProcess) p $
|
|
|
|
parse <$$> hGetContentsStrict
|
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)]
|
|
|
|
|
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.
|
|
|
|
-}
|
2018-10-13 05:36:06 +00:00
|
|
|
parse :: LsofParser
|
|
|
|
parse 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, [])
|
2014-11-12 22:00:40 +00:00
|
|
|
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
|
|
|
|
|
2017-01-31 22:40:42 +00:00
|
|
|
splitnull = splitc '\0'
|
2012-12-13 04:24:19 +00:00
|
|
|
|
|
|
|
parsefail = error $ "failed to parse lsof output: " ++ show s
|