Revision cf51a981
b/src/Ganeti/UDSServer.hs | ||
---|---|---|
32 | 32 |
, RecvResult(..) |
33 | 33 |
, MsgKeys(..) |
34 | 34 |
, strOfKey |
35 |
-- * Unix sockets |
|
36 |
, openClientSocket |
|
37 |
, closeClientSocket |
|
38 |
, openServerSocket |
|
39 |
, closeServerSocket |
|
40 |
, acceptSocket |
|
41 |
-- * Client and server |
|
35 | 42 |
, connectClient |
36 | 43 |
, connectServer |
37 | 44 |
, acceptClient |
... | ... | |
132 | 139 |
, serverConfig :: ConnectConfig |
133 | 140 |
} |
134 | 141 |
|
142 |
-- * Unix sockets |
|
143 |
|
|
144 |
-- | Creates a Unix socket and connects it to the specified @path@, |
|
145 |
-- where @timeout@ specifies the connection timeout. |
|
146 |
openClientSocket |
|
147 |
:: Int -- ^ connection timeout |
|
148 |
-> FilePath -- ^ socket path |
|
149 |
-> IO Handle |
|
150 |
openClientSocket tmo path = do |
|
151 |
sock <- S.socket S.AF_UNIX S.Stream S.defaultProtocol |
|
152 |
withTimeout tmo "creating a connection" $ |
|
153 |
S.connect sock (S.SockAddrUnix path) |
|
154 |
S.socketToHandle sock ReadWriteMode |
|
155 |
|
|
156 |
closeClientSocket :: Handle -> IO () |
|
157 |
closeClientSocket = hClose |
|
158 |
|
|
159 |
-- | Creates a Unix socket and binds it to the specified @path@. |
|
160 |
openServerSocket :: FilePath -> IO S.Socket |
|
161 |
openServerSocket path = do |
|
162 |
sock <- S.socket S.AF_UNIX S.Stream S.defaultProtocol |
|
163 |
S.bindSocket sock (S.SockAddrUnix path) |
|
164 |
return sock |
|
165 |
|
|
166 |
closeServerSocket :: S.Socket -> FilePath -> IO () |
|
167 |
closeServerSocket sock path = do |
|
168 |
S.sClose sock |
|
169 |
removeFile path |
|
170 |
|
|
171 |
acceptSocket :: S.Socket -> IO Handle |
|
172 |
acceptSocket sock = do |
|
173 |
-- ignore client socket address |
|
174 |
(clientSock, _) <- S.accept sock |
|
175 |
S.socketToHandle clientSock ReadWriteMode |
|
176 |
|
|
177 |
-- * Client and server |
|
135 | 178 |
|
136 | 179 |
-- | Connects to the master daemon and returns a Client. |
137 | 180 |
connectClient |
... | ... | |
140 | 183 |
-> FilePath -- ^ socket path |
141 | 184 |
-> IO Client |
142 | 185 |
connectClient conf tmo path = do |
143 |
s <- S.socket S.AF_UNIX S.Stream S.defaultProtocol |
|
144 |
withTimeout tmo "creating a connection" $ |
|
145 |
S.connect s (S.SockAddrUnix path) |
|
186 |
h <- openClientSocket tmo path |
|
146 | 187 |
rf <- newIORef B.empty |
147 |
h <- S.socketToHandle s ReadWriteMode |
|
148 | 188 |
return Client { socket=h, rbuf=rf, clientConfig=conf } |
149 | 189 |
|
150 | 190 |
-- | Creates and returns a server endpoint. |
151 | 191 |
connectServer :: ConnectConfig -> Bool -> FilePath -> IO Server |
152 | 192 |
connectServer conf setOwner path = do |
153 |
s <- S.socket S.AF_UNIX S.Stream S.defaultProtocol |
|
154 |
S.bindSocket s (S.SockAddrUnix path) |
|
193 |
s <- openServerSocket path |
|
155 | 194 |
when setOwner . setOwnerAndGroupFromNames path (connDaemon conf) $ |
156 | 195 |
ExtraGroup DaemonsGroup |
157 | 196 |
S.listen s 5 -- 5 is the max backlog |
158 | 197 |
return Server { sSocket=s, sPath=path, serverConfig=conf } |
159 | 198 |
|
160 | 199 |
-- | Closes a server endpoint. |
161 |
-- FIXME: this should be encapsulated into a nicer type. |
|
162 | 200 |
closeServer :: Server -> IO () |
163 |
closeServer server = do |
|
164 |
S.sClose (sSocket server) |
|
165 |
removeFile (sPath server) |
|
201 |
closeServer server = |
|
202 |
closeServerSocket (sSocket server) (sPath server) |
|
166 | 203 |
|
167 | 204 |
-- | Accepts a client |
168 | 205 |
acceptClient :: Server -> IO Client |
169 | 206 |
acceptClient s = do |
170 |
-- second return is the address of the client, which we ignore here |
|
171 |
(client_socket, _) <- S.accept (sSocket s) |
|
207 |
handle <- acceptSocket (sSocket s) |
|
172 | 208 |
new_buffer <- newIORef B.empty |
173 |
handle <- S.socketToHandle client_socket ReadWriteMode |
|
174 | 209 |
return Client { socket=handle |
175 | 210 |
, rbuf=new_buffer |
176 | 211 |
, clientConfig=serverConfig s |
... | ... | |
178 | 213 |
|
179 | 214 |
-- | Closes the client socket. |
180 | 215 |
closeClient :: Client -> IO () |
181 |
closeClient = hClose . socket
|
|
216 |
closeClient = closeClientSocket . socket
|
|
182 | 217 |
|
183 | 218 |
-- | Sends a message over a transport. |
184 | 219 |
sendMsg :: Client -> String -> IO () |
Also available in: Unified diff