r2646: - use a talloc destructor to ensure that sockets from the new socket
[kai/samba.git] / source / smbd / process_standard.c
1 /* 
2    Unix SMB/CIFS implementation.
3    process model: standard (1 process per client connection)
4    Copyright (C) Andrew Tridgell 1992-2003
5    Copyright (C) James J Myers 2003 <myersjj@samba.org>
6    Copyright (C) Stefan (metze) Metzmacher 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26   called when the process model is selected
27 */
28 static void standard_model_startup(void)
29 {
30         signal(SIGCHLD, SIG_IGN);
31         smbd_process_init();
32 }
33
34 /*
35   called when a listening socket becomes readable
36 */
37 static void standard_accept_connection(struct event_context *ev, struct fd_event *srv_fde, time_t t, uint16_t flags)
38 {
39         NTSTATUS status;
40         struct socket_context *sock;
41         struct server_socket *server_socket = srv_fde->private;
42         struct server_connection *conn;
43         pid_t pid;
44
45         /* accept an incoming connection. */
46         status = socket_accept(server_socket->socket, &sock, 0);
47         if (!NT_STATUS_IS_OK(status)) {
48                 DEBUG(0,("accept_connection_single: accept: %s\n",
49                          nt_errstr(status)));
50                 return;
51         }
52
53         pid = fork();
54
55         if (pid != 0) {
56                 /* parent or error code ... */
57
58                 socket_destroy(sock);
59                 /* go back to the event loop */
60                 return;
61         }
62
63         /* Child code ... */
64
65         /* close all the listening sockets */
66         event_remove_fd_all_handler(ev, standard_accept_connection);
67                         
68         /* tdb needs special fork handling */
69         if (tdb_reopen_all() == -1) {
70                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
71         }
72
73         /* Ensure that the forked children do not expose identical random streams */
74
75         set_need_random_reseed();
76
77         conn = server_setup_connection(ev, server_socket, sock, t);
78         if (!conn) {
79                 DEBUG(0,("server_setup_connection(ev, server_socket, sock, t) failed\n"));
80                 return;
81         }
82
83         talloc_steal(conn, sock);
84
85         DLIST_ADD(server_socket->connection_list,conn);
86
87         /* return to the event loop */
88 }
89
90
91 /* called when a SMB connection goes down */
92 static void standard_terminate_connection(struct server_connection *conn, const char *reason) 
93 {
94         DEBUG(2,("single_terminate_connection: reason[%s]\n",reason));
95
96         if (conn) {
97                 if (conn->service) {
98                         conn->service->ops->close_connection(conn,reason);
99                 }
100
101                 if (conn->server_socket) {
102                         DLIST_REMOVE(conn->server_socket->connection_list,conn);
103                 }
104
105                 server_destroy_connection(conn);
106         }
107
108         /* terminate this process */
109         exit(0);
110 }
111
112 static int standard_get_id(struct smbsrv_request *req)
113 {
114         return (int)req->smb_conn->pid;
115 }
116
117 static void standard_exit_server(struct server_context *srv_ctx, const char *reason)
118 {
119         DEBUG(1,("standard_exit_server: reason[%s]\n",reason));
120 }
121
122 /*
123   initialise the standard process model, registering ourselves with the process model subsystem
124  */
125 NTSTATUS process_model_standard_init(void)
126 {
127         NTSTATUS ret;
128         struct model_ops ops;
129
130         ZERO_STRUCT(ops);
131
132         /* fill in our name */
133         ops.name = "standard";
134
135         /* fill in all the operations */
136         ops.model_startup = standard_model_startup;
137         ops.accept_connection = standard_accept_connection;
138         ops.terminate_connection = standard_terminate_connection;
139         ops.exit_server = standard_exit_server;
140         ops.get_id = standard_get_id;
141
142         /* register ourselves with the PROCESS_MODEL subsystem. */
143         ret = register_backend("process_model", &ops);
144         if (!NT_STATUS_IS_OK(ret)) {
145                 DEBUG(0,("Failed to register process_model 'standard'!\n"));
146                 return ret;
147         }
148
149         return ret;
150 }