Who stole my file descriptors?

I am looking into a function socketpair()

. The file descriptors returned by this function are 5 and 6. How can I check which file and socket descriptors 3 and 4 have been assigned?

+3


source to share


2 answers


To improve on Frederik Deweerdt 's answer (and assuming a Linux system), for debugging purposes, you can add the following (for Linux systems) after a successful call to socketpair (2) (so after verifying that it didn't work):

 char cmdbuf[64];
 snprintf (cmdbuf, sizeof(cmdbuf), 
           "/bin/ls -l /proc/%d/fd/", (int) getpid());
 system(cmdbuf);

      

but this is just a disgusting debug hack. Perhaps you can opendir (3) , then readdir (3) the /proc/self/fd/

(and don't forget closedir

it) if you really want information like that from your running program (will of course opendir

use a file descriptor to read the directory ...). See proc (5) for details .

Alternatively, if your program is running in process pid 1234, just type ls -l /proc/1234/fd/

in another terminal.



You can strace (1) your entire program as well.

BTW, why are you worried about which file descriptors are used?

Of course, you could readlink (2) /proc/self/fd/4

if you like, from within your program, to understand how file descriptor 4 is used in it. Note that it readlink

takes a file path, not an open file descriptor.

+1


source


Below is a Linux specific answer:



A possible way would be to add the call pause(3)

immediately after the call sockerpair()

. This will stop the program and give you a look at '/ proc / [program pid] / fd'. This should give you some information on what the other open file descriptors are.

+3


source







All Articles