[dovecot-cvs] dovecot/src/lib fd-close-on-exec.c,1.10,1.10.2.1

tss at dovecot.org tss at dovecot.org
Thu Dec 28 22:52:00 UTC 2006


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv10944

Modified Files:
      Tag: branch_1_0
	fd-close-on-exec.c 
Log Message:
debug: OSX leaks an fd from gettimeofday(), ignore it.
Also cleaned up the code a bit.



Index: fd-close-on-exec.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/fd-close-on-exec.c,v
retrieving revision 1.10
retrieving revision 1.10.2.1
diff -u -d -r1.10 -r1.10.2.1
--- fd-close-on-exec.c	14 Feb 2006 18:28:15 -0000	1.10
+++ fd-close-on-exec.c	28 Dec 2006 22:51:58 -0000	1.10.2.1
@@ -27,50 +27,58 @@
 	struct ip_addr addr, raddr;
 	unsigned int port, rport;
 	struct stat st;
+	int old_errno;
 
-	while (first_fd < last_fd) {
-		if (fcntl(first_fd, F_GETFD, 0) != -1 || errno != EBADF) {
-			int old_errno = errno;
+	for (; first_fd < last_fd; first_fd++) {
+		if (fcntl(first_fd, F_GETFD, 0) == -1 && errno == EBADF)
+			continue;
 
-			if (net_getsockname(first_fd, &addr, &port) == 0) {
-				if (addr.family == AF_UNIX) {
-					struct sockaddr_un sa;
-					socklen_t socklen = sizeof(sa);
+		old_errno = errno;
 
-					if (getsockname(first_fd, (void *)&sa,
-							&socklen) < 0)
-						sa.sun_path[0] = '\0';
+		if (net_getsockname(first_fd, &addr, &port) == 0) {
+			if (addr.family == AF_UNIX) {
+				struct sockaddr_un sa;
 
-					i_panic("Leaked UNIX socket fd %d: %s",
-						first_fd, sa.sun_path);
-				}
+				socklen_t socklen = sizeof(sa);
 
-				if (net_getpeername(first_fd,
-						    &raddr, &rport) < 0) {
-					memset(&raddr, 0, sizeof(raddr));
-					rport = 0;
-				}
-				i_panic("Leaked socket fd %d: %s:%u -> %s:%u",
-					first_fd, net_ip2addr(&addr), port,
-					net_ip2addr(&raddr), rport);
+				if (getsockname(first_fd, (void *)&sa,
+						&socklen) < 0)
+					sa.sun_path[0] = '\0';
+
+				i_panic("Leaked UNIX socket fd %d: %s",
+					first_fd, sa.sun_path);
 			}
 
-			if (fstat(first_fd, &st) == 0) {
+			if (net_getpeername(first_fd, &raddr, &rport) < 0) {
+				memset(&raddr, 0, sizeof(raddr));
+				rport = 0;
+			}
+			i_panic("Leaked socket fd %d: %s:%u -> %s:%u",
+				first_fd, net_ip2addr(&addr), port,
+				net_ip2addr(&raddr), rport);
+		}
+
+		if (fstat(first_fd, &st) == 0) {
+#ifdef __APPLE__
+			/* OSX workaround: gettimeofday() calls shm_open()
+			   internally and the fd won't get closed on exec.
+			   We'll just skip all ino/dev=0 files and hope they
+			   weren't anything else. */
+			if (st.st_ino == 0 && st.st_dev == 0)
+				continue;
+#endif
 #ifdef HAVE_SYS_SYSMACROS_H
-				i_panic("Leaked file fd %d: dev %s.%s inode %s", first_fd,
-					dec2str(major(st.st_dev)),
-					dec2str(minor(st.st_dev)),
-					dec2str(st.st_ino));
+			i_panic("Leaked file fd %d: dev %s.%s inode %s",
+				first_fd, dec2str(major(st.st_dev)),
+				dec2str(minor(st.st_dev)), dec2str(st.st_ino));
 #else
-				i_panic("Leaked file fd %d: dev %s inode %s",
-					first_fd, dec2str(st.st_dev),
-					dec2str(st.st_ino));
+			i_panic("Leaked file fd %d: dev %s inode %s",
+				first_fd, dec2str(st.st_dev),
+				dec2str(st.st_ino));
 #endif
-			}
-
-			i_panic("Leaked unknown fd %d (errno = %s)",
-				first_fd, strerror(old_errno));
 		}
-		first_fd++;
+
+		i_panic("Leaked unknown fd %d (errno = %s)",
+			first_fd, strerror(old_errno));
 	}
 }



More information about the dovecot-cvs mailing list