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
77 /* **************** NEW SERVICE API ********************** */
80 * Handle to a service.
82 struct GNUNET_SERVICE_Handle;
86 * Handle to a client that is connected to a service.
88 struct GNUNET_SERVICE_Client;
92 * Callback to initialize a service, called exactly once when the service is run.
94 * @param cls closure passed to #GNUNET_SERVICE_MAIN
95 * @param cfg configuration to use for this service
96 * @param sh handle to the newly create service
99 (*GNUNET_SERVICE_InitCallback)(void *cls,
100 const struct GNUNET_CONFIGURATION_Handle *cfg,
101 struct GNUNET_SERVICE_Handle *sh);
105 * Callback to be called when a client connects to the service.
107 * @param cls closure for the service
108 * @param c the new client that connected to the service
109 * @param mq the message queue used to send messages to the client
110 * @return the client-specific (`internal') closure
113 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
114 struct GNUNET_SERVICE_Client *c,
115 struct GNUNET_MQ_Handle *mq);
119 * Callback to be called when a client disconnected from the service
121 * @param cls closure for the service
122 * @param c the client that disconnected
123 * @param internal_cls the client-specific (`internal') closure
126 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
127 struct GNUNET_SERVICE_Client *c,
132 * Low-level function to start a service if the scheduler
133 * is already running. Should only be used directly in
136 * The function will launch the service with the name @a service_name
137 * using the @a service_options to configure its shutdown
138 * behavior. When clients connect or disconnect, the respective
139 * @a connect_cb or @a disconnect_cb functions will be called. For
140 * messages received from the clients, the respective @a handlers will
141 * be invoked; for the closure of the handlers we use the return value
142 * from the @a connect_cb invocation of the respective client.
144 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
145 * message to receive further messages from this client. If
146 * #GNUNET_SERVICE_client_continue() is not called within a short
147 * time, a warning will be logged. If delays are expected, services
148 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
149 * disable the warning.
151 * Clients sending invalid messages (based on @a handlers) will be
152 * dropped. Additionally, clients can be dropped at any time using
153 * #GNUNET_SERVICE_client_drop().
155 * The service must be stopped using #GNUNET_SERVICE_stop().
157 * @param service_name name of the service to run
158 * @param cfg configuration to use
159 * @param connect_cb function to call whenever a client connects
160 * @param disconnect_cb function to call whenever a client disconnects
161 * @param cls closure argument for @a connect_cb and @a disconnect_cb
162 * @param handlers NULL-terminated array of message handlers for the service,
163 * the closure will be set to the value returned by
164 * the @a connect_cb for the respective connection
165 * @return NULL on error
167 struct GNUNET_SERVICE_Handle *
168 GNUNET_SERVICE_start (const char *service_name,
169 const struct GNUNET_CONFIGURATION_Handle *cfg,
170 GNUNET_SERVICE_ConnectHandler connect_cb,
171 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
173 const struct GNUNET_MQ_MessageHandler *handlers);
177 * Stops a service that was started with #GNUNET_SERVICE_start().
179 * @param srv service to stop
182 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Handle *srv);
186 * Creates the "main" function for a GNUnet service. You
187 * should almost always use the #GNUNET_SERVICE_MAIN macro
188 * instead of calling this function directly (except
189 * for ARM, which should call this function directly).
191 * The function will launch the service with the name @a service_name
192 * using the @a service_options to configure its shutdown
193 * behavior. Once the service is ready, the @a init_cb will be called
194 * for service-specific initialization. @a init_cb will be given the
195 * service handler which can be used to control the service's
196 * availability. When clients connect or disconnect, the respective
197 * @a connect_cb or @a disconnect_cb functions will be called. For
198 * messages received from the clients, the respective @a handlers will
199 * be invoked; for the closure of the handlers we use the return value
200 * from the @a connect_cb invocation of the respective client.
202 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
203 * message to receive further messages from this client. If
204 * #GNUNET_SERVICE_client_continue() is not called within a short
205 * time, a warning will be logged. If delays are expected, services
206 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
207 * disable the warning.
209 * Clients sending invalid messages (based on @a handlers) will be
210 * dropped. Additionally, clients can be dropped at any time using
211 * #GNUNET_SERVICE_client_drop().
213 * @param argc number of command-line arguments in @a argv
214 * @param argv array of command-line arguments
215 * @param service_name name of the service to run
216 * @param options options controlling shutdown of the service
217 * @param service_init_cb function to call once the service is ready
218 * @param connect_cb function to call whenever a client connects
219 * @param disconnect_cb function to call whenever a client disconnects
220 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
221 * @param handlers NULL-terminated array of message handlers for the service,
222 * the closure will be set to the value returned by
223 * the @a connect_cb for the respective connection
224 * @return 0 on success, non-zero on error
227 GNUNET_SERVICE_run_ (int argc,
229 const char *service_name,
230 enum GNUNET_SERVICE_Options options,
231 GNUNET_SERVICE_InitCallback service_init_cb,
232 GNUNET_SERVICE_ConnectHandler connect_cb,
233 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
235 const struct GNUNET_MQ_MessageHandler *handlers);
239 * Creates the "main" function for a GNUnet service. You
240 * MUST use this macro to define GNUnet services (except
241 * for ARM, which MUST NOT use the macro). The reason is
242 * the GNUnet-as-a-library project, where we will not define
243 * a main function anywhere but in ARM.
245 * The macro will launch the service with the name @a service_name
246 * using the @a service_options to configure its shutdown
247 * behavior. Once the service is ready, the @a init_cb will be called
248 * for service-specific initialization. @a init_cb will be given the
249 * service handler which can be used to control the service's
250 * availability. When clients connect or disconnect, the respective
251 * @a connect_cb or @a disconnect_cb functions will be called. For
252 * messages received from the clients, the respective @a handlers will
253 * be invoked; for the closure of the handlers we use the return value
254 * from the @a connect_cb invocation of the respective client.
256 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
257 * message to receive further messages from this client. If
258 * #GNUNET_SERVICE_client_continue() is not called within a short
259 * time, a warning will be logged. If delays are expected, services
260 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
261 * disable the warning.
263 * Clients sending invalid messages (based on @a handlers) will be
264 * dropped. Additionally, clients can be dropped at any time using
265 * #GNUNET_SERVICE_client_drop().
267 * @param service_name name of the service to run
268 * @param options options controlling shutdown of the service
269 * @param service_init_cb function to call once the service is ready
270 * @param connect_cb function to call whenever a client connects
271 * @param disconnect_cb function to call whenever a client disconnects
272 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
273 * @param ... array of message handlers for the service, terminated
274 * by #GNUNET_MQ_handler_end();
275 * the closure will be set to the value returned by
276 * the @a connect_cb for the respective connection
277 * @return 0 on success, non-zero on error
281 * GNUNET_SERVICE_MAIN
283 * GNUNET_SERVICE_OPTION_NONE,
288 * GNUNET_MQ_hd_var_size (get,
289 * GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
290 * struct GNUNET_RESOLVER_GetMessage,
292 * GNUNET_MQ_handler_end ());
295 #define GNUNET_SERVICE_MAIN(service_name,service_options,init_cb,connect_cb,disconnect_cb,cls,...) \
300 struct GNUNET_MQ_MessageHandler mh[] = { \
303 return GNUNET_SERVICE_run_ (argc, \
316 * Suspend accepting connections from the listen socket temporarily.
317 * Resume activity using #GNUNET_SERVICE_resume.
319 * @param sh service to stop accepting connections.
322 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
326 * Resume accepting connections from the listen socket.
328 * @param sh service to resume accepting connections.
331 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
335 * Continue receiving further messages from the given client.
336 * Must be called after each message received.
338 * @param c the client to continue receiving from
341 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
345 * Obtain the message queue of @a c. Convenience function.
347 * @param c the client to continue receiving from
348 * @return the message queue of @a c
350 struct GNUNET_MQ_Handle *
351 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c);
355 * Disable the warning the server issues if a message is not
356 * acknowledged in a timely fashion. Use this call if a client is
357 * intentionally delayed for a while. Only applies to the current
360 * @param c client for which to disable the warning
363 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c);
367 * Ask the server to disconnect from the given client. This is the
368 * same as returning #GNUNET_SYSERR within the check procedure when
369 * handling a message, except that it allows dropping of a client even
370 * when not handling a message from that client. The `disconnect_cb`
371 * will be called on @a c even if the application closes the connection
372 * using this function.
374 * This function should be called (outside of util's internal logic)
375 * if (and usually only if) the client has violated the
376 * protocol. Otherwise, we should leave it to the client to disconnect
379 * @param c client to disconnect now
382 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
386 * Explicitly stops the service.
388 * @param sh server to shutdown
391 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
395 * Set the 'monitor' flag on this client. Clients which have been
396 * marked as 'monitors' won't prevent the server from shutting down
397 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
398 * that for "normal" clients we likely want to allow them to process
399 * their requests; however, monitor-clients are likely to 'never'
400 * disconnect during shutdown and thus will not be considered when
401 * determining if the server should continue to exist after
402 * shutdown has been triggered.
404 * @param c client to mark as a monitor
407 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
411 * Set the persist option on this client. Indicates that the
412 * underlying socket or fd should never really be closed. Used for
413 * indicating process death.
415 * @param c client to persist the socket (never to be closed)
418 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
421 #if 0 /* keep Emacsens' auto-indent happy */
428 /* ifndef GNUNET_SERVICE_LIB_H */
431 /** @} */ /* end of group service */
433 /* end of gnunet_service_lib.h */