r3466: split out request.h, signing.h, and smb_server.h
[tprouty/samba.git] / source4 / 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 #include "dlinklist.h"
25 #include "smb_server/smb_server.h"
26 #include "process_model.h"
27
28 /*
29   called when the process model is selected
30 */
31 static void standard_model_startup(void)
32 {
33         signal(SIGCHLD, SIG_IGN);
34         smbd_process_init();
35 }
36
37 /*
38   called when a listening socket becomes readable
39 */
40 static void standard_accept_connection(struct event_context *ev, struct fd_event *srv_fde,
41                                        time_t t, uint16_t flags)
42 {
43         NTSTATUS status;
44         struct socket_context *sock;
45         struct server_socket *server_socket = srv_fde->private;
46         struct server_connection *conn;
47         pid_t pid;
48
49         /* accept an incoming connection. */
50         status = socket_accept(server_socket->socket, &sock);
51         if (!NT_STATUS_IS_OK(status)) {
52                 DEBUG(0,("standard_accept_connection: accept: %s\n",
53                          nt_errstr(status)));
54                 return;
55         }
56
57         pid = fork();
58
59         if (pid != 0) {
60                 /* parent or error code ... */
61
62                 socket_destroy(sock);
63                 /* go back to the event loop */
64                 return;
65         }
66
67         /* Child code ... */
68
69         /* close all the listening sockets */
70         service_close_listening_sockets(server_socket->service->srv_ctx);
71
72         /* we don't care if the dup fails, as its only a select()
73            speed optimisation */
74         socket_dup(sock);
75                         
76         /* tdb needs special fork handling */
77         if (tdb_reopen_all() == -1) {
78                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
79         }
80
81         /* Ensure that the forked children do not expose identical random streams */
82
83         set_need_random_reseed();
84
85         conn = server_setup_connection(ev, server_socket, sock, t, getpid());
86         if (!conn) {
87                 DEBUG(0,("server_setup_connection(ev, server_socket, sock, t) failed\n"));
88                 return;
89         }
90
91         talloc_steal(conn, sock);
92
93         DLIST_ADD(server_socket->connection_list,conn);
94
95         /* return to the event loop */
96 }
97
98
99 /* called when a SMB connection goes down */
100 static void standard_terminate_connection(struct server_connection *conn, const char *reason) 
101 {
102         DEBUG(2,("standard_terminate_connection: reason[%s]\n",reason));
103
104         if (conn) {
105                 talloc_free(conn->service->srv_ctx);
106         }
107
108         /* this init_iconv() has the effect of freeing the iconv context memory,
109            which makes leak checking easier */
110         init_iconv();
111
112         /* the secrets db should really hang off the connection structure */
113         secrets_shutdown();
114
115         /* terminate this process */
116         exit(0);
117 }
118
119 static int standard_get_id(struct smbsrv_request *req)
120 {
121         return (int)req->smb_conn->pid;
122 }
123
124 static void standard_exit_server(struct server_context *srv_ctx, const char *reason)
125 {
126         DEBUG(1,("standard_exit_server: reason[%s]\n",reason));
127 }
128
129 /*
130   initialise the standard process model, registering ourselves with the process model subsystem
131  */
132 NTSTATUS process_model_standard_init(void)
133 {
134         NTSTATUS ret;
135         struct model_ops ops;
136
137         ZERO_STRUCT(ops);
138
139         /* fill in our name */
140         ops.name = "standard";
141
142         /* fill in all the operations */
143         ops.model_startup = standard_model_startup;
144         ops.accept_connection = standard_accept_connection;
145         ops.terminate_connection = standard_terminate_connection;
146         ops.exit_server = standard_exit_server;
147         ops.get_id = standard_get_id;
148
149         /* register ourselves with the PROCESS_MODEL subsystem. */
150         ret = register_backend("process_model", &ops);
151         if (!NT_STATUS_IS_OK(ret)) {
152                 DEBUG(0,("Failed to register process_model 'standard'!\n"));
153                 return ret;
154         }
155
156         return ret;
157 }