r1498: (merge from 3.0)
[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
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         /* Ensure that the forked children do not expose identical random streams */
78
79         set_need_random_reseed();
80
81         mem_ctx = talloc_init("server_service_connection");
82         if (!mem_ctx) {
83                 DEBUG(0,("talloc_init(server_service_connection) failed\n"));
84                 return;
85         }
86
87         conn = talloc_p(mem_ctx, struct server_connection);
88         if (!conn) {
89                 DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
90                 talloc_destroy(mem_ctx);
91                 return;
92         }
93
94         ZERO_STRUCTP(conn);
95         conn->mem_ctx = mem_ctx;
96
97         fde.private     = conn;
98         fde.fd          = accepted_fd;
99         fde.flags       = EVENT_FD_READ;
100         fde.handler     = server_io_handler;
101
102         idle.private    = conn;
103         idle.next_event = t + 300;
104         idle.handler    = server_idle_handler;
105         
106
107         conn->event.ctx         = ev;
108         conn->event.fde         = &fde;
109         conn->event.idle        = &idle;
110         conn->event.idle_time   = 300;
111
112         conn->server_socket     = server_socket;
113         conn->service           = server_socket->service;
114
115         /* TODO: we need a generic socket subsystem */
116         conn->socket            = talloc_p(conn->mem_ctx, struct socket_context);
117         if (!conn->socket) {
118                 DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n"));
119                 talloc_destroy(mem_ctx);
120                 return;
121         }
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;
127
128         /* create a smb server context and add it to out event
129            handling */
130         server_socket->service->ops->accept_connection(conn);
131
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);
135
136         conn->socket->fde       = conn->event.fde;
137
138         DLIST_ADD(server_socket->connection_list,conn);
139
140         /* return to the event loop */
141 }
142
143
144 /* called when a SMB connection goes down */
145 static void standard_terminate_connection(struct server_connection *conn, const char *reason) 
146 {
147         DEBUG(0,("single_terminate_connection: reason[%s]\n",reason));
148         conn->service->ops->close_connection(conn,reason);
149         /* terminate this process */
150         exit(0);
151 }
152
153 static int standard_get_id(struct smbsrv_request *req)
154 {
155         return (int)req->smb_conn->pid;
156 }
157
158 static void standard_exit_server(struct server_context *srv_ctx, const char *reason)
159 {
160         DEBUG(1,("standard_exit_server: reason[%s]\n",reason));
161 }
162
163 /*
164   initialise the standard process model, registering ourselves with the process model subsystem
165  */
166 NTSTATUS process_model_standard_init(void)
167 {
168         NTSTATUS ret;
169         struct model_ops ops;
170
171         ZERO_STRUCT(ops);
172
173         /* fill in our name */
174         ops.name = "standard";
175
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;
182
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"));
187                 return ret;
188         }
189
190         return ret;
191 }