Remove set_cloexec wrapper and require FD_CLOEXEC
[unicorn.git] / lib / unicorn / util.rb
blob0400fd084895f3b5ef7ab8b1d275a2b8cc12f9cd
1 require 'fcntl'
3 module Unicorn
4   class Util
5     class << self
7       # this reopens logs that have been rotated (using logrotate(8) or
8       # similar).  It is recommended that you install
9       # A +File+ object is considered for reopening if it is:
10       #   1) opened with the O_APPEND and O_WRONLY flags
11       #   2) opened with an absolute path (starts with "/")
12       #   3) the current open file handle does not match its original open path
13       #   4) unbuffered (as far as userspace buffering goes)
14       # Returns the number of files reopened
15       def reopen_logs
16         nr = 0
17         ObjectSpace.each_object(File) do |fp|
18           next if fp.closed?
19           next unless (fp.sync && fp.path[0..0] == "/")
21           flags = fp.fcntl(Fcntl::F_GETFL)
22           open_flags = File::WRONLY | File::APPEND
23           next unless (flags & open_flags) == open_flags
25           begin
26             a, b = fp.stat, File.stat(fp.path)
27             next if a.ino == b.ino && a.dev == b.dev
28           rescue Errno::ENOENT
29           end
30           fp.reopen(fp.path, "a")
31           fp.sync = true
32           nr += 1
33         end # each_object
34         nr
35       end
37     end
39   end
40 end