X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=support%2Fnfs%2Frpcmisc.c;h=a0854e7ef3f720a158b36f8aa6c8cf249e5dac01;hp=bb6820334dda88aa526c476cf5f06afcea5410cd;hb=695906482ffc0c3778c3df765cf980d19da25414;hpb=173f4c79c08a947742970a5e12d529249c2c3631 diff --git a/support/nfs/rpcmisc.c b/support/nfs/rpcmisc.c index bb68203..a0854e7 100644 --- a/support/nfs/rpcmisc.c +++ b/support/nfs/rpcmisc.c @@ -1,9 +1,8 @@ /* - * support/nfs/rpcmisc.c + * Miscellaneous functions for RPC service startup and shutdown. * - * Miscellaneous functions for RPC startup and shutdown. * This code is partially snarfed from rpcgen -s tcp -s udp, - * partly written by Mark Shand, Donald Becker, and Rick + * partly written by Mark Shand, Donald Becker, and Rick * Sladkey. It was tweaked slightly by Olaf Kirch to be * usable by both unfsd and mountd. * @@ -38,14 +37,76 @@ #define socklen_t int #endif -static void closedown(int sig); -static int makesock(int port, int proto); - #define _RPCSVC_CLOSEDOWN 120 int _rpcpmstart = 0; int _rpcfdtype = 0; int _rpcsvcdirty = 0; +static void +closedown(int sig) +{ + (void) signal(sig, closedown); + + if (_rpcsvcdirty == 0) { + static int size; + int i, openfd; + + if (_rpcfdtype == SOCK_DGRAM) + exit(0); + + if (size == 0) + size = getdtablesize(); + + for (i = 0, openfd = 0; i < size && openfd < 2; i++) + if (FD_ISSET(i, &svc_fdset)) + openfd++; + if (openfd <= 1) + exit(0); + } + + (void) alarm(_RPCSVC_CLOSEDOWN); +} + +/* + * Create listener socket for a given port + * + * Return an open network socket on success; otherwise return -1 + * if some error occurs. + */ +static int +makesock(int port, int proto) +{ + struct sockaddr_in sin; + int sock, sock_type, val; + + sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; + sock = socket(AF_INET, sock_type, proto); + if (sock < 0) { + xlog(L_FATAL, "Could not make a socket: %s", + strerror(errno)); + return -1; + } + memset((char *) &sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = htonl(INADDR_ANY); + sin.sin_port = htons(port); + + val = 1; + if (proto == IPPROTO_TCP) + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + &val, sizeof(val)) < 0) + xlog(L_ERROR, "setsockopt failed: %s", + strerror(errno)); + + if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) { + xlog(L_FATAL, "Could not bind name to socket: %s", + strerror(errno)); + return -1; + } + + return sock; +} + void rpc_init(char *name, int prog, int vers, void (*dispatch)(struct svc_req *, register SVCXPRT *), @@ -60,7 +121,7 @@ rpc_init(char *name, int prog, int vers, sock = 0; if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0 && saddr.sin_family == AF_INET) { - socklen_t ssize = sizeof (int); + socklen_t ssize = sizeof(int); int fdtype = 0; if (getsockopt(0, SOL_SOCKET, SO_TYPE, (char *)&fdtype, &ssize) == -1) @@ -80,7 +141,7 @@ rpc_init(char *name, int prog, int vers, if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { static SVCXPRT *last_transp = NULL; - + if (_rpcpmstart == 0) { if (last_transp && (!defport || defport == last_transp->xp_port)) { @@ -89,10 +150,8 @@ rpc_init(char *name, int prog, int vers, } if (defport == 0) sock = RPC_ANYSOCK; - else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) { - xlog(L_FATAL, "%s: cannot make a UDP socket\n", - name); - } + else + sock = makesock(defport, IPPROTO_UDP); } if (sock == RPC_ANYSOCK) sock = svcudp_socket (prog, 1); @@ -119,10 +178,8 @@ rpc_init(char *name, int prog, int vers, } if (defport == 0) sock = RPC_ANYSOCK; - else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) { - xlog(L_FATAL, "%s: cannot make a TCP socket\n", - name); - } + else + sock = makesock(defport, IPPROTO_TCP); } if (sock == RPC_ANYSOCK) sock = svctcp_socket (prog, 1); @@ -139,69 +196,7 @@ rpc_init(char *name, int prog, int vers, } if (_rpcpmstart) { - signal (SIGALRM, closedown); - alarm (_RPCSVC_CLOSEDOWN); + signal(SIGALRM, closedown); + alarm(_RPCSVC_CLOSEDOWN); } } - -static void closedown(sig) -int sig; -{ - (void) signal(sig, closedown); - if (_rpcsvcdirty == 0) { - static int size; - int i, openfd; - - if (_rpcfdtype == SOCK_DGRAM) - exit(0); - if (size == 0) { - size = getdtablesize(); - } - for (i = 0, openfd = 0; i < size && openfd < 2; i++) - if (FD_ISSET(i, &svc_fdset)) - openfd++; - if (openfd <= 1) - exit(0); - } - (void) alarm(_RPCSVC_CLOSEDOWN); -} - -/* - * Create listener socket for a given port - * - * Return an open network socket on success; otherwise return -1 - * if some error occurs. - */ -static int -makesock(int port, int proto) -{ - struct sockaddr_in sin; - int sock, sock_type, val; - - sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; - sock = socket(AF_INET, sock_type, proto); - if (sock < 0) { - xlog(L_FATAL, "Could not make a socket: %s", - strerror(errno)); - return -1; - } - memset((char *) &sin, 0, sizeof(sin)); - sin.sin_family = AF_INET; - sin.sin_addr.s_addr = htonl(INADDR_ANY); - sin.sin_port = htons(port); - - val = 1; - if (proto == IPPROTO_TCP) - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - &val, sizeof(val)) < 0) - xlog(L_ERROR, "setsockopt failed: %s", - strerror(errno)); - - if (bind(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) { - xlog(L_FATAL, "Could not bind name to socket: %s", - strerror(errno)); - return -1; - } - - return sock; -}