See Socket Options on Existing Sockets Created by Other Apps

See socket options on existing sockets created by other apps?

Unfortunately, nailer's answer only catches the SOL_TCP level socket options and does not the SOL_SOCKET level ones (like SO_KEEPALIVE).

Some of the distributions ships some examples together with systemtap. One of them is pfiles.stp that you can use to get the socket options from the sockets of a running process. Example from the file:

$ ./pfiles.stp `pgrep udevd`
787: udevd
Current rlimit: 32 file descriptors
0: S_IFCHR mode:0666 dev:0,15 ino:396 uid:0 gid:0 rdev:1,3
O_RDWR|O_LARGEFILE
/dev/null
1: S_IFCHR mode:0666 dev:0,15 ino:396 uid:0 gid:0 rdev:1,3
O_RDWR|O_LARGEFILE
/dev/null
2: S_IFCHR mode:0666 dev:0,15 ino:396 uid:0 gid:0 rdev:1,3
O_RDWR|O_LARGEFILE
/dev/null
3: S_IFDIR mode:0600 dev:0,9 ino:1 uid:0 gid:0 rdev:0,0
O_RDONLY
inotify
4: S_IFSOCK mode:0777 dev:0,4 ino:2353 uid:0 gid:0 rdev:0,0
O_RDWR
socket:[2353]
SO_PASSCRED,SO_TYPE(2),SO_SNDBUF(111616),SO_RCVBUF(111616)
sockname: AF_UNIX
5: S_IFSOCK mode:0777 dev:0,4 ino:2354 uid:0 gid:0 rdev:0,0
O_RDWR
socket:[2354]
SO_TYPE(2),SO_SNDBUF(111616),SO_RCVBUF(33554432)
ulocks: rcv
6: S_IFIFO mode:0600 dev:0,6 ino:2355 uid:0 gid:0 rdev:0,0
O_RDONLY|O_NONBLOCK
pipe:[2355]
7: S_IFIFO mode:0600 dev:0,6 ino:2355 uid:0 gid:0 rdev:0,0
O_WRONLY|O_NONBLOCK
pipe:[2355]

Is it possible to override the default socket options in requests?

Newer versions of urllib3 (since 1.8.3, released 2014-06-23) supports settings socket options.

You can set these options from requests (since 2.4.0, released 2014-08-29) by creating a custom adapter:

class HTTPAdapterWithSocketOptions(requests.adapters.HTTPAdapter):
def __init__(self, *args, **kwargs):
self.socket_options = kwargs.pop("socket_options", None)
super(HTTPAdapterWithSocketOptions, self).__init__(*args, **kwargs)

def init_poolmanager(self, *args, **kwargs):
if self.socket_options is not None:
kwargs["socket_options"] = self.socket_options
super(HTTPAdapterWithSocketOptions, self).init_poolmanager(*args, **kwargs)

Then you can mount this adapter to the sessions that need custom socket options (e.g. setting SO_KEEPALIVE):

adapter = HTTPAdapterWithSocketOptions(socket_options=[(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)])
s = requests.session()
s.mount("http://", adapter)
s.mount("https://", adapter)

How to get the file descriptors of TCP socket for a given process in Linux?

No. You simply cannot do what you are asking.

A file descriptor is just an integer, but it refers to an open file object in a given process. That integer value in another process refers to a different, possibly unopened file object.

Without involving the ptrace debugging API, or remote code injection, you are limited to what the kernel exposes to you via /proc.

Check out the man page for ss. If this utility can't show you information about a socket you desire, then nothing can.

Socket select() Time Switching?

I think you may have misunderstood what the select call is actually doing, the man page for select says the following:

Three independent sets of file descriptors are watched. Those
listed in readfds will be watched to see if characters become
available for reading (more precisely, to see if a read will not
block; in particu- lar, a file descriptor is also ready on
end-of-file), those in writefds will be watched to see if a
write will not block, and those in exceptfds will be watched for
exceptions. On exit, the sets are modified in place to indicate
which file descriptors actually changed status
. Each of the three
file descriptor sets may be specified as NULL if no file descriptors
are to be watched for the corresponding class of events.

So when your call to select returns what it will tell you is which, if any, of the file descriptors are (in your case) ready to be read from. It's then up to you to decide which to read and how to read it.

If you can I'd reccomend tracking down a copy of Unix Network Programming (by Stevens, Fenner, Rudoff). This will give you all the background information and example C code that you will ever want on network programming.

Or look at the tutorial here

check all socket opened in linux OS

/proc/net/tcp -a list of open tcp sockets

/proc/net/udp -a list of open udp sockets

/proc/net/raw -a list all the 'raw' sockets

These are the files, use cat command to view them. For example:

cat /proc/net/tcp

You can also use the lsof command.

lsof is a command meaning "list open files", which is used in many
Unix-like systems to report a list of all open files and the processes
that opened them.



Related Topics



Leave a reply



Submit