rpc.mountd: set nonblocking mode if no libtirpc
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
NeilBrown
Reviewed-byQuoted 11 lines · On 05 Nov 2014 21:22:56 +0100 [email protected] wrote:
From: Bodo Stroesser <[email protected]>
Date: Thu, 09 Oct 2014 13:06:19 +0200
Subject: [nfs-utils] [PATCH 1/3] rpc.mountd: set nonblocking mode if no libtirpcIf mountd is built without libtirpc and it is started using "-p XXX" option, the tcp listeners and the sockets waiting for UDP messages are not in non-blocking mode. Thus if running with multiple threads (-t XX), all threads will wake up from select on a connection request or a UDP message, but only one thread will succeed. All others will wait on
accept()orread()for the next event.
Quoted 2 lines
Signed-off-by Bodo Stroesser <[email protected]>---
This is just taking the code that already applies in svc_socket() when no explicit port number is requested, and applying it also in makesock() which is used when an explicit port number *is* requested.
Thanks,
NeilBrown
Quoted 104 lines
support/include/nfslib.h +1
-0@@ -174,6 +174,7 @@ void closeall(int min);int svctcp_socket (u_long __number,int __reuse);int svcudp_socket (u_long __number); +int svcsock_nonblock (int __sock);/* Misc shared code prototypes */ size_t strlcat (char *,const char *,size_t );support/nfs/svc_socket.c +34
-32@@ -76,6 +76,39 @@ int getservport(u_long number, const chareturn 0 ; } +int +svcsock_nonblock (int sock) +{ +int flags; + +if (sock <0 ) +return sock; + +/* This socket might be shared among multiple processes +* if mountd is run multi-threaded. So it is safest to +* make it non-blocking, else all threads might wake +* one will get the data, and the others will block +* indefinitely. +* In all cases, transaction on this socket are atomic +* (accept for TCP, packet-read and packet-write for UDP) +* so O_NONBLOCK will not confuse unprepared code causing +* it to corrupt messages. +* It generally safest to have O_NONBLOCK when doing an accept +* as if we get a RST after the SYN and before accept runs, +* we can block despite being told there was an acceptable +* connection. +*/ +if ((flags =fcntl (sock,F_GETFL )) <0 ) +perror (_ ("svc_socket: can't get socket flags" )); +else if (fcntl (sock,F_SETFL , flags|O_NONBLOCK ) <0 ) +perror (_ ("svc_socket: can't set socket flags" )); +else +return sock; + + (void )__close (sock); +return -1 ; +} +static int svc_socket (u_long number,int type,int protocol,int reuse) {@@ -113,38 +146,7 @@ svc_socket (u_long number, int type, int sock =-1 ; } -if (sock >=0 ) - { -/* This socket might be shared among multiple processes -* if mountd is run multi-threaded. So it is safest to -* make it non-blocking, else all threads might wake -* one will get the data, and the others will block -* indefinitely. -* In all cases, transaction on this socket are atomic -* (accept for TCP, packet-read and packet-write for UDP) -* so O_NONBLOCK will not confuse unprepared code causing -* it to corrupt messages. -* It generally safest to have O_NONBLOCK when doing an accept -* as if we get a RST after the SYN and before accept runs, -* we can block despite being told there was an acceptable -* connection. -*/ -int flags; -if ((flags =fcntl (sock,F_GETFL )) <0 ) - { -perror (_ ("svc_socket: can't get socket flags" )); - (void )__close (sock); - sock =-1 ; - } -else if (fcntl (sock,F_SETFL , flags|O_NONBLOCK ) <0 ) - { -perror (_ ("svc_socket: can't set socket flags" )); - (void )__close (sock); - sock =-1 ; - } - } - -return sock; +return svcsock_nonblock (sock); }/* support/nfs/rpcmisc.c +1
-1@@ -104,7 +104,7 @@ makesock(int port, int proto)return -1 ; } -return sock; +return svcsock_nonblock (sock); }void