r1486: commit the start of the generic server infastructure
[kai/samba-autobuild/.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
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         int accepted_fd;
40         struct sockaddr addr;
41         socklen_t in_addrlen = sizeof(addr);
42         pid_t pid;
43         struct fd_event fde;
44         struct timed_event idle;
45         struct server_socket *server_socket = srv_fde->private;
46         struct server_connection *conn;
47         TALLOC_CTX *mem_ctx;
48
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",
53                          strerror(errno)));
54                 return;
55         }
56
57         pid = fork();
58
59         if (pid != 0) {
60                 /* parent or error code ... */
61
62                 close(accepted_fd);
63                 /* go back to the event loop */
64                 return;
65         }
66
67         /* Child code ... */
68
69         /* close all the listening sockets */
70         event_remove_fd_all_handler(ev, standard_accept_connection);
71                         
72         /* tdb needs special fork handling */
73         if (tdb_reopen_all() == -1) {
74                 DEBUG(0,("standard_accept_connection: tdb_reopen_all failed.\n"));
75         }
76
77         mem_ctx = talloc_init("server_service_connection");
78         if (!mem_ctx) {
79                 DEBUG(0,("talloc_init(server_service_connection) failed\n"));
80                 return;
81         }
82
83         conn = talloc_p(mem_ctx, struct server_connection);
84         if (!conn) {
85                 DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
86                 talloc_destroy(mem_ctx);
87                 return;
88         }
89
90         ZERO_STRUCTP(conn);
91         conn->mem_ctx = mem_ctx;
92
93         fde.private     = conn;
94         fde.fd          = accepted_fd;
95         fde.flags       = EVENT_FD_READ;
96         fde.handler     = server_io_handler;
97
98         idle.private    = conn;
99         idle.next_event = t + 300;
100         idle.handler    = server_idle_handler;
101         
102
103         conn->event.ctx         = ev;
104         conn->event.fde         = &fde;
105         conn->event.idle        = &idle;
106         conn->event.idle_time   = 300;
107
108         conn->server_socket     = server_socket;
109         conn->service           = server_socket->service;
110
111         /* TODO: we need a generic socket subsystem */
112         conn->socket            = talloc_p(conn->mem_ctx, struct socket_context);
113         if (!conn->socket) {
114                 DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n"));
115                 talloc_destroy(mem_ctx);
116                 return;
117         }
118         conn->socket->private_data      = NULL;
119         conn->socket->ops               = NULL;
120         conn->socket->client_addr       = NULL;
121         conn->socket->pkt_count         = 0;
122         conn->socket->fde               = conn->event.fde;
123
124         /* create a smb server context and add it to out event
125            handling */
126         server_socket->service->ops->accept_connection(conn);
127
128         /* accpect_connection() of the service may changed idle.next_event */
129         conn->event.fde         = event_add_fd(ev,&fde);
130         conn->event.idle        = event_add_timed(ev,&idle);
131
132         conn->socket->fde       = conn->event.fde;
133
134         DLIST_ADD(server_socket->connection_list,conn);
135
136         /* return to the event loop */
137 }
138
139
140 /* called when a SMB connection goes down */
141 static void standard_terminate_connection(struct server_connection *conn, const char *reason) 
142 {
143         DEBUG(0,("single_terminate_connection: reason[%s]\n",reason));
144         conn->service->ops->close_connection(conn,reason);
145         /* terminate this process */
146         exit(0);
147 }
148
149 static int standard_get_id(struct smbsrv_request *req)
150 {
151         return (int)req->smb_conn->pid;
152 }
153
154 static void standard_exit_server(struct server_context *srv_ctx, const char *reason)
155 {
156         DEBUG(1,("standard_exit_server: reason[%s]\n",reason));
157 }
158
159 /*
160   initialise the standard process model, registering ourselves with the process model subsystem
161  */
162 NTSTATUS process_model_standard_init(void)
163 {
164         NTSTATUS ret;
165         struct model_ops ops;
166
167         ZERO_STRUCT(ops);
168
169         /* fill in our name */
170         ops.name = "standard";
171
172         /* fill in all the operations */
173         ops.model_startup = standard_model_startup;
174         ops.accept_connection = standard_accept_connection;
175         ops.terminate_connection = standard_terminate_connection;
176         ops.exit_server = standard_exit_server;
177         ops.get_id = standard_get_id;
178
179         /* register ourselves with the PROCESS_MODEL subsystem. */
180         ret = register_backend("process_model", &ops);
181         if (!NT_STATUS_IS_OK(ret)) {
182                 DEBUG(0,("Failed to register process_model 'standard'!\n"));
183                 return ret;
184         }
185
186         return ret;
187 }