Module: Ohai::Mixin::Command

Included in:
System
Defined in:
lib/ohai/mixin/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.popen4(cmd, args = {}, &b) ⇒ Object

This is taken directly from Ara T Howard’s Open4 library, and then modified to suit the needs of Ohai. Any bugs here are most likely my own, and not Ara’s.

The original appears in external/open4.rb in its unmodified form.

Thanks Ara!



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
# File 'lib/ohai/mixin/command.rb', line 130

def popen4(cmd, args={}, &b)
	
	## Disable garbage collection to work around possible bug in MRI
  # Ruby 1.8 suffers from intermittent segfaults believed to be due to GC while IO.select
  # See OHAI-330 / CHEF-2916 / CHEF-1305
	GC.disable
	
  # Waitlast - this is magic.
  #
  # Do we wait for the child process to die before we yield
  # to the block, or after?  That is the magic of waitlast.
  #
  # By default, we are waiting before we yield the block.
  args[:waitlast] ||= false

  args[:user] ||= nil
  unless args[:user].kind_of?(Integer)
    args[:user] = Etc.getpwnam(args[:user]).uid if args[:user]
  end
  args[:group] ||= nil
  unless args[:group].kind_of?(Integer)
    args[:group] = Etc.getgrnam(args[:group]).gid if args[:group]
  end
  args[:environment] ||= {}

  # Default on C locale so parsing commands output can be done
  # independently of the node's default locale.
  # "LC_ALL" could be set to nil, in which case we also must ignore it.
  unless args[:environment].has_key?("LC_ALL")
    args[:environment]["LC_ALL"] = "C"
  end

  pw, pr, pe, ps = IO.pipe, IO.pipe, IO.pipe, IO.pipe

  verbose = $VERBOSE
  begin
    $VERBOSE = nil
    ps.last.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)

    cid = fork {
      Process.setsid

      pw.last.close
      STDIN.reopen pw.first
      pw.first.close

      pr.first.close
      STDOUT.reopen pr.last
      pr.last.close

      pe.first.close
      STDERR.reopen pe.last
      pe.last.close

      STDOUT.sync = STDERR.sync = true

      if args[:group]
        Process.egid = args[:group]
        Process.gid = args[:group]
      end

      if args[:user]
        Process.euid = args[:user]
        Process.uid = args[:user]
      end

      args[:environment].each do |key,value|
        ENV[key] = value
      end

      if args[:umask]
        umask = ((args[:umask].respond_to?(:oct) ? args[:umask].oct : args[:umask].to_i) & 007777)
        File.umask(umask)
      end

      begin
        if cmd.kind_of?(Array)
          exec(*cmd)
        else
          exec(cmd)
        end
        raise 'forty-two'
      rescue Exception => e
        Marshal.dump(e, ps.last)
        ps.last.flush
      end
      ps.last.close unless (ps.last.closed?)
      exit!
    }
  ensure
    $VERBOSE = verbose
  end

  [pw.first, pr.last, pe.last, ps.last].each{|fd| fd.close}

  begin
    e = Marshal.load ps.first
    raise(Exception === e ? e : "unknown failure!")
  rescue EOFError # If we get an EOF error, then the exec was successful
    42
  ensure
    ps.first.close
  end

  pw.last.sync = true

  pi = [pw.last, pr.first, pe.first]

  if b
    begin
      if args[:waitlast]
        b[cid, *pi]
        # send EOF so that if the child process is reading from STDIN
        # it will actually finish up and exit
        pi[0].close_write
        Process.waitpid2(cid).last
      else
        # This took some doing.
        # The trick here is to close STDIN
        # Then set our end of the childs pipes to be O_NONBLOCK
        # Then wait for the child to die, which means any IO it
        # wants to do must be done - it's dead.  If it isn't,
        # it's because something totally skanky is happening,
        # and we don't care.
        o = StringIO.new
        e = StringIO.new

        #pi[0].close

        stdout = pi[1]
        stderr = pi[2]

        stdout.sync = true
        stderr.sync = true

        stdout.fcntl(Fcntl::F_SETFL, pi[1].fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)
        stderr.fcntl(Fcntl::F_SETFL, pi[2].fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)

        stdout_finished = false
        stderr_finished = false

        results = nil

        while !stdout_finished || !stderr_finished
          begin
            channels_to_watch = []
            channels_to_watch << stdout if !stdout_finished
            channels_to_watch << stderr if !stderr_finished
            ready = IO.select(channels_to_watch, nil, nil, 1.0)
          rescue Errno::EAGAIN
          ensure
            results = Process.waitpid2(cid, Process::WNOHANG)
            if results
              stdout_finished = true
              stderr_finished = true
            end
          end

          if ready && ready.first.include?(stdout)
            line = results ? stdout.gets(nil) : stdout.gets
            if line
              o.write(line)
            else
              stdout_finished = true
            end
          end
          if ready && ready.first.include?(stderr)
            line = results ? stderr.gets(nil) : stderr.gets
            if line
              e.write(line)
            else
              stderr_finished = true
            end
          end
        end
        results = Process.waitpid2(cid) unless results
        o.rewind
        e.rewind

        # **OHAI-275**
        # The way we read from the pipes causes ruby to mark the strings
        # as ASCII-8BIT (i.e., binary), but the content should be encoded
        # as the default external encoding. For example, a command may
        # return data encoded as UTF-8, but the strings will be marked as
        # ASCII-8BIT. Later, when you attempt to print the values as
        # UTF-8, Ruby will try to convert them and fail, raising an
        # error.
        #
        # Ruby always marks strings as binary when read from IO in
        # incomplete chunks, since you may have split the data within a
        # multibyte char. In our case, we concat the chunks back
        # together, so any multibyte chars will be reassembled.
        #
        # Note that all of this applies only to Ruby 1.9, which we check
        # for by making sure that the Encoding class exists and strings
        # have encoding methods.
        if "".respond_to?(:force_encoding) && defined?(Encoding)
          o.string.force_encoding(Encoding.default_external)
          e.string.force_encoding(Encoding.default_external)
        end

        b[cid, pi[0], o, e]
        results.last
      end
    ensure
      pi.each{|fd| fd.close unless fd.closed?}
    end
  else
    [cid, pw.last, pr.first, pe.first]
  end
