Class: Process::Daemon::Listen

Inherits:
Object
  • Object
show all
Defined in:
lib/process/daemon/listen.rb

Overview

Access incoming file descriptors from daemons started by systemd.

Constant Summary collapse

LISTEN_PID =
'LISTEN_PID'
LISTEN_FDS =
'LISTEN_FDS'
LISTEN_FDNAMES =
'LISTEN_FDNAMES'
FD_START =
3
SEPERATOR =
':'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, names) ⇒ Listen

Returns a new instance of Listen.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/process/daemon/listen.rb', line 62

def initialize(files, names)
	@files = files
	@names = names
	
	@named = {}
	@unnamed = []
	
	@names.each_with_index do |name, index|
		if name
			@named[name] = @files[index]
		else
			@unnamed << @files[index]
		end
	end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



78
79
80
# File 'lib/process/daemon/listen.rb', line 78

def files
  @files
end

#namedObject (readonly)

Returns the value of attribute named.



81
82
83
# File 'lib/process/daemon/listen.rb', line 81

def named
  @named
end

#namesObject (readonly)

Returns the value of attribute names.



79
80
81
# File 'lib/process/daemon/listen.rb', line 79

def names
  @names
end

#unnamedObject (readonly)

Returns the value of attribute unnamed.



82
83
84
# File 'lib/process/daemon/listen.rb', line 82

def unnamed
  @unnamed
end

Class Method Details

.file_descriptors(env = ENV) ⇒ Object

Returns a Array or Hash of file descriptors. If LISTEN_FDNAMES is set, a Hash is returned which includes key => value pairs for named file descriptors.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/process/daemon/listen.rb', line 43

def self.file_descriptors(env = ENV)
	pid, fds, names = env.values_at(LISTEN_PID, LISTEN_FDS, LISTEN_FDNAMES)
	
	# Are the PIDs valid for this process?
	unless pid and Integer(pid) == Process.pid
		return nil
	end
	
	files = Integer(fds).times.collect do |i|
		self.open(FD_START + i)
	end
	
	if names
		names = names.split(SEPARATOR, -1)
	end
	
	self.new(files, names)
end

.open(fd) ⇒ Object



36
37
38
39
40
# File 'lib/process/daemon/listen.rb', line 36

def self.open(fd)
	set_close_at_exec(fd)
	
	return IO.for_fd(fd)
end

.set_close_at_exec(fd) ⇒ Object



32
33
34
# File 'lib/process/daemon/listen.rb', line 32

def self.set_close_at_exec(fd)
	fd.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::F_SETFD
end