How to close a file descriptor from another process in unix systems

前端 未结 5 989
轮回少年
轮回少年 2021-01-30 21:56

You can use command lsof to get file descriptors for all running processes, but what I would like to do is to close some of those descriptors without being insi

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 22:59

    I don't know why you are trying to do this, but you should be able to attach to the process using gdb and then call close() on the fd. Example:

    In one shell: cat

    In another shell:

    $pidof cat
    7213
    
    $gdb -p 7213
    
    ...
    lots of output
    ...
    
    (gdb)
    

    Now you tell gdb to execute close(0):

    (gdb) p close(0)
    
    $1 = 0
    
    (gdb) c
    
    Continuing.
    
    Program exited with code 01.
    (gdb)
    

    In the first shell I get this output:

    cat: -: Bad file descriptor
    
    cat: closing standard input: Bad file descriptor
    

提交回复
热议问题