summaryrefslogtreecommitdiff
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
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)
-rw-r--r--source4/ldap_server/ldap_server.c2
-rw-r--r--source4/lib/socket/socket.c32
-rw-r--r--source4/lib/time.c2
-rw-r--r--source4/libcli/auth/gensec.c3
-rw-r--r--source4/rpc_server/dcerpc_server.c4
-rw-r--r--source4/rpc_server/dcerpc_tcp.c2
-rw-r--r--source4/smb_server/smb_server.c2
-rw-r--r--source4/smbd/process_single.c2
-rw-r--r--source4/smbd/process_standard.c2
-rw-r--r--source4/smbd/process_thread.c2
-rw-r--r--source4/smbd/server.h1
-rw-r--r--source4/smbd/service.c24
-rw-r--r--source4/smbd/service.h3
13 files changed, 43 insertions, 38 deletions
diff --git a/source4/ldap_server/ldap_server.c b/source4/ldap_server/ldap_server.c
index 976b55ee01..45b96ba8e9 100644
--- a/source4/ldap_server/ldap_server.c
+++ b/source4/ldap_server/ldap_server.c
@@ -38,7 +38,7 @@ static void add_socket(struct server_service *service,
{
struct server_socket *srv_sock;
uint16_t port = 389;
- char *ip_str = talloc_strdup(service->mem_ctx, inet_ntoa(*ifip));
+ char *ip_str = talloc_strdup(service, inet_ntoa(*ifip));
srv_sock = service_setup_socket(service, model_ops, ip_str, &port);
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,
diff --git a/source4/lib/time.c b/source4/lib/time.c
index bd8d7ddb30..fec4dd62a2 100644
--- a/source4/lib/time.c
+++ b/source4/lib/time.c
@@ -137,7 +137,7 @@ void unix_to_nt_time(NTTIME *nt, time_t t)
/****************************************************************************
check if it's a null mtime
****************************************************************************/
-BOOL null_mtime(time_t mtime)
+static BOOL null_mtime(time_t mtime)
{
return mtime == 0 ||
mtime == (time_t)0xFFFFFFFF ||
diff --git a/source4/libcli/auth/gensec.c b/source4/libcli/auth/gensec.c
index 3d8246cd97..b6c4f91610 100644
--- a/source4/libcli/auth/gensec.c
+++ b/source4/libcli/auth/gensec.c
@@ -124,11 +124,10 @@ static NTSTATUS gensec_start(TALLOC_CTX *mem_ctx, struct gensec_security **gense
return NT_STATUS_INTERNAL_ERROR;
}
- (*gensec_security) = talloc_p(NULL, struct gensec_security);
+ (*gensec_security) = talloc_p(mem_ctx, struct gensec_security);
if (!(*gensec_security)) {
return NT_STATUS_NO_MEMORY;
}
- talloc_set_name(*gensec_security, "gensec_start");
(*gensec_security)->ops = NULL;
diff --git a/source4/rpc_server/dcerpc_server.c b/source4/rpc_server/dcerpc_server.c
index 22e677acd8..17e629d81e 100644
--- a/source4/rpc_server/dcerpc_server.c
+++ b/source4/rpc_server/dcerpc_server.c
@@ -988,7 +988,6 @@ NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, struct dcesrv_context **dce_ct
if (! *dce_ctx) {
return NT_STATUS_NO_MEMORY;
}
- talloc_set_name(*dce_ctx, "struct dcesrv_context");
(*dce_ctx)->endpoint_list = NULL;
@@ -1030,12 +1029,11 @@ static void dcesrv_init(struct server_service *service, const struct model_ops *
return;
}
- dce_ctx = talloc_p(NULL, struct dcesrv_context);
+ dce_ctx = talloc_p(service, struct dcesrv_context);
if (!dce_ctx) {
DEBUG(0,("talloc_p(mem_ctx, struct dcesrv_context) failed\n"));
return;
}
- talloc_set_name(dce_ctx, "dcesrv_init");
ZERO_STRUCTP(dce_ctx);
dce_ctx->endpoint_list = NULL;
diff --git a/source4/rpc_server/dcerpc_tcp.c b/source4/rpc_server/dcerpc_tcp.c
index a77d02e796..9a22c5eb5e 100644
--- a/source4/rpc_server/dcerpc_tcp.c
+++ b/source4/rpc_server/dcerpc_tcp.c
@@ -59,7 +59,7 @@ static void add_socket_rpc(struct server_service *service,
struct in_addr *ifip)
{
struct dcesrv_endpoint *e;
- char *ip_str = talloc_strdup(service->mem_ctx, inet_ntoa(*ifip));
+ char *ip_str = talloc_strdup(service, inet_ntoa(*ifip));
for (e=dce_ctx->endpoint_list;e;e=e->next) {
if (e->ep_description.type == ENDPOINT_TCP) {
diff --git a/source4/smb_server/smb_server.c b/source4/smb_server/smb_server.c
index 8eb799cdb1..c3c5a6c456 100644
--- a/source4/smb_server/smb_server.c
+++ b/source4/smb_server/smb_server.c
@@ -688,7 +688,7 @@ static void add_socket(struct server_service *service,
{
const char **ports = lp_smb_ports();
int i;
- char *ip_str = talloc_strdup(service->mem_ctx, inet_ntoa(*ifip));
+ char *ip_str = talloc_strdup(service, inet_ntoa(*ifip));
for (i=0;ports[i];i++) {
uint16_t port = atoi(ports[i]);
diff --git a/source4/smbd/process_single.c b/source4/smbd/process_single.c
index 4c0dcd4e35..62d780277c 100644
--- a/source4/smbd/process_single.c
+++ b/source4/smbd/process_single.c
@@ -55,6 +55,8 @@ static void single_accept_connection(struct event_context *ev, struct fd_event *
return;
}
+ talloc_steal(conn, sock);
+
DLIST_ADD(server_socket->connection_list,conn);
/* return to event handling */
diff --git a/source4/smbd/process_standard.c b/source4/smbd/process_standard.c
index 22e47d1b1f..1bb30c2ef0 100644
--- a/source4/smbd/process_standard.c
+++ b/source4/smbd/process_standard.c
@@ -80,6 +80,8 @@ static void standard_accept_connection(struct event_context *ev, struct fd_event
return;
}
+ talloc_steal(conn, sock);
+
DLIST_ADD(server_socket->connection_list,conn);
/* return to the event loop */
diff --git a/source4/smbd/process_thread.c b/source4/smbd/process_thread.c
index ef4d53e188..4e11137f37 100644
--- a/source4/smbd/process_thread.c
+++ b/source4/smbd/process_thread.c
@@ -87,6 +87,8 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event *
return;
}
+ talloc_steal(conn, sock);
+
/* TODO: is this MUTEX_LOCK in the right place here?
* --metze
*/
diff --git a/source4/smbd/server.h b/source4/smbd/server.h
index e8c32d01fa..e76adec44d 100644
--- a/source4/smbd/server.h
+++ b/source4/smbd/server.h
@@ -25,7 +25,6 @@ struct server_service;
struct event_context;
struct server_context {
- TALLOC_CTX *mem_ctx;
struct server_service *service_list;
struct event_context *events;
};
diff --git a/source4/smbd/service.c b/source4/smbd/service.c
index 0733e6fc0f..e3eb4a02c1 100644
--- a/source4/smbd/service.c
+++ b/source4/smbd/service.c
@@ -27,7 +27,6 @@ struct server_context *server_service_startup(const char *model)
{
int i;
const char **server_services = lp_server_services();
- TALLOC_CTX *mem_ctx;
struct server_context *srv_ctx;
const struct model_ops *model_ops;
@@ -42,20 +41,12 @@ struct server_context *server_service_startup(const char *model)
return NULL;
}
- mem_ctx = talloc_init("server_context");
- if (!mem_ctx) {
- DEBUG(0,("talloc_init(server_context) failed\n"));
- return NULL;
- }
-
- srv_ctx = talloc_p(mem_ctx, struct server_context);
+ srv_ctx = talloc_p(NULL, struct server_context);
if (!srv_ctx) {
- DEBUG(0,("talloc_p(mem_ctx, struct server_context) failed\n"));
return NULL;
}
ZERO_STRUCTP(srv_ctx);
- srv_ctx->mem_ctx = mem_ctx;
srv_ctx->events = event_context_init();
if (!srv_ctx->events) {
@@ -65,7 +56,6 @@ struct server_context *server_service_startup(const char *model)
for (i=0;server_services[i];i++) {
- TALLOC_CTX *mem_ctx2;
const struct server_service_ops *service_ops;
struct server_service *service;
@@ -75,16 +65,12 @@ struct server_context *server_service_startup(const char *model)
return NULL;
}
- mem_ctx2 = talloc_init("server_service");
-
- service = talloc_p(mem_ctx2, struct server_service);
+ service = talloc_p(srv_ctx, struct server_service);
if (!service) {
- DEBUG(0,("talloc_p(mem_ctx, struct server_service) failed\n"));
return NULL;
}
ZERO_STRUCTP(service);
- service->mem_ctx = mem_ctx2;
service->ops = service_ops;
service->model_ops = model_ops;
service->srv_ctx = srv_ctx;
@@ -129,6 +115,8 @@ struct server_socket *service_setup_socket(struct server_service *service,
return NULL;
}
+ talloc_steal(service, socket_ctx);
+
/* ready to listen */
status = socket_set_option(socket_ctx, "SO_KEEPALIVE SO_REUSEADDR=1", NULL);
if (!NT_STATUS_IS_OK(status)) {
@@ -169,7 +157,6 @@ struct server_socket *service_setup_socket(struct server_service *service,
fde.handler = model_ops->accept_connection;
ZERO_STRUCTP(srv_sock);
- srv_sock->mem_ctx = srv_sock;
srv_sock->service = service;
srv_sock->socket = socket_ctx;
srv_sock->event.ctx = service->srv_ctx->events;
@@ -201,7 +188,6 @@ struct server_connection *server_setup_connection(struct event_context *ev,
}
ZERO_STRUCTP(srv_conn);
- srv_conn->mem_ctx = srv_conn;
fde.private = srv_conn;
fde.fd = socket_get_fd(sock);
@@ -255,7 +241,7 @@ void server_destroy_connection(struct server_connection *srv_conn)
event_remove_timed(srv_conn->event.ctx, srv_conn->event.idle);
srv_conn->event.idle = NULL;
- talloc_destroy(srv_conn->mem_ctx);
+ talloc_free(srv_conn);
}
void server_io_handler(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags)
diff --git a/source4/smbd/service.h b/source4/smbd/service.h
index 5d72d388fd..88618964ce 100644
--- a/source4/smbd/service.h
+++ b/source4/smbd/service.h
@@ -67,7 +67,6 @@ struct socket_context;
struct server_socket {
struct server_socket *next,*prev;
- TALLOC_CTX *mem_ctx;
void *private_data;
struct {
@@ -84,7 +83,6 @@ struct server_socket {
struct server_service {
struct server_service *next,*prev;
- TALLOC_CTX *mem_ctx;
void *private_data;
const struct server_service_ops *ops;
@@ -97,7 +95,6 @@ struct server_service {
struct server_connection {
struct server_connection *next,*prev;
- TALLOC_CTX *mem_ctx;
void *private_data;
struct {