2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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_server_lib.h"
48 #include "gnunet_mq_lib.h"
52 * Function called by the service's run
53 * method to run service-specific setup code.
56 * @param server the initialized server
57 * @param cfg configuration to use
60 (*GNUNET_SERVICE_Main) (void *cls,
61 struct GNUNET_SERVER_Handle *server,
62 const struct GNUNET_CONFIGURATION_Handle *cfg);
66 * Options for the service (bitmask).
68 enum GNUNET_SERVICE_Options
71 * Use defaults. Terminates all client connections and the listen
72 * sockets immediately upon receiving the shutdown signal.
74 GNUNET_SERVICE_OPTION_NONE = 0,
77 * Do not trigger server shutdown on signal at all; instead, allow
78 * for the user to terminate the server explicitly when needed
79 * by calling #GNUNET_SERVICE_shutdown().
81 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
84 * Trigger a SOFT server shutdown on signals, allowing active
85 * non-monitor clients to complete their transactions.
87 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2
92 * Run a standard GNUnet service startup sequence (initialize loggers
93 * and configuration, parse options).
95 * @param argc number of command line arguments in @a argv
96 * @param argv command line arguments
97 * @param service_name our service name
98 * @param options service options
99 * @param task main task of the service
100 * @param task_cls closure for @a task
101 * @return #GNUNET_SYSERR on error, #GNUNET_OK
102 * if we shutdown nicely
106 GNUNET_SERVICE_run (int argc,
108 const char *service_name,
109 enum GNUNET_SERVICE_Options options,
110 GNUNET_SERVICE_Main task,
115 * Opaque handle for a service.
117 struct GNUNET_SERVICE_Context;
121 * Run a service startup sequence within an existing
122 * initialized system.
124 * @param service_name our service name
125 * @param cfg configuration to use
126 * @param options service options
127 * @return NULL on error, service handle
130 struct GNUNET_SERVICE_Context *
131 GNUNET_SERVICE_start (const char *service_name,
132 const struct GNUNET_CONFIGURATION_Handle *cfg,
133 enum GNUNET_SERVICE_Options options);
137 * Obtain the server used by a service. Note that the server must NOT
138 * be destroyed by the caller.
140 * @param ctx the service context returned from the start function
141 * @return handle to the server for this service, NULL if there is none
144 struct GNUNET_SERVER_Handle *
145 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx);
149 * Get the NULL-terminated array of listen sockets for this service.
151 * @param ctx service context to query
152 * @return NULL if there are no listen sockets, otherwise NULL-terminated
153 * array of listen sockets.
156 struct GNUNET_NETWORK_Handle *const *
157 GNUNET_SERVICE_get_listen_sockets (struct GNUNET_SERVICE_Context *ctx);
161 * Stop a service that was started with #GNUNET_SERVICE_start.
163 * @param sctx the service context returned from the start function
167 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx);
170 /* **************** NEW SERVICE API ********************** */
173 * Handle to a service.
175 struct GNUNET_SERVICE_Handle;
179 * Handle to a client that is connected to a service.
181 struct GNUNET_SERVICE_Client;
185 * Callback to initialize a service, called exactly once when the service is run.
187 * @param cls closure passed to #GNUNET_SERVICE_MAIN
188 * @param cfg configuration to use for this service
189 * @param sh handle to the newly create service
192 (*GNUNET_SERVICE_InitCallback)(void *cls,
193 const struct GNUNET_CONFIGURATION_Handle *cfg,
194 struct GNUNET_SERVICE_Handle *sh);
198 * Callback to be called when a client connects to the service.
200 * @param cls closure for the service
201 * @param c the new client that connected to the service
202 * @param mq the message queue used to send messages to the client
203 * @return the client-specific (`internal') closure
206 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
207 struct GNUNET_SERVICE_Client *c,
208 struct GNUNET_MQ_Handle *mq);
212 * Callback to be called when a client disconnected from the service
214 * @param cls closure for the service
215 * @param c the client that disconnected
216 * @param internal_cls the client-specific (`internal') closure
219 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
220 struct GNUNET_SERVICE_Client *c,
225 * Low-level function to start a service if the scheduler
226 * is already running. Should only be used directly in
229 * The function will launch the service with the name @a service_name
230 * using the @a service_options to configure its shutdown
231 * behavior. When clients connect or disconnect, the respective
232 * @a connect_cb or @a disconnect_cb functions will be called. For
233 * messages received from the clients, the respective @a handlers will
234 * be invoked; for the closure of the handlers we use the return value
235 * from the @a connect_cb invocation of the respective client.
237 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
238 * message to receive further messages from this client. If
239 * #GNUNET_SERVICE_client_continue() is not called within a short
240 * time, a warning will be logged. If delays are expected, services
241 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
242 * disable the warning.
244 * Clients sending invalid messages (based on @a handlers) will be
245 * dropped. Additionally, clients can be dropped at any time using
246 * #GNUNET_SERVICE_client_drop().
248 * The service must be stopped using #GNUNET_SERVICE_stoP().
250 * @param service_name name of the service to run
251 * @param cfg configuration to use
252 * @param connect_cb function to call whenever a client connects
253 * @param disconnect_cb function to call whenever a client disconnects
254 * @param cls closure argument for @a connect_cb and @a disconnect_cb
255 * @param handlers NULL-terminated array of message handlers for the service,
256 * the closure will be set to the value returned by
257 * the @a connect_cb for the respective connection
258 * @return NULL on error
260 struct GNUNET_SERVICE_Handle *
261 GNUNET_SERVICE_starT (const char *service_name,
262 const struct GNUNET_CONFIGURATION_Handle *cfg,
263 GNUNET_SERVICE_ConnectHandler connect_cb,
264 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
266 const struct GNUNET_MQ_MessageHandler *handlers);
270 * Stops a service that was started with #GNUNET_SERVICE_starT().
272 * @param srv service to stop
275 GNUNET_SERVICE_stoP (struct GNUNET_SERVICE_Handle *srv);
279 * Creates the "main" function for a GNUnet service. You
280 * should almost always use the #GNUNET_SERVICE_MAIN macro
281 * instead of calling this function directly (except
282 * for ARM, which should call this function directly).
284 * The function will launch the service with the name @a service_name
285 * using the @a service_options to configure its shutdown
286 * behavior. Once the service is ready, the @a init_cb will be called
287 * for service-specific initialization. @a init_cb will be given the
288 * service handler which can be used to control the service's
289 * availability. When clients connect or disconnect, the respective
290 * @a connect_cb or @a disconnect_cb functions will be called. For
291 * messages received from the clients, the respective @a handlers will
292 * be invoked; for the closure of the handlers we use the return value
293 * from the @a connect_cb invocation of the respective client.
295 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
296 * message to receive further messages from this client. If
297 * #GNUNET_SERVICE_client_continue() is not called within a short
298 * time, a warning will be logged. If delays are expected, services
299 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
300 * disable the warning.
302 * Clients sending invalid messages (based on @a handlers) will be
303 * dropped. Additionally, clients can be dropped at any time using
304 * #GNUNET_SERVICE_client_drop().
306 * @param argc number of command-line arguments in @a argv
307 * @param argv array of command-line arguments
308 * @param service_name name of the service to run
309 * @param options options controlling shutdown of the service
310 * @param service_init_cb function to call once the service is ready
311 * @param connect_cb function to call whenever a client connects
312 * @param disconnect_cb function to call whenever a client disconnects
313 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
314 * @param handlers NULL-terminated array of message handlers for the service,
315 * the closure will be set to the value returned by
316 * the @a connect_cb for the respective connection
317 * @return 0 on success, non-zero on error
320 GNUNET_SERVICE_ruN_ (int argc,
322 const char *service_name,
323 enum GNUNET_SERVICE_Options options,
324 GNUNET_SERVICE_InitCallback service_init_cb,
325 GNUNET_SERVICE_ConnectHandler connect_cb,
326 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
328 const struct GNUNET_MQ_MessageHandler *handlers);
332 * Creates the "main" function for a GNUnet service. You
333 * MUST use this macro to define GNUnet services (except
334 * for ARM, which MUST NOT use the macro). The reason is
335 * the GNUnet-as-a-library project, where we will not define
336 * a main function anywhere but in ARM.
338 * The macro will launch the service with the name @a service_name
339 * using the @a service_options to configure its shutdown
340 * behavior. Once the service is ready, the @a init_cb will be called
341 * for service-specific initialization. @a init_cb will be given the
342 * service handler which can be used to control the service's
343 * availability. When clients connect or disconnect, the respective
344 * @a connect_cb or @a disconnect_cb functions will be called. For
345 * messages received from the clients, the respective @a handlers will
346 * be invoked; for the closure of the handlers we use the return value
347 * from the @a connect_cb invocation of the respective client.
349 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
350 * message to receive further messages from this client. If
351 * #GNUNET_SERVICE_client_continue() is not called within a short
352 * time, a warning will be logged. If delays are expected, services
353 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
354 * disable the warning.
356 * Clients sending invalid messages (based on @a handlers) will be
357 * dropped. Additionally, clients can be dropped at any time using
358 * #GNUNET_SERVICE_client_drop().
360 * @param service_name name of the service to run
361 * @param options options controlling shutdown of the service
362 * @param service_init_cb function to call once the service is ready
363 * @param connect_cb function to call whenever a client connects
364 * @param disconnect_cb function to call whenever a client disconnects
365 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
366 * @param ... array of message handlers for the service, terminated
367 * by #GNUNET_MQ_handler_end();
368 * the closure will be set to the value returned by
369 * the @a connect_cb for the respective connection
370 * @return 0 on success, non-zero on error
374 * GNUNET_SERVICE_MAIN
376 * GNUNET_SERVICE_OPTION_NONE,
381 * GNUNET_MQ_hd_var_size (get,
382 * GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
383 * struct GNUNET_RESOLVER_GetMessage,
385 * GNUNET_MQ_handler_end ());
388 #define GNUNET_SERVICE_MAIN(service_name,service_options,init_cb,connect_cb,disconnect_cb,cls,...) \
393 struct GNUNET_MQ_MessageHandler mh[] = { \
396 return GNUNET_SERVICE_ruN_ (argc, \
409 * Suspend accepting connections from the listen socket temporarily.
410 * Resume activity using #GNUNET_SERVICE_resume.
412 * @param sh service to stop accepting connections.
415 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
419 * Resume accepting connections from the listen socket.
421 * @param sh service to resume accepting connections.
424 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
428 * Continue receiving further messages from the given client.
429 * Must be called after each message received.
431 * @param c the client to continue receiving from
434 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
438 * Obtain the message queue of @a c. Convenience function.
440 * @param c the client to continue receiving from
441 * @return the message queue of @a c
443 struct GNUNET_MQ_Handle *
444 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c);
448 * Disable the warning the server issues if a message is not
449 * acknowledged in a timely fashion. Use this call if a client is
450 * intentionally delayed for a while. Only applies to the current
453 * @param c client for which to disable the warning
456 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c);
460 * Ask the server to disconnect from the given client. This is the
461 * same as returning #GNUNET_SYSERR within the check procedure when
462 * handling a message, wexcept that it allows dropping of a client even
463 * when not handling a message from that client. The `disconnect_cb`
464 * will be called on @a c even if the application closes the connection
465 * using this function.
467 * @param c client to disconnect now
470 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
474 * Explicitly stops the service.
476 * @param sh server to shutdown
479 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
483 * Set the 'monitor' flag on this client. Clients which have been
484 * marked as 'monitors' won't prevent the server from shutting down
485 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
486 * that for "normal" clients we likely want to allow them to process
487 * their requests; however, monitor-clients are likely to 'never'
488 * disconnect during shutdown and thus will not be considered when
489 * determining if the server should continue to exist after
490 * shutdown has been triggered.
492 * @param c client to mark as a monitor
495 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
499 * Set the persist option on this client. Indicates that the
500 * underlying socket or fd should never really be closed. Used for
501 * indicating process death.
503 * @param c client to persist the socket (never to be closed)
506 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
509 #if 0 /* keep Emacsens' auto-indent happy */
516 /* ifndef GNUNET_SERVICE_LIB_H */
519 /** @} */ /* end of group service */
521 /* end of gnunet_service_lib.h */