changeset 14691:3945a3646c67

Changed i_close_fd() API to set the fd to -1 after closing.
author Timo Sirainen <tss@iki.fi>
date Thu, 28 Jun 2012 00:27:13 +0300
parents 183adc90781c
children 9774ae8fff97
files src/auth/db-checkpassword.c src/auth/db-passwd-file.c src/auth/mech-winbind.c src/config/config-parser.c src/config/sysinfo-get.c src/director/director-test.c src/doveadm/doveadm-dump-log.c src/doveadm/doveadm-dump-mailboxlog.c src/doveadm/doveadm-instance.c src/doveadm/doveadm-master.c src/doveadm/doveadm-sis.c src/doveadm/doveadm-stats.c src/doveadm/dsync/doveadm-dsync.c src/doveadm/dsync/dsync-slave-io.c src/imap/main.c src/lda/main.c src/lib-dict/dict-file.c src/lib-dns/dns-lookup.c src/lib-imap-client/imapc-client.c src/lib-index/mail-cache-compress.c src/lib-lda/smtp-client.c src/lib-master/master-login.c src/lib-master/master-service-settings.c src/lib-master/mountpoint-list.c src/lib-settings/settings-parser.c src/lib-storage/index/index-attachment.c src/lib-storage/index/maildir/maildir-storage.c src/lib-storage/index/maildir/maildir-uidlist.c src/lib-storage/index/mbox/mbox-file.c src/lib-storage/index/mbox/mbox-lock.c src/lib-storage/index/mbox/mbox-storage.c src/lib-storage/index/pop3c/pop3c-client.c src/lib-storage/list/mailbox-list-maildir.c src/lib-storage/mailbox-uidvalidity.c src/lib/askpass.c src/lib/failures.c src/lib/fdatasync-path.c src/lib/file-copy.c src/lib/file-dotlock.c src/lib/ioloop-notify-dn.c src/lib/ioloop-notify-kqueue.c src/lib/iostream-rawlog.c src/lib/istream-seekable.c src/lib/lib.c src/lib/lib.h src/lib/macros.h src/lib/network.c src/lib/nfs-workarounds.c src/lib/randgen.c src/lib/safe-mkstemp.c src/lib/test-ostream-file.c src/lib/unlink-directory.c src/lmtp/commands.c src/login-common/access-lookup.c src/login-common/ssl-proxy-gnutls.c src/master/main.c src/master/service-listen.c src/master/service-monitor.c src/master/service.c src/plugins/acl/acl-backend-vfile-acllist.c src/plugins/acl/acl-backend-vfile.c src/plugins/fts-lucene/fts-backend-lucene.c src/plugins/fts-squat/squat-trie.c src/plugins/fts/doveadm-dump-fts-expunge-log.c src/plugins/fts/fts-expunge-log.c src/plugins/fts/fts-indexer.c src/plugins/fts/fts-parser-script.c src/plugins/quota/quota-maildir.c src/plugins/trash/trash-plugin.c src/plugins/virtual/virtual-config.c src/plugins/zlib/doveadm-zlib.c src/plugins/zlib/zlib-plugin.c src/pop3/main.c src/ssl-params/ssl-params.c src/util/gdbhelper.c src/util/rawlog.c
diffstat 76 files changed, 178 insertions(+), 175 deletions(-) [+]
line wrap: on
line diff
--- a/src/auth/db-checkpassword.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/auth/db-checkpassword.c	Thu Jun 28 00:27:13 2012 +0300
@@ -473,8 +473,8 @@
 		auth_request_log_error(request, "checkpassword",
 				       "pipe() failed: %m");
 		if (fd_in[0] != -1) {
-			i_close_fd(fd_in[0]);
-			i_close_fd(fd_in[1]);
+			i_close_fd(&fd_in[0]);
+			i_close_fd(&fd_in[1]);
 		}
 		callback(request, DB_CHECKPASSWORD_STATUS_INTERNAL_FAILURE,
 			 NULL, request_callback);
@@ -485,10 +485,10 @@
 	if (pid == -1) {
 		auth_request_log_error(request, "checkpassword",
 				       "fork() failed: %m");
-		i_close_fd(fd_in[0]);
-		i_close_fd(fd_in[1]);
-		i_close_fd(fd_out[0]);
-		i_close_fd(fd_out[1]);
+		i_close_fd(&fd_in[0]);
+		i_close_fd(&fd_in[1]);
+		i_close_fd(&fd_out[0]);
+		i_close_fd(&fd_out[1]);
 		callback(request, DB_CHECKPASSWORD_STATUS_INTERNAL_FAILURE,
 			 NULL, request_callback);
 		return;
@@ -496,8 +496,8 @@
 
 	if (pid == 0) {
 		/* child */
-		i_close_fd(fd_in[0]);
-		i_close_fd(fd_out[1]);
+		i_close_fd(&fd_in[0]);
+		i_close_fd(&fd_out[1]);
 		checkpassword_exec(db, request, fd_in[1], fd_out[0],
 				   auth_password != NULL);
 		/* not reached */
--- a/src/auth/db-passwd-file.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/auth/db-passwd-file.c	Thu Jun 28 00:27:13 2012 +0300
@@ -184,7 +184,7 @@
 
 	if (fstat(fd, &st) != 0) {
 		i_error("passwd-file %s: fstat() failed: %m", pw->path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return FALSE;
 	}
 
--- a/src/auth/mech-winbind.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/auth/mech-winbind.c	Thu Jun 28 00:27:13 2012 +0300
@@ -111,15 +111,15 @@
 		return;
 	}
 	if (pipe(outfd) < 0) {
-		i_close_fd(infd[0]); i_close_fd(infd[1]);
+		i_close_fd(&infd[0]); i_close_fd(&infd[1]);
 		return;
 	}
 
 	pid = fork();
 	if (pid < 0) {
 		i_error("fork() failed: %m");
-		i_close_fd(infd[0]); i_close_fd(infd[1]);
-		i_close_fd(outfd[0]); i_close_fd(outfd[1]);
+		i_close_fd(&infd[0]); i_close_fd(&infd[1]);
+		i_close_fd(&outfd[0]); i_close_fd(&outfd[1]);
 		return;
 	}
 
@@ -127,8 +127,8 @@
 		/* child */
 		const char *args[3];
 
-		i_close_fd(infd[0]);
-		i_close_fd(outfd[1]);
+		i_close_fd(&infd[0]);
+		i_close_fd(&outfd[1]);
 
 		if (dup2(outfd[0], STDIN_FILENO) < 0 ||
 		    dup2(infd[1], STDOUT_FILENO) < 0)
@@ -141,8 +141,8 @@
 	}
 
 	/* parent */
-	i_close_fd(infd[1]);
-	i_close_fd(outfd[0]);
+	i_close_fd(&infd[1]);
+	i_close_fd(&outfd[0]);
 
 	winbind->pid = pid;
 	winbind->in_pipe =
--- a/src/config/config-parser.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/config/config-parser.c	Thu Jun 28 00:27:13 2012 +0300
@@ -439,7 +439,7 @@
 		*error_r = t_strdup_printf("%s: read(%s) failed: %m",
 					   key, path);
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return ret < 0 ? -1 : 0;
 }
 
