1 # -*- encoding: binary -*-
10 # for easier env["rack.input"] compatibility
21 append_flags = File::WRONLY | File::APPEND
26 (fp.fcntl(Fcntl::F_GETFL) & append_flags) == append_flags
27 rescue IOError, Errno::EBADF
31 def chown_logs(uid, gid)
32 ObjectSpace.each_object(File) do |fp|
33 fp.chown(uid, gid) if is_log?(fp)
37 # This reopens ALL logfiles in the process that have been rotated
38 # using logrotate(8) (without copytruncate) or similar tools.
39 # A +File+ object is considered for reopening if it is:
40 # 1) opened with the O_APPEND and O_WRONLY flags
41 # 2) opened with an absolute path (starts with "/")
42 # 3) the current open file handle does not match its original open path
43 # 4) unbuffered (as far as userspace buffering goes, not O_SYNC)
44 # Returns the number of files reopened
48 ObjectSpace.each_object(File) { |fp| is_log?(fp) and to_reopen << fp }
50 to_reopen.each do |fp|
53 rescue IOError, Errno::EBADF
58 b = File.stat(fp.path)
59 next if orig_st.ino == b.ino && orig_st.dev == b.dev
64 File.open(fp.path, 'a') { |tmpfp| fp.reopen(tmpfp) }
68 # this should only happen in the master:
69 if orig_st.uid != new_st.uid || orig_st.gid != new_st.gid
70 fp.chown(orig_st.uid, orig_st.gid)
74 rescue IOError, Errno::EBADF
75 # not much we can do...
82 # creates and returns a new File object. The File is unlinked
83 # immediately, switched to binary mode, and userspace output
84 # buffering is disabled
87 TmpIO.open("#{Dir::tmpdir}/#{rand}",
88 File::RDWR|File::CREAT|File::EXCL, 0600)