first public release of samba4 code
[bbaumbach/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    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /*
25   called when the process model is selected
26 */
27 static void model_startup(void)
28 {
29 }
30
31 /*
32   called when a listening socket becomes readable
33 */
34 static void accept_connection(struct event_context *ev, struct fd_event *fde, time_t t, uint16 flags)
35 {
36         int accepted_fd;
37         struct sockaddr addr;
38         socklen_t in_addrlen = sizeof(addr);
39         pid_t pid;
40         struct model_ops *model_ops = fde->private;
41
42         accepted_fd = accept(fde->fd,&addr,&in_addrlen);
43         if (accepted_fd == -1) {
44                 DEBUG(0,("accept_connection_standard: accept: %s\n",
45                          strerror(errno)));
46                 return;
47         }
48
49         pid = fork();
50
51         if (pid != 0) {
52                 /* parent or error code ... */
53
54                 close(accepted_fd);
55                 /* go back to the event loop */
56                 return;
57         }
58
59         /* Child code ... */
60
61         /* close all the listening sockets */
62         event_remove_fd_all_handler(ev, accept_connection);
63                         
64         /* tdb needs special fork handling */
65         if (tdb_reopen_all() == -1) {
66                 DEBUG(0,("accept_connection_standard: tdb_reopen_all failed.\n"));
67         }
68
69         /* Load DSO's */
70         init_modules();
71                 
72         /* initialize new process */
73         smbd_process_init();
74                 
75         init_smbsession(ev, model_ops, accepted_fd); 
76
77         /* return to the event loop */
78 }
79
80 /* called when a SMB connection goes down */
81 static void terminate_connection(struct server_context *server, const char *reason) 
82 {
83         server_terminate(server);
84         /* terminate this process */
85         exit(0);
86 }
87
88 static int get_id(struct request_context *req)
89 {
90         return (int)req->smb->pid;
91 }
92
93 /*
94   initialise the standard process model, registering ourselves with the model subsystem
95  */
96 void process_model_standard_init(void)
97 {
98         struct model_ops ops;
99
100         ZERO_STRUCT(ops);
101         
102         /* fill in all the operations */
103         ops.model_startup = model_startup;
104         ops.accept_connection = accept_connection;
105         ops.terminate_connection = terminate_connection;
106         ops.get_id = get_id;
107
108         /* register ourselves with the process model subsystem. We register under the name 'standard'. */
109         register_process_model("standard", &ops);
110 }