summaryrefslogtreecommitdiff
path: root/source4/lib/socket
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2004-09-26 03:05:04 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 12:59:16 -0500
commit764eddb69647681f784f343a122251ca1ecf62df (patch)
tree1d64246d07b4f040c4e36367e4172135c8af5011 /source4/lib/socket
parent4b1050a6cf3e6d9f7a8e75dd90ed1ccd52f29abb (diff)
downloadsamba-764eddb69647681f784f343a122251ca1ecf62df.tar.gz
samba-764eddb69647681f784f343a122251ca1ecf62df.tar.bz2
samba-764eddb69647681f784f343a122251ca1ecf62df.zip
r2646: - use a talloc destructor to ensure that sockets from the new socket
library are closed on abnormal termination - convert the service.h structures to the new talloc methods (This used to be commit 2dc334a3284858eb1c7190f9687c9b6c879ecc9d)
Diffstat (limited to 'source4/lib/socket')
-rw-r--r--source4/lib/socket/socket.c32
1 files changed, 26 insertions, 6 deletions
diff --git a/source4/lib/socket/socket.c b/source4/lib/socket/socket.c
index 4814cc6a5a..4fde41a3c0 100644
--- a/source4/lib/socket/socket.c
+++ b/source4/lib/socket/socket.c
@@ -20,6 +20,18 @@
#include "includes.h"
+/*
+ auto-close sockets on free
+*/
+static int socket_destructor(void *ptr)
+{
+ struct socket_context *sock = ptr;
+ if (sock->ops->close) {
+ sock->ops->close(sock);
+ }
+ return 0;
+}
+
NTSTATUS socket_create(const char *name, enum socket_type type, struct socket_context **new_sock, uint32_t flags)
{
NTSTATUS status;
@@ -38,24 +50,24 @@ NTSTATUS socket_create(const char *name, enum socket_type type, struct socket_co
(*new_sock)->private_data = NULL;
(*new_sock)->ops = socket_getops_byname(name, type);
if (!(*new_sock)->ops) {
- talloc_free((*new_sock));
+ talloc_free(*new_sock);
return NT_STATUS_INVALID_PARAMETER;
}
status = (*new_sock)->ops->init((*new_sock));
if (!NT_STATUS_IS_OK(status)) {
- talloc_free((*new_sock));
+ talloc_free(*new_sock);
return status;
}
+ talloc_set_destructor(*new_sock, socket_destructor);
+
return NT_STATUS_OK;
}
void socket_destroy(struct socket_context *sock)
{
- if (sock->ops->close) {
- sock->ops->close(sock);
- }
+ /* the close is handled by the destructor */
talloc_free(sock);
}
@@ -98,6 +110,8 @@ NTSTATUS socket_listen(struct socket_context *sock, const char *my_address, int
NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_sock, uint32_t flags)
{
+ NTSTATUS status;
+
if (sock->type != SOCKET_TYPE_STREAM) {
return NT_STATUS_INVALID_PARAMETER;
}
@@ -110,7 +124,13 @@ NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_
return NT_STATUS_NOT_IMPLEMENTED;
}
- return sock->ops->accept(sock, new_sock, flags);
+ status = sock->ops->accept(sock, new_sock, flags);
+
+ if (NT_STATUS_IS_OK(status)) {
+ talloc_set_destructor(*new_sock, socket_destructor);
+ }
+
+ return status;
}
NTSTATUS socket_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx,