1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | process model: onefork (1 child process)
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 1992-2005
|
---|
7 | Copyright (C) James J Myers 2003 <myersjj@samba.org>
|
---|
8 | Copyright (C) Stefan (metze) Metzmacher 2004
|
---|
9 | Copyright (C) Andrew Bartlett 2008 <abartlet@samba.org>
|
---|
10 | Copyright (C) David Disseldorp 2008 <ddiss@sgi.com>
|
---|
11 |
|
---|
12 | This program is free software; you can redistribute it and/or modify
|
---|
13 | it under the terms of the GNU General Public License as published by
|
---|
14 | the Free Software Foundation; either version 3 of the License, or
|
---|
15 | (at your option) any later version.
|
---|
16 |
|
---|
17 | This program is distributed in the hope that it will be useful,
|
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | GNU General Public License for more details.
|
---|
21 |
|
---|
22 | You should have received a copy of the GNU General Public License
|
---|
23 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "includes.h"
|
---|
27 | #include "lib/events/events.h"
|
---|
28 | #include "lib/socket/socket.h"
|
---|
29 | #include "smbd/process_model.h"
|
---|
30 | #include "system/filesys.h"
|
---|
31 | #include "cluster/cluster.h"
|
---|
32 | #include "param/param.h"
|
---|
33 | #include "ldb_wrap.h"
|
---|
34 |
|
---|
35 | #ifdef HAVE_SETPROCTITLE
|
---|
36 | #ifdef HAVE_SETPROCTITLE_H
|
---|
37 | #include <setproctitle.h>
|
---|
38 | #endif
|
---|
39 | #else
|
---|
40 | #define setproctitle none_setproctitle
|
---|
41 | static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
|
---|
42 | static int none_setproctitle(const char *fmt, ...)
|
---|
43 | {
|
---|
44 | return 0;
|
---|
45 | }
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | /*
|
---|
49 | called when the process model is selected
|
---|
50 | */
|
---|
51 | static void onefork_model_init(void)
|
---|
52 | {
|
---|
53 | signal(SIGCHLD, SIG_IGN);
|
---|
54 | }
|
---|
55 |
|
---|
56 | static void onefork_reload_after_fork(void)
|
---|
57 | {
|
---|
58 | ldb_wrap_fork_hook();
|
---|
59 |
|
---|
60 | /* Ensure that the forked children do not expose identical random streams */
|
---|
61 | set_need_random_reseed();
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*
|
---|
65 | called when a listening socket becomes readable.
|
---|
66 | */
|
---|
67 | static void onefork_accept_connection(struct tevent_context *ev,
|
---|
68 | struct loadparm_context *lp_ctx,
|
---|
69 | struct socket_context *listen_socket,
|
---|
70 | void (*new_conn)(struct tevent_context *,
|
---|
71 | struct loadparm_context *, struct socket_context *,
|
---|
72 | struct server_id , void *),
|
---|
73 | void *private_data)
|
---|
74 | {
|
---|
75 | NTSTATUS status;
|
---|
76 | struct socket_context *connected_socket;
|
---|
77 | pid_t pid = getpid();
|
---|
78 |
|
---|
79 | /* accept an incoming connection. */
|
---|
80 | status = socket_accept(listen_socket, &connected_socket);
|
---|
81 | if (!NT_STATUS_IS_OK(status)) {
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | talloc_steal(private_data, connected_socket);
|
---|
86 |
|
---|
87 | new_conn(ev, lp_ctx, connected_socket, cluster_id(pid, socket_get_fd(connected_socket)), private_data);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /*
|
---|
91 | called to create a new server task
|
---|
92 | */
|
---|
93 | static void onefork_new_task(struct tevent_context *ev,
|
---|
94 | struct loadparm_context *lp_ctx,
|
---|
95 | const char *service_name,
|
---|
96 | void (*new_task_fn)(struct tevent_context *, struct loadparm_context *lp_ctx, struct server_id , void *),
|
---|
97 | void *private_data)
|
---|
98 | {
|
---|
99 | pid_t pid;
|
---|
100 |
|
---|
101 | pid = fork();
|
---|
102 |
|
---|
103 | if (pid != 0) {
|
---|
104 | /* parent or error code ... go back to the event loop */
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | pid = getpid();
|
---|
109 |
|
---|
110 | if (tevent_re_initialise(ev) != 0) {
|
---|
111 | smb_panic("Failed to re-initialise tevent after fork");
|
---|
112 | }
|
---|
113 |
|
---|
114 | setproctitle("task %s server_id[%d]", service_name, (int)pid);
|
---|
115 |
|
---|
116 | onefork_reload_after_fork();
|
---|
117 |
|
---|
118 | /* setup this new connection: process will bind to it's sockets etc */
|
---|
119 | new_task_fn(ev, lp_ctx, cluster_id(pid, 0), private_data);
|
---|
120 |
|
---|
121 | event_loop_wait(ev);
|
---|
122 |
|
---|
123 | talloc_free(ev);
|
---|
124 | exit(0);
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /* called when a task goes down */
|
---|
130 | static void onefork_terminate(struct tevent_context *ev, struct loadparm_context *lp_ctx, const char *reason)
|
---|
131 | {
|
---|
132 | DEBUG(2,("onefork_terminate: reason[%s]\n",reason));
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* called to set a title of a task or connection */
|
---|
136 | static void onefork_set_title(struct tevent_context *ev, const char *title)
|
---|
137 | {
|
---|
138 | if (title) {
|
---|
139 | setproctitle("%s", title);
|
---|
140 | } else {
|
---|
141 | setproctitle(NULL);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | static const struct model_ops onefork_ops = {
|
---|
146 | .name = "onefork",
|
---|
147 | .model_init = onefork_model_init,
|
---|
148 | .accept_connection = onefork_accept_connection,
|
---|
149 | .new_task = onefork_new_task,
|
---|
150 | .terminate = onefork_terminate,
|
---|
151 | .set_title = onefork_set_title,
|
---|
152 | };
|
---|
153 |
|
---|
154 | /*
|
---|
155 | initialise the onefork process model, registering ourselves with the process model subsystem
|
---|
156 | */
|
---|
157 | NTSTATUS process_model_onefork_init(void)
|
---|
158 | {
|
---|
159 | return register_process_model(&onefork_ops);
|
---|
160 | }
|
---|