--- a/src/config/sysinfo-get.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/config/sysinfo-get.c	Thu Jun 28 00:27:13 2012 +0300
@@ -21,7 +21,7 @@
 	if (fd == -1)
 		return FALSE;
 	ret = read(fd, buf, sizeof(buf));
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	if (ret <= 0)
 		return FALSE;
 
--- a/src/director/director-test.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/director/director-test.c	Thu Jun 28 00:27:13 2012 +0300
@@ -338,7 +338,7 @@
 
 	out_fd = net_connect_ip(local_ip, DIRECTOR_OUT_PORT, NULL);
 	if (out_fd == -1) {
-		i_close_fd(in_fd);
+		i_close_fd(&in_fd);
 		return;
 	}
 
--- a/src/doveadm/doveadm-dump-log.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/doveadm/doveadm-dump-log.c	Thu Jun 28 00:27:13 2012 +0300
@@ -509,7 +509,7 @@
 	    hdr.major_version == MAIL_TRANSACTION_LOG_MAJOR_VERSION &&
 	    hdr.hdr_size >= MAIL_TRANSACTION_LOG_HEADER_MIN_SIZE)
 		ret = TRUE;
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return ret;
 }
 
--- a/src/doveadm/doveadm-dump-mailboxlog.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/doveadm/doveadm-dump-mailboxlog.c	Thu Jun 28 00:27:13 2012 +0300
@@ -100,7 +100,7 @@
 			break;
 		}
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return ret;
 }
 
--- a/src/doveadm/doveadm-instance.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/doveadm/doveadm-instance.c	Thu Jun 28 00:27:13 2012 +0300
@@ -39,7 +39,7 @@
 				  (kill(pid, 0) < 0 && errno == ESRCH));
 		}
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return found;
 }
 
--- a/src/doveadm/doveadm-master.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/doveadm/doveadm-master.c	Thu Jun 28 00:27:13 2012 +0300
@@ -41,7 +41,7 @@
 				  (kill(*pid_r, 0) < 0 && errno == ESRCH));
 		}
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return found;
 }
 
--- a/src/doveadm/doveadm-sis.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/doveadm/doveadm-sis.c	Thu Jun 28 00:27:13 2012 +0300
@@ -48,7 +48,7 @@
 	if (fd1 == -1) {
 		if (errno != ENOENT)
 			i_error("open(%s) failed: %m", path2);
-		i_close_fd(fd1);
+		i_close_fd(&fd1);
 		return -1;
 	}
 
--- a/src/doveadm/doveadm-stats.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/doveadm/doveadm-stats.c	Thu Jun 28 00:27:13 2012 +0300
@@ -496,7 +496,7 @@
 	array_free(&ctx.lines);
 	pool_unref(&ctx.prev_pool);
 	pool_unref(&ctx.cur_pool);
-	i_close_fd(ctx.fd);
+	i_close_fd(&ctx.fd);
 }
 
 static void cmd_stats_top(int argc, char *argv[])
--- a/src/doveadm/dsync/doveadm-dsync.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/doveadm/dsync/doveadm-dsync.c	Thu Jun 28 00:27:13 2012 +0300
@@ -79,12 +79,12 @@
 		    dup2(fd_err[1], STDERR_FILENO) < 0)
 			i_fatal("dup2() failed: %m");
 
-		i_close_fd(fd_in[0]);
-		i_close_fd(fd_in[1]);
-		i_close_fd(fd_out[0]);
-		i_close_fd(fd_out[1]);
-		i_close_fd(fd_err[0]);
-		i_close_fd(fd_err[1]);
+		i_close_fd(&fd_in[0]);
+		i_close_fd(&fd_in[1]);
+		i_close_fd(&fd_out[0]);
+		i_close_fd(&fd_out[1]);
+		i_close_fd(&fd_err[0]);
+		i_close_fd(&fd_err[1]);
 
 		execvp_const(args[0], args);
 	default:
@@ -92,9 +92,9 @@
 		break;
 	}
 
-	i_close_fd(fd_in[0]);
-	i_close_fd(fd_out[1]);
-	i_close_fd(fd_err[1]);
+	i_close_fd(&fd_in[0]);
+	i_close_fd(&fd_out[1]);
+	i_close_fd(&fd_err[1]);
 	ctx->fd_in = fd_out[0];
 	ctx->fd_out = fd_in[1];
 	ctx->fd_err = fd_err[0];
@@ -362,10 +362,8 @@
 		dsync_slave_deinit(&slave2);
 	if (ctx->io_err != NULL)
 		io_remove(&ctx->io_err);
-	if (ctx->fd_err != -1) {
-		i_close_fd(ctx->fd_err);
-		ctx->fd_err = -1;
-	}
+	if (ctx->fd_err != -1)
+		i_close_fd(&ctx->fd_err);
 	return ret;
 }
 
