File: sid.rb

package info (click to toggle)
ruby-spring 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 428 kB
  • sloc: ruby: 3,373; sh: 9; makefile: 7
file content (42 lines) | stat: -rw-r--r-- 993 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
begin
  # If rubygems is present, keep it out of the way when loading fiddle,
  # otherwise if fiddle is not installed then rubygems will load all
  # gemspecs in its (futile) search for fiddle, which is slow.
  if respond_to?(:gem_original_require, true)
    gem_original_require 'fiddle'
  else
    require 'fiddle'
  end
rescue LoadError
end

module Spring
  module SID
    def self.fiddle_func
      @fiddle_func ||= Fiddle::Function.new(
        DL::Handle::DEFAULT['getsid'],
        [Fiddle::TYPE_INT],
        Fiddle::TYPE_INT
      )
    end

    def self.sid
      @sid ||= begin
        if Process.respond_to?(:getsid)
          # Ruby 2
          Process.getsid
        elsif defined?(Fiddle) and defined?(DL)
          # Ruby 1.9.3 compiled with libffi support
          fiddle_func.call(0)
        else
          # last resort: shell out
          `ps -p #{Process.pid} -o sess=`.to_i
        end
      end
    end

    def self.pgid
      Process.getpgid(sid)
    end
  end
end