-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathunix_server.rb
More file actions
56 lines (49 loc) · 1.55 KB
/
unix_server.rb
File metadata and controls
56 lines (49 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Thin
module Backends
# Backend to act as a UNIX domain socket server.
class UnixServer < Base
# UNIX domain socket on which the server is listening for connections.
attr_accessor :socket
def initialize(socket)
raise PlatformNotSupported, 'UNIX domain sockets not available on Windows' if Thin.win?
@socket = socket
super()
end
# Connect the server
def connect
at_exit { remove_socket_file } # In case it crashes
old_umask = File.umask(0)
begin
EventMachine.start_unix_domain_server(@socket, UnixConnection, &method(:initialize_connection))
# HACK EventMachine.start_unix_domain_server doesn't return the connection signature
# so we have to go in the internal stuff to find it.
@signature = EventMachine.instance_eval{@acceptors.keys.first}
ensure
File.umask(old_umask)
end
end
# Stops the server
def disconnect
EventMachine.stop_server(@signature)
end
# Free up resources used by the backend.
def close
remove_socket_file
end
def to_s
@socket
end
protected
def remove_socket_file
File.delete(@socket) if @socket && File.exist?(@socket)
end
end
end
# Connection through a UNIX domain socket.
class UnixConnection < Connection
protected
def socket_address
'127.0.0.1' # Unix domain sockets can only be local
end
end
end