r3314: added a option "socket:testnonblock" to the generic socket code. If
[bbaumbach/samba-autobuild/.git] / source4 / smbd / process_standard.c
index 84166196d5d4b99233b980392d4a73f511a06fa7..106be7792549496ecfa2c4e523cc8e0913ab53c7 100644 (file)
@@ -3,6 +3,7 @@
    process model: standard (1 process per client connection)
    Copyright (C) Andrew Tridgell 1992-2003
    Copyright (C) James J Myers 2003 <myersjj@samba.org>
+   Copyright (C) Stefan (metze) Metzmacher 2004
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 /*
   called when the process model is selected
 */
-static void model_startup(void)
+static void standard_model_startup(void)
 {
        signal(SIGCHLD, SIG_IGN);
+       smbd_process_init();
 }
 
 /*
   called when a listening socket becomes readable
 */
-static void accept_connection(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags)
+static void standard_accept_connection(struct event_context *ev, struct fd_event *srv_fde, time_t t, uint16_t flags)
 {
-       int accepted_fd;
-       struct sockaddr addr;
-       socklen_t in_addrlen = sizeof(addr);
+       NTSTATUS status;
+       struct socket_context *sock;
+       struct server_socket *server_socket = srv_fde->private;
+       struct server_connection *conn;
        pid_t pid;
-       struct model_ops *model_ops = fde->private;
 
-       accepted_fd = accept(fde->fd,&addr,&in_addrlen);
-       if (accepted_fd == -1) {
-               DEBUG(0,("accept_connection_standard: accept: %s\n",
-                        strerror(errno)));
+       /* accept an incoming connection. */
+       status = socket_accept(server_socket->socket, &sock);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("standard_accept_connection: accept: %s\n",
+                        nt_errstr(status)));
                return;
        }
 
@@ -52,7 +55,7 @@ static void accept_connection(struct event_context *ev, struct fd_event *fde, ti
        if (pid != 0) {
                /* parent or error code ... */
 
-               close(accepted_fd);
+               socket_destroy(sock);
                /* go back to the event loop */
                return;
        }
@@ -60,82 +63,57 @@ static void accept_connection(struct event_context *ev, struct fd_event *fde, ti
        /* Child code ... */
 
        /* close all the listening sockets */
-       event_remove_fd_all_handler(ev, model_ops->accept_connection);
-       event_remove_fd_all_handler(ev, model_ops->accept_rpc_connection);
+       event_remove_fd_all_handler(ev, standard_accept_connection);
                        
        /* tdb needs special fork handling */
        if (tdb_reopen_all() == -1) {
-               DEBUG(0,("accept_connection_standard: tdb_reopen_all failed.\n"));
+               DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
        }
 
-       /* Load DSO's */
-       init_modules();
-               
-       /* initialize new process */
-       smbd_process_init();
-               
-       init_smbsession(ev, model_ops, accepted_fd, smbd_read_handler);
+       /* Ensure that the forked children do not expose identical random streams */
 
-       /* return to the event loop */
-}
+       set_need_random_reseed();
 
-/*
-  called when a rpc listening socket becomes readable
-*/
-static void accept_rpc_connection(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags)
-{
-       int accepted_fd;
-       struct sockaddr addr;
-       socklen_t in_addrlen = sizeof(addr);
-       pid_t pid;
-
-       accepted_fd = accept(fde->fd,&addr,&in_addrlen);
-       if (accepted_fd == -1) {
-               DEBUG(0,("accept_connection_standard: accept: %s\n",
-                        strerror(errno)));
+       conn = server_setup_connection(ev, server_socket, sock, t, getpid());
+       if (!conn) {
+               DEBUG(0,("server_setup_connection(ev, server_socket, sock, t) failed\n"));
                return;
        }
 
-       pid = fork();
+       talloc_steal(conn, sock);
 
-       if (pid != 0) {
-               /* parent or error code ... */
-               close(accepted_fd);
-               /* go back to the event loop */
-               return;
-       }
+       DLIST_ADD(server_socket->connection_list,conn);
 
-       /* Child code ... */
-
-       /* close all the listening sockets */
-       event_remove_fd_all_handler(ev, accept_connection);
-       event_remove_fd_all_handler(ev, accept_rpc_connection);
-                       
-       init_rpc_session(ev, fde->private, accepted_fd); 
+       /* return to the event loop */
 }
 
+
 /* called when a SMB connection goes down */
-static void terminate_connection(struct smbsrv_context *server, const char *reason) 
+static void standard_terminate_connection(struct server_connection *conn, const char *reason) 
 {
-       server_terminate(server);
-       /* terminate this process */
-       exit(0);
-}
+       DEBUG(2,("standard_terminate_connection: reason[%s]\n",reason));
+
+       if (conn) {
+               talloc_free(conn->service->srv_ctx);
+       }
+
+       /* this init_iconv() has the effect of freeing the iconv context memory,
+          which makes leak checking easier */
+       init_iconv();
+
+       /* the secrets db should really hang off the connection structure */
+       secrets_shutdown();
 
-/* called when a rpc connection goes down */
-static void terminate_rpc_connection(void *r, const char *reason) 
-{
-       rpc_server_terminate(r);
        /* terminate this process */
        exit(0);
 }
 
-static int get_id(struct smbsrv_request *req)
+static int standard_get_id(struct smbsrv_request *req)
 {
-       return (int)req->smb_ctx->pid;
+       return (int)req->smb_conn->pid;
 }
 
-static void standard_exit_server(struct smbsrv_context *smb, const char *reason)
+static void standard_exit_server(struct server_context *srv_ctx, const char *reason)
 {
        DEBUG(1,("standard_exit_server: reason[%s]\n",reason));
 }
@@ -154,13 +132,11 @@ NTSTATUS process_model_standard_init(void)
        ops.name = "standard";
 
        /* fill in all the operations */
-       ops.model_startup = model_startup;
-       ops.accept_connection = accept_connection;
-       ops.accept_rpc_connection = accept_rpc_connection;
-       ops.terminate_connection = terminate_connection;
-       ops.terminate_rpc_connection = terminate_rpc_connection;
+       ops.model_startup = standard_model_startup;
+       ops.accept_connection = standard_accept_connection;
+       ops.terminate_connection = standard_terminate_connection;
        ops.exit_server = standard_exit_server;
-       ops.get_id = get_id;
+       ops.get_id = standard_get_id;
 
        /* register ourselves with the PROCESS_MODEL subsystem. */
        ret = register_backend("process_model", &ops);