r15343: Some small cleanups.
[samba.git] / source / smbd / process_single.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    process model: process (1 process handles all client connections)
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) James J Myers 2003 <myersjj@samba.org>
8    Copyright (C) Stefan (metze) Metzmacher 2004
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "smbd/process_model.h"
27
28 /* For sepecifiying event context to GSSAPI below */
29 #include "system/kerberos.h"
30 #include "heimdal/lib/gssapi/gssapi_locl.h"
31
32
33 /*
34   called when the process model is selected
35 */
36 static void single_model_init(struct event_context *ev)
37 {
38         /* Hack to ensure that GSSAPI uses the right event context */
39         gssapi_krb5_init_ev(ev);
40 }
41
42 /*
43   called when a listening socket becomes readable. 
44 */
45 static void single_accept_connection(struct event_context *ev, 
46                                      struct socket_context *sock,
47                                      void (*new_conn)(struct event_context *, struct socket_context *, 
48                                                       uint32_t , void *), 
49                                      void *private)
50 {
51         NTSTATUS status;
52         struct socket_context *sock2;
53
54         /* accept an incoming connection. */
55         status = socket_accept(sock, &sock2);
56         if (!NT_STATUS_IS_OK(status)) {
57                 DEBUG(0,("accept_connection_single: accept: %s\n", nt_errstr(status)));
58                 /* this looks strange, but is correct. We need to
59                    throttle things until the system clears enough
60                    resources to handle this new socket. If we don't
61                    then we will spin filling the log and causing more
62                    problems. We don't panic as this is probably a
63                    temporary resource constraint */
64                 sleep(1);
65                 return;
66         }
67
68         talloc_steal(private, sock);
69
70         new_conn(ev, sock2, socket_get_fd(sock2), private);
71 }
72
73 /*
74   called to startup a new task
75 */
76 static void single_new_task(struct event_context *ev, 
77                             void (*new_task)(struct event_context *, uint32_t, void *), 
78                             void *private)
79 {
80         static uint32_t taskid = 0x10000000;
81         new_task(ev, taskid++, private);
82 }
83
84
85 /* called when a task goes down */
86 static void single_terminate(struct event_context *ev, const char *reason) 
87 {
88         DEBUG(2,("single_terminate: reason[%s]\n",reason));
89 }
90
91 /* called to set a title of a task or connection */
92 static void single_set_title(struct event_context *ev, const char *title) 
93 {
94 }
95
96 static const struct model_ops single_ops = {
97         .name                   = "single",
98         .model_init             = single_model_init,
99         .new_task               = single_new_task,
100         .accept_connection      = single_accept_connection,
101         .terminate              = single_terminate,
102         .set_title              = single_set_title,
103 };
104
105 /*
106   initialise the single process model, registering ourselves with the
107   process model subsystem
108  */
109 NTSTATUS process_model_single_init(void)
110 {
111         return register_process_model(&single_ops);
112 }