Skip to contentSkip to conversation

nfs-utils 2 of 10 - Incorporate some clean up code from Ulrich Drepper

PATCH In review Steve Dickson 2 participants 4 messages Sep 23, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfs-utils 2 of 10 - Incorporate some clean up code from Ulrich Drepper

Incorporate some clean up code from Ulrich Drepper (bz# 134025)

As stated in https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=134025
Various daemons in nfs-utils close all file descriptors before starting work. This happens in a very inefficient way. All iterate over all possible descriptor values and make a close(2) call. Image what happens if the file descriptor limit is high?

There is no reason for this, programs can learn exactly which descriptors are used from the /proc/self/fd directory.

---------

utils/mountd/mountd.c renamed from utils/mountd/mountd.c.orig +18 -3
@@ -18,6 +18,7 @@
 #include <getopt.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <dirent.h>
 #include <sys/resource.h>
 #include "xmalloc.h"
 #include "misc.h"
@@ -553,9 +554,23 @@
 
 	/* Daemons should close all extra filehandles ... *before* RPC init. */
 	if (!foreground) {
-		int fd = sysconf (_SC_OPEN_MAX);
-		while (--fd > 2)
-			(void) close(fd);
+		DIR *dir = opendir("/proc/self/fd");
+		if (dir != NULL) {
+			int dfd = dirfd(dir);
+			struct dirent *d;
+
+			while ((d = readdir(dir)) != NULL) {
+				char *endp;
+				long n = strtol(d->d_name, &endp, 10);
+				if (*endp == '\0' && n > 2 && n != dfd)
+					(void) close(n);
+			}
+			closedir(dir);
+		} else {
+			int fd = sysconf (_SC_OPEN_MAX);
+			while (--fd > 2)
+				(void) close(fd);
+		}
 	}
 
 	new_cache = check_new_cache();
utils/nfsd/nfsd.c renamed from utils/nfsd/nfsd.c.orig +18 -3
@@ -18,6 +18,7 @@
 #include <getopt.h>
 #include <syslog.h>
 #include <netdb.h>
+#include <dirent.h>
 #include "nfslib.h"
 
 static void	usage(const char *);
@@ -27,6 +28,7 @@
 {
 	int	count = 1, c, error, port, fd;
 	struct servent *ent;
+	DIR *dir;
 
 	ent = getservbyname ("nfs", "udp");
 	if (ent != NULL)
@@ -80,9 +82,22 @@
 		(void) dup2(fd, 1);
 		(void) dup2(fd, 2);
 	}
-	fd = sysconf(_SC_OPEN_MAX);
-	while (--fd > 2)
-		(void) close(fd);
+	dir = opendir("/proc/self/fd");
+	if (dir != NULL) {
+		int dfd = dirfd(dir);
+		struct dirent *d;
+		while ((d = readdir(dir)) != NULL) {
+			char *endp;
+			long int n = strtol(d->d_name, &endp, 10);
+			if (*endp == '\0' && n > 2 && n != dfd)
+				(void) close(n);
+		}
+		closedir(dir);
+	} else {
+		fd = sysconf(_SC_OPEN_MAX);
+		while (--fd > 2)
+			(void) close(fd);
+	}
 
 	if ((error = nfssvc(port, count)) < 0) {
 		int e = errno;
utils/statd/statd.c renamed from utils/statd/statd.c.orig +34 -10

74 lines not on this page · raw message

utils/idmapd/idmapd.c renamed from utils/idmapd/idmapd.c.orig +22 -7
@@ -58,6 +58,7 @@
 #include <grp.h>
 #include <limits.h>
 #include <ctype.h>
+#include <dirent.h>
 #include <nfsidmap.h>
 
 #ifdef HAVE_CONFIG_H
@@ -859,14 +860,28 @@
 	}
 
 	if (noclose == 0) {
+		DIR *dir;
 		tempfd = open("/dev/null", O_RDWR);
-		close(0); dup2(tempfd, 0);
-		close(1); dup2(tempfd, 1);
-		close(2); dup2(tempfd, 2);
-		fdmax = sysconf (_SC_OPEN_MAX);
-		for (filedes = 3; filedes < fdmax; filedes++)
-			if (filedes != pipefds[1])
-				close (filedes);
+ 		dup2(tempfd, 0);
+ 		dup2(tempfd, 1);
+ 		dup2(tempfd, 2);
+ 		dir = opendir("/proc/self/fd");
+ 		if (dir != NULL) {
+ 			int dfd = dirfd(dir);
+ 			struct dirent *d;
+ 			while ((d = readdir(dir)) != NULL) {
+ 				char *endp;
+ 				long int n = strtol(d->d_name, &endp, 10);
+ 				if (*endp == '\0' && n > 2 && n != dfd &&
+ 				    n != pipefds[1])
+ 					(void) close(n);
+ 			}
+ 		} else {
+ 			fdmax = sysconf (_SC_OPEN_MAX);
+ 			for (filedes = 3; filedes < fdmax; filedes++) 
+ 				if (filedes != pipefds[1])
+ 					close (filedes);
+ 		}
 	}
 
 	return;
utils/svcgssd/svcgssd.c renamed from utils/svcgssd/svcgssd.c.orig +23 -7
@@ -52,6 +52,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
+#include <dirent.h>
 #include "svcgssd.h"
 #include "gss_util.h"
 #include "err_util.h"
@@ -110,14 +111,29 @@
 	}
 
 	if (noclose == 0) {
+		DIR *dir;
 		tempfd = open("/dev/null", O_RDWR);
-		close(0); dup2(tempfd, 0);
-		close(1); dup2(tempfd, 1);
-		close(2); dup2(tempfd, 2);
-		fdmax = sysconf (_SC_OPEN_MAX);
-		for (filedes = 3; filedes < fdmax; filedes++)
-			if (filedes != pipefds[1])
-				close (filedes);
+ 		dup2(tempfd, 0);
+ 		dup2(tempfd, 1);
+ 		dup2(tempfd, 2);
+ 		dir = opendir("/proc/self/fd");
+ 		if (dir != NULL) {
+ 			int dfd = dirfd(dir);
+ 			struct dirent *d;
+ 			while ((d = readdir(dir)) != NULL) {
+ 				char *endp;
+ 				long int n = strtol(d->d_name, &endp, 10);
+ 				if (*endp == '\0' && n > 2 && n != dfd &&
+ 				    n != pipefds[1])
+ 					(void) close(n);
+ 			}
+ 			closedir(dir);
+ 		} else {
+ 			fdmax = sysconf (_SC_OPEN_MAX);
+ 			for (filedes = 3; filedes < fdmax; filedes++) 
+ 				if (filedes != pipefds[1])
+ 					close (filedes);
+ 		}
 	}
 
 	return;
