2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Christian Grothoff
25 * Functions related to starting services
27 * @defgroup service Service library
28 * Start service processes.
30 * @see [Documentation](https://gnunet.org/developer-handbook-util-services)
35 #ifndef GNUNET_SERVICE_LIB_H
36 #define GNUNET_SERVICE_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
46 #include "gnunet_configuration_lib.h"
47 #include "gnunet_mq_lib.h"
51 * Options for the service (bitmask).
53 enum GNUNET_SERVICE_Options
56 * Use defaults. Terminates all client connections and the listen
57 * sockets immediately upon receiving the shutdown signal.
59 GNUNET_SERVICE_OPTION_NONE = 0,
62 * Do not trigger server shutdown on signal at all; instead, allow
63 * for the user to terminate the server explicitly when needed
64 * by calling #GNUNET_SERVICE_shutdown().
66 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
69 * Trigger a SOFT server shutdown on signals, allowing active
70 * non-monitor clients to complete their transactions.
72 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2,
75 * Bitmask over the shutdown options.
77 GNUNET_SERVICE_OPTION_SHUTDOWN_BITMASK = 3,
80 * Instead of listening on lsocks passed by the parent,
81 * close them *after* opening our own listen socket(s).
83 GNUNET_SERVICE_OPTION_CLOSE_LSOCKS = 4
87 /* **************** NEW SERVICE API ********************** */
90 * Handle to a service.
92 struct GNUNET_SERVICE_Handle;
96 * Handle to a client that is connected to a service.
98 struct GNUNET_SERVICE_Client;
102 * Callback to initialize a service, called exactly once when the service is run.
104 * @param cls closure passed to #GNUNET_SERVICE_MAIN
105 * @param cfg configuration to use for this service
106 * @param sh handle to the newly create service
109 (*GNUNET_SERVICE_InitCallback)(void *cls,
110 const struct GNUNET_CONFIGURATION_Handle *cfg,
111 struct GNUNET_SERVICE_Handle *sh);
115 * Callback to be called when a client connects to the service.
117 * @param cls closure for the service
118 * @param c the new client that connected to the service
119 * @param mq the message queue used to send messages to the client
120 * @return the client-specific (`internal') closure
123 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
124 struct GNUNET_SERVICE_Client *c,
125 struct GNUNET_MQ_Handle *mq);
129 * Callback to be called when a client disconnected from the service
131 * @param cls closure for the service
132 * @param c the client that disconnected
133 * @param internal_cls the client-specific (`internal') closure
136 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
137 struct GNUNET_SERVICE_Client *c,
142 * Low-level function to start a service if the scheduler
143 * is already running. Should only be used directly in
146 * The function will launch the service with the name @a service_name
147 * using the @a service_options to configure its shutdown
148 * behavior. When clients connect or disconnect, the respective
149 * @a connect_cb or @a disconnect_cb functions will be called. For
150 * messages received from the clients, the respective @a handlers will
151 * be invoked; for the closure of the handlers we use the return value
152 * from the @a connect_cb invocation of the respective client.
154 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
155 * message to receive further messages from this client. If
156 * #GNUNET_SERVICE_client_continue() is not called within a short
157 * time, a warning will be logged. If delays are expected, services
158 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
159 * disable the warning.
161 * Clients sending invalid messages (based on @a handlers) will be
162 * dropped. Additionally, clients can be dropped at any time using
163 * #GNUNET_SERVICE_client_drop().
165 * The service must be stopped using #GNUNET_SERVICE_stop().
167 * @param service_name name of the service to run
168 * @param cfg configuration to use
169 * @param connect_cb function to call whenever a client connects
170 * @param disconnect_cb function to call whenever a client disconnects
171 * @param cls closure argument for @a connect_cb and @a disconnect_cb
172 * @param handlers NULL-terminated array of message handlers for the service,
173 * the closure will be set to the value returned by
174 * the @a connect_cb for the respective connection
175 * @return NULL on error
177 struct GNUNET_SERVICE_Handle *
178 GNUNET_SERVICE_start (const char *service_name,
179 const struct GNUNET_CONFIGURATION_Handle *cfg,
180 GNUNET_SERVICE_ConnectHandler connect_cb,
181 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
183 const struct GNUNET_MQ_MessageHandler *handlers);
187 * Stops a service that was started with #GNUNET_SERVICE_start().
189 * @param srv service to stop
192 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Handle *srv);
196 * Creates the "main" function for a GNUnet service. You
197 * should almost always use the #GNUNET_SERVICE_MAIN macro
198 * instead of calling this function directly (except
199 * for ARM, which should call this function directly).
201 * The function will launch the service with the name @a service_name
202 * using the @a service_options to configure its shutdown
203 * behavior. Once the service is ready, the @a init_cb will be called
204 * for service-specific initialization. @a init_cb will be given the
205 * service handler which can be used to control the service's
206 * availability. When clients connect or disconnect, the respective
207 * @a connect_cb or @a disconnect_cb functions will be called. For
208 * messages received from the clients, the respective @a handlers will
209 * be invoked; for the closure of the handlers we use the return value
210 * from the @a connect_cb invocation of the respective client.
212 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
213 * message to receive further messages from this client. If
214 * #GNUNET_SERVICE_client_continue() is not called within a short
215 * time, a warning will be logged. If delays are expected, services
216 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
217 * disable the warning.
219 * Clients sending invalid messages (based on @a handlers) will be
220 * dropped. Additionally, clients can be dropped at any time using
221 * #GNUNET_SERVICE_client_drop().
223 * @param argc number of command-line arguments in @a argv
224 * @param argv array of command-line arguments
225 * @param service_name name of the service to run
226 * @param options options controlling shutdown of the service
227 * @param service_init_cb function to call once the service is ready
228 * @param connect_cb function to call whenever a client connects
229 * @param disconnect_cb function to call whenever a client disconnects
230 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
231 * @param handlers NULL-terminated array of message handlers for the service,
232 * the closure will be set to the value returned by
233 * the @a connect_cb for the respective connection
234 * @return 0 on success, non-zero on error
237 GNUNET_SERVICE_run_ (int argc,
239 const char *service_name,
240 enum GNUNET_SERVICE_Options options,
241 GNUNET_SERVICE_InitCallback service_init_cb,
242 GNUNET_SERVICE_ConnectHandler connect_cb,
243 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
245 const struct GNUNET_MQ_MessageHandler *handlers);
249 * Creates the "main" function for a GNUnet service. You
250 * MUST use this macro to define GNUnet services (except
251 * for ARM, which MUST NOT use the macro). The reason is
252 * the GNUnet-as-a-library project, where we will not define
253 * a main function anywhere but in ARM.
255 * The macro will launch the service with the name @a service_name
256 * using the @a service_options to configure its shutdown
257 * behavior. Once the service is ready, the @a init_cb will be called
258 * for service-specific initialization. @a init_cb will be given the
259 * service handler which can be used to control the service's
260 * availability. When clients connect or disconnect, the respective
261 * @a connect_cb or @a disconnect_cb functions will be called. For
262 * messages received from the clients, the respective @a handlers will
263 * be invoked; for the closure of the handlers we use the return value
264 * from the @a connect_cb invocation of the respective client.
266 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
267 * message to receive further messages from this client. If
268 * #GNUNET_SERVICE_client_continue() is not called within a short
269 * time, a warning will be logged. If delays are expected, services
270 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
271 * disable the warning.
273 * Clients sending invalid messages (based on @a handlers) will be
274 * dropped. Additionally, clients can be dropped at any time using
275 * #GNUNET_SERVICE_client_drop().
277 * @param service_name name of the service to run
278 * @param options options controlling shutdown of the service
279 * @param service_init_cb function to call once the service is ready
280 * @param connect_cb function to call whenever a client connects
281 * @param disconnect_cb function to call whenever a client disconnects
282 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
283 * @param ... array of message handlers for the service, terminated
284 * by #GNUNET_MQ_handler_end();
285 * the closure will be set to the value returned by
286 * the @a connect_cb for the respective connection
287 * @return 0 on success, non-zero on error
291 * GNUNET_SERVICE_MAIN
293 * GNUNET_SERVICE_OPTION_NONE,
298 * GNUNET_MQ_hd_var_size (get,
299 * GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
300 * struct GNUNET_RESOLVER_GetMessage,
302 * GNUNET_MQ_handler_end ());
305 #define GNUNET_SERVICE_MAIN(service_name, service_options, init_cb, connect_cb, \
306 disconnect_cb, cls, ...) \
311 struct GNUNET_MQ_MessageHandler mh[] = { \
314 return GNUNET_SERVICE_run_ (argc, \
327 * Suspend accepting connections from the listen socket temporarily.
328 * Resume activity using #GNUNET_SERVICE_resume.
330 * @param sh service to stop accepting connections.
333 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
337 * Resume accepting connections from the listen socket.
339 * @param sh service to resume accepting connections.
342 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
346 * Continue receiving further messages from the given client.
347 * Must be called after each message received.
349 * @param c the client to continue receiving from
352 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
356 * Obtain the message queue of @a c. Convenience function.
358 * @param c the client to continue receiving from
359 * @return the message queue of @a c
361 struct GNUNET_MQ_Handle *
362 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c);
366 * Disable the warning the server issues if a message is not
367 * acknowledged in a timely fashion. Use this call if a client is
368 * intentionally delayed for a while. Only applies to the current
371 * @param c client for which to disable the warning
374 GNUNET_SERVICE_client_disable_continue_warning (struct
375 GNUNET_SERVICE_Client *c);
379 * Ask the server to disconnect from the given client. This is the
380 * same as returning #GNUNET_SYSERR within the check procedure when
381 * handling a message, except that it allows dropping of a client even
382 * when not handling a message from that client. The `disconnect_cb`
383 * will be called on @a c even if the application closes the connection
384 * using this function.
386 * This function should be called (outside of util's internal logic)
387 * if (and usually only if) the client has violated the
388 * protocol. Otherwise, we should leave it to the client to disconnect
391 * @param c client to disconnect now
394 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
398 * Explicitly stops the service.
400 * @param sh server to shutdown
403 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
407 * Set the 'monitor' flag on this client. Clients which have been
408 * marked as 'monitors' won't prevent the server from shutting down
409 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
410 * that for "normal" clients we likely want to allow them to process
411 * their requests; however, monitor-clients are likely to 'never'
412 * disconnect during shutdown and thus will not be considered when
413 * determining if the server should continue to exist after
414 * shutdown has been triggered.
416 * @param c client to mark as a monitor
419 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
423 * Set the persist option on this client. Indicates that the
424 * underlying socket or fd should never really be closed. Used for
425 * indicating process death.
427 * @param c client to persist the socket (never to be closed)
430 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
433 #if 0 /* keep Emacsens' auto-indent happy */
440 /* ifndef GNUNET_SERVICE_LIB_H */
443 /** @} */ /* end of group service */
445 /* end of gnunet_service_lib.h */