Module: Nand::ProcOperation

Included in:
Daemon
Defined in:
lib/nand/proc_operation.rb

Instance Method Summary collapse

Instance Method Details

#all_runningsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nand/proc_operation.rb', line 31

def all_runnings
  uid = Process.uid
  runnings = Sys::ProcTable.ps.select{ |ps| running_ps?(ps) and ps.ppid == 1 and (ps.euid == uid or uid == 0)}
  dirs = runnings.map do |run|
    boot_dir = run.environ["PWD"]
    run_dir = run.cmdline.scan(/--run_dir\s+(\S+)/).flatten.first
    Pathname.new(run_dir || boot_dir)
  end.uniq
  daemons = dirs.map{ |d| running_in(d) }.flatten
  unless daemons.size == runnings.size
    unmanageds = runnings.reject{ |ps| daemons.find{|d| d.pid == ps.pid} }.map{ |ps| ps.pid}
    raise "Found Unmanaged Nand Process [#{unmanageds.join(", ")}], Send Signal Yourself"
  end
  daemons
end

#name_from_pidfile(file) ⇒ Object



22
23
24
# File 'lib/nand/proc_operation.rb', line 22

def name_from_pidfile(file)
  File.basename(file).to_s.scan(/^\.nand_(\S+)\.pid$/).flatten.first
end

#pid_filename(name) ⇒ Object



7
# File 'lib/nand/proc_operation.rb', line 7

def pid_filename( name ); ".nand_#{name}.pid" end

#pid_from_pidfile(file) ⇒ Object



17
18
19
20
21
# File 'lib/nand/proc_operation.rb', line 17

def pid_from_pidfile(file)
  raise "File Not Found #{file}"    unless FileTest.exist? file
  raise "File Not Readable #{file}" unless FileTest.readable? file
  File.open(file, "r"){|fs| fs.read }.strip.to_i
end

#ps(pid) ⇒ Object



8
# File 'lib/nand/proc_operation.rb', line 8

def ps(pid);   Sys::ProcTable.ps(pid)  end

#running_in(dir, pattern = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/nand/proc_operation.rb', line 25

def running_in(dir, pattern = nil)
  pattern ||= "*"
  Dir.glob(dir.join(pid_filename(pattern))).
    map{|f|[pid_from_pidfile(f), name_from_pidfile(f)]}.
    map{ |pid, name| Daemon.new(dir, name, :uid => ps(pid).euid)}
end

#running_ps?(ps, name = nil) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/nand/proc_operation.rb', line 10

def running_ps?(ps, name = nil)
  ps.cmdline =~ /#{$PROGRAM_NAME}/ and ps.cmdline =~/start/ and ( name.nil? or ps.cmdline.include? name.to_s)
end

#running_with?(pid, name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/nand/proc_operation.rb', line 13

def running_with?(pid, name)
  proc = ps(pid)
  !proc.nil? and running_ps?(proc, name)
end