From 45d1ca9da3a7652af535c50614bae73a57a669b3 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 4 Jul 2013 23:23:18 -0400 Subject: search parent pids for tty and fall back to DISPLAY variable. --- whosocket.hs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/whosocket.hs b/whosocket.hs index 5968b152..6955830a 100644 --- a/whosocket.hs +++ b/whosocket.hs @@ -74,18 +74,31 @@ scanProc uid inode = do fmap (fmap (first (read :: String -> CPid))) $ searchPids pids -ttyOrDisplay pid ttydev | take 8 ttydev == "/dev/tty" = return (drop 5 ttydev) ttyOrDisplay pid ttydev = do - env <- handleIO_ (return "") . readFile $ "/proc/"++show pid++"/environ" - let vs = unzero $ groupBy (\_ c->c/='\0') env - unzero [] = [] - unzero (v:vs) = v:map tail vs - keyvalue xs = (key,value) - where - (key,ys) = break (=='=') xs - value = case ys of { [] -> []; (_:ys') -> ys' } - display = listToMaybe . filter ((=="DISPLAY").fst) . map keyvalue $ vs - putStrLn $ "display = " ++ show display - case display of - Just (_,disp) -> return disp - _ -> return ttydev + ptty <- searchParentsForTTY (show pid) ttydev + case ptty of + Just tty -> return tty + Nothing -> do + env <- handleIO_ (return "") . readFile $ "/proc/"++show pid++"/environ" + let vs = unzero $ groupBy (\_ c->c/='\0') env + unzero [] = [] + unzero (v:vs) = v:map tail vs + keyvalue xs = (key,value) + where + (key,ys) = break (=='=') xs + value = case ys of { [] -> []; (_:ys') -> ys' } + display = listToMaybe . filter ((=="DISPLAY").fst) . map keyvalue $ vs + putStrLn $ "display = " ++ show display + case display of + Just (_,disp) -> return disp + _ -> return ttydev + +searchParentsForTTY pid ttydev | take 8 ttydev == "/dev/tty" = return . Just $ drop 5 ttydev +searchParentsForTTY "1" ttydev | otherwise = return Nothing +searchParentsForTTY pid ttydev = do + stat <- handleIO_ (return "") . readFile $ "/proc/"++pid++"/stat" + case words stat ?? 3 of + Nothing -> return Nothing + Just ppid -> do + tty <- handleIO_ (return "") $ readSymbolicLink $ "/proc/"++ppid++"/fd/0" + searchParentsForTTY ppid tty -- cgit v1.2.3