lib/puppet/util.rb in puppet-0.24.9 vs lib/puppet/util.rb in puppet-0.25.0

- old
+ new

@@ -1,17 +1,18 @@ # A module to collect utility functions. +require 'puppet/util/monkey_patches' require 'sync' require 'puppet/external/lock' module Puppet # A command failed to execute. class ExecutionFailure < Puppet::Error end module Util require 'benchmark' - + # These are all for backward compatibility -- these are methods that used # to be in Puppet::Util but have been moved into external modules. require 'puppet/util/posix' extend Puppet::Util::POSIX @@ -24,23 +25,19 @@ return @@syncresources[resource] end # Change the process to a different user def self.chuser - if Facter["operatingsystem"].value == "Darwin" - $stderr.puts "Ruby on darwin is broken; puppetmaster will not set its UID to 'puppet' and must run as root" - return - end if group = Puppet[:group] group = self.gid(group) unless group raise Puppet::Error, "No such group %s" % Puppet[:group] end unless Puppet::Util::SUIDManager.gid == group begin - Puppet::Util::SUIDManager.egid = group - Puppet::Util::SUIDManager.gid = group + Puppet::Util::SUIDManager.egid = group + Puppet::Util::SUIDManager.gid = group rescue => detail Puppet.warning "could not change to group %s: %s" % [group.inspect, detail] $stderr.puts "could not change to group %s" % group.inspect @@ -56,12 +53,12 @@ unless user raise Puppet::Error, "No such user %s" % Puppet[:user] end unless Puppet::Util::SUIDManager.uid == user begin - Puppet::Util::SUIDManager.uid = user - Puppet::Util::SUIDManager.euid = user + Puppet::Util::SUIDManager.uid = user + Puppet::Util::SUIDManager.euid = user rescue $stderr.puts "could not change to user %s" % user exit(74) end end @@ -253,18 +250,18 @@ if respond_to? :debug debug "Executing '%s'" % str else Puppet.debug "Executing '%s'" % str end - + if arguments[:uid] arguments[:uid] = Puppet::Util::SUIDManager.convert_xid(:uid, arguments[:uid]) end if arguments[:gid] arguments[:gid] = Puppet::Util::SUIDManager.convert_xid(:gid, arguments[:gid]) end - + @@os ||= Facter.value(:operatingsystem) output = nil child_pid, child_status = nil # There are problems with read blocking with badly behaved children # read.partialread doesn't seem to capture either stdout or stderr @@ -280,11 +277,11 @@ error_file=output_file end end oldverb = $VERBOSE - $VERBOSE = false + $VERBOSE = nil child_pid = Kernel.fork $VERBOSE = oldverb if child_pid # Parent process executes this child_status = (Process.waitpid2(child_pid)[1]).to_i >> 8 @@ -298,11 +295,11 @@ $stdin.reopen("/dev/null") end $stdout.reopen(output_file) $stderr.reopen(error_file) - 3.upto(256){|fd| IO::new(fd).close rescue nil} + 3.upto(256){|fd| IO::new(fd).close rescue nil} if arguments[:gid] Process.egid = arguments[:gid] Process.gid = arguments[:gid] unless @@os == "Darwin" end if arguments[:uid] @@ -318,11 +315,11 @@ rescue => detail puts detail.to_s exit!(1) end # begin; rescue end # if child_pid - + # read output in if required if ! arguments[:squelch] # Make sure the file's actually there. This is # basically a race condition, and is probably a horrible @@ -427,31 +424,9 @@ return seconds end module_function :memory, :thinmark - - def secure_open(file,must_be_w,&block) - raise Puppet::DevError,"secure_open only works with mode 'w'" unless must_be_w == 'w' - raise Puppet::DevError,"secure_open only requires a block" unless block_given? - Puppet.warning "#{file} was a symlink to #{File.readlink(file)}" if File.symlink?(file) - if File.exists?(file) or File.symlink?(file) - wait = File.symlink?(file) ? 5.0 : 0.1 - File.delete(file) - sleep wait # give it a chance to reappear, just in case someone is actively trying something. - end - begin - File.open(file,File::CREAT|File::EXCL|File::TRUNC|File::WRONLY,&block) - rescue Errno::EEXIST - desc = File.symlink?(file) ? "symlink to #{File.readlink(file)}" : File.stat(file).ftype - puts "Warning: #{file} was apparently created by another process (as" - puts "a #{desc}) as soon as it was deleted by this process. Someone may be trying" - puts "to do something objectionable (such as tricking you into overwriting system" - puts "files if you are running as root)." - raise - end - end - module_function :secure_open end end require 'puppet/util/errors' require 'puppet/util/methodhelper'