diff options
author | Joey Hess <joey@kitenet.net> | 2013-02-11 17:24:12 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2013-02-11 17:33:45 -0400 |
commit | fc1f365321bcb9ca4e737c3219393c390c2f85d7 (patch) | |
tree | 92379482d1cdb9dd581c4d4b987521e4ae43aa7e /Utility | |
parent | c4e7c6af35c3abf66b4c0c68d4224ae010e1f2a8 (diff) |
support Android's crippled lsof
Diffstat (limited to 'Utility')
-rw-r--r-- | Utility/Lsof.hs | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/Utility/Lsof.hs b/Utility/Lsof.hs index 9a877a3c9..6e1d2f084 100644 --- a/Utility/Lsof.hs +++ b/Utility/Lsof.hs @@ -5,7 +5,7 @@ - Licensed under the GNU GPL version 3 or higher. -} -{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE BangPatterns, CPP #-} module Utility.Lsof where @@ -52,6 +52,15 @@ query opts = where p = proc "lsof" ("-F0can" : opts) +type LsofParser = String -> [(FilePath, LsofOpenMode, ProcessInfo)] + +parse :: LsofParser +#ifdef WITH_ANDROID +parse = parseDefault +#else +parse = parseFormatted +#endif + {- Parsing null-delimited output like: - - pPID\0cCMDLINE\0 @@ -62,8 +71,8 @@ query opts = - Where each new process block is started by a pid, and a process can - have multiple files open. -} -parse :: String -> [(FilePath, LsofOpenMode, ProcessInfo)] -parse s = bundle $ go [] $ lines s +parseFormatted :: LsofParser +parseFormatted s = bundle $ go [] $ lines s where bundle = concatMap (\(fs, p) -> map (\(f, m) -> (f, m, p)) fs) @@ -97,3 +106,14 @@ parse s = bundle $ go [] $ lines s splitnull = split "\0" parsefail = error $ "failed to parse lsof output: " ++ show s + +{- Parses lsof's default output format. -} +parseDefault :: LsofParser +parseDefault = catMaybes . map parse . drop 1 . lines + where + parse l = case words l of + (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 |