Class: HrrRbSsh::Connection::RequestHandler::ReferenceExecRequestHandler

Inherits:
HrrRbSsh::Connection::RequestHandler show all
Defined in:
lib/hrr_rb_ssh/connection/request_handler/reference_exec_request_handler.rb

Instance Method Summary collapse

Methods inherited from HrrRbSsh::Connection::RequestHandler

#run

Constructor Details

#initializeReferenceExecRequestHandler



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hrr_rb_ssh/connection/request_handler/reference_exec_request_handler.rb', line 12

def initialize
  @logger = Logger.new self.class.name
  @proc = Proc.new { |context|
    ptm = context.vars[:ptm]
    pts = context.vars[:pts]

    context.chain_proc { |chain|
      passwd = Etc.getpwnam(context.username)

      env = context.vars.fetch(:env, Hash.new)
      env['USER']  = passwd.name
      env['HOME']  = passwd.dir
      env['SHELL'] = passwd.shell

      program = context.command

      args = Array.new

      options = Hash.new
      options[:unsetenv_others] = true
      options[:close_others] = true
      unless ptm
        options[:in]  = context.io[0]
        options[:out] = context.io[1]
        options[:err] = context.io[2]
      end

      pid = fork do
        Process.setsid
        Dir.chdir passwd.dir
        Process::GID.change_privilege passwd.gid
        Process::UID.change_privilege passwd.uid
        if ptm
          STDIN.reopen  pts, 'r'
          STDOUT.reopen pts, 'w'
          STDERR.reopen pts, 'w'
          pts.close
        end
        exec env, program, *args, options
      end

      unless ptm
        pid, status = Process.waitpid2 pid
        status.exitstatus
      else
        pts.close

        begin
          pid, status = Process.waitpid2 pid
          context.logger.info { "program exited with status #{status.inspect}" }
          status.exitstatus
        ensure
          unless status
            context.logger.info { "exiting program" }
            Process.kill :TERM, pid
            begin
              Timeout.timeout(1) do
                pid, status = Process.waitpid2 pid
              end
            rescue Timeout::Error
              context.logger.warn { "force exiting program" }
              Process.kill :KILL, pid
              pid, status = Process.waitpid2 pid
            end
            context.logger.info { "program exited with status #{status.inspect}" }
          end
        end
      end
    }
  }
end