// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
movq %rdi, %rbx
movl (%rdi), %ecx
leaq 0x3411(%rip), %rdx      # 0x7a1c
leaq 0x90(%rsp), %r14
pushq $0x64
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x22e0
movq %r14, %rdi
callq 0x22a0
testq %rax, %rax
je 0x4773
movq %rax, %r14
leaq 0xf0(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0x120(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x150(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x100(%rsp), %r15
leaq 0x20(%rsp), %r13
pushq $0x64
popq %rbp
movq %r14, %rdi
callq 0x22b0
testq %rax, %rax
je 0x4759
movq %rax, %r12
cmpb $0xa, 0x12(%rax)
jne 0x4668
addq $0x13, %r12
movl $0x401, %esi            # imm = 0x401
movq %r15, %rdi
leaq 0x3394(%rip), %rdx      # 0x7a29
leaq 0x90(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x22e0
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x21b0
testl %eax, %eax
js 0x4741
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x20(%rsp,%rax)
movq %r13, %rdi
leaq 0x335e(%rip), %rsi      # 0x7a2f
pushq $0x9
popq %rdx
callq 0x2190
testl %eax, %eax
je 0x46f2
movabsq $0x7974742f7665642f, %rax # imm = 0x7974742F7665642F
cmpq %rax, 0x20(%rsp)
jne 0x4668
movq %r12, %rdi
callq 0x22d0
movl %eax, 0x4(%rsp)
movl (%rbx), %esi
leaq 0x3332(%rip), %rdi      # 0x7a39
movl %eax, %edx
xorl %eax, %eax
callq 0x2040
movq 0x18(%rsp), %rdi
leaq 0x4(%rsp), %r12
movq %r12, %rsi
callq 0x4b0a
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x4b0a
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x4b0a
jmp 0x4668
movl (%rbx), %esi
leaq 0x3315(%rip), %rdi      # 0x7a5f
movq %r15, %rdx
xorl %eax, %eax
callq 0x2040
jmp 0x4668
movq %r14, %rdi
callq 0x22f0
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3302(%rip), %rdi      # 0x7a7c
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2130