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"
24 enum pf_worker_status {
38 * @brief This structure is shared betwee the controlling parent and the
39 * the child. The parent can only write to the 'cmds' and
40 * 'allowed_clients' variables, while a child is running.
41 * The child can change 'status', and 'num_clients'.
42 * All other variables are initialized by the parent before forking the
45 struct pf_worker_data {
47 enum pf_worker_status status;
52 enum pf_server_cmds cmds;
57 * @brief This is the 'main' function called by a child right after the fork.
58 * It is daemon specific and should initialize and perform whatever
59 * operation the child is meant to do. Returning from this function will
60 * cause the termination of the child.
62 * @param ev The event context
63 * @param pf The mmaped area used to communicate with parent
64 * @param listen_fd_size The number of file descriptors to monitor
65 * @param listen_fds The array of file descriptors
66 * @param lock_fd The locking file descriptor
67 * @param private_data Private data that needs to be passed to the main
68 * function from the calling parent.
70 * @return Returns the exit status to be reported to the parent via exit()
72 typedef int (prefork_main_fn_t)(struct tevent_context *ev,
73 struct pf_worker_data *pf,
82 /* ==== Functions used by controlling process ==== */
85 * @brief Creates the first pool of preforked processes
87 * @param ev_ctx The event context
88 * @param mem_ctx The memory context used to hold the pool structure
89 * @param listen_fd_size The number of file descriptors to monitor
90 * @param listen_fds The array of file descriptors to monitor
91 * @param min_children Minimum number of children that must be available at
93 * @param max_children Maximum number of children that can be started. Also
94 * determines the initial size of the pool.
95 * @param main_fn The children 'main' function to be called after fork
96 * @param private_data The children private data.
97 * @param pf_pool The allocated pool.
99 * @return True if it was successful, False otherwise.
101 bool prefork_create_pool(struct tevent_context *ev_ctx, TALLOC_CTX *mem_ctx,
102 int listen_fd_size, int *listen_fds,
103 int min_children, int max_children,
104 prefork_main_fn_t *main_fn, void *private_data,
105 struct prefork_pool **pf_pool);
107 * @brief Function used to attemp to expand the size of children.
109 * @param pfp The pool structure.
110 * @param new_max The new max number of children.
112 * @return 0 if operation was successful
113 * ENOSPC if the mmap area could not be grown to the requested size
114 * EINVAL if the new max is invalid.
116 * NOTE: this funciton can easily fail if the mmap area cannot be enlarged.
117 * A well behaving parent MUST NOT error out if this happen.
119 int prefork_expand_pool(struct prefork_pool *pfp, int new_max);
122 * @brief Used to prefork a number of new children
124 * @param ev_ctx The event context
125 * @param pfp The pool structure
126 * @param num_children The number of children to be started
128 * @return The number of new children effectively forked.
130 * NOTE: This method does not expand the pool, if the max number of children
131 * has already been forked it will do nothing.
133 int prefork_add_children(struct tevent_context *ev_ctx,
134 struct prefork_pool *pfp,
137 * @brief Commands a number fo children to stop and exit
139 * @param pfp The pool.
140 * @param num_children The number of children we need to retire.
141 * @param age_limit The minimum age a child has been active to be
142 * considered for retirement. (Compared against the
143 * 'started' value in the pf_worker_data structure of the
146 * @return Number of children that were signaled to stop
148 * NOTE: Only children that has no attached clients can be stopped.
149 * If all the available children are too young or are busy than it
150 * is possible that none will be asked to stop.
152 int prefork_retire_children(struct prefork_pool *pfp,
153 int num_children, time_t age_limit);
155 * @brief Count the number of active children
157 * @param pfp The pool.
158 * @param total Returns the number of children currently alive
160 * @return The number of children actually serving clients
162 int prefork_count_active_children(struct prefork_pool *pfp, int *total);
164 * @brief Mark a child structure as free, based on the dead child pid.
165 * This function is called when the parent gets back notice a child
166 * has died through waitpid. It is critical to call this function
167 * when children are reaped so that memory slots can be freed.
169 * @param pfp The pool.
170 * @param pid The child pid.
172 * @return True if the slot was clared. False if the pid is not listed.
174 bool prefork_mark_pid_dead(struct prefork_pool *pfp, pid_t pid);
177 * @brief Inform all children that they are allowed to accept 'max' clients
178 * now. Use this when all children are already busy and more clients
179 * are trying to connect. It will allow each child to handle more than
180 * one client at a time, up to 'max'.
182 * @param pfp The pool.
183 * @param max Max number of clients per child.
185 void prefork_increase_allowed_clients(struct prefork_pool *pfp, int max);
188 * @brief Reset the maximum allowd clients per child to 1.
189 * Does not reduce the number of clients actually beeing served by
190 * any given child, but prevents children from overcommitting from
193 * @param pfp The pool.
195 void prefork_reset_allowed_clients(struct prefork_pool *pfp);
198 * @brief Send a specific signal to all children.
199 * Used to send SIGHUP when a reload of the configuration is needed
202 * @param pfp The pool.
203 * @param signal_num The signal number to be sent.
205 void prefork_send_signal_to_all(struct prefork_pool *pfp, int signal_num);
207 /* ==== Functions used by children ==== */
210 * @brief Try to listen and accept on one of the listening sockets.
211 * Asynchronusly tries to grab the lock and perform an accept.
212 * Will automatically updated the 'status' of the child and handle
213 * all the locking/unlocking/timingout as necessary.
214 * Changes behavior depending on whether the child already has other
215 * client connections. If not it blocks on the lock call for periods of
216 * time. Otherwise it loops on the lock using a timer in order to allow
217 * processing of the other clients requests.
219 * @param mem_ctx The memory context on whic to allocate the request
220 * @param ev The event context
221 * @param pf The child/parent shared structure
222 * @param listen_fd_size The number of listening file descriptors
223 * @param listen_fds The array of listening file descriptors
224 * @param lock_fd The locking file descriptor
225 * @param addr The structure that will hold the client address on
227 * @param addrlen The structure length on return.
229 * @return The tevent request pointer or NULL on allocation errors.
231 struct tevent_req *prefork_listen_send(TALLOC_CTX *mem_ctx,
232 struct tevent_context *ev,
233 struct pf_worker_data *pf,
237 struct sockaddr *addr,
240 * @brief Returns the file descriptor after the new client connection has
243 * @param req The request
244 * @param fd The new file descriptor.
246 * @return The error in case the operation failed.
248 int prefork_listen_recv(struct tevent_req *req, int *fd);