diff options
author | Joe Crayne <joe@jerkface.net> | 2018-09-08 08:15:21 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-10-03 07:00:40 -0400 |
commit | 9c02c2816c826d8f40cfaa4409175c88cfc1ea12 (patch) | |
tree | 69d2a079d19b7affa9e2b5f41552ed2b35c68d7d /examples/whosocket.hs | |
parent | fbf9890a6bcd4e6212b5947f908bc34f233b279d (diff) |
Move example utilities into examples directory.
Diffstat (limited to 'examples/whosocket.hs')
-rw-r--r-- | examples/whosocket.hs | 60 |
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 #-} | ||
3 | module Main where | ||
4 | |||
5 | import LocalPeerCred | ||
6 | import ControlMaybe | ||
7 | import UTmp | ||
8 | import ByteStringOperators | ||
9 | |||
10 | import System.Directory | ||
11 | import Data.Char | ||
12 | import System.Posix.Types | ||
13 | import System.Posix.Files | ||
14 | import qualified Data.ByteString.Lazy.Char8 as L | ||
15 | ( unpack | ||
16 | , pack | ||
17 | , take | ||
18 | , putStrLn | ||
19 | ) | ||
20 | import Data.List (groupBy) | ||
21 | import Data.Maybe (listToMaybe,mapMaybe,catMaybes) | ||
22 | |||
23 | import Network.Socket | ||
24 | import System.Environment | ||
25 | import Control.Arrow (first) | ||
26 | import System.Endian | ||
27 | |||
28 | usage = do | ||
29 | putStrLn $ "whosocket numeric-address port" | ||
30 | |||
31 | main = 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 | |||
37 | whosocket :: HostName -> ServiceName -> IO () | ||
38 | whosocket 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 | |||