Skip to contentSkip to conversation

fix a file descriptor leak in rpc_subs.c

PATCH In review Jeff Moyer 3 participants 4 messages Aug 23, 2004 – Sep 11, 2004

Subsystems (2)
lore
Share & tools
lore

Jeff Moyer

author
#0 lore raw

PATCH 1/1 fix a file descriptor leak in rpc_subs.c

Hi Ian, list,

We never close the file descriptors we open in the rpc ping path. Here is a patch which addresses this. The patch appears in my latest rpms on my people page.

Comments welcome.

-Jeff

lib/rpc_subs.c renamed from lib/rpc_subs.c~ +5 -2
@@ -34,6 +34,7 @@
 
 struct conn_info {
 	const char *host;
+	int fd;
 	unsigned short port;
 	unsigned long program;
 	unsigned long version;
@@ -48,7 +49,6 @@ struct conn_info {
  */
 static CLIENT* create_udp_client(struct conn_info *info)
 {
-	int fd = -1;
 	CLIENT *client;
 	struct sockaddr_in addr;
 	struct hostent *hp;
@@ -62,13 +62,14 @@ static CLIENT* create_udp_client(struct 
 	if (!hp)
 		return NULL;
 
+	info->fd = RPC_ANYSOCK;
 	addr.sin_family = AF_INET;
 	addr.sin_port = htons(info->port);
 	memcpy(&addr.sin_addr.s_addr, hp->h_addr, hp->h_length);
 
 	client = clntudp_bufcreate(&addr,
 				   info->program, info->version,
-				   info->timeout, &fd,
+				   info->timeout, &info->fd,
 				   info->send_sz, info->recv_sz);
 
 	return client;
@@ -169,6 +170,7 @@ static CLIENT* create_tcp_client(struct 
 	if (!client)
 		goto out_close;
 
+	info->fd = fd;
 	return client;
 
 out_close:
@@ -214,6 +216,7 @@ static unsigned short portmap_getport(st
 			 pmap_info.timeout);
 
 	clnt_destroy(client);
+	close(pmap_info.fd);
 
 	if (stat != RPC_SUCCESS)
 		return 0;
#1 lore raw
Quoted 5 lines · Jeff Moyer

Hi Ian, list,

We never close the file descriptors we open in the rpc ping path. Here is a patch which addresses this. The patch appears in my latest rpms on my people page.

While I was looking at the problem Bryce reported I got the impression that the socket leak patch may not have completely covered the closeing of open sockets. I'm probably wrong but I came up with this alternative patch which I've called autofs-4.1.3-sock-leak-fix-2.patch.

Any thoughts?

lib/rpc_subs.c renamed from lib/rpc_subs.c.sock-leak-fix-2 +7 -1
@@ -48,7 +48,7 @@
  */
 static CLIENT* create_udp_client(struct conn_info *info)
 {
-	int fd = -1;
+	int fd = RPC_ANYSOCK;
 	CLIENT *client;
 	struct sockaddr_in addr;
 	struct hostent *hp;
@@ -179,6 +179,12 @@
 	if (!client)
 		goto out_close;
 
+	/* Close socket fd on destroy, as is default for rpcowned fds */
+	if  (!clnt_control(client, CLSET_FD_CLOSE, NULL)) {
+		clnt_destroy(client);
+		goto out_close;
+	}
+
 	return client;
 
 out_close:
#2 lore raw
Quoted 30 lines · Jeff Moyer
Quoted 6 lines

Hi Ian, list,

We never close the file descriptors we open in the rpc ping path. Here is a patch which addresses this. The patch appears in my latest rpms on my people page.

While I was looking at the problem Bryce reported I got the impression that the socket leak patch may not have completely covered the closeing of open sockets. I'm probably wrong but I came up with this alternative patch which I've called autofs-4.1.3-sock-leak-fix-2.patch.

Any thoughts?

lib/rpc_subs.c renamed from lib/rpc_subs.c.sock-leak-fix-2 +1 -1
@@ -48,7 +48,7 @@
  */
 static CLIENT* create_udp_client(struct conn_info *info)
 {
-	int fd = -1;
+	int fd = RPC_ANYSOCK;
 	CLIENT *client;
 	struct sockaddr_in addr;
 	struct hostent *hp;
@@ -179,6 +179,12 @@
 	if (!client)
 		goto out_close;

lib/rpc_subs.c line 190

Quoted 10 lines
lib/rpc_subs.c renamed from lib/rpc_subs.c.sock-leak-fix-2 +6 -0
 
+	/* Close socket fd on destroy, as is default for rpcowned fds */
+	if  (!clnt_control(client, CLSET_FD_CLOSE, NULL)) {
+		clnt_destroy(client);
+		goto out_close;
+	}
+
 	return client;
 
 out_close:

Applied and tried.. (and failed. Same messages about "nfs bindresvport:
Address already in use" in the log file)
One thing of note from an earlier email is that quite a few sockets are left around from just one mount

[root@localhost test]# netstat --inet
Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.1.40:ssh        192.168.1.100:32985     ESTABLISHED 

[root@localhost test]# ls 1
1
[root@localhost test]# netstat --inet
Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.1.40:855        192.168.1.100:sunrpc    TIME_WAIT   
tcp        0      0 192.168.1.40:858        192.168.1.100:sunrpc    TIME_WAIT   
tcp        0      0 192.168.1.40:800        192.168.1.100:nfs       ESTABLISHED 
tcp        0      0 192.168.1.40:ssh        192.168.1.100:32985     ESTABLISHED 
tcp        0      0 192.168.1.40:856        192.168.1.100:1013      TIME_WAIT 

so when it comes around to doing an ls * on my setup [root@localhost test]# netstat --inet | wc -l
2011
Thats an awful lot of sockets for a meer 200+ mount points Admittidly the vast majority are in TIME_WAIT states

Unfortunately I can't help beyond just observing results as my knowledge of RPC extends to just being able to spell it 8/

Phil
=--=

#3 lore raw
Quoted 39 lines · Bryce as root
Quoted 39 lines · Jeff Moyer
Quoted 6 lines

Hi Ian, list,

We never close the file descriptors we open in the rpc ping path. Here is a patch which addresses this. The patch appears in my latest rpms on my people page.

While I was looking at the problem Bryce reported I got the impression that the socket leak patch may not have completely covered the closeing of open sockets. I'm probably wrong but I came up with this alternative patch which I've called autofs-4.1.3-sock-leak-fix-2.patch.

Any thoughts?

lib/rpc_subs.c renamed from lib/rpc_subs.c.sock-leak-fix-2 +7 -1
@@ -48,7 +48,7 @@
  */
 static CLIENT* create_udp_client(struct conn_info *info)
 {
-	int fd = -1;
+	int fd = RPC_ANYSOCK;
 	CLIENT *client;
 	struct sockaddr_in addr;
 	struct hostent *hp;
@@ -179,6 +179,12 @@
 	if (!client)
 		goto out_close;
 
+	/* Close socket fd on destroy, as is default for rpcowned fds */
+	if  (!clnt_control(client, CLSET_FD_CLOSE, NULL)) {
+		clnt_destroy(client);
+		goto out_close;
+	}
+
 	return client;
 
Quoted 7 lines

lib/rpc_subs.c line 190

Quoted 1 line
lib/rpc_subs.c renamed from lib/rpc_subs.c.sock-leak-fix-2 +0 -0
 out_close:

Applied and tried.. (and failed. Same messages about "nfs bindresvport:
Address already in use" in the log file)
One thing of note from an earlier email is that quite a few sockets are left around from just one mount

After thinking about this further I find the fact that the socket leak made any difference at all mysterious.

In fact with an autofs mount that does not have replicated mount syntax none of the rpc code in autofs should be called. I'll be investigating this.

Bryce, if you do a mount manually, are there any open sockets resulting from that?

Ian