summaryrefslogtreecommitdiff
path: root/examples/whosocket.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-09-08 08:15:21 -0400
committerJoe Crayne <joe@jerkface.net>2018-10-03 07:00:40 -0400
commit9c02c2816c826d8f40cfaa4409175c88cfc1ea12 (patch)
tree69d2a079d19b7affa9e2b5f41552ed2b35c68d7d /examples/whosocket.hs
parentfbf9890a6bcd4e6212b5947f908bc34f233b279d (diff)
Move example utilities into examples directory.
Diffstat (limited to 'examples/whosocket.hs')
-rw-r--r--examples/whosocket.hs60
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/whosocket.hs b/examples/whosocket.hs
new file mode 100644
index 00000000..f84e3178
--- /dev/null
+++ b/examples/whosocket.hs
@@ -0,0 +1,60 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE TupleSections #-}
3module Main where
4
5import LocalPeerCred
6import ControlMaybe
7import UTmp
8import ByteStringOperators
9
10import System.Directory
11import Data.Char
12import System.Posix.Types
13import System.Posix.Files
14import qualified Data.ByteString.Lazy.Char8 as L
15 ( unpack
16 , pack
17 , take
18 , putStrLn
19 )
20import Data.List (groupBy)
21import Data.Maybe (listToMaybe,mapMaybe,catMaybes)
22
23import Network.Socket
24import System.Environment
25import Control.Arrow (first)
26import System.Endian
27
28usage = do
29 putStrLn $ "whosocket numeric-address port"
30
31main = do
32 args <- getArgs
33 case (args??0,args??1) of
34 (Just addr_str,Just port_str) -> whosocket addr_str port_str
35 _ -> usage
36
37whosocket :: HostName -> ServiceName -> IO ()
38whosocket addr_str port_str = do
39 info <- getAddrInfo (Just $ defaultHints { addrFlags = [ AI_NUMERICHOST ] })
40 (Just addr_str)
41 (Just port_str)
42 let addr = head $ map addrAddress info
43 r <- getLocalPeerCred' addr
44 putStrLn $ "r{"++show addr++"} = " ++ show r
45
46 us <- UTmp.users
47 let filterTTYs (_,tty,pid) =
48 if L.take 3 tty == "tty"
49 then Just (tty,pid)
50 else Nothing
51 tty_pids = mapMaybe filterTTYs us
52
53 tty <- maybe (return Nothing)
54 (fmap fst . uncurry (identifyTTY tty_pids))
55 r
56 putStrLn $ "uid = " ++ show (fmap fst r)
57 L.putStrLn $ "tty = " <++?> tty
58
59 return ()
60