summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHemmo Nieminen <hemmo.nieminen@iki.fi>2013-01-16 15:29:14 -0500
committerSteve Dickson <steved@redhat.com>2013-01-16 15:33:29 -0500
commiteb8229338f060f3974fd5557c8bc86fdb9005a81 (patch)
treead75eb303399691dfe6cfc4759589f54c30e343e
parent5ac9bcfd820f09af4d3f87f1f7346d896f70bc9a (diff)
downloadnfs-utils-eb8229338f060f3974fd5557c8bc86fdb9005a81.zip
nfs-utils-eb8229338f060f3974fd5557c8bc86fdb9005a81.tar.gz
nfs-utils-eb8229338f060f3974fd5557c8bc86fdb9005a81.tar.xz
rpc.statd: Fix socket binding loop.
From: Hemmo Nieminen <hemmo.nieminen@iki.fi> Instead of closing the sockets before requesting a new one, keep them open until a suitable one is found. Otherwise bindresvport will return the same port over and over again. Signed-off-by: Steve Dickson <steved@redhat.com>
-rw-r--r--utils/statd/rmtcall.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/utils/statd/rmtcall.c b/utils/statd/rmtcall.c
index 4ecb03c..fd576d9 100644
--- a/utils/statd/rmtcall.c
+++ b/utils/statd/rmtcall.c
@@ -68,21 +68,19 @@ statd_get_socket(void)
{
struct sockaddr_in sin;
struct servent *se;
- int loopcnt = 100;
+ const int loopcnt = 100;
+ int i, tmp_sockets[loopcnt];
if (sockfd >= 0)
return sockfd;
- while (loopcnt-- > 0) {
-
- if (sockfd >= 0) close(sockfd);
+ for (i = 0; i < loopcnt; ++i) {
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
xlog(L_ERROR, "%s: Can't create socket: %m", __func__);
- return -1;
+ break;
}
-
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
@@ -96,7 +94,16 @@ statd_get_socket(void)
if (se == NULL)
break;
/* rather not use that port, try again */
+
+ tmp_sockets[i] = sockfd;
}
+
+ while (--i >= 0)
+ close(tmp_sockets[i]);
+
+ if (sockfd < 0)
+ return -1;
+
FD_SET(sockfd, &SVC_FDSET);
return sockfd;
}