Class: Msf::Util::ServiceHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/util/service_helper.rb

Class Method Summary collapse

Class Method Details

.process_active?(pid) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/msf/util/service_helper.rb', line 32

def self.process_active?(pid)
  begin
    Process.kill(0, pid)
    true
  rescue Errno::ESRCH
    false
  end
end

.run_cmd(cmd, input: nil, env: {}, debug: false) ⇒ Object



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
# File 'lib/msf/util/service_helper.rb', line 6

def self.run_cmd(cmd, input: nil, env: {}, debug: false)
  exitstatus = 0
  err = out = ""

  $stdout.puts "run_cmd: cmd=#{cmd}, input=#{input}, env=#{env}" if debug

  Open3.popen3(env, cmd) do |stdin, stdout, stderr, wait_thr|
    stdin.puts(input) if input
    if debug
      err = stderr.read
      out = stdout.read
    end
    exitstatus = wait_thr.value.exitstatus
  end

  if exitstatus != 0
    if debug
      $stdout.puts "'#{cmd}' returned #{exitstatus}"
      $stdout.puts out
      $stdout.puts err
    end
  end

  exitstatus
end

.tail(file) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/msf/util/service_helper.rb', line 41

def self.tail(file)
  begin
    File.readlines(file).last.to_s.strip
  rescue
    nil
  end
end

.thin_cmd(conf:, address:, port:, ssl:, ssl_key:, ssl_cert:, ssl_disable_verify:, env: 'production', daemonize:, log:, pid:, tag:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/msf/util/service_helper.rb', line 49

def self.thin_cmd(conf:, address:, port:, ssl:, ssl_key:, ssl_cert:, ssl_disable_verify:,
                  env: 'production', daemonize:, log:, pid:, tag:)
  server_opts = "--rackup #{conf} --address #{address} --port #{port}"
  ssl_opts = ssl ? "--ssl --ssl-key-file #{ssl_key} --ssl-cert-file #{ssl_cert}" : ''
  ssl_opts << ' --ssl-disable-verify' if ssl_disable_verify
  adapter_opts = "--environment #{env}"
  daemon_opts = daemonize ? "--daemonize --log #{log} --pid #{pid} --tag #{tag}" : ''
  all_opts = [server_opts, ssl_opts, adapter_opts, daemon_opts].reject(&:empty?).join(' ')

  "thin #{all_opts}"
end