#1 lore raw

Hmm... There seems to be a lot of duplicated code snippets here. Things like

	DIR *dir = opendir("/proc/self/fd");
	if (dir != NULL) {
		int dfd = dirfd(dir);
		struct dirent *d;

		while ((d = readdir(dir)) != NULL) {
			char *endp;
			long n = strtol(d->d_name, &endp, 10);
			if (*endp == '\0' && n > 2 && n != dfd)
				(void) close(n);
		}
		closedir(dir);
	} else {
		int fd = sysconf (_SC_OPEN_MAX);
		while (--fd > 2)
			(void) close(fd);
	}

Why not clean that up further by putting it into a simple library
function that takes an argument consisting of the pipes you do not want it to close?

Cheers,
Trond

-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download it for free - -and be entered to win a 42" plasma tv or your very own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#2 lore raw
Quoted 3 lines · Trond Myklebust

Why not clean that up further by putting it into a simple library
function that takes an argument consisting of the pipes you do not want it to close?

Good point... its just one things that appeared one day (Uli when nuts and changed a bunch of system daemons) and since everything seem to still work I never gave it a second thought...

Where would be the best place for this this new routine? If we put the routine in libnfs.a, then both the idmapd and svcgssd daemons would have to start linking it which may or may not cause a problem.. Or maybe a new local library??

steved.

-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download it for free - -and be entered to win a 42" plasma tv or your very own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#3 lore raw

fr den 23.09.2005 Klokka 12:56 (-0400) skreiv Steve Dickson:

Quoted 5 lines

Where would be the best place for this this new routine? If we put the routine in libnfs.a, then both the idmapd and svcgssd daemons would have to start linking it which may or may not cause a problem.. Or maybe a new local library??

How about in libmisc?

Cheers,
Trond

-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download it for free - -and be entered to win a 42" plasma tv or your very own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs