From b11e1a41d8bc30d1849e95bf47b84a081570bd07 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 15 Jul 2004 08:59:07 +0000 Subject: r1515: move dublicate code to a function metze (This used to be commit a8ec53c81ad939156654c9ad99a53aa2d679f711) --- source4/smbd/process_single.c | 61 ++-------------------------------- source4/smbd/process_standard.c | 62 ++-------------------------------- source4/smbd/process_thread.c | 61 ++-------------------------------- source4/smbd/server.h | 3 ++ source4/smbd/service.c | 73 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 175 deletions(-) (limited to 'source4/smbd') diff --git a/source4/smbd/process_single.c b/source4/smbd/process_single.c index 1cba3faee3..554f3e91da 100644 --- a/source4/smbd/process_single.c +++ b/source4/smbd/process_single.c @@ -39,11 +39,8 @@ static void single_accept_connection(struct event_context *ev, struct fd_event * int accepted_fd; struct sockaddr addr; socklen_t in_addrlen = sizeof(addr); - struct fd_event fde; - struct timed_event idle; struct server_socket *server_socket = srv_fde->private; struct server_connection *conn; - TALLOC_CTX *mem_ctx; /* accept an incoming connection. */ accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen); @@ -53,61 +50,11 @@ static void single_accept_connection(struct event_context *ev, struct fd_event * return; } - mem_ctx = talloc_init("server_service_connection"); - if (!mem_ctx) { - DEBUG(0,("talloc_init(server_service_connection) failed\n")); - return; - } - - conn = talloc_p(mem_ctx, struct server_connection); + conn = server_setup_connection(ev, server_socket, accepted_fd, t); if (!conn) { - DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n")); - talloc_destroy(mem_ctx); - return; - } - - ZERO_STRUCTP(conn); - conn->mem_ctx = mem_ctx; - - fde.private = conn; - fde.fd = accepted_fd; - fde.flags = EVENT_FD_READ; - fde.handler = server_io_handler; - - idle.private = conn; - idle.next_event = t + 300; - idle.handler = server_idle_handler; - - conn->event.ctx = ev; - conn->event.fde = &fde; - conn->event.idle = &idle; - conn->event.idle_time = 300; - - conn->server_socket = server_socket; - conn->service = server_socket->service; - - /* TODO: we need a generic socket subsystem */ - conn->socket = talloc_p(conn->mem_ctx, struct socket_context); - if (!conn->socket) { - DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n")); - talloc_destroy(mem_ctx); + DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n")); return; } - conn->socket->private_data = NULL; - conn->socket->ops = NULL; - conn->socket->client_addr = NULL; - conn->socket->pkt_count = 0; - conn->socket->fde = conn->event.fde; - - /* create a smb server context and add it to out event - handling */ - server_socket->service->ops->accept_connection(conn); - - /* accpect_connection() of the service may changed idle.next_event */ - conn->event.fde = event_add_fd(ev,&fde); - conn->event.idle = event_add_timed(ev,&idle); - - conn->socket->fde = conn->event.fde; DLIST_ADD(server_socket->connection_list,conn); @@ -122,9 +69,7 @@ static void single_terminate_connection(struct server_connection *conn, const ch { DEBUG(0,("single_terminate_connection: reason[%s]\n",reason)); conn->service->ops->close_connection(conn,reason); - close(conn->event.fde->fd); - event_remove_fd(conn->event.ctx, conn->event.fde); - event_remove_timed(conn->event.ctx, conn->event.idle); + server_destroy_connection(conn); } static int single_get_id(struct smbsrv_request *req) diff --git a/source4/smbd/process_standard.c b/source4/smbd/process_standard.c index 424d0b417a..19306656b5 100644 --- a/source4/smbd/process_standard.c +++ b/source4/smbd/process_standard.c @@ -40,11 +40,8 @@ static void standard_accept_connection(struct event_context *ev, struct fd_event struct sockaddr addr; socklen_t in_addrlen = sizeof(addr); pid_t pid; - struct fd_event fde; - struct timed_event idle; struct server_socket *server_socket = srv_fde->private; struct server_connection *conn; - TALLOC_CTX *mem_ctx; /* accept an incoming connection. */ accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen); @@ -78,62 +75,11 @@ static void standard_accept_connection(struct event_context *ev, struct fd_event set_need_random_reseed(); - mem_ctx = talloc_init("server_service_connection"); - if (!mem_ctx) { - DEBUG(0,("talloc_init(server_service_connection) failed\n")); - return; - } - - conn = talloc_p(mem_ctx, struct server_connection); + conn = server_setup_connection(ev, server_socket, accepted_fd, t); if (!conn) { - DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n")); - talloc_destroy(mem_ctx); - return; - } - - ZERO_STRUCTP(conn); - conn->mem_ctx = mem_ctx; - - fde.private = conn; - fde.fd = accepted_fd; - fde.flags = EVENT_FD_READ; - fde.handler = server_io_handler; - - idle.private = conn; - idle.next_event = t + 300; - idle.handler = server_idle_handler; - - - conn->event.ctx = ev; - conn->event.fde = &fde; - conn->event.idle = &idle; - conn->event.idle_time = 300; - - conn->server_socket = server_socket; - conn->service = server_socket->service; - - /* TODO: we need a generic socket subsystem */ - conn->socket = talloc_p(conn->mem_ctx, struct socket_context); - if (!conn->socket) { - DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n")); - talloc_destroy(mem_ctx); + DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n")); return; } - conn->socket->private_data = NULL; - conn->socket->ops = NULL; - conn->socket->client_addr = NULL; - conn->socket->pkt_count = 0; - conn->socket->fde = conn->event.fde; - - /* create a smb server context and add it to out event - handling */ - server_socket->service->ops->accept_connection(conn); - - /* accpect_connection() of the service may changed idle.next_event */ - conn->event.fde = event_add_fd(ev,&fde); - conn->event.idle = event_add_timed(ev,&idle); - - conn->socket->fde = conn->event.fde; DLIST_ADD(server_socket->connection_list,conn); @@ -146,9 +92,7 @@ static void standard_terminate_connection(struct server_connection *conn, const { DEBUG(0,("single_terminate_connection: reason[%s]\n",reason)); conn->service->ops->close_connection(conn,reason); - close(conn->event.fde->fd); - event_remove_fd(conn->event.ctx, conn->event.fde); - event_remove_timed(conn->event.ctx, conn->event.idle); + server_destroy_connection(conn); /* terminate this process */ exit(0); } diff --git a/source4/smbd/process_thread.c b/source4/smbd/process_thread.c index 2ba9905f1f..687dafea04 100644 --- a/source4/smbd/process_thread.c +++ b/source4/smbd/process_thread.c @@ -54,11 +54,8 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event * socklen_t in_addrlen = sizeof(addr); pthread_t thread_id; pthread_attr_t thread_attr; - struct fd_event fde; - struct timed_event idle; struct server_socket *server_socket = srv_fde->private; struct server_connection *conn; - TALLOC_CTX *mem_ctx; /* accept an incoming connection. */ accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen); @@ -82,61 +79,11 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event * return; } - mem_ctx = talloc_init("server_service_connection"); - if (!mem_ctx) { - DEBUG(0,("talloc_init(server_service_connection) failed\n")); - return; - } - - conn = talloc_p(mem_ctx, struct server_connection); + conn = server_setup_connection(ev, server_socket, accepted_fd, t); if (!conn) { - DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n")); - talloc_destroy(mem_ctx); - return; - } - - ZERO_STRUCTP(conn); - conn->mem_ctx = mem_ctx; - - fde.private = conn; - fde.fd = accepted_fd; - fde.flags = EVENT_FD_READ; - fde.handler = server_io_handler; - - idle.private = conn; - idle.next_event = t + 300; - idle.handler = server_idle_handler; - - conn->event.ctx = ev; - conn->event.fde = &fde; - conn->event.idle = &idle; - conn->event.idle_time = 300; - - conn->server_socket = server_socket; - conn->service = server_socket->service; - - /* TODO: we need a generic socket subsystem */ - conn->socket = talloc_p(conn->mem_ctx, struct socket_context); - if (!conn->socket) { - DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n")); - talloc_destroy(mem_ctx); + DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n")); return; } - conn->socket->private_data = NULL; - conn->socket->ops = NULL; - conn->socket->client_addr = NULL; - conn->socket->pkt_count = 0; - conn->socket->fde = conn->event.fde; - - /* create a smb server context and add it to out event - handling */ - server_socket->service->ops->accept_connection(conn); - - /* accpect_connection() of the service may changed idle.next_event */ - conn->event.fde = event_add_fd(ev,&fde); - conn->event.idle = event_add_timed(ev,&idle); - - conn->socket->fde = conn->event.fde; /* TODO: is this MUTEX_LOCK in the right place here? * --metze @@ -162,9 +109,7 @@ static void thread_terminate_connection(struct server_connection *conn, const ch { DEBUG(0,("thread_terminate_connection: reason[%s]\n",reason)); conn->service->ops->close_connection(conn,reason); - close(conn->event.fde->fd); - event_remove_fd(conn->event.ctx, conn->event.fde); - event_remove_timed(conn->event.ctx, conn->event.idle); + server_destroy_connection(conn); /* terminate this thread */ pthread_exit(NULL); /* thread cleanup routine will do actual cleanup */ } diff --git a/source4/smbd/server.h b/source4/smbd/server.h index c47cf02d8a..e8c32d01fa 100644 --- a/source4/smbd/server.h +++ b/source4/smbd/server.h @@ -37,4 +37,7 @@ struct server_context { #define SERVER_TCP_LOW_PORT 1024 #define SERVER_TCP_HIGH_PORT 1300 +/* the default idle time of a service */ +#define SERVER_DEFAULT_IDLE_TIME 300 + #endif /* _SERVER_H */ diff --git a/source4/smbd/service.c b/source4/smbd/service.c index 52ff088db3..d225a69b17 100644 --- a/source4/smbd/service.c +++ b/source4/smbd/service.c @@ -169,6 +169,72 @@ struct server_socket *service_setup_socket(struct server_service *service, return sock; } +struct server_connection *server_setup_connection(struct event_context *ev, struct server_socket *server_socket, int accepted_fd, time_t t) +{ + struct fd_event fde; + struct timed_event idle; + struct server_connection *srv_conn; + TALLOC_CTX *mem_ctx; + + mem_ctx = talloc_init("server_service_connection"); + if (!mem_ctx) { + DEBUG(0,("talloc_init(server_service_connection) failed\n")); + return NULL; + } + + srv_conn = talloc_p(mem_ctx, struct server_connection); + if (!srv_conn) { + DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n")); + talloc_destroy(mem_ctx); + return NULL; + } + + ZERO_STRUCTP(srv_conn); + srv_conn->mem_ctx = mem_ctx; + + fde.private = srv_conn; + fde.fd = accepted_fd; + fde.flags = EVENT_FD_READ; + fde.handler = server_io_handler; + + idle.private = srv_conn; + idle.next_event = t + SERVER_DEFAULT_IDLE_TIME; + idle.handler = server_idle_handler; + + srv_conn->event.ctx = ev; + srv_conn->event.fde = &fde; + srv_conn->event.idle = &idle; + srv_conn->event.idle_time = SERVER_DEFAULT_IDLE_TIME; + + srv_conn->server_socket = server_socket; + srv_conn->service = server_socket->service; + + /* TODO: we need a generic socket subsystem */ + srv_conn->socket = talloc_p(srv_conn->mem_ctx, struct socket_context); + if (!srv_conn->socket) { + DEBUG(0,("talloc_p(srv_conn->mem_ctx, struct socket_context) failed\n")); + talloc_destroy(mem_ctx); + return NULL; + } + srv_conn->socket->private_data = NULL; + srv_conn->socket->ops = NULL; + srv_conn->socket->client_addr = NULL; + srv_conn->socket->pkt_count = 0; + srv_conn->socket->fde = srv_conn->event.fde; + + /* create a smb server context and add it to out event + handling */ + server_socket->service->ops->accept_connection(srv_conn); + + /* accpect_connection() of the service may changed idle.next_event */ + srv_conn->event.fde = event_add_fd(ev,&fde); + srv_conn->event.idle = event_add_timed(ev,&idle); + + srv_conn->socket->fde = srv_conn->event.fde; + + return srv_conn; +} + /* close the socket and shutdown a server_context */ @@ -178,6 +244,13 @@ void server_terminate_connection(struct server_connection *srv_conn, const char srv_conn->service->model_ops->terminate_connection(srv_conn, reason); } +void server_destroy_connection(struct server_connection *srv_conn) +{ + close(srv_conn->event.fde->fd); + event_remove_fd(srv_conn->event.ctx, srv_conn->event.fde); + event_remove_timed(srv_conn->event.ctx, srv_conn->event.idle); +} + void server_io_handler(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags) { struct server_connection *conn = fde->private; -- cgit