// File: /workspace/github_cpp_makefile_star2_O3/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 0x3187(%rip), %rdx      # 0x7a38
leaq 0x90(%rsp), %r14
movl $0x64, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x22d0
movq %r14, %rdi
callq 0x2290
testq %rax, %rax
je 0x4a3c
movq %rax, %r14
movq %rax, %rdi
callq 0x22a0
testq %rax, %rax
je 0x4a22
movq %rax, %rbp
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 0x90(%rsp), %r12
leaq 0x20(%rsp), %r13
cmpb $0xa, 0x12(%rbp)
jne 0x4a0e
addq $0x13, %rbp
movl $0x401, %esi            # imm = 0x401
movq %r15, %rdi
leaq 0x30ff(%rip), %rdx      # 0x7a45
movq %r12, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x22d0
movl $0x64, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x21a0
movb $0x0, 0x84(%rsp)
testl %eax, %eax
js 0x49fb
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x20(%rsp,%rax)
movq 0x20(%rsp), %rax
movq %rax, %rcx
movabsq $0x7374702f7665642f, %rdx # imm = 0x7374702F7665642F
xorq %rdx, %rcx
movzbl 0x28(%rsp), %edx
xorq $0x2f, %rdx
orq %rcx, %rdx
je 0x49af
movabsq $0x7974742f7665642f, %rcx # imm = 0x7974742F7665642F
cmpq %rcx, %rax
jne 0x4a0e
movq %rbp, %rdi
callq 0x22c0
movl %eax, 0x4(%rsp)
movl (%rbx), %esi
leaq 0x3091(%rip), %rdi      # 0x7a55
movl %eax, %edx
xorl %eax, %eax
callq 0x2040
movq 0x18(%rsp), %rdi
leaq 0x4(%rsp), %rbp
movq %rbp, %rsi
callq 0x4c8c
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x4c8c
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x4c8c
jmp 0x4a0e
movl (%rbx), %esi
leaq 0x3077(%rip), %rdi      # 0x7a7b
movq %r15, %rdx
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
callq 0x22a0
movq %rax, %rbp
testq %rax, %rax
jne 0x4929
movq %r14, %rdi
callq 0x22e0
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3055(%rip), %rdi      # 0x7a98
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2130
nop