8 APPEND_FLAGS = File::WRONLY | File::APPEND
10 # This reopens ALL logfiles in the process that have been rotated
11 # using logrotate(8) (without copytruncate) or similar tools.
12 # A +File+ object is considered for reopening if it is:
13 # 1) opened with the O_APPEND and O_WRONLY flags
14 # 2) opened with an absolute path (starts with "/")
15 # 3) the current open file handle does not match its original open path
16 # 4) unbuffered (as far as userspace buffering goes, not O_SYNC)
17 # Returns the number of files reopened
20 ObjectSpace.each_object(File) do |fp|
22 next unless (fp.sync && fp.path[0..0] == "/")
23 next unless (fp.fcntl(Fcntl::F_GETFL) & APPEND_FLAGS) == APPEND_FLAGS
26 a, b = fp.stat, File.stat(fp.path)
27 next if a.ino == b.ino && a.dev == b.dev
32 if fp.respond_to?(:external_encoding) && enc = fp.external_encoding
33 open_arg << ":#{enc.to_s}"
34 enc = fp.internal_encoding and open_arg << ":#{enc.to_s}"
36 fp.reopen(fp.path, open_arg)
43 # creates and returns a new File object. The File is unlinked
44 # immediately, switched to binary mode, and userspace output
45 # buffering is disabled
48 File.open("#{Dir::tmpdir}/#{rand}",
49 File::RDWR|File::CREAT|File::EXCL, 0600)