--- a/src/doveadm/dsync/dsync-slave-io.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/doveadm/dsync/dsync-slave-io.c	Thu Jun 28 00:27:13 2012 +0300
@@ -1365,7 +1365,7 @@
 	if (unlink(str_c(path)) < 0) {
 		/* shouldn't happen.. */
 		i_error("unlink(%s) failed: %m", str_c(path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 
--- a/src/imap/main.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/imap/main.c	Thu Jun 28 00:27:13 2012 +0300
@@ -271,13 +271,15 @@
 				 client->auth_req.data_size);
 	if (client_create_from_input(&input, client, client->fd, client->fd,
 				     &input_buf, &error) < 0) {
-		if (write(client->fd, MSG_BYE_INTERNAL_ERROR,
+		int fd = client->fd;
+
+		if (write(fd, MSG_BYE_INTERNAL_ERROR,
 			  strlen(MSG_BYE_INTERNAL_ERROR)) < 0) {
 			if (errno != EAGAIN && errno != EPIPE)
 				i_error("write(client) failed: %m");
 		}
 		i_error("%s", error);
-		i_close_fd(client->fd);
+		i_close_fd(&fd);
 		master_service_client_connection_destroyed(master_service);
 	}
 }
--- a/src/lda/main.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lda/main.c	Thu Jun 28 00:27:13 2012 +0300
@@ -101,7 +101,7 @@
 	if (unlink(str_c(path)) < 0) {
 		/* shouldn't happen.. */
 		i_error("unlink(%s) failed: %m", str_c(path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 
--- a/src/lib-dict/dict-file.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-dict/dict-file.c	Thu Jun 28 00:27:13 2012 +0300
@@ -494,7 +494,7 @@
 		if (dotlock != NULL)
 			file_dotlock_delete(&dotlock);
 		else {
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			file_unlock(&lock);
 		}
 		return -1;
@@ -522,7 +522,7 @@
 	if (o_stream_nfinish(output) < 0) {
 		i_error("write(%s) failed: %m", temp_path);
 		o_stream_destroy(&output);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	o_stream_destroy(&output);
@@ -530,7 +530,7 @@
 	if (dotlock != NULL) {
 		if (file_dotlock_replace(&dotlock,
 				DOTLOCK_REPLACE_FLAG_DONT_CLOSE_FD) < 0) {
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			return -1;
 		}
 	} else {
@@ -538,14 +538,14 @@
 			i_error("rename(%s, %s) failed: %m",
 				temp_path, dict->path);
 			file_unlock(&lock);
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			return -1;
 		}
 		file_lock_free(&lock);
 	}
 
 	if (dict->fd != -1)
-		i_close_fd(dict->fd);
+		i_close_fd(&dict->fd);
 	dict->fd = fd;
 	return 0;
 }
--- a/src/lib-dns/dns-lookup.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-dns/dns-lookup.c	Thu Jun 28 00:27:13 2012 +0300
@@ -144,7 +144,7 @@
 	if (write_full(fd, cmd, strlen(cmd)) < 0) {
 		result.error = t_strdup_printf("write(%s) failed: %m",
 					       set->dns_client_socket_path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		callback(&result, context);
 		return -1;
 	}
--- a/src/lib-imap-client/imapc-client.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-imap-client/imapc-client.c	Thu Jun 28 00:27:13 2012 +0300
@@ -405,7 +405,7 @@
 	if (unlink(str_c(path)) < 0) {
 		/* shouldn't happen.. */
 		i_error("unlink(%s) failed: %m", str_c(path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	*path_r = str_c(path);
--- a/src/lib-index/mail-cache-compress.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-index/mail-cache-compress.c	Thu Jun 28 00:27:13 2012 +0300
@@ -324,7 +324,7 @@
 		}
 
 		ret = read_full(fd, &hdr, sizeof(hdr));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 
 		if (ret >= 0) {
 			if (ret == 0)
@@ -407,7 +407,7 @@
 				 DOTLOCK_REPLACE_FLAG_DONT_CLOSE_FD) < 0) {
 		mail_cache_set_syscall_error(cache,
 					     "file_dotlock_replace()");
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		array_free(&ext_offsets);
 		return -1;
 	}
--- a/src/lib-lda/smtp-client.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-lda/smtp-client.c	Thu Jun 28 00:27:13 2012 +0300
@@ -94,15 +94,15 @@
 
 	if ((pid = fork()) == (pid_t)-1) {
 		i_error("fork() failed: %m");
-		i_close_fd(fd[0]); i_close_fd(fd[1]);
+		i_close_fd(&fd[0]); i_close_fd(&fd[1]);
 		return smtp_client_devnull(output_r);
 	}
 	if (pid == 0) {
 		/* child */
-		i_close_fd(fd[1]);
+		i_close_fd(&fd[1]);
 		smtp_client_run_sendmail(set, destination, return_path, fd[0]);
 	}
-	i_close_fd(fd[0]);
+	i_close_fd(&fd[0]);
 
 	client = i_new(struct smtp_client, 1);
 	client->output = o_stream_create_fd(fd[1], IO_BLOCK_SIZE, TRUE);
@@ -132,7 +132,7 @@
 	if (unlink(str_c(path)) < 0) {
 		/* shouldn't happen.. */
 		i_error("unlink(%s) failed: %m", str_c(path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 
--- a/src/lib-master/master-login.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-master/master-login.c	Thu Jun 28 00:27:13 2012 +0300
@@ -330,7 +330,7 @@
 			i_error("write(%s) failed: partial write",
 				login->postlogin_socket_path);
 		}
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	net_set_nonblock(fd, TRUE);
--- a/src/lib-master/master-service-settings.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-master/master-service-settings.c	Thu Jun 28 00:27:13 2012 +0300
@@ -347,7 +347,7 @@
 			return -1;
 
 		if (config_send_request(input, fd, path, error_r) < 0) {
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			config_exec_fallback(service, input);
 			return -1;
 		}
@@ -405,7 +405,7 @@
 				*error_r = t_strdup_printf(
 					"Timeout reading config from %s", path);
 			}
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			config_exec_fallback(service, input);
 			return -1;
 		}
@@ -414,7 +414,7 @@
 		    service->config_fd == -1 && input->config_path == NULL)
 			service->config_fd = fd;
 		else
-			i_close_fd(fd);
+			i_close_fd(&fd);
 	}
 
 	if (fd == -1 || service->keep_environment) {
--- a/src/lib-master/mountpoint-list.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-master/mountpoint-list.c	Thu Jun 28 00:27:13 2012 +0300
@@ -203,10 +203,10 @@
 	}
 	if (write_full(fd, str_data(data), str_len(data)) < 0) {
 		i_error("write(%s) failed: %m", str_c(temp_path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 	} else if (fdatasync(fd) < 0) {
 		i_error("fdatasync(%s) failed: %m", str_c(temp_path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 	} else if (close(fd) < 0) {
 		i_error("close(%s) failed: %m", str_c(temp_path));
 	} else if (rename(str_c(temp_path), path) < 0) {
--- a/src/lib-settings/settings-parser.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-settings/settings-parser.c	Thu Jun 28 00:27:13 2012 +0300
@@ -1046,8 +1046,8 @@
 	pid = fork();
 	if (pid == (pid_t)-1) {
 		i_error("fork() failed: %m");
-		i_close_fd(fd[0]);
-		i_close_fd(fd[1]);
+		i_close_fd(&fd[0]);
+		i_close_fd(&fd[1]);
 		return -1;
 	}
 	if (pid == 0) {
@@ -1061,13 +1061,13 @@
 		argv[0] = bin_path;
 		argv[2] = config_path;
 		argv[4] = service;
-		i_close_fd(fd[0]);
+		i_close_fd(&fd[0]);
 		if (dup2(fd[1], STDOUT_FILENO) < 0)
 			i_fatal("dup2() failed: %m");
 
 		execv_const(argv[0], argv);
 	}
-	i_close_fd(fd[1]);
+	i_close_fd(&fd[1]);
 
 	input = i_stream_create_fd(fd[0], (size_t)-1, TRUE);
 	ret = settings_parse_stream_read(ctx, input);
--- a/src/lib-storage/index/index-attachment.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/index/index-attachment.c	Thu Jun 28 00:27:13 2012 +0300
@@ -180,7 +180,7 @@
 	if (unlink(str_c(temp_path)) < 0) {
 		mail_storage_set_critical(storage,
 			"unlink(%s) failed: %m", str_c(temp_path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	return fd;
--- a/src/lib-storage/index/maildir/maildir-storage.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/index/maildir/maildir-storage.c	Thu Jun 28 00:27:13 2012 +0300
@@ -401,7 +401,7 @@
 				"fchown(%s) failed: %m", path);
 		}
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return 0;
 }
 
--- a/src/lib-storage/index/maildir/maildir-uidlist.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/index/maildir/maildir-uidlist.c	Thu Jun 28 00:27:13 2012 +0300
@@ -740,7 +740,7 @@
 	}
 
 	if (fstat(fd, &st) < 0) {
-                i_close_fd(fd);
+                i_close_fd(&fd);
                 if (errno == ESTALE && try_retry) {
                         *retry_r = TRUE;
                         return -1;
@@ -1468,7 +1468,7 @@
 		maildir_uidlist_update_hdr(uidlist, &st);
 	}
 	if (ret < 0)
-		i_close_fd(fd);
+		i_close_fd(&fd);
 	return ret;
 }
 
--- a/src/lib-storage/index/mbox/mbox-file.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/index/mbox/mbox-file.c	Thu Jun 28 00:27:13 2012 +0300
@@ -39,7 +39,7 @@
 
 	if (fstat(fd, &st) < 0) {
 		mbox_set_syscall_error(mbox, "fstat()");
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 
--- a/src/lib-storage/index/mbox/mbox-lock.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/index/mbox/mbox-lock.c	Thu Jun 28 00:27:13 2012 +0300
@@ -281,7 +281,7 @@
 		if (chdir(dir) < 0) {
 			mail_storage_set_critical(&mbox->storage->storage,
 				"chdir(%s) failed: %m", dir);
-			i_close_fd(orig_dir_fd);
+			i_close_fd(&orig_dir_fd);
 			return -1;
 		}
 		fname++;
@@ -295,7 +295,7 @@
 	}
 
 	if (restrict_access_use_priv_gid() < 0) {
-		i_close_fd(orig_dir_fd);
+		i_close_fd(&orig_dir_fd);
 		return -1;
 	}
 
@@ -336,7 +336,7 @@
 		mail_storage_set_critical(&mbox->storage->storage,
 			"fchdir() failed: %m");
 	}
-	i_close_fd(orig_dir_fd);
+	i_close_fd(&orig_dir_fd);
 	errno = orig_errno;
 	return ret;
 }
--- a/src/lib-storage/index/mbox/mbox-storage.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/index/mbox/mbox-storage.c	Thu Jun 28 00:27:13 2012 +0300
@@ -512,7 +512,7 @@
 		restrict_access_drop_priv_gid();
 	}
 	if (fd != -1) {
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return 0;
 	} else if (errno == EACCES) {
 		mail_storage_set_critical(box->storage, "%s",
@@ -553,7 +553,7 @@
 					       "Mailbox already exists");
 			return -1;
 		}
-		i_close_fd(fd);
+		i_close_fd(&fd);
 	}
 	return update == NULL ? 0 : mbox_mailbox_update(box, update);
 }
--- a/src/lib-storage/index/pop3c/pop3c-client.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/index/pop3c/pop3c-client.c	Thu Jun 28 00:27:13 2012 +0300
@@ -715,7 +715,7 @@
 	if (unlink(str_c(path)) < 0) {
 		/* shouldn't happen.. */
 		i_error("unlink(%s) failed: %m", str_c(path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 
--- a/src/lib-storage/list/mailbox-list-maildir.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/list/mailbox-list-maildir.c	Thu Jun 28 00:27:13 2012 +0300
@@ -304,7 +304,7 @@
 				"fchown(%s) failed: %m", path);
 		}
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return 0;
 }
 
--- a/src/lib-storage/mailbox-uidvalidity.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib-storage/mailbox-uidvalidity.c	Thu Jun 28 00:27:13 2012 +0300
@@ -168,7 +168,7 @@
 			i_error("creat(%s) failed: %m", tmp);
 			return cur_value;
 		}
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		mailbox_uidvalidity_write(list, path, cur_value);
 		return cur_value;
 	}
@@ -201,14 +201,14 @@
 	ret = read_full(fd, buf, sizeof(buf)-1);
 	if (ret < 0) {
 		i_error("read(%s) failed: %m", path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return mailbox_uidvalidity_next_rescan(list, path);
 	}
 	buf[sizeof(buf)-1] = 0;
 	cur_value = strtoul(buf, &endp, 16);
 	if (ret == 0 || endp != buf+sizeof(buf)-1) {
 		/* broken value */
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return mailbox_uidvalidity_next_rescan(list, path);
 	}
 
--- a/src/lib/askpass.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/askpass.c	Thu Jun 28 00:27:13 2012 +0300
@@ -50,7 +50,7 @@
 			(void)tcsetattr(fd, TCSAFLUSH, &old_tio);
 
 		fputs("\n", stderr); fflush(stderr);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 	}
 }
 
--- a/src/lib/failures.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/failures.c	Thu Jun 28 00:27:13 2012 +0300
@@ -714,17 +714,17 @@
 		log_info_fd = STDERR_FILENO;
 
 	if (log_fd != STDERR_FILENO) {
-		i_close_fd(log_fd);
+		i_close_fd(&log_fd);
 		log_fd = STDERR_FILENO;
 	}
 
 	if (log_info_fd != STDERR_FILENO) {
-		i_close_fd(log_info_fd);
+		i_close_fd(&log_info_fd);
 		log_info_fd = STDERR_FILENO;
 	}
 
 	if (log_debug_fd != STDERR_FILENO) {
-		i_close_fd(log_debug_fd);
+		i_close_fd(&log_debug_fd);
 		log_debug_fd = STDERR_FILENO;
 	}
 
--- a/src/lib/fdatasync-path.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/fdatasync-path.c	Thu Jun 28 00:27:13 2012 +0300
@@ -26,6 +26,6 @@
 			ret = -1;
 		}
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return ret;
 }
