r1516: remove the server_connection from the list on the server_socket
[samba.git] / source / smbd / process_single.c
1 /* 
2    Unix SMB/CIFS implementation.
3    process model: process (1 process handles all client connections)
4    Copyright (C) Andrew Tridgell 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 /*
27   called when the process model is selected
28 */
29 static void single_start_server(void)
30 {
31         smbd_process_init();
32 }
33
34 /*
35   called when a listening socket becomes readable
36 */
37 static void single_accept_connection(struct event_context *ev, struct fd_event *srv_fde, time_t t, uint16_t flags)
38 {
39         int accepted_fd;
40         struct sockaddr addr;
41         socklen_t in_addrlen = sizeof(addr);
42         struct server_socket *server_socket = srv_fde->private;
43         struct server_connection *conn;
44
45         /* accept an incoming connection. */
46         accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen);
47         if (accepted_fd == -1) {
48                 DEBUG(0,("accept_connection_single: accept: %s\n",
49                          strerror(errno)));
50                 return;
51         }
52
53         conn = server_setup_connection(ev, server_socket, accepted_fd, t);
54         if (!conn) {
55                 DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n"));
56                 return;
57         }
58
59         DLIST_ADD(server_socket->connection_list,conn);
60
61         /* return to event handling */
62         return;
63 }
64
65
66
67 /* called when a SMB connection goes down */
68 static void single_terminate_connection(struct server_connection *conn, const char *reason) 
69 {
70         DEBUG(0,("single_terminate_connection: reason[%s]\n",reason));
71
72         if (conn) {
73                 if (conn->service) {
74                         conn->service->ops->close_connection(conn,reason);
75                 }
76
77                 if (conn->server_socket) {
78                         DLIST_REMOVE(conn->server_socket->connection_list,conn);
79                 }
80
81                 server_destroy_connection(conn);
82         }
83 }
84
85 static int single_get_id(struct smbsrv_request *req)
86 {
87         return (int)req->smb_conn->pid;
88 }
89
90 static void single_exit_server(struct server_context *srv_ctx, const char *reason)
91 {
92         DEBUG(1,("single_exit_server: reason[%s]\n",reason));
93 }
94
95 /*
96   initialise the single process model, registering ourselves with the process model subsystem
97  */
98 NTSTATUS process_model_single_init(void)
99 {
100         NTSTATUS ret;
101         struct model_ops ops;
102
103         ZERO_STRUCT(ops);
104
105         /* fill in our name */
106         ops.name = "single";
107
108         /* fill in all the operations */
109         ops.model_startup               = single_start_server;
110         ops.accept_connection           = single_accept_connection;
111         ops.terminate_connection        = single_terminate_connection;
112         ops.exit_server                 = single_exit_server;
113         ops.get_id                      = single_get_id;
114
115         /* register ourselves with the PROCESS_MODEL subsystem. */
116         ret = register_backend("process_model", &ops);
117         if (!NT_STATUS_IS_OK(ret)) {
118                 DEBUG(0,("Failed to register process_model 'single'!\n"));
119                 return ret;
120         }
121
122         return ret;
123 }