summaryrefslogtreecommitdiff
path: root/lib/mixlib/shellout/windows.rb
blob: 14aca55044f2a32ad6f70c979a3221b61a9cdf33 (plain)
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#--
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: John Keiser (<jkeiser@opscode.com>)
# Author:: Ho-Sheng Hsiao (<hosh@opscode.com>)
# Copyright:: Copyright (c) 2011, 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'win32/process'
require 'mixlib/shellout/windows/core_ext'

module Mixlib
  class ShellOut
    module Windows

      include Process::Functions
      include Process::Constants

      TIME_SLICE = 0.05

      # Option validation that is windows specific
      def validate_options(opts)
        if opts[:user]
          unless opts[:password]
            raise InvalidCommandOption, "You must supply both a username and password when supplying a user in windows"
          end
        end
      end

      #--
      # Missing lots of features from the UNIX version, such as
      # uid, etc.
      def run_command
        #
        # Create pipes to capture stdout and stderr,
        #
        stdout_read, stdout_write = IO.pipe
        stderr_read, stderr_write = IO.pipe
        stdin_read, stdin_write = IO.pipe
        open_streams = [ stdout_read, stderr_read ]

        begin

          #
          # Set cwd, environment, appname, etc.
          #
          app_name, command_line = command_to_run(self.command)
          create_process_args = {
            :app_name => app_name,
            :command_line => command_line,
            :startup_info => {
              :stdout => stdout_write,
              :stderr => stderr_write,
              :stdin => stdin_read
            },
            :environment => inherit_environment.map { |k,v| "#{k}=#{v}" },
            :close_handles => false
          }
          create_process_args[:cwd] = cwd if cwd
          # default to local account database if domain is not specified
          create_process_args[:domain] = domain.nil? ? "." : domain
          create_process_args[:with_logon] = with_logon if with_logon
          create_process_args[:password] = password if password

          #
          # Start the process
          #
          process = Process.create(create_process_args)
          logger.debug(Utils.format_process(process, app_name, command_line, timeout)) if logger
          begin
            # Start pushing data into input
            stdin_write << input if input

            # Close pipe to kick things off
            stdin_write.close

            #
            # Wait for the process to finish, consuming output as we go
            #
            start_wait = Time.now
            while true
              wait_status = WaitForSingleObject(process.process_handle, 0)
              case wait_status
              when WAIT_OBJECT_0
                # Get process exit code
                exit_code = [0].pack('l')
                unless GetExitCodeProcess(process.process_handle, exit_code)
                  raise get_last_error
                end
                @status = ThingThatLooksSortOfLikeAProcessStatus.new
                @status.exitstatus = exit_code.unpack('l').first

                return self
              when WAIT_TIMEOUT
                # Kill the process
                if (Time.now - start_wait) > timeout
                  begin
                    require 'wmi-lite/wmi'
                    wmi = WmiLite::Wmi.new
                    Utils.kill_process_tree(process.process_id, wmi, logger)
                    Process.kill(:KILL, process.process_id)
                  rescue Errno::EIO, SystemCallError
                    logger.warn("Failed to kill timed out process #{process.process_id}") if logger
                  end

                  raise Mixlib::ShellOut::CommandTimeout, [
                    "command timed out:",
                    format_for_exception,
                    Utils.format_process(process, app_name, command_line, timeout)
                  ].join("\n")
                end

                consume_output(open_streams, stdout_read, stderr_read)
              else
                raise "Unknown response from WaitForSingleObject(#{process.process_handle}, #{timeout*1000}): #{wait_status}"
              end

            end

          ensure
            CloseHandle(process.thread_handle) if process.thread_handle
            CloseHandle(process.process_handle) if process.process_handle
          end

        ensure
          #
          # Consume all remaining data from the pipes until they are closed
          #
          stdout_write.close
          stderr_write.close

          while consume_output(open_streams, stdout_read, stderr_read)
          end
        end
      end

      private

      class ThingThatLooksSortOfLikeAProcessStatus
        attr_accessor :exitstatus
        def success?
          exitstatus == 0
        end
      end

      def consume_output(open_streams, stdout_read, stderr_read)
        return false if open_streams.length == 0
        ready = IO.select(open_streams, nil, nil, READ_WAIT_TIME)
        return true if ! ready

        if ready.first.include?(stdout_read)
          begin
            next_chunk = stdout_read.readpartial(READ_SIZE)
            @stdout << next_chunk
            @live_stdout << next_chunk if @live_stdout
          rescue EOFError
            stdout_read.close
            open_streams.delete(stdout_read)
          end
        end

        if ready.first.include?(stderr_read)
          begin
            next_chunk = stderr_read.readpartial(READ_SIZE)
            @stderr << next_chunk
            @live_stderr << next_chunk if @live_stderr
          rescue EOFError
            stderr_read.close
            open_streams.delete(stderr_read)
          end
        end

        return true
      end

      IS_BATCH_FILE = /\.bat"?$|\.cmd"?$/i

      def command_to_run(command)
        return _run_under_cmd(command) if Utils.should_run_under_cmd?(command)

        candidate = candidate_executable_for_command(command)

        # Don't do searching for empty commands.  Let it fail when it runs.
        return [ nil, command ] if candidate.length == 0

        # Check if the exe exists directly.  Otherwise, search PATH.
        exe = Utils.find_executable(candidate)
        exe = Utils.which(unquoted_executable_path(command)) if exe.nil? && exe !~ /[\\\/]/

        # Batch files MUST use cmd; and if we couldn't find the command we're looking for,
        # we assume it must be a cmd builtin.
        if exe.nil? || exe =~ IS_BATCH_FILE
          _run_under_cmd(command)
        else
          _run_directly(command, exe)
        end
      end

      # cmd does not parse multiple quotes well unless the whole thing is wrapped up in quotes.
      # https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4837859
      # http://ss64.com/nt/syntax-esc.html
      def _run_under_cmd(command)
        [ ENV['COMSPEC'], "cmd /c \"#{command}\"" ]
      end

      def _run_directly(command, exe)
        [ exe, command ]
      end

      def unquoted_executable_path(command)
        command[0,command.index(/\s/) || command.length]
      end

      def candidate_executable_for_command(command)
        if command =~ /^\s*"(.*?)"/
          # If we have quotes, do an exact match
          $1
        else
          # Otherwise check everything up to the first space
          unquoted_executable_path(command).strip
        end
      end

      def inherit_environment
        result = {}
        ENV.each_pair do |k,v|
          result[k] = v
        end

        environment.each_pair do |k,v|
          if v == nil
            result.delete(k)
          else
            result[k] = v
          end
        end
        result
      end

      module Utils
        # api: semi-private
        # If there are special characters parsable by cmd.exe (such as file redirection), then
        # this method should return true.
        #
        # This parser is based on
        # https://github.com/ruby/ruby/blob/9073db5cb1d3173aff62be5b48d00f0fb2890991/win32/win32.c#L1437
        def self.should_run_under_cmd?(command)
          return true if command =~ /^@/

          quote = nil
          env = false
          env_first_char = false

          command.dup.each_char do |c|
            case c
            when "'", '"'
              if (!quote)
                quote = c
              elsif quote == c
                quote = nil
              end
              next
            when '>', '<', '|', '&', "\n"
              return true unless quote
            when '%'
              return true if env
              env = env_first_char = true
              next
            else
              next unless env
              if env_first_char
                env_first_char = false
                env = false and next if c !~ /[A-Za-z_]/
              end
              env = false if c !~ /[A-Za-z1-9_]/
            end
          end
          return false
        end

        def self.pathext
          @pathext ||= ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') + [''] : ['']
        end

        # which() mimicks the Unix which command
        # FIXME: it is not working
        def self.which(cmd)
          ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
            exe = find_executable("#{path}/#{cmd}")
            return exe if exe
          end
          return nil
        end

        # Windows has a different notion of what "executable" means
        # The OS will search through valid the extensions and look
        # for a binary there.
        def self.find_executable(path)
          return path if executable? path

          pathext.each do |ext|
            exe = "#{path}#{ext}"
            return exe if executable? exe
          end
          return nil
        end

        def self.executable?(path)
          File.executable?(path) && !File.directory?(path)
        end

        # recursively kills all child processes of given pid
        # calls itself querying for children child procs until
        # none remain. Important that a single WmiLite instance
        # is passed in since each creates its own WMI rpc process
        def self.kill_process_tree(pid, wmi, logger)
          wmi.query("select * from Win32_Process where ParentProcessID=#{pid}").each do |instance|
            child_pid = instance.wmi_ole_object.processid
            kill_process_tree(child_pid, wmi, logger)
            begin
              logger.debug([
                "killing child process #{child_pid}::",
                "#{instance.wmi_ole_object.Name} of parent #{pid}"
                ].join) if logger
              kill_process(instance)
            rescue Errno::EIO, SystemCallError
              logger.debug([
                "Failed to kill child process #{child_pid}::",
                "#{instance.wmi_ole_object.Name} of parent #{pid}"
              ].join) if logger
            end
          end
        end

        def self.kill_process(instance)
          Process.kill(:KILL, instance.wmi_ole_object.processid)
        end

        def self.format_process(process, app_name, command_line, timeout)
          msg = []
          msg << "ProcessId: #{process.process_id}"
          msg << "app_name: #{app_name}"
          msg << "command_line: #{command_line}"
          msg << "timeout: #{timeout}"
          msg.join("\n")
        end
      end
    end # class
  end
end