--- a/src/lib/file-copy.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/file-copy.c	Thu Jun 28 00:27:13 2012 +0300
@@ -52,7 +52,7 @@
 
 	if (fstat(fd_in, &st) < 0) {
 		i_error("fstat(%s) failed: %m", srcpath);
-		i_close_fd(fd_in);
+		i_close_fd(&fd_in);
 		return -1;
 	}
 
@@ -61,7 +61,7 @@
 	umask(old_umask);
 	if (fd_out == -1) {
 		i_error("open(%s, O_CREAT) failed: %m", tmppath);
-		i_close_fd(fd_in);
+		i_close_fd(&fd_in);
 		return -1;
 	}
 
--- a/src/lib/file-dotlock.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/file-dotlock.c	Thu Jun 28 00:27:13 2012 +0300
@@ -105,7 +105,7 @@
 
 	/* read line */
 	ret = read(fd, buf, sizeof(buf)-1);
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	if (ret <= 0)
 		return -1;
 
@@ -364,8 +364,7 @@
 			if (file_write_pid(lock_info->fd,
 					   str_c(tmp_path),
 					   lock_info->set->nfs_flush) < 0) {
-				i_close_fd(lock_info->fd);
-				lock_info->fd = -1;
+				i_close_fd(&lock_info->fd);
 				return -1;
 			}
 		}
