s3: Optimize g_lock_lock for a heavily contended case
[Samba.git] / source4 / smbd / process_single.c
blobff57a0bc34b487b315702032346e06b2aab9f787
1 /*
2 Unix SMB/CIFS implementation.
4 process model: process (1 process handles all client connections)
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) James J Myers 2003 <myersjj@samba.org>
8 Copyright (C) Stefan (metze) Metzmacher 2004
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 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "smbd/process_model.h"
26 #include "system/filesys.h"
27 #include "cluster/cluster.h"
30 called when the process model is selected
32 static void single_model_init(struct tevent_context *ev)
37 called when a listening socket becomes readable.
39 static void single_accept_connection(struct tevent_context *ev,
40 struct loadparm_context *lp_ctx,
41 struct socket_context *listen_socket,
42 void (*new_conn)(struct tevent_context *,
43 struct loadparm_context *,
44 struct socket_context *,
45 struct server_id , void *),
46 void *private_data)
48 NTSTATUS status;
49 struct socket_context *connected_socket;
51 /* accept an incoming connection. */
52 status = socket_accept(listen_socket, &connected_socket);
53 if (!NT_STATUS_IS_OK(status)) {
54 DEBUG(0,("single_accept_connection: accept: %s\n", nt_errstr(status)));
55 /* this looks strange, but is correct.
57 We can only be here if woken up from select, due to
58 an incomming connection.
60 We need to throttle things until the system clears
61 enough resources to handle this new socket.
63 If we don't then we will spin filling the log and
64 causing more problems. We don't panic as this is
65 probably a temporary resource constraint */
66 sleep(1);
67 return;
70 talloc_steal(private_data, connected_socket);
72 /* The cluster_id(0, fd) cannot collide with the incrementing
73 * task below, as the first component is 0, not 1 */
74 new_conn(ev, lp_ctx, connected_socket,
75 cluster_id(0, socket_get_fd(connected_socket)), private_data);
79 called to startup a new task
81 static void single_new_task(struct tevent_context *ev,
82 struct loadparm_context *lp_ctx,
83 const char *service_name,
84 void (*new_task)(struct tevent_context *, struct loadparm_context *, struct server_id, void *),
85 void *private_data)
87 /* start our taskids at 1, zero is reserved for the top
88 level samba task */
89 static uint32_t taskid = 1;
91 /* We use 1 so we cannot collide in with cluster ids generated
92 * in the accept connection above, and unlikly to collide with
93 * PIDs from process modal standard (don't run samba as
94 * init) */
95 new_task(ev, lp_ctx, cluster_id(1, taskid++), private_data);
99 /* called when a task goes down */
100 static void single_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx, const char *reason)
102 DEBUG(2,("single_terminate: reason[%s]\n",reason));
105 /* called to set a title of a task or connection */
106 static void single_set_title(struct tevent_context *ev, const char *title)
110 const struct model_ops single_ops = {
111 .name = "single",
112 .model_init = single_model_init,
113 .new_task = single_new_task,
114 .accept_connection = single_accept_connection,
115 .terminate = single_terminate,
116 .set_title = single_set_title,
120 initialise the single process model, registering ourselves with the
121 process model subsystem
123 NTSTATUS process_model_single_init(void)
125 return register_process_model(&single_ops);