class Process::Daemon::Listen

Access incoming file descriptors from daemons started by systemd.

def self.file_descriptors(env = ENV)

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.
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

def self.open(fd)

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

def self.set_close_at_exec(fd)

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

def initialize(files, names)

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