2 Unix SMB/CIFS implementation.
5 Copyright (C) Simo Sorce <idra@samba.org> 2011
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.
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.
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/>.
21 #include "system/network.h"
23 #include "lib/tsocket/tsocket.h"
27 enum pf_worker_status {
41 * @brief This structure is shared between 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
48 struct pf_worker_data {
50 enum pf_worker_status status;
55 enum pf_server_cmds cmds;
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.
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.
74 * @return Returns the exit status to be reported to the parent via exit()
76 typedef int (prefork_main_fn_t)(struct tevent_context *ev,
77 struct messaging_context *msg_ctx,
78 struct pf_worker_data *pf,
86 * @brief Callback function for parents that also want to be called on sigchld
88 * @param ev_ctx The event context
89 * @param pool The pool handler
90 * @param private_data Data private to the parent
92 typedef void (prefork_sigchld_fn_t)(struct tevent_context *ev_ctx,
93 struct prefork_pool *pool,
96 /* ==== Functions used by controlling process ==== */
99 * @brief Creates the first pool of preforked processes
101 * @param mem_ctx The memory context used to hold the pool structure
102 * @param ev_ctx The event context
103 * @param msg_ctx The messaging context
104 * @param listen_fd_size The number of file descriptors to monitor
105 * @param listen_fds The array of file descriptors to monitor
106 * @param min_children Minimum number of children that must be available at
108 * @param max_children Maximum number of children that can be started. Also
109 * determines the initial size of the pool.
110 * @param main_fn The children 'main' function to be called after fork
111 * @param private_data The children private data.
112 * @param pf_pool The allocated pool.
114 * @return True if it was successful, False otherwise.
116 bool prefork_create_pool(TALLOC_CTX *mem_ctx,
117 struct tevent_context *ev_ctx,
118 struct messaging_context *msg_ctx,
119 int listen_fd_size, int *listen_fds,
120 int min_children, int max_children,
121 prefork_main_fn_t *main_fn, void *private_data,
122 struct prefork_pool **pf_pool);
124 * @brief Function used to attempt to expand the size of children.
126 * @param pfp The pool structure.
127 * @param new_max The new max number of children.
129 * @return 0 if operation was successful
130 * ENOSPC if the mmap area could not be grown to the requested size
131 * EINVAL if the new max is invalid.
133 * NOTE: this function can easily fail if the mmap area cannot be enlarged.
134 * A well behaving parent MUST NOT error out if this happen.
136 int prefork_expand_pool(struct prefork_pool *pfp, int new_max);
139 * @brief Used to prefork a number of new children
141 * @param ev_ctx The event context
142 * @param msg_ctx The messaging context
143 * @param pfp The pool structure
144 * @param num_children The number of children to be started
146 * @return The number of new children effectively forked.
148 * NOTE: This method does not expand the pool, if the max number of children
149 * has already been forked it will do nothing.
151 int prefork_add_children(struct tevent_context *ev_ctx,
152 struct messaging_context *msg_ctx,
153 struct prefork_pool *pfp,
156 * @brief Commands a number of children to stop and exit
158 * @param pfp The pool.
159 * @param num_children The number of children we need to retire.
160 * @param age_limit The minimum age a child has been active to be
161 * considered for retirement. (Compared against the
162 * 'started' value in the pf_worker_data structure of the
165 * @return Number of children that were signaled to stop
167 * NOTE: Only children that have no attached clients can be stopped.
168 * If all the available children are too young or are busy then it
169 * is possible that none will be asked to stop.
171 int prefork_retire_children(struct prefork_pool *pfp,
172 int num_children, time_t age_limit);
174 * @brief Count the number of active children
176 * @param pfp The pool.
177 * @param total Returns the number of children currently alive
179 * @return The number of children actually serving clients
181 int prefork_count_active_children(struct prefork_pool *pfp, int *total);
184 * @brief Count the number of actual connections currently allowed
186 * @param pfp The pool.
188 * @return The number of connections that can still be opened by clients
189 * with the current pool of children.
191 int prefork_count_allowed_connections(struct prefork_pool *pfp);
194 * @brief Increase the amount of clients each child is allowed to handle
195 * simultaneaously. It will allow each child to handle more than
196 * one client at a time, up to 'max' (currently set to 100).
198 * @param pfp The pool.
199 * @param max Max number of allowed connections per child
201 void prefork_increase_allowed_clients(struct prefork_pool *pfp, int max);
204 * @brief Decrease the amount of clients each child is allowed to handle.
207 * @param pfp The pool.
209 void prefork_decrease_allowed_clients(struct prefork_pool *pfp);
212 * @brief Reset the maximum allowd clients per child to 1.
213 * Does not reduce the number of clients actually beeing served by
214 * any given child, but prevents children from overcommitting from
217 * @param pfp The pool.
219 void prefork_reset_allowed_clients(struct prefork_pool *pfp);
222 * @brief Send a specific signal to all children.
223 * Used to send SIGHUP when a reload of the configuration is needed
226 * @param pfp The pool.
227 * @param signal_num The signal number to be sent.
229 void prefork_send_signal_to_all(struct prefork_pool *pfp, int signal_num);
232 * @brief Sets the SIGCHLD callback
234 * @param pfp The pool handler.
235 * @param sigchld_fn The callback function (pass NULL to unset).
236 * @param private_data Private data for the callback function.
238 void prefork_set_sigchld_callback(struct prefork_pool *pfp,
239 prefork_sigchld_fn_t *sigchld_fn,
242 /* ==== Functions used by children ==== */
245 * @brief Try to listen and accept on one of the listening sockets.
246 * Asynchronusly tries to grab the lock and perform an accept.
247 * Will automatically update the 'status' of the child and handle
248 * all the locking/unlocking/timingout as necessary.
249 * Changes behavior depending on whether the child already has other
250 * client connections. If not it blocks on the lock call for periods of
251 * time. Otherwise it loops on the lock using a timer in order to allow
252 * processing of the other clients requests.
254 * @param mem_ctx The memory context on whic to allocate the request
255 * @param ev The event context
256 * @param pf The child/parent shared structure
257 * @param listen_fd_size The number of listening file descriptors
258 * @param listen_fds The array of listening file descriptors
259 * @param lock_fd The locking file descriptor
261 * @return The tevent request pointer or NULL on allocation errors.
263 struct tevent_req *prefork_listen_send(TALLOC_CTX *mem_ctx,
264 struct tevent_context *ev,
265 struct pf_worker_data *pf,
270 * @brief Returns the file descriptor after the new client connection has
273 * @param req The request
274 * @param mem_ctx The memory context for cli_addr and srv_addr
275 * @param fd The new file descriptor.
276 * @param srv_addr The server address in tsocket_address format
277 * @param cli_addr The client address in tsocket_address format
279 * @return The error in case the operation failed.
281 int prefork_listen_recv(struct tevent_req *req,
282 TALLOC_CTX *mem_ctx, int *fd,
283 struct tsocket_address **srv_addr,
284 struct tsocket_address **cli_addr);