X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=support%2Fnfs%2Fsvc_socket.c;h=03a53256c1f4469ec5a36839672eaa6a5a5f0c28;hp=888c915931b000f42d86d026bc94e86ed8329139;hb=0cdfd35cecc17eb1927f15d33205e43ec66675f2;hpb=11d34d11153df198103a57291937ea9ff8b7356e diff --git a/support/nfs/svc_socket.c b/support/nfs/svc_socket.c index 888c915..03a5325 100644 --- a/support/nfs/svc_socket.c +++ b/support/nfs/svc_socket.c @@ -42,7 +42,7 @@ svc_socket (u_long number, int type, int protocol, int reuse) socklen_t len = sizeof (struct sockaddr_in); char rpcdata [1024], servdata [1024]; struct rpcent rpcbuf, *rpcp; - struct servent servbuf, *servp; + struct servent servbuf, *servp = NULL; int sock, ret; const char *proto = protocol == IPPROTO_TCP ? "tcp" : "udp"; @@ -64,7 +64,7 @@ svc_socket (u_long number, int type, int protocol, int reuse) } } - __bzero ((char *) &addr, sizeof (addr)); + memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; ret = getrpcbynumber_r (number, &rpcbuf, rpcdata, sizeof rpcdata, @@ -101,8 +101,6 @@ svc_socket (u_long number, int type, int protocol, int reuse) } else { - if (bindresvport (sock, &addr)) - { addr.sin_port = 0; if (bind (sock, (struct sockaddr *) &addr, len) < 0) { @@ -110,14 +108,24 @@ svc_socket (u_long number, int type, int protocol, int reuse) (void) __close (sock); sock = -1; } - } } - if (sock >= 0 && protocol == IPPROTO_TCP) + if (sock >= 0) { - /* Make the TCP rendezvous socket non-block to avoid - * problems with blocking in accept() after a spurious - * wakeup from the kernel */ + /* 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) { @@ -149,9 +157,9 @@ svctcp_socket (u_long number, int reuse) * Create and bind a UDP socket based on program number */ int -svcudp_socket (u_long number, int reuse) +svcudp_socket (u_long number) { - return svc_socket (number, SOCK_DGRAM, IPPROTO_UDP, reuse); + return svc_socket (number, SOCK_DGRAM, IPPROTO_UDP, FALSE); } #ifdef TEST @@ -166,7 +174,7 @@ check (u_long number, u_short port, int protocol, int reuse) if (protocol == IPPROTO_TCP) socket = svctcp_socket (number, reuse); else - socket = svcudp_socket (number, reuse); + socket = svcudp_socket (number); if (socket < 0) return 1;