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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9bb01a2e4b1d24261fce56f2b66bd25975e33b5
4
- data.tar.gz: fa1a0fc24b4d2e0e7f53f948cc0ae0765566ef44
3
+ metadata.gz: bbba093ffbc8962d151f715d579043d0ad84c9c3
4
+ data.tar.gz: 433ef664dc0ce7a7c5607dd54bd131c25b8db5d6
5
5
  SHA512:
6
- metadata.gz: e55c7365353edd8d87b9862f3e52ae328b247360ae738225d49085a2e05099636ac846b9d48a319eac0f1f413fec6e515dcbe1d3e2d98b709edc0c2cd0b3fddd
7
- data.tar.gz: a1bf0d11bcd74d917ae0c0a64e1ee60c862a0189799c3dd32ec3df9b423ab1d0dac80998dae1e61491c7f4168fb93bd2864c110ed139a9adcb888bc8bc34446b
6
+ metadata.gz: 5724116c5a2f1d859dd9ab1d6532d73ffe5272ccf107f09e56aa74329ca9151fc0615407eec5d307a2052b393a02f59f2e67ad787f19701a478d864a8dd95c34
7
+ data.tar.gz: dcf9c51370d19e3af1cea9d37d4c9bd1d6be504b3395c73e72633b80cfef552a013c18218960d414b5a2121abaf83af9f4a6cd0d905968b946cd47233323b56e
data/examples/listd.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
3
4
  require 'sockd'
4
5
 
5
6
  Sockd.run 'listd' do |sockd|
@@ -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
- drop_privileges options[:user], options[:group]
96
+ server do |server|
97
+ drop_privileges options[:user], options[:group]
96
98
 
97
- setup
99
+ setup
98
100
 
99
- on_interrupt do |signal|
100
- log "#{signal} received, shutting down..."
101
- teardown
102
- cleanup
103
- exit 130
104
- end
101
+ on_interrupt do |signal|
102
+ log "#{signal} received, shutting down..."
103
+ teardown
104
+ # cleanup
105
+ exit 130
106
+ end
105
107
 
106
- serve
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(&block)
186
- if options[:socket]
180
+ def server
181
+ server = if options[:socket]
187
182
  begin
188
- UNIXServer.open(options[:socket], &block)
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
- log "socket is stale, reopening"
202
- cleanup
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.open(options[:host], options[:port], &block)
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
@@ -1,3 +1,3 @@
1
1
  module Sockd
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.4
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-13 00:00:00.000000000 Z
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler