sockd 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/listd.rb +1 -0
- data/lib/sockd/optparse.rb +5 -0
- data/lib/sockd/runner.rb +53 -52
- data/lib/sockd/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbba093ffbc8962d151f715d579043d0ad84c9c3
|
4
|
+
data.tar.gz: 433ef664dc0ce7a7c5607dd54bd131c25b8db5d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5724116c5a2f1d859dd9ab1d6532d73ffe5272ccf107f09e56aa74329ca9151fc0615407eec5d307a2052b393a02f59f2e67ad787f19701a478d864a8dd95c34
|
7
|
+
data.tar.gz: dcf9c51370d19e3af1cea9d37d4c9bd1d6be504b3395c73e72633b80cfef552a013c18218960d414b5a2121abaf83af9f4a6cd0d905968b946cd47233323b56e
|
data/examples/listd.rb
CHANGED
data/lib/sockd/optparse.rb
CHANGED
@@ -14,6 +14,7 @@ module Sockd
|
|
14
14
|
host: "127.0.0.1",
|
15
15
|
port: 0,
|
16
16
|
socket: false,
|
17
|
+
mode: 0660,
|
17
18
|
daemonize: true,
|
18
19
|
pid_path: "/var/run/#{safe_name}.pid",
|
19
20
|
log_path: false,
|
@@ -63,6 +64,10 @@ module Sockd
|
|
63
64
|
options[:socket] = File.expand_path(x)
|
64
65
|
end
|
65
66
|
|
67
|
+
opts.on("-m", "--mode MODE", String, "Set file permissions when using Unix socket", "(default: #{options[:mode]})") do |x|
|
68
|
+
options[:mode] = x
|
69
|
+
end
|
70
|
+
|
66
71
|
opts.on("-P", "--pid FILE", String, "Where to write the PID file", "(default: #{options[:pid_path]})") do |x|
|
67
72
|
options[:pid_path] = File.expand_path(x)
|
68
73
|
end
|
data/lib/sockd/runner.rb
CHANGED
@@ -21,6 +21,7 @@ module Sockd
|
|
21
21
|
:host => "127.0.0.1",
|
22
22
|
:port => 0,
|
23
23
|
:socket => false,
|
24
|
+
:mode => 0660,
|
24
25
|
:daemonize => true,
|
25
26
|
:pid_path => "/var/run/#{safe_name}.pid",
|
26
27
|
:log_path => false,
|
@@ -92,18 +93,38 @@ module Sockd
|
|
92
93
|
return self unless daemonize
|
93
94
|
end
|
94
95
|
|
95
|
-
|
96
|
+
server do |server|
|
97
|
+
drop_privileges options[:user], options[:group]
|
96
98
|
|
97
|
-
|
99
|
+
setup
|
98
100
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
101
|
+
on_interrupt do |signal|
|
102
|
+
log "#{signal} received, shutting down..."
|
103
|
+
teardown
|
104
|
+
# cleanup
|
105
|
+
exit 130
|
106
|
+
end
|
105
107
|
|
106
|
-
|
108
|
+
while true
|
109
|
+
sock = server.accept
|
110
|
+
begin
|
111
|
+
# wait for input
|
112
|
+
if IO.select([sock], nil, nil, 2.0)
|
113
|
+
msg = sock.recv(256, Socket::MSG_PEEK)
|
114
|
+
if msg.chomp == "ping"
|
115
|
+
sock.print "pong\r\n"
|
116
|
+
else
|
117
|
+
handle msg, sock
|
118
|
+
end
|
119
|
+
else
|
120
|
+
log "connection timed out"
|
121
|
+
end
|
122
|
+
rescue Errno::EPIPE, Errno::ECONNRESET
|
123
|
+
log "connection broken"
|
124
|
+
end
|
125
|
+
sock.close unless sock.closed?
|
126
|
+
end
|
127
|
+
end
|
107
128
|
end
|
108
129
|
|
109
130
|
# stop our service
|
@@ -155,37 +176,11 @@ module Sockd
|
|
155
176
|
|
156
177
|
protected
|
157
178
|
|
158
|
-
# run a server loop, passing data off to our handler
|
159
|
-
def serve
|
160
|
-
server do |server|
|
161
|
-
log "listening on " + server.local_address.inspect_sockaddr
|
162
|
-
while 1
|
163
|
-
sock = server.accept
|
164
|
-
begin
|
165
|
-
# wait for input
|
166
|
-
if IO.select([sock], nil, nil, 2.0)
|
167
|
-
msg = sock.recv(256, Socket::MSG_PEEK)
|
168
|
-
if msg.chomp == "ping"
|
169
|
-
sock.print "pong\r\n"
|
170
|
-
else
|
171
|
-
handle msg, sock
|
172
|
-
end
|
173
|
-
else
|
174
|
-
log "connection timed out"
|
175
|
-
end
|
176
|
-
rescue Errno::EPIPE, Errno::ECONNRESET
|
177
|
-
log "connection broken"
|
178
|
-
end
|
179
|
-
sock.close unless sock.closed?
|
180
|
-
end
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
179
|
# return a UNIXServer or TCPServer instance depending on config
|
185
|
-
def server
|
186
|
-
if options[:socket]
|
180
|
+
def server
|
181
|
+
server = if options[:socket]
|
187
182
|
begin
|
188
|
-
UNIXServer.
|
183
|
+
UNIXServer.new(options[:socket])
|
189
184
|
rescue Errno::EADDRINUSE
|
190
185
|
begin
|
191
186
|
Timeout.timeout(5) do
|
@@ -198,13 +193,28 @@ module Sockd
|
|
198
193
|
end
|
199
194
|
raise ProcError, "socket #{options[:socket]} already in use by another process"
|
200
195
|
rescue Errno::ECONNREFUSED, Timeout::Error
|
201
|
-
|
202
|
-
|
203
|
-
UNIXServer.open(options[:socket], &block)
|
196
|
+
File.delete(options[:socket])
|
197
|
+
UNIXServer.new(options[:socket])
|
204
198
|
end
|
199
|
+
end.tap do
|
200
|
+
# get user and group ids
|
201
|
+
uid = Etc.getpwnam(options[:user]).uid if options[:user]
|
202
|
+
gid = Etc.getgrnam(options[:group]).gid if options[:group]
|
203
|
+
gid = Etc.getpwnam(options[:user]).gid if !gid && options[:user]
|
204
|
+
File.chown(uid, gid, options[:socket]) if uid || gid
|
205
|
+
|
206
|
+
# ensure mode is octal if string provided
|
207
|
+
options[:mode] = options[:mode].to_i(8) if options[:mode].is_a?(String)
|
208
|
+
File.chmod(options[:mode], options[:socket])
|
205
209
|
end
|
206
210
|
else
|
207
|
-
TCPServer.
|
211
|
+
TCPServer.new(options[:host], options[:port])
|
212
|
+
end
|
213
|
+
begin
|
214
|
+
log "listening on " + server.local_address.inspect_sockaddr
|
215
|
+
yield(server)
|
216
|
+
ensure
|
217
|
+
server.close
|
208
218
|
end
|
209
219
|
rescue Errno::EACCES
|
210
220
|
sock = options[:socket] || "#{options[:host]}:#{options[:port]}"
|
@@ -223,15 +233,6 @@ module Sockd
|
|
223
233
|
raise ProcError, "unable to open socket: #{sock} (check permissions)"
|
224
234
|
end
|
225
235
|
|
226
|
-
# clean up UNIXSocket upon termination
|
227
|
-
def cleanup
|
228
|
-
if options[:socket] && File.exists?(options[:socket])
|
229
|
-
File.delete(options[:socket])
|
230
|
-
end
|
231
|
-
rescue StandardError
|
232
|
-
raise ProcError, "unable to unlink socket: #{options[:socket]} (check permissions)"
|
233
|
-
end
|
234
|
-
|
235
236
|
# handle process termination signals
|
236
237
|
def on_interrupt(&block)
|
237
238
|
trap("INT") { yield "SIGINT" }
|
@@ -295,7 +296,7 @@ module Sockd
|
|
295
296
|
|
296
297
|
Process::Sys.setgid(gid) if gid
|
297
298
|
Process::Sys.setuid(uid) if uid
|
298
|
-
rescue ArgumentError => e
|
299
|
+
rescue ArgumentError, Errno::EPERM => e
|
299
300
|
# user or group does not exist
|
300
301
|
raise ProcError, "unable to drop privileges (#{e})"
|
301
302
|
end
|
data/lib/sockd/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sockd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Greiling
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|