Class: DRb::DRbUNIXSocket

Inherits:
DRbTCPSocket show all
Defined in:
lib/drb/unix.rb

Overview

Implements DRb over a UNIX socket

DRb UNIX socket URIs look like drbunix:<path>?<option>. The option is optional.

Constant Summary collapse

Max_try =

import from tempfile.rb

10

Instance Attribute Summary

Attributes inherited from DRbTCPSocket

#uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DRbTCPSocket

#alive?, getservername, open_server_inaddr_any, #peeraddr, #recv_reply, #recv_request, #send_reply, #send_request, #shutdown, #stream

Constructor Details

#initialize(uri, soc, config = {}, server_mode = false) ⇒ DRbUNIXSocket

Returns a new instance of DRbUNIXSocket.



62
63
64
65
66
67
# File 'lib/drb/unix.rb', line 62

def initialize(uri, soc, config={}, server_mode = false)
  super(uri, soc, config)
  set_sockopt(@socket)
  @server_mode = server_mode
  @acl = nil
end

Class Method Details

.open(uri, config) ⇒ Object



27
28
29
30
31
32
# File 'lib/drb/unix.rb', line 27

def self.open(uri, config)
  filename, = parse_uri(uri)
  filename.untaint
  soc = UNIXSocket.open(filename)
  self.new(uri, soc, config)
end

.open_server(uri, config) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/drb/unix.rb', line 34

def self.open_server(uri, config)
  filename, = parse_uri(uri)
  if filename.size == 0
    soc = temp_server
    filename = soc.path
    uri = 'drbunix:' + soc.path
  else
    soc = UNIXServer.open(filename)
  end
  owner = config[:UNIXFileOwner]
  group = config[:UNIXFileGroup]
  if owner || group
    require 'etc'
    owner = Etc.getpwnam( owner ).uid  if owner
    group = Etc.getgrnam( group ).gid  if group
    File.chown owner, group, filename
  end
  mode = config[:UNIXFileMode]
  File.chmod(mode, filename) if mode

  self.new(uri, soc, config, true)
end

.parse_uri(uri) ⇒ Object

:stopdoc:



16
17
18
19
20
21
22
23
24
25
# File 'lib/drb/unix.rb', line 16

def self.parse_uri(uri)
  if /^drbunix:(.*?)(\?(.*))?$/ =~ uri
    filename = $1
    option = $3
    [filename, option]
  else
    raise(DRbBadScheme, uri) unless uri =~ /^drbunix:/
    raise(DRbBadURI, 'can\'t parse uri:' + uri)
  end
end

.uri_option(uri, config) ⇒ Object



57
58
59
60
# File 'lib/drb/unix.rb', line 57

def self.uri_option(uri, config)
  filename, option = parse_uri(uri)
  return "drbunix:#{filename}", option
end

Instance Method Details

#acceptObject



104
105
106
107
108
# File 'lib/drb/unix.rb', line 104

def accept
  s = accept_or_shutdown
  return nil unless s
  self.class.new(nil, s, @config)
end

#closeObject



95
96
97
98
99
100
101
102
# File 'lib/drb/unix.rb', line 95

def close
  return unless @socket
  path = @socket.path if @server_mode
  @socket.close
  File.unlink(path) if @server_mode
  @socket = nil
  close_shutdown_pipe
end

#set_sockopt(soc) ⇒ Object



110
111
112
# File 'lib/drb/unix.rb', line 110

def set_sockopt(soc)
  soc.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::FD_CLOEXEC
end