Search code examples
cfile-descriptorlsof

verify open file descriptor with lsof


I think that my program leaves one open file descriptor.

int main()
{
    int my_fd = dup(STDOUT_FILENO);
    char *cmd[2];

    cmd[0] = "/bin/ls";
    cmd[1] = NULL;

    dprintf(2, "pid = %d\n", getpid());
    if (fork() != 0)
    {
        // close(my_fd);
        waitpid(-1, NULL, WUNTRACED);
    }
    else
    {
        dup2 (my_fd, STDERR_FILENO);
        // close(my_fd);
        execve(cmd[0], cmd, NULL);
    }
}

The output of the program is:

pid = 8718
myprog  myprog.c

I'd like to see this file descriptor with lsof or another command, I don't see it

I've tried to find similar questions on stackoverflow.

I've tried lsof -c myprog, it shows nothing.

I've tried lsof | grep myprog, it shows :

bash      7020                              an  cwd       DIR                9,0       4096    7082347 /mnt/md0/42/myprog
cpptools  7393                              an  cwd       DIR                9,0       4096    7082347 /mnt/md0/42/myprog
cpptools  7393 7395 cpptools                an  cwd       DIR                9,0       4096    7082347 /mnt/md0/42/myprog
cpptools  7393 7396 cpptools                an  cwd       DIR                9,0       4096    7082347 /mnt/md0/42/myprog
cpptools  7393 7397 cpptools                an  cwd       DIR                9,0       4096    7082347 /mnt/md0/42/myprog
cpptools  7393 7398 cpptools                an  cwd       DIR                9,0       4096    7082347 /mnt/md0/42/myprog
...
etc

I've tried ls -l /proc/$$/fd, it shows

lrwx------ 1 an an 64 oct.  29 23:00 0 -> /dev/pts/0
lrwx------ 1 an an 64 oct.  29 23:00 1 -> /dev/pts/0
lr-x------ 1 an an 64 oct.  29 23:00 103 -> /snap/code/143/usr/share/code/v8_context_snapshot.bin
lrwx------ 1 an an 64 oct.  29 23:00 2 -> /dev/pts/0
lrwx------ 1 an an 64 oct.  29 23:00 255 -> /dev/pts/0
l-wx------ 1 an an 64 oct.  29 23:00 36 -> /home/an/.config/Code/logs/20231029T225917/ptyhost.log
lrwx------ 1 an an 64 oct.  29 23:00 37 -> 'socket:[69635]'
lr-x------ 1 an an 64 oct.  29 23:00 38 -> /snap/code/143/usr/share/code/resources/app/node_modules.asar

I've tried lsof -a -p $$, it shows:

COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF    NODE NAME
bash    7020   an  cwd    DIR                9,0     4096 7082347 /mnt/md0/42/myprog
bash    7020   an  rtd    DIR               8,20     4096       2 /
bash    7020   an  txt    REG               8,20  1183448 5245518 /usr/bin/bash
bash    7020   an  mem    REG               8,20   183318 6030673 /usr/share/locale-langpack/fr/LC_MESSAGES/bash.mo
bash    7020   an  mem    REG               8,20    51856 5249542 /usr/lib/x86_64-linux-gnu/libnss_files-2.31.so
bash    7020   an  mem    REG                7,4   340640     187 /snap/code/143/usr/lib/locale/aa_DJ.utf8/LC_CTYPE
bash    7020   an  mem    REG                7,4  2586930     186 /snap/code/143/usr/lib/locale/aa_DJ.utf8/LC_COLLATE
...
etc

I've tried ls -l /proc/8718/fd, it answers:

ls: impossible d'accéder à '/proc/8718/fd': Aucun fichier ou dossier de ce type

Solution

  • Both your parent and child processes are exiting very quickly (i.e., before you have time to run any of the commands you tried). When a process exits1, all of its remaining open file descriptors are automatically closed by the kernel. If you want to have time to see them open, pick a longer-running command, e.g.:

        char *cmd[3];
    
        cmd[0] = "/bin/sleep";
        cmd[1] = "60";
        cmd[2] = NULL;
    

    1: Shenanigans with CLONE_FILES can change this. A more technically correct statement would be "when all threads with a given file descriptor table exit, all of the remaining file descriptors in that table are closed."