@@ -418,7 +417,7 @@
 	if (write_pid) {
 		if (file_write_pid(fd, lock_info->lock_path,
 				   lock_info->set->nfs_flush) < 0) {
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			return -1;
 		}
 	}
--- a/src/lib/ioloop-notify-dn.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/ioloop-notify-dn.c	Thu Jun 28 00:27:13 2012 +0300
@@ -120,7 +120,7 @@
 		if (errno != EINVAL)
 			i_error("fcntl(F_SETSIG) failed: %m");
 		ioloop_dnotify_disable(ctx);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return IO_NOTIFY_NOSUPPORT;
 	}
 	if (fcntl(fd, F_NOTIFY, DN_CREATE | DN_DELETE | DN_RENAME |
@@ -135,7 +135,7 @@
 			ioloop_dnotify_disable(ctx);
 		}
 		(void)fcntl(fd, F_SETSIG, 0);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return IO_NOTIFY_NOSUPPORT;
 	}
 
--- a/src/lib/ioloop-notify-kqueue.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/ioloop-notify-kqueue.c	Thu Jun 28 00:27:13 2012 +0300
@@ -141,7 +141,7 @@
 		  NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_REVOKE, 0, io);
 	if (kevent(ctx->kq, &ev, 1, NULL, 0, NULL) < 0) {
 		i_error("kevent(%d, %s) for notify failed: %m", fd, path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		i_free(io);
 		return IO_NOTIFY_NOSUPPORT;
 	}
--- a/src/lib/iostream-rawlog.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/iostream-rawlog.c	Thu Jun 28 00:27:13 2012 +0300
@@ -101,7 +101,7 @@
 	out_fd = open(out_path, O_CREAT | O_APPEND | O_WRONLY, 0600);
 	if (out_fd == -1) {
 		i_error("creat(%s) failed: %m", out_path);
-		i_close_fd(in_fd);
+		i_close_fd(&in_fd);
 		(void)unlink(in_path);
 		return -1;
 	}
--- a/src/lib/istream-seekable.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/istream-seekable.c	Thu Jun 28 00:27:13 2012 +0300
@@ -87,7 +87,7 @@
 	if (write_full(fd, sstream->buffer->data, sstream->buffer->used) < 0) {
 		if (!ENOSPACE(errno))
 			i_error("write_full(%s) failed: %m", path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	sstream->temp_path = i_strdup(path);
@@ -209,8 +209,7 @@
 		return -1;
 	}
 	i_stream_destroy(&sstream->fd_input);
-	i_close_fd(sstream->fd);
-	sstream->fd = -1;
+	i_close_fd(&sstream->fd);
 
 	stream->max_buffer_size = (size_t)-1;
 	i_free_and_null(sstream->temp_path);
--- a/src/lib/lib.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/lib.c	Thu Jun 28 00:27:13 2012 +0300
@@ -33,6 +33,18 @@
 	hostpid_init();
 }
 
+int close_keep_errno(int *fd)
+{
+	int ret, old_errno = errno;
+
+	i_assert(*fd != -1);
+
+	ret = close(*fd);
+	*fd = -1;
+	errno = old_errno;
+	return ret;
+}
+
 void lib_deinit(void)
 {
 	ipwd_deinit();
--- a/src/lib/lib.h	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/lib.h	Thu Jun 28 00:27:13 2012 +0300
@@ -40,6 +40,7 @@
 #include "strnum.h"
 
 size_t nearest_power(size_t num) ATTR_CONST;
+int close_keep_errno(int *fd);
 
 void lib_init(void);
 void lib_deinit(void);
--- a/src/lib/macros.h	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/macros.h	Thu Jun 28 00:27:13 2012 +0300
@@ -204,11 +204,9 @@
 #endif
 
 #define i_close_fd(fd) STMT_START {  \
-	int old_errno = errno; \
-	if (unlikely(close(fd)) < 0) \
+	if (unlikely(close_keep_errno(fd)) < 0) \
 		i_error("close(%d[%s:%d]) failed: %m", \
-			fd, __FILE__, __LINE__); \
-	errno = old_errno; \
+			*(fd), __FILE__, __LINE__); \
 	} STMT_END
 
 #define i_unreached() \
--- a/src/lib/network.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/network.c	Thu Jun 28 00:27:13 2012 +0300
@@ -209,7 +209,7 @@
 		sin_set_ip(&so, my_ip);
 		if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
 			i_error("bind(%s) failed: %m", net_ip2addr(my_ip));
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			return -1;
 		}
 	}
