r3314: added a option "socket:testnonblock" to the generic socket code. If
[bbaumbach/samba-autobuild/.git] / source4 / smbd / process_thread.c
index 2ba9905f1f69e5b3c6404ac673fc2fd69c09612b..2b8746efb2ecef32bdfcce51e038d1b67675d04c 100644 (file)
@@ -49,22 +49,19 @@ static int thread_get_id(struct smbsrv_request *req)
 static void thread_accept_connection(struct event_context *ev, struct fd_event *srv_fde, 
                              time_t t, uint16_t flags)
 {              
-       int accepted_fd, rc;
-       struct sockaddr addr;
-       socklen_t in_addrlen = sizeof(addr);
+       NTSTATUS status;
+       struct socket_context *sock;
+       int rc;
        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);
-       if (accepted_fd == -1) {
-               DEBUG(0,("standard_accept_connection: accept: %s\n",
-                        strerror(errno)));
+       status = socket_accept(server_socket->socket, &sock);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("accept_connection_single: accept: %s\n",
+                        nt_errstr(status)));
                return;
        }
        
@@ -75,68 +72,23 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event *
           main event_context is continued.
        */
 
-
-       ev = event_context_init();
+       ev = event_context_init(server_socket);
        if (!ev) {
                DEBUG(0,("thread_accept_connection: failed to create event_context!\n"));
+               socket_destroy(sock);
                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, sock, t, pthread_self());
        if (!conn) {
-               DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
-               talloc_destroy(mem_ctx);
+               DEBUG(0,("server_setup_connection(ev, server_socket, sock, t) failed\n"));
+               event_context_destroy(ev);
+               socket_destroy(sock);
                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);
-               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;
+       talloc_steal(conn, ev);
+       talloc_steal(conn, sock);
 
        /* TODO: is this MUTEX_LOCK in the right place here?
         *       --metze
@@ -151,9 +103,12 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event *
        pthread_attr_destroy(&thread_attr);
        if (rc == 0) {
                DEBUG(4,("accept_connection_thread: created thread_id=%lu for fd=%d\n", 
-                       (unsigned long int)thread_id, accepted_fd));
+                       (unsigned long int)thread_id, socket_get_fd(sock)));
        } else {
-               DEBUG(0,("accept_connection_thread: thread create failed for fd=%d, rc=%d\n", accepted_fd, rc));
+               DEBUG(0,("accept_connection_thread: thread create failed for fd=%d, rc=%d\n", socket_get_fd(sock), rc));
+               event_context_destroy(ev);
+               socket_destroy(sock);
+               return;
        }
 }
 
@@ -161,10 +116,11 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event *
 static void thread_terminate_connection(struct server_connection *conn, const char *reason) 
 {
        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);
+
+       if (conn) {
+               talloc_free(conn);
+       }
+
        /* terminate this thread */
        pthread_exit(NULL);  /* thread cleanup routine will do actual cleanup */
 }