////////////////////////////////////////////////////////////////////////// Process a file (FD)
| 1766 | /////////////////////////////////////////////////////////////////////////////// |
| 1767 | // Process a file (FD) |
| 1768 | int |
| 1769 | process_file(int in_fd, off_t offset, unsigned max_age) |
| 1770 | { |
| 1771 | char buffer[MAX_LOGBUFFER_SIZE]; |
| 1772 | int nread, buffer_bytes; |
| 1773 | |
| 1774 | Dbg(dbg_ctl_logstats, "Processing file [offset=%" PRId64 "].", (int64_t)offset); |
| 1775 | while (true) { |
| 1776 | Dbg(dbg_ctl_logstats, "Reading initial header."); |
| 1777 | buffer[0] = '\0'; |
| 1778 | |
| 1779 | unsigned first_read_size = sizeof(uint32_t) + sizeof(uint32_t); |
| 1780 | LogBufferHeader *header = (LogBufferHeader *)&buffer[0]; |
| 1781 | |
| 1782 | // Find the next log header, aligning us properly. This is not |
| 1783 | // particularly optimal, but we should only have to do this |
| 1784 | // once, and hopefully we'll be aligned immediately. |
| 1785 | if (offset > 0) { |
| 1786 | Dbg(dbg_ctl_logstats, "Re-aligning file read."); |
| 1787 | while (true) { |
| 1788 | if (lseek(in_fd, offset, SEEK_SET) < 0) { |
| 1789 | Dbg(dbg_ctl_logstats, "Internal seek failed (offset=%" PRId64 ").", (int64_t)offset); |
| 1790 | return 1; |
| 1791 | } |
| 1792 | |
| 1793 | // read the first 8 bytes of the header, which will give us the |
| 1794 | // cookie and the version number. |
| 1795 | nread = read(in_fd, buffer, first_read_size); |
| 1796 | if (!nread || EOF == nread) { |
| 1797 | return 0; |
| 1798 | } |
| 1799 | // ensure that this is a valid logbuffer header |
| 1800 | if (header->cookie && (LOG_SEGMENT_COOKIE == header->cookie)) { |
| 1801 | offset = 0; |
| 1802 | break; |
| 1803 | } |
| 1804 | offset++; |
| 1805 | } |
| 1806 | if (!header->cookie) { |
| 1807 | return 0; |
| 1808 | } |
| 1809 | } else { |
| 1810 | nread = read(in_fd, buffer, first_read_size); |
| 1811 | if (!nread || EOF == nread || !header->cookie) { |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
| 1815 | // ensure that this is a valid logbuffer header |
| 1816 | if (header->cookie != LOG_SEGMENT_COOKIE) { |
| 1817 | Dbg(dbg_ctl_logstats, "Invalid segment cookie (expected %d, got %d)", LOG_SEGMENT_COOKIE, header->cookie); |
| 1818 | return 1; |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | Dbg(dbg_ctl_logstats, "LogBuffer version %d, current = %d", header->version, LOG_SEGMENT_VERSION); |
| 1823 | if (header->version != LOG_SEGMENT_VERSION) { |
| 1824 | return 1; |
| 1825 | } |
no test coverage detected