@@ -225,7 +225,7 @@
 	if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
 #endif
 	{
-                i_close_fd(fd);
+                i_close_fd(&fd);
 		return -1;
 	}
 
@@ -263,10 +263,10 @@
 
 	sin_set_ip(&so, ip);
 	if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return 0;
 }
 
@@ -295,7 +295,7 @@
 	/* connect */
 	ret = connect(fd, &sa.sa, sizeof(sa));
 	if (ret < 0 && errno != EINPROGRESS) {
-                i_close_fd(fd);
+                i_close_fd(&fd);
 		return -1;
 	}
 
@@ -427,7 +427,7 @@
 	}
 
         /* error */
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return -1;
 }
 
@@ -467,7 +467,7 @@
 			i_error("listen() failed: %m");
 	}
 
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return -1;
 }
 
@@ -483,7 +483,7 @@
 		/* see if it really exists */
 		fd = net_connect_unix(path);
 		if (fd != -1 || errno != ECONNREFUSED) {
-			if (fd != -1) i_close_fd(fd);
+			if (fd != -1) i_close_fd(&fd);
 			errno = EADDRINUSE;
 			return -1;
 		}
--- a/src/lib/nfs-workarounds.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/nfs-workarounds.c	Thu Jun 28 00:27:13 2012 +0300
@@ -259,7 +259,7 @@
 	/* Try to flush the attribute cache the nice way first. */
 	fd = open(path, O_RDONLY);
 	if (fd != -1)
-		i_close_fd(fd);
+		i_close_fd(&fd);
 	else if (errno == ESTALE) {
 		/* this already flushed the cache */
 	} else {
@@ -326,7 +326,7 @@
 		if (t_get_current_dir(&cur_path) < 0) {
 			i_error("nfs_flush_file_handle_cache_dir: "
 				"getcwd() failed");
-			i_close_fd(cur_dir_fd);
+			i_close_fd(&cur_dir_fd);
 			return TRUE;
 		}
 		p = strrchr(cur_path, '/');
@@ -341,7 +341,7 @@
 		ret = nfs_flush_file_handle_cache_dir(path, FALSE);
 		if (fchdir(cur_dir_fd) < 0)
 			i_error("fchdir() failed: %m");
-		i_close_fd(cur_dir_fd);
+		i_close_fd(&cur_dir_fd);
 		return ret;
 	} else {
 		i_error("nfs_flush_file_handle_cache_dir: "
--- a/src/lib/randgen.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/randgen.c	Thu Jun 28 00:27:13 2012 +0300
@@ -63,8 +63,7 @@
 	if (--init_refcount > 0)
 		return;
 
-	i_close_fd(urandom_fd);
-	urandom_fd = -1;
+	i_close_fd(&urandom_fd);
 }
 
 #elif defined(HAVE_OPENSSL_RAND_H)
--- a/src/lib/safe-mkstemp.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/safe-mkstemp.c	Thu Jun 28 00:27:13 2012 +0300
@@ -63,7 +63,7 @@
 				uid == (uid_t)-1 ? -1L : (long)uid,
 				gid == (gid_t)-1 ? -1L : (long)gid);
 		}
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		(void)unlink(str_c(prefix));
 		return -1;
 	}
--- a/src/lib/test-ostream-file.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/test-ostream-file.c	Thu Jun 28 00:27:13 2012 +0300
@@ -54,7 +54,7 @@
 		test_assert(memcmp(buf, buf2, ret) == 0);
 	}
 	o_stream_unref(&output);
-	i_close_fd(fd);
+	i_close_fd(&fd);
 }
 
 void test_ostream_file(void)
--- a/src/lib/unlink-directory.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lib/unlink-directory.c	Thu Jun 28 00:27:13 2012 +0300
@@ -72,26 +72,26 @@
 		return -1;
 
 	if (fstat(dir_fd, &st2) < 0) {
-		i_close_fd(dir_fd);
+		i_close_fd(&dir_fd);
 		return -1;
 	}
 
 	if (st.st_ino != st2.st_ino ||
 	    !CMP_DEV_T(st.st_dev, st2.st_dev)) {
 		/* directory was just replaced with something else. */
-		i_close_fd(dir_fd);
+		i_close_fd(&dir_fd);
 		errno = ENOTDIR;
 		return -1;
 	}
 #endif
 	if (fchdir(dir_fd) < 0) {
-                i_close_fd(dir_fd);
+                i_close_fd(&dir_fd);
 		return -1;
 	}
 
 	dirp = opendir(".");
 	if (dirp == NULL) {
-		i_close_fd(dir_fd);
+		i_close_fd(&dir_fd);
 		return -1;
 	}
 
@@ -147,7 +147,7 @@
 	}
 	old_errno = errno;
 
-	i_close_fd(dir_fd);
+	i_close_fd(&dir_fd);
 	if (closedir(dirp) < 0)
 		return -1;
 
@@ -176,7 +176,7 @@
 		i_fatal("unlink_directory(%s): "
 			"Can't fchdir() back to our original dir: %m", dir);
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 
 	if (ret < 0) {
 		errno = old_errno;
--- a/src/lmtp/commands.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/lmtp/commands.c	Thu Jun 28 00:27:13 2012 +0300
@@ -833,7 +833,7 @@
 	if (unlink(str_c(path)) < 0) {
 		/* shouldn't happen.. */
 		i_error("unlink(%s) failed: %m", str_c(path));
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 
--- a/src/login-common/access-lookup.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/login-common/access-lookup.c	Thu Jun 28 00:27:13 2012 +0300
@@ -82,7 +82,7 @@
 			i_error("fd_send(%s) failed: %m", path);
 		else
 			i_error("fd_send(%s) didn't write enough bytes", path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return NULL;
 	}
 
--- a/src/login-common/ssl-proxy-gnutls.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/login-common/ssl-proxy-gnutls.c	Thu Jun 28 00:27:13 2012 +0300
@@ -463,7 +463,7 @@
 	read_dh_parameters(fd, fname);
 	read_rsa_parameters(fd, fname);
 
-	i_close_fd(fd);
+	i_close_fd(&fd);
 }
 
 static void gcrypt_log_handler(void *context ATTR_UNUSED, int level,
--- a/src/master/main.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/master/main.c	Thu Jun 28 00:27:13 2012 +0300
@@ -156,7 +156,7 @@
 			VA_COPY(args2, args);
 			str = t_strdup_vprintf(format, args2);
 			(void)write_full(fd, str, strlen(str));
-			i_close_fd(fd);
+			i_close_fd(&fd);
 		}
 	}
 
