s3-prefork: Return tsocket_address for client and server
[ira/wip.git] / source3 / lib / server_prefork.h
1 /*
2    Unix SMB/CIFS implementation.
3    Common server globals
4
5    Copyright (C) Simo Sorce <idra@samba.org> 2011
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "system/network.h"
22 #include <tevent.h>
23 #include "lib/tsocket/tsocket.h"
24
25 struct prefork_pool;
26
27 enum pf_worker_status {
28         PF_WORKER_NONE = 0,
29         PF_WORKER_IDLE,
30         PF_WORKER_ACCEPTING,
31         PF_WORKER_BUSY,
32         PF_WORKER_EXITING
33 };
34
35 enum pf_server_cmds {
36         PF_SRV_MSG_NONE = 0,
37         PF_SRV_MSG_EXIT
38 };
39
40 /**
41 * @brief This structure is shared betwee the controlling parent and the
42 *        the child. The parent can only write to the 'cmds' and
43 *        'allowed_clients' variables, while a child is running.
44 *        The child can change 'status', and 'num_clients'.
45 *        All other variables are initialized by the parent before forking the
46 *        child.
47 */
48 struct pf_worker_data {
49         pid_t pid;
50         enum pf_worker_status status;
51         time_t started;
52         time_t last_used;
53         int num_clients;
54
55         enum pf_server_cmds cmds;
56         int allowed_clients;
57 };
58
59 /**
60 * @brief This is the 'main' function called by a child right after the fork.
61 *        It is daemon specific and should initialize and perform whatever
62 *        operation the child is meant to do. Returning from this function will
63 *        cause the termination of the child.
64 *
65 * @param ev             The event context
66 * @param msg_ctx        The messaging context
67 * @param pf             The mmaped area used to communicate with parent
68 * @param listen_fd_size The number of file descriptors to monitor
69 * @param listen_fds     The array of file descriptors
70 * @param lock_fd        The locking file descriptor
71 * @param private_data   Private data that needs to be passed to the main
72 *                       function from the calling parent.
73 *
74 * @return Returns the exit status to be reported to the parent via exit()
75 */
76 typedef int (prefork_main_fn_t)(struct tevent_context *ev,
77                                 struct messaging_context *msg_ctx,
78                                 struct pf_worker_data *pf,
79                                 int listen_fd_size,
80                                 int *listen_fds,
81                                 int lock_fd,
82                                 void *private_data);
83
84 /**
85 * @brief Callback function for parents that also want to be called on sigchld
86 *
87 * @param ev_ctx         The event context
88 * @param pool           The pool handler
89 * @param private_data   Data private to the parent
90 */
91 typedef void (prefork_sigchld_fn_t)(struct tevent_context *ev_ctx,
92                                     struct prefork_pool *pool,
93                                     void *private_data);
94
95 /* ==== Functions used by controlling process ==== */
96
97 /**
98 * @brief Creates the first pool of preforked processes
99 *
100 * @param mem_ctx        The memory context used to hold the pool structure
101 * @param ev_ctx         The event context
102 * @param msg_ctx        The messaging context
103 * @param listen_fd_size The number of file descriptors to monitor
104 * @param listen_fds     The array of file descriptors to monitor
105 * @param min_children   Minimum number of children that must be available at
106 *                       any given time
107 * @param max_children   Maximum number of children that can be started. Also
108 *                       determines the initial size of the pool.
109 * @param main_fn        The children 'main' function to be called after fork
110 * @param private_data   The children private data.
111 * @param pf_pool        The allocated pool.
112 *
113 * @return True if it was successful, False otherwise.
114 */
115 bool prefork_create_pool(TALLOC_CTX *mem_ctx,
116                          struct tevent_context *ev_ctx,
117                          struct messaging_context *msg_ctx,
118                          int listen_fd_size, int *listen_fds,
119                          int min_children, int max_children,
120                          prefork_main_fn_t *main_fn, void *private_data,
121                          struct prefork_pool **pf_pool);
122 /**
123 * @brief Function used to attemp to expand the size of children.
124 *
125 * @param pfp            The pool structure.
126 * @param new_max        The new max number of children.
127 *
128 * @return 0 if operation was successful
129 *         ENOSPC if the mmap area could not be grown to the requested size
130 *         EINVAL if the new max is invalid.
131 *
132 * NOTE: this funciton can easily fail if the mmap area cannot be enlarged.
133 *       A well behaving parent MUST NOT error out if this happen.
134 */
135 int prefork_expand_pool(struct prefork_pool *pfp, int new_max);
136
137 /**
138 * @brief Used to prefork a number of new children
139 *
140 * @param ev_ctx         The event context
141 * @param msg_ctx        The messaging context
142 * @param pfp            The pool structure
143 * @param num_children   The number of children to be started
144 *
145 * @return The number of new children effectively forked.
146 *
147 * NOTE: This method does not expand the pool, if the max number of children
148 *       has already been forked it will do nothing.
149 */
150 int prefork_add_children(struct tevent_context *ev_ctx,
151                          struct messaging_context *msg_ctx,
152                          struct prefork_pool *pfp,
153                          int num_children);
154 /**
155 * @brief Commands a number fo children to stop and exit
156 *
157 * @param pfp            The pool.
158 * @param num_children   The number of children we need to retire.
159 * @param age_limit      The minimum age a child has been active to be
160 *                       considered for retirement. (Compared against the
161 *                       'started' value in the pf_worker_data structure of the
162 *                       children.
163 *
164 * @return Number of children that were signaled to stop
165 *
166 * NOTE: Only children that has no attached clients can be stopped.
167 *       If all the available children are too young or are busy than it
168 *       is possible that none will be asked to stop.
169 */
170 int prefork_retire_children(struct prefork_pool *pfp,
171                             int num_children, time_t age_limit);
172 /**
173 * @brief Count the number of active children
174 *
175 * @param pfp    The pool.
176 * @param total  Returns the number of children currently alive
177 *
178 * @return The number of children actually serving clients
179 */
180 int prefork_count_active_children(struct prefork_pool *pfp, int *total);
181
182 /**
183 * @brief Inform all children that they are allowed to accept 'max' clients
184 *        now. Use this when all children are already busy and more clients
185 *        are trying to connect. It will allow each child to handle more than
186 *        one client at a time, up to 'max'.
187 *
188 * @param pfp    The pool.
189 * @param max    Max number of clients per child.
190 */
191 void prefork_increase_allowed_clients(struct prefork_pool *pfp, int max);
192
193 /**
194 * @brief Reset the maximum allowd clients per child to 1.
195 *        Does not reduce the number of clients actually beeing served by
196 *        any given child, but prevents children from overcommitting from
197 *        now on.
198 *
199 * @param pfp    The pool.
200 */
201 void prefork_reset_allowed_clients(struct prefork_pool *pfp);
202
203 /**
204 * @brief Send a specific signal to all children.
205 *        Used to send SIGHUP when a reload of the configuration is needed
206 *        for example.
207 *
208 * @param pfp            The pool.
209 * @param signal_num     The signal number to be sent.
210 */
211 void prefork_send_signal_to_all(struct prefork_pool *pfp, int signal_num);
212
213 /**
214 * @brief Sets the SIGCHLD callback
215 *
216 * @param pfp            The pool handler.
217 * @param sigchld_fn     The callback function (pass NULL to unset).
218 * @param private_data   Private data for the callback function.
219 */
220 void prefork_set_sigchld_callback(struct prefork_pool *pfp,
221                                   prefork_sigchld_fn_t *sigchld_fn,
222                                   void *private_data);
223
224 /* ==== Functions used by children ==== */
225
226 /**
227 * @brief Try to listen and accept on one of the listening sockets.
228 *        Asynchronusly tries to grab the lock and perform an accept.
229 *        Will automatically updated the 'status' of the child and handle
230 *        all the locking/unlocking/timingout as necessary.
231 *        Changes behavior depending on whether the child already has other
232 *        client connections. If not it blocks on the lock call for periods of
233 *        time. Otherwise it loops on the lock using a timer in order to allow
234 *        processing of the other clients requests.
235 *
236 * @param mem_ctx        The memory context on whic to allocate the request
237 * @param ev             The event context
238 * @param pf             The child/parent shared structure
239 * @param listen_fd_size The number of listening file descriptors
240 * @param listen_fds     The array of listening file descriptors
241 * @param lock_fd        The locking file descriptor
242 *
243 * @return The tevent request pointer or NULL on allocation errors.
244 */
245 struct tevent_req *prefork_listen_send(TALLOC_CTX *mem_ctx,
246                                         struct tevent_context *ev,
247                                         struct pf_worker_data *pf,
248                                         int listen_fd_size,
249                                         int *listen_fds,
250                                         int lock_fd);
251 /**
252 * @brief Returns the file descriptor after the new client connection has
253 *        been accepted.
254 *
255 * @param req            The request
256 * @param mem_ctx        The memory context for cli_addr and srv_addr
257 * @param fd             The new file descriptor.
258 * @param srv_addr       The server address in tsocket_address format
259 * @param cli_addr       The client address in tsocket_address format
260 *
261 * @return       The error in case the operation failed.
262 */
263 int prefork_listen_recv(struct tevent_req *req,
264                         TALLOC_CTX *mem_ctx, int *fd,
265                         struct tsocket_address **srv_addr,
266                         struct tsocket_address **cli_addr);
267