All of lore.kernel.org
 help / color / mirror / Atom feed
* [nfs-utils PATCH] Binding a privileged port in rpc.statd.
@ 2013-01-10 14:27 Hemmo Nieminen
  2013-01-10 15:25 ` J. Bruce Fields
  2013-01-16 20:40 ` Steve Dickson
  0 siblings, 2 replies; 3+ messages in thread
From: Hemmo Nieminen @ 2013-01-10 14:27 UTC (permalink / raw)
  To: linux-nfs

[-- Attachment #1: Type: text/plain, Size: 259 bytes --]

Hi,

It seems that rpc.statd will fail to bind to a (free) port, if
the first port it receives is listed in /etc/services. The
bindresvport will keep on returning the same port if we close
the socket too early.

The attached patch should fix this.

-- 
Hemmo

[-- Attachment #2: 0001-Fix-socket-binding-loop.patch --]
[-- Type: text/plain, Size: 1501 bytes --]

>From abdaa0c27d66c71c9500b983fc4366cb3e514fff Mon Sep 17 00:00:00 2001
From: Hemmo Nieminen <hemmo.nieminen@iki.fi>
Date: Thu, 3 Jan 2013 10:00:17 +0200
Subject: [PATCH] Fix socket binding loop.

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.
---
 utils/statd/rmtcall.c | 19 +++++++++++++------
 1 file 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;
 }
-- 
1.8.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [nfs-utils PATCH] Binding a privileged port in rpc.statd.
  2013-01-10 14:27 [nfs-utils PATCH] Binding a privileged port in rpc.statd Hemmo Nieminen
@ 2013-01-10 15:25 ` J. Bruce Fields
  2013-01-16 20:40 ` Steve Dickson
  1 sibling, 0 replies; 3+ messages in thread
From: J. Bruce Fields @ 2013-01-10 15:25 UTC (permalink / raw)
  To: Hemmo Nieminen; +Cc: linux-nfs, steved, Chuck Lever

On Thu, Jan 10, 2013 at 04:27:10PM +0200, Hemmo Nieminen wrote:
> Hi,
> 
> It seems that rpc.statd will fail to bind to a (free) port, if
> the first port it receives is listed in /etc/services. The
> bindresvport will keep on returning the same port if we close
> the socket too early.
> 
> The attached patch should fix this.

Looks right to me.--b.

> 
> -- 
> Hemmo

> >From abdaa0c27d66c71c9500b983fc4366cb3e514fff Mon Sep 17 00:00:00 2001
> From: Hemmo Nieminen <hemmo.nieminen@iki.fi>
> Date: Thu, 3 Jan 2013 10:00:17 +0200
> Subject: [PATCH] Fix socket binding loop.
> 
> 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.
> ---
>  utils/statd/rmtcall.c | 19 +++++++++++++------
>  1 file 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;
>  }
> -- 
> 1.8.1
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [nfs-utils PATCH] Binding a privileged port in rpc.statd.
  2013-01-10 14:27 [nfs-utils PATCH] Binding a privileged port in rpc.statd Hemmo Nieminen
  2013-01-10 15:25 ` J. Bruce Fields
@ 2013-01-16 20:40 ` Steve Dickson
  1 sibling, 0 replies; 3+ messages in thread
From: Steve Dickson @ 2013-01-16 20:40 UTC (permalink / raw)
  To: Hemmo Nieminen; +Cc: linux-nfs



On 10/01/13 09:27, Hemmo Nieminen wrote:
> Hi,
> 
> It seems that rpc.statd will fail to bind to a (free) port, if
> the first port it receives is listed in /etc/services. The
> bindresvport will keep on returning the same port if we close
> the socket too early.
> 
> The attached patch should fix this.
> 
Committed....

steved.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-01-16 21:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-10 14:27 [nfs-utils PATCH] Binding a privileged port in rpc.statd Hemmo Nieminen
2013-01-10 15:25 ` J. Bruce Fields
2013-01-16 20:40 ` Steve Dickson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.