svn commit: samba r2613 - in branches/SAMBA_4_0/source/ntvfs/posix: .

tridge at samba.org tridge at samba.org
Sat Sep 25 04:45:53 GMT 2004


Author: tridge
Date: 2004-09-25 04:45:52 +0000 (Sat, 25 Sep 2004)
New Revision: 2613

WebSVN: http://websvn.samba.org/websvn/changeset.php?rep=samba&path=/branches/SAMBA_4_0/source/ntvfs/posix&rev=2613&nolog=1

Log:
use a talloc destructor to ensure that file descriptors are not leaked
on abnormal termination of a connection. As long as the top level
connection structure is freed then that should cascade down to the
file structure, and call this destructor which will close the open file descriptor.

In general I'd like to use this technique in any place in Samba4 where
we hold operating system resources that we need to make sure are
released on abnormal termination.

Modified:
   branches/SAMBA_4_0/source/ntvfs/posix/pvfs_open.c


Changeset:
Modified: branches/SAMBA_4_0/source/ntvfs/posix/pvfs_open.c
===================================================================
--- branches/SAMBA_4_0/source/ntvfs/posix/pvfs_open.c	2004-09-25 03:39:35 UTC (rev 2612)
+++ branches/SAMBA_4_0/source/ntvfs/posix/pvfs_open.c	2004-09-25 04:45:52 UTC (rev 2613)
@@ -39,6 +39,21 @@
 }
 
 /*
+  by using a destructor we make sure that abnormal cleanup will not 
+  leak file descriptors (assuming at least the top level pointer is freed, which
+  will cascade down to here)
+*/
+static int pvfs_fd_destructor(void *p)
+{
+	struct pvfs_file *f = p;
+	if (f->fd != -1) {
+		close(f->fd);
+		f->fd = -1;
+	}
+	return 0;
+}
+
+/*
   open a file
   TODO: this is a temporary implementation derived from the simple backend
   its purpose is to allow other tests to run 
@@ -132,6 +147,10 @@
 	f->fd = fd;
 	f->name = talloc_steal(f, name);
 
+	/* setup a destructor to avoid file descriptor leaks on
+	   abnormal termination */
+	talloc_set_destructor(f, pvfs_fd_destructor);
+
 	DLIST_ADD(pvfs->open_files, f);
 
 	ZERO_STRUCT(io->generic.out);
@@ -157,6 +176,7 @@
 {
 	NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
 	struct pvfs_file *f;
+	NTSTATUS status;
 
 	if (io->generic.level != RAW_CLOSE_CLOSE) {
 		/* we need a mapping function */
@@ -169,12 +189,16 @@
 	}
 
 	if (close(f->fd) != 0) {
-		return pvfs_map_errno(pvfs, errno);
+		status = pvfs_map_errno(pvfs, errno);
+	} else {
+		status = NT_STATUS_OK;
 	}
 
+	talloc_set_destructor(f, NULL);
+
 	DLIST_REMOVE(pvfs->open_files, f);
 	talloc_free(f);
 
-	return NT_STATUS_OK;
+	return status;
 }
 



More information about the samba-cvs mailing list