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
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.
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.
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.
26 called when the process model is selected
28 static void standard_model_startup(void)
30 signal(SIGCHLD, SIG_IGN);
35 called when a listening socket becomes readable
37 static void standard_accept_connection(struct event_context *ev, struct fd_event *srv_fde, time_t t, uint16_t flags)
41 socklen_t in_addrlen = sizeof(addr);
44 struct timed_event idle;
45 struct server_socket *server_socket = srv_fde->private;
46 struct server_connection *conn;
49 /* accept an incoming connection. */
50 accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen);
51 if (accepted_fd == -1) {
52 DEBUG(0,("standard_accept_connection: accept: %s\n",
60 /* parent or error code ... */
63 /* go back to the event loop */
69 /* close all the listening sockets */
70 event_remove_fd_all_handler(ev, standard_accept_connection);
72 /* tdb needs special fork handling */
73 if (tdb_reopen_all() == -1) {
74 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
77 /* Ensure that the forked children do not expose identical random streams */
79 set_need_random_reseed();
81 mem_ctx = talloc_init("server_service_connection");
83 DEBUG(0,("talloc_init(server_service_connection) failed\n"));
87 conn = talloc_p(mem_ctx, struct server_connection);
89 DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
90 talloc_destroy(mem_ctx);
95 conn->mem_ctx = mem_ctx;
99 fde.flags = EVENT_FD_READ;
100 fde.handler = server_io_handler;
103 idle.next_event = t + 300;
104 idle.handler = server_idle_handler;
107 conn->event.ctx = ev;
108 conn->event.fde = &fde;
109 conn->event.idle = &idle;
110 conn->event.idle_time = 300;
112 conn->server_socket = server_socket;
113 conn->service = server_socket->service;
115 /* TODO: we need a generic socket subsystem */
116 conn->socket = talloc_p(conn->mem_ctx, struct socket_context);
118 DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n"));
119 talloc_destroy(mem_ctx);
122 conn->socket->private_data = NULL;
123 conn->socket->ops = NULL;
124 conn->socket->client_addr = NULL;
125 conn->socket->pkt_count = 0;
126 conn->socket->fde = conn->event.fde;
128 /* create a smb server context and add it to out event
130 server_socket->service->ops->accept_connection(conn);
132 /* accpect_connection() of the service may changed idle.next_event */
133 conn->event.fde = event_add_fd(ev,&fde);
134 conn->event.idle = event_add_timed(ev,&idle);
136 conn->socket->fde = conn->event.fde;
138 DLIST_ADD(server_socket->connection_list,conn);
140 /* return to the event loop */
144 /* called when a SMB connection goes down */
145 static void standard_terminate_connection(struct server_connection *conn, const char *reason)
147 DEBUG(0,("single_terminate_connection: reason[%s]\n",reason));
148 conn->service->ops->close_connection(conn,reason);
149 /* terminate this process */
153 static int standard_get_id(struct smbsrv_request *req)
155 return (int)req->smb_conn->pid;
158 static void standard_exit_server(struct server_context *srv_ctx, const char *reason)
160 DEBUG(1,("standard_exit_server: reason[%s]\n",reason));
164 initialise the standard process model, registering ourselves with the process model subsystem
166 NTSTATUS process_model_standard_init(void)
169 struct model_ops ops;
173 /* fill in our name */
174 ops.name = "standard";
176 /* fill in all the operations */
177 ops.model_startup = standard_model_startup;
178 ops.accept_connection = standard_accept_connection;
179 ops.terminate_connection = standard_terminate_connection;
180 ops.exit_server = standard_exit_server;
181 ops.get_id = standard_get_id;
183 /* register ourselves with the PROCESS_MODEL subsystem. */
184 ret = register_backend("process_model", &ops);
185 if (!NT_STATUS_IS_OK(ret)) {
186 DEBUG(0,("Failed to register process_model 'standard'!\n"));