@@ -245,7 +245,7 @@
 		found = !(*pid_r == getpid() ||
 			  (kill(*pid_r, 0) < 0 && errno == ESRCH));
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return found;
 }
 
@@ -272,7 +272,7 @@
 		i_fatal("open(%s) failed: %m", path);
 	if (write_full(fd, pid, strlen(pid)) < 0)
 		i_fatal("write() failed in %s: %m", path);
-	i_close_fd(fd);
+	i_close_fd(&fd);
 }
 
 static void create_config_symlink(const struct master_settings *set)
--- a/src/master/service-listen.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/master/service-listen.c	Thu Jun 28 00:27:13 2012 +0300
@@ -86,7 +86,7 @@
 		fd = net_connect_unix(set->path);
 		if (fd != -1 || errno != ECONNREFUSED || i >= 3) {
 			if (fd != -1)
-				i_close_fd(fd);
+				i_close_fd(&fd);
 			service_error(service, "Socket already exists: %s",
 				      set->path);
 			return 0;
@@ -104,7 +104,7 @@
 	i_assert(fd != -1);
 
 	if (service_file_chown(l) < 0) {
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	net_set_nonblock(fd, TRUE);
--- a/src/master/service-monitor.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/master/service-monitor.c	Thu Jun 28 00:27:13 2012 +0300
@@ -371,7 +371,7 @@
 
 static int service_login_create_notify_fd(struct service *service)
 {
-	int fd;
+	int fd, ret;
 
 	if (service->login_notify_fd != -1)
 		return 0;
@@ -397,9 +397,10 @@
 		}
 	} T_END;
 
+	ret = fd == -1 ? -1 : 0;
 	if (fd != service->login_notify_fd)
-		i_close_fd(fd);
-	return fd == -1 ? -1 : 0;
+		i_close_fd(&fd);
+	return ret;
 }
 
 void services_monitor_start(struct service_list *service_list)
--- a/src/master/service.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/master/service.c	Thu Jun 28 00:27:13 2012 +0300
@@ -690,7 +690,7 @@
 			}
 			while ((fd = net_accept((*listenerp)->fd,
 						NULL, NULL)) >= 0)
-				i_close_fd(fd);
+				i_close_fd(&fd);
 			break;
 		case SERVICE_LISTENER_FIFO:
 			break;
--- a/src/plugins/acl/acl-backend-vfile-acllist.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/acl/acl-backend-vfile-acllist.c	Thu Jun 28 00:27:13 2012 +0300
@@ -109,7 +109,7 @@
 	}
 	if (fstat(fd, &st) < 0) {
 		i_error("fstat(%s) failed: %m", path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	backend->acllist_mtime = st.st_mtime;
--- a/src/plugins/acl/acl-backend-vfile.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/acl/acl-backend-vfile.c	Thu Jun 28 00:27:13 2012 +0300
@@ -482,18 +482,18 @@
 
 	if (fstat(fd, &st) < 0) {
 		if (errno == ESTALE && try_retry) {
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			return 0;
 		}
 
 		i_error("fstat(%s) failed: %m", path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	if (S_ISDIR(st.st_mode)) {
 		/* we opened a directory. */
 		*is_dir_r = TRUE;
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return 0;
 	}
 
--- a/src/plugins/fts-lucene/fts-backend-lucene.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/fts-lucene/fts-backend-lucene.c	Thu Jun 28 00:27:13 2012 +0300
@@ -273,7 +273,7 @@
 				str_tabescape(ctx->first_box_vname));
 			fd = fts_indexer_cmd(user, cmd, &path);
 			if (fd != -1)
-				i_close_fd(fd);
+				i_close_fd(&fd);
 		}
 	}
 
--- a/src/plugins/fts-squat/squat-trie.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/fts-squat/squat-trie.c	Thu Jun 28 00:27:13 2012 +0300
@@ -1635,7 +1635,7 @@
 					i_error("file_wait_lock(%s) failed: %m",
 						path);
 				}
-				i_close_fd(fd);
+				i_close_fd(&fd);
 				return -1;
 			}
 		}
--- a/src/plugins/fts/doveadm-dump-fts-expunge-log.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/fts/doveadm-dump-fts-expunge-log.c	Thu Jun 28 00:27:13 2012 +0300
@@ -84,7 +84,7 @@
 		} T_END;
 	} while (ret > 0);
 	buffer_free(&buf);
-	i_close_fd(fd);
+	i_close_fd(&fd);
 }
 
 static bool test_dump_fts_expunge_log(const char *path)
