Display socket options

后端 未结 3 1139
不思量自难忘°
不思量自难忘° 2021-02-20 02:24

How I can see from shell what socket options are set? In particular I\'m interesting to know if SO_BROADCAST is set?

3条回答
  •  天涯浪人
    2021-02-20 02:56

    I had the same issue today; unfortunately, on my system, the -T option of lsof doesn't accept the f flag, and I also didn't want to build the knetstat kernel module.

    Luckily, I was in the position of being able to strace the application while it was setting up the socket, like this:

    strace -e trace=setsockopt -f -o /tmp/log ./program arg1 arg2
    

    This traces ./program arg1 arg2, writing the trace to /tmp/log. We only trace the setsockopt() system call, which is used to set socket options. The option -f makes strace also trace any child processes created by the traced program.

    If you are lucky, /tmp/log will contain lines like this one:

    18806 setsockopt(60, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0
    

    This indicates that process 18806 called setsockopt() on FD 60 to set SO_KEEPALIVE to 1 (enabling it), and that the system call succeeded with return code 0.

    It's also possible to attach to an existing process:

    strace -e trace=setsockopt -f -o /tmp/log -p PID
    

    You can detach from the process using CTRL-C, and omit the -o option and its argument to send the trace to stderr.

提交回复
热议问题