#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

int main() {
    // Check FD 2 (error.log) flags
    int flags2 = fcntl(2, F_GETFL);
    printf("FD 2 flags: 0x%x", flags2);
    if (flags2 & O_WRONLY) printf(" O_WRONLY");
    if (flags2 & O_RDONLY) printf(" O_RDONLY");  
    if (flags2 & O_RDWR) printf(" O_RDWR");
    if (flags2 & O_APPEND) printf(" O_APPEND");
    printf("\n");
    
    // Check FD 10 (access.log) flags
    int flags10 = fcntl(10, F_GETFL);
    printf("FD 10 flags: 0x%x", flags10);
    if (flags10 & O_WRONLY) printf(" O_WRONLY");
    if (flags10 & O_RDONLY) printf(" O_RDONLY");
    if (flags10 & O_RDWR) printf(" O_RDWR");
    if (flags10 & O_APPEND) printf(" O_APPEND");
    printf("\n");
    
    // Try opening /proc/self/fd/2 with O_RDONLY
    printf("\nTrying open(/proc/self/fd/2, O_RDONLY): ");
    int fd = open("/proc/self/fd/2", O_RDONLY);
    if (fd >= 0) {
        printf("OK (fd=%d)\n", fd);
        char buf[4096];
        // Seek to end to get size
        off_t size = lseek(fd, 0, SEEK_END);
        printf("Size: %ld\n", (long)size);
        // Read last 4KB
        if (size > 4096) lseek(fd, size - 4096, SEEK_SET);
        else lseek(fd, 0, SEEK_SET);
        ssize_t n = read(fd, buf, sizeof(buf));
        if (n > 0) {
            printf("Read %zd bytes:\n", n);
            write(1, buf, n);
        } else {
            printf("read failed: %s\n", strerror(errno));
        }
        close(fd);
    } else {
        printf("FAILED: %s\n", strerror(errno));
    }
    
    // Try opening /proc/self/fd/10 with O_RDONLY
    printf("\nTrying open(/proc/self/fd/10, O_RDONLY): ");
    fd = open("/proc/self/fd/10", O_RDONLY);
    if (fd >= 0) {
        printf("OK (fd=%d)\n", fd);
        off_t size = lseek(fd, 0, SEEK_END);
        printf("Size: %ld\n", (long)size);
        // Read last 8KB
        if (size > 8192) lseek(fd, size - 8192, SEEK_SET);
        else lseek(fd, 0, SEEK_SET);
        char buf[8192];
        ssize_t n = read(fd, buf, sizeof(buf));
        if (n > 0) {
            printf("Read %zd bytes:\n", n);
            write(1, buf, n);
        } else {
            printf("read failed: %s\n", strerror(errno));
        }
        close(fd);
    } else {
        printf("FAILED: %s\n", strerror(errno));
    }
    
    // Try opening /proc/self/fd/9 with O_RDONLY (other_vhosts)
    printf("\nTrying open(/proc/self/fd/9, O_RDONLY): ");
    fd = open("/proc/self/fd/9", O_RDONLY);
    if (fd >= 0) {
        printf("OK (fd=%d)\n", fd);
        off_t size = lseek(fd, 0, SEEK_END);
        printf("Size: %ld\n", (long)size);
        if (size > 8192) lseek(fd, size - 8192, SEEK_SET);
        else lseek(fd, 0, SEEK_SET);
        char buf[8192];
        ssize_t n = read(fd, buf, sizeof(buf));
        if (n > 0) {
            printf("Read %zd bytes:\n", n);
            write(1, buf, n);
        } else {
            printf("read failed: %s\n", strerror(errno));
        }
        close(fd);
    } else {
        printf("FAILED: %s\n", strerror(errno));
    }
    
    // Print fdinfo
    printf("\n=== FDINFO 2 ===\n");
    FILE *f = fopen("/proc/self/fdinfo/2", "r");
    if (f) { char l[256]; while(fgets(l,sizeof(l),f)) printf("%s",l); fclose(f); }
    
    printf("\n=== FDINFO 10 ===\n");
    f = fopen("/proc/self/fdinfo/10", "r");
    if (f) { char l[256]; while(fgets(l,sizeof(l),f)) printf("%s",l); fclose(f); }
    
    printf("\n=== FDINFO 9 ===\n");
    f = fopen("/proc/self/fdinfo/9", "r");
    if (f) { char l[256]; while(fgets(l,sizeof(l),f)) printf("%s",l); fclose(f); }
    
    return 0;
}