--- a/src/plugins/fts/fts-expunge-log.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/fts/fts-expunge-log.c	Thu Jun 28 00:27:13 2012 +0300
@@ -99,7 +99,7 @@
 	}
 	if (fstat(fd, &log->st) < 0) {
 		i_error("fstat(%s) failed: %m", log->path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	log->fd = fd;
--- a/src/plugins/fts/fts-indexer.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/fts/fts-indexer.c	Thu Jun 28 00:27:13 2012 +0300
@@ -51,7 +51,7 @@
 	cmd = t_strconcat(INDEXER_HANDSHAKE, cmd, NULL);
 	if (write_full(fd, cmd, strlen(cmd)) < 0) {
 		i_error("write(%s) failed: %m", path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	*path_r = path;
--- a/src/plugins/fts/fts-parser-script.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/fts/fts-parser-script.c	Thu Jun 28 00:27:13 2012 +0300
@@ -79,7 +79,7 @@
 	cmd = t_strdup_printf(SCRIPT_HANDSHAKE"\n");
 	if (write_full(fd, cmd, strlen(cmd)) < 0) {
 		i_error("write(%s) failed: %m", path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	input = i_stream_create_fd(fd, 1024, TRUE);
@@ -205,7 +205,7 @@
 	cmd = t_strdup_printf(SCRIPT_HANDSHAKE"%s\n\n", content_type);
 	if (write_full(fd, cmd, strlen(cmd)) < 0) {
 		i_error("write(%s) failed: %m", path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return NULL;
 	}
 
--- a/src/plugins/quota/quota-maildir.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/quota/quota-maildir.c	Thu Jun 28 00:27:13 2012 +0300
@@ -592,8 +592,7 @@
 
 	if (ret < 0 && size == 0) {
 		/* the read failed and there's no usable header, fail. */
-		i_close_fd(root->fd);
-		root->fd = -1;
+		i_close_fd(&root->fd);
 		return -1;
 	}
 
@@ -609,8 +608,7 @@
 		ret = 1;
 	else {
 		/* broken file / need recalculation */
-		i_close_fd(root->fd);
-		root->fd = -1;
+		i_close_fd(&root->fd);
 		ret = 0;
 	}
 	return ret;
@@ -766,7 +764,7 @@
 	struct maildir_quota_root *root = (struct maildir_quota_root *)_root;
 
 	if (root->fd != -1)
-		i_close_fd(root->fd);
+		i_close_fd(&root->fd);
 	i_free(root);
 }
 
@@ -885,14 +883,11 @@
 	} else if (root->fd == -1)
 		(void)maildirsize_recalculate(root);
 	else if (ctx->recalculate) {
-		i_close_fd(root->fd);
-		root->fd = -1;
+		i_close_fd(&root->fd);
 		(void)maildirsize_recalculate(root);
 	} else if (maildirsize_update(root, ctx->count_used, ctx->bytes_used) < 0) {
-		if (root->fd != -1) {
-			i_close_fd(root->fd);
-			root->fd = -1;
-		}
+		if (root->fd != -1)
+			i_close_fd(&root->fd);
 		maildirsize_rebuild_later(root);
 	}
 
--- a/src/plugins/trash/trash-plugin.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/trash/trash-plugin.c	Thu Jun 28 00:27:13 2012 +0300
@@ -311,7 +311,7 @@
 		}
 	}
 	i_stream_destroy(&input);
-	i_close_fd(fd);
+	i_close_fd(&fd);
 
 	array_sort(&tuser->trash_boxes, trash_mailbox_priority_cmp);
 	return ret;
--- a/src/plugins/virtual/virtual-config.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/virtual/virtual-config.c	Thu Jun 28 00:27:13 2012 +0300
@@ -427,7 +427,7 @@
 	if (ret == 0)
 		virtual_config_search_args_dup(mbox);
 	i_stream_unref(&ctx.input);
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return ret;
 }
 
--- a/src/plugins/zlib/doveadm-zlib.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/zlib/doveadm-zlib.c	Thu Jun 28 00:27:13 2012 +0300
@@ -79,7 +79,7 @@
 		match = strstr(buf, " ok begin compression.") != NULL ||
 			strstr(buf, " compress deflate") != NULL;
 	}
-	i_close_fd(fd);
+	i_close_fd(&fd);
 	return match;
 }
 
--- a/src/plugins/zlib/zlib-plugin.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/plugins/zlib/zlib-plugin.c	Thu Jun 28 00:27:13 2012 +0300
@@ -320,7 +320,7 @@
 			return 0;
 		}
 		if (fstat(fd, &st) == 0 && S_ISDIR(st.st_mode)) {
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			return 0;
 		}
 		input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE);
--- a/src/pop3/main.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/pop3/main.c	Thu Jun 28 00:27:13 2012 +0300
@@ -171,8 +171,10 @@
 				 client->auth_req.data_size);
 	if (client_create_from_input(&input, client->fd, client->fd,
 				     &input_buf, &error) < 0) {
+		int fd = client->fd;
+
 		i_error("%s", error);
-		i_close_fd(client->fd);
+		i_close_fd(&fd);
 		master_service_client_connection_destroyed(master_service);
 	}
 }
--- a/src/ssl-params/ssl-params.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/ssl-params/ssl-params.c	Thu Jun 28 00:27:13 2012 +0300
@@ -78,14 +78,14 @@
 	}
 	if (st.st_ino != st2.st_ino) {
 		/* nope. so someone else just generated the file. */
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return;
 	}
 
 	/* check that the parameters file is still the same */
 	if (stat(path, &st) == 0) {
 		if (st.st_mtime != mtime) {
-			i_close_fd(fd);
+			i_close_fd(&fd);
 			return;
 		}
 	} else if (errno != ENOENT)
@@ -165,7 +165,7 @@
 
 	if (fstat(fd, &st) < 0) {
 		i_error("fstat(%s) failed: %m", param->path);
-		i_close_fd(fd);
+		i_close_fd(&fd);
 		return -1;
 	}
 	if (st.st_size == 0 || st.st_size > MAX_PARAM_FILE_SIZE) {
--- a/src/util/gdbhelper.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/util/gdbhelper.c	Thu Jun 28 00:27:13 2012 +0300
@@ -65,7 +65,7 @@
 			}
 			if (ret < 0)
 				i_fatal("read(pipe) failed: %m");
-			i_close_fd(fd_log);
+			i_close_fd(&fd_log);
 		}
 	}
 	return 0;
--- a/src/util/rawlog.c	Wed Jun 27 12:03:51 2012 +0300
+++ b/src/util/rawlog.c	Thu Jun 28 00:27:13 2012 +0300
@@ -130,8 +130,7 @@
 			     data, size) < 0) {
 		/* failed, disable logging */
 		i_error("write(in) failed: %m");
-		i_close_fd(proxy->fd_in);
-		proxy->fd_in = -1;
+		i_close_fd(&proxy->fd_in);
 	}
 }
 
@@ -142,8 +141,7 @@
 			     data, size) < 0) {
 		/* failed, disable logging */
 		i_error("write(out) failed: %m");
-		i_close_fd(proxy->fd_out);
-		proxy->fd_out = -1;
+		i_close_fd(&proxy->fd_out);
 	}
 }
 
@@ -247,8 +245,7 @@
 		proxy->fd_out = open(fname, O_CREAT|O_EXCL|O_WRONLY, 0600);
 		if (proxy->fd_out == -1) {
 			i_error("rawlog_open: open() failed for %s: %m", fname);
-			i_close_fd(proxy->fd_in);
-			proxy->fd_in = -1;
+			i_close_fd(&proxy->fd_in);
 			return;
 		}
 	}
@@ -336,11 +333,11 @@
 			i_fatal("dup2(sfd, 0)");
 		if (dup2(sfd[1], 1) < 0)
 			i_fatal("dup2(sfd, 1)");
-		i_close_fd(sfd[0]);
-		i_close_fd(sfd[1]);
+		i_close_fd(&sfd[0]);
+		i_close_fd(&sfd[1]);
 		return;
 	}
-	i_close_fd(sfd[1]);
+	i_close_fd(&sfd[1]);
 
 	restrict_access_by_env(getenv("HOME"), TRUE);