// File: overfl0[P]iojack/processes.cpp
// Function: processInfo::guessFds()
void processInfo::guessFds()
{
    // TODO: perform error checking!
    char path[100];
    snprintf(path, 100, "/proc/%d/fd/", pid);

    DIR *dp;
    struct dirent *ep;
    if((dp = opendir(path)) != NULL)
    {
        while((ep = readdir(dp)))
        {
            //printf("[%d] %s (inode: %ld)\n", pid, ep->d_name, (long)ep->d_ino);
            if(ep->d_type == DT_LNK)
            {
                char fdpath[1025];
                snprintf(fdpath, 1025, "%s/%s", path, ep->d_name);

                //struct stat retstat, retlstat;
                //stat(fdpath, &retstat);
                //lstat(fdpath, &retlstat);
                //printf("st_dev:\t%d\t%d\n", retstat.st_dev, retlstat.st_dev);
                //printf("st_ino:\t%d\t%d\n", (int)retstat.st_ino, (int)retlstat.st_ino);
                //printf("st_rdev:\t%d\t%d\n", (int)retstat.st_rdev, (int)retlstat.st_rdev);

                char linkName[101];
                int retval = readlink(fdpath, linkName, 100);
                linkName[100] = '\0';
                if(retval >= 0)
                {
                    linkName[retval] = '\0';
                    //printf("[%d] -> %s\n", pid, linkName);
                    if(!strncmp(linkName, "/dev/pts/", strlen("/dev/pts/"))
                    || !strncmp(linkName, "/dev/tty",  strlen("/dev/tty")))
                    {
                        //printf("[%d] This fd points to stdin/out/err\n", pid);
                        int fd = atoi(ep->d_name);
                        printf("[%d] Adding fd %d to watched streams\n", pid, fd);

                        // Add this fd to all the streams. This should do the trick
                        // until we find a more reliable way find which fd is which stream
                        stdin.insert(fd);
                        stdout.insert(fd);
                        stderr.insert(fd);
                    }

                } else {
                    printf("[%d] Readlink on %s failed!\n", pid, fdpath);
                }

            }
        }

        closedir(dp);
    }
    else
        perror ("Couldn't open the /proc/... directory");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x540, %rsp            # imm = 0x540
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x540(%rbp)
leaq -0x70(%rbp), %rdi
movl (%rax), %ecx
movl $0x64, %esi
leaq 0x56d9(%rip), %rdx      # 0xf5d5
movb $0x0, %al
callq 0x22c0
leaq -0x70(%rbp), %rdi
callq 0x2290
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0xa0c2
jmp 0x9f1c
movq -0x78(%rbp), %rdi
callq 0x22a0
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0xa0b7
movq -0x80(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0xa, %eax
jne 0xa0b2
leaq -0x490(%rbp), %rdi
leaq -0x70(%rbp), %rcx
movq -0x80(%rbp), %r8
addq $0x13, %r8
movl $0x401, %esi            # imm = 0x401
leaq 0x567f(%rip), %rdx      # 0xf5e2
movb $0x0, %al
callq 0x22c0
leaq -0x490(%rbp), %rdi
leaq -0x500(%rbp), %rsi
movl $0x64, %edx
callq 0x21b0
movl %eax, -0x504(%rbp)
movb $0x0, -0x49c(%rbp)
cmpl $0x0, -0x504(%rbp)
jl 0xa092
movslq -0x504(%rbp), %rax
movb $0x0, -0x500(%rbp,%rax)
leaq -0x500(%rbp), %rdi
leaq 0x562f(%rip), %rsi      # 0xf5e8
movl $0x9, %edx
callq 0x20c0
cmpl $0x0, %eax
je 0x9fe9
leaq -0x500(%rbp), %rdi
leaq 0x561c(%rip), %rsi      # 0xf5f2
movl $0x8, %edx
callq 0x20c0
cmpl $0x0, %eax
jne 0xa090
movq -0x80(%rbp), %rdi
addq $0x13, %rdi
callq 0x22b0
movl %eax, %ecx
movq -0x540(%rbp), %rax
movl %ecx, -0x508(%rbp)
movl (%rax), %esi
movl -0x508(%rbp), %edx
leaq 0x55e7(%rip), %rdi      # 0xf5fb
movb $0x0, %al
callq 0x2040
movq -0x540(%rbp), %rdi
addq $0xf0, %rdi
leaq -0x508(%rbp), %rsi
callq 0xa280
movq -0x540(%rbp), %rdi
movq %rax, -0x518(%rbp)
movb %dl, -0x510(%rbp)
addq $0x120, %rdi            # imm = 0x120
leaq -0x508(%rbp), %rsi
callq 0xa280
movq -0x540(%rbp), %rdi
movq %rax, -0x528(%rbp)
movb %dl, -0x520(%rbp)
addq $0x150, %rdi            # imm = 0x150
leaq -0x508(%rbp), %rsi
callq 0xa280
movq %rax, -0x538(%rbp)
movb %dl, -0x530(%rbp)
jmp 0xa0b0
movq -0x540(%rbp), %rax
movl (%rax), %esi
leaq -0x490(%rbp), %rdx
leaq 0x5578(%rip), %rdi      # 0xf621
movb $0x0, %al
callq 0x2040
jmp 0xa0b2
jmp 0x9f1c
movq -0x78(%rbp), %rdi
callq 0x22d0
jmp 0xa0ce
leaq 0x5575(%rip), %rdi      # 0xf63e
callq 0x2140
addq $0x540, %rsp            # imm = 0x540
popq %rbp
retq
nopw (%rax,%rax)