rescue Errno::ENOENT
  raise Ohai::Exceptions::Exec, "command #{cmd} doesn't exist or is not in the PATH"
ensure
	# we disabled GC entering
	GC.enable
end

.run_command(args = {}) ⇒ Object



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
# File 'lib/ohai/mixin/command.rb', line 32

def run_command(args={})
  if args.has_key?(:creates)
    if File.exists?(args[:creates])
      Ohai::Log.debug("Skipping #{args[:command]} - creates #{args[:creates]} exists.")
      return false
    end
  end

  stdout_string = nil
  stderr_string = nil

  args[:cwd] ||= Dir.tmpdir
  unless File.directory?(args[:cwd])
    raise Ohai::Exceptions::Exec, "#{args[:cwd]} does not exist or is not a directory"
  end

  status = nil
  Dir.chdir(args[:cwd]) do
    status, stdout_string, stderr_string = run_command_backend(args[:command], args[:timeout])
    # systemu returns 42 when it hits unexpected errors
    if status.exitstatus == 42 and stderr_string == ""
      stderr_string = "Failed to run: #{args[:command]}, assuming command not found"
      Ohai::Log.debug(stderr_string)
    end

    if stdout_string
      Ohai::Log.debug("---- Begin #{args[:command]} STDOUT ----")
      Ohai::Log.debug(stdout_string.strip)
      Ohai::Log.debug("---- End #{args[:command]} STDOUT ----")
    end
    if stderr_string
      Ohai::Log.debug("---- Begin #{args[:command]} STDERR ----")
      Ohai::Log.debug(stderr_string.strip)
      Ohai::Log.debug("---- End #{args[:command]} STDERR ----")
    end

    args[:returns] ||= 0
    args[:no_status_check] ||= false
    if status.exitstatus != args[:returns] and not args[:no_status_check]
      raise Ohai::Exceptions::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}"
    else
      Ohai::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}")
    end
  end
  return status, stdout_string, stderr_string
end

Instance Method Details

#run_command_unix(command, timeout) ⇒ Object Also known as: run_command_backend



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ohai/mixin/command.rb', line 81

def run_command_unix(command, timeout)
  stderr_string, stdout_string, status = "", "", nil

  exec_processing_block = lambda do |pid, stdin, stdout, stderr|
    stdout_string, stderr_string = stdout.string.chomp, stderr.string.chomp
  end

  if timeout
    begin
      Timeout.timeout(timeout) do
        status = popen4(command, {}, &exec_processing_block)
      end
    rescue Timeout::Error => e
      Chef::Log.error("#{command} exceeded timeout #{timeout}")
      raise(e)
    end
  else
    status = popen4(command, {}, &exec_processing_block)
  end
  return status, stdout_string, stderr_string
end

#run_command_windows(command, timeout) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ohai/mixin/command.rb', line 103

def run_command_windows(command, timeout)
  if timeout
    begin
      systemu(command)
    rescue SystemExit => e
      raise
    rescue Timeout::Error => e
      Ohai::Log.error("#{command} exceeded timeout #{timeout}")
      raise(e)
    end
  else
    systemu(command)
  end
end