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