X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=support%2Fnfs%2Fsvc_socket.c;h=33076009ac0538de8569fd3489282bbeb2d672ed;hb=582e963f4aa62c9c6957c3868d487a82385cc5a1;hp=a3cb7ce2e1751dddedaf7e28ba63d4a8cf4d36c7;hpb=253a4182bcdb46a4da412513ba5880e352a8fa93;p=nfs-utils.git diff --git a/support/nfs/svc_socket.c b/support/nfs/svc_socket.c index a3cb7ce..3307600 100644 --- a/support/nfs/svc_socket.c +++ b/support/nfs/svc_socket.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #ifdef _LIBC @@ -41,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"; @@ -63,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, @@ -112,6 +113,37 @@ svc_socket (u_long number, int type, int protocol, int reuse) } } + 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; }