use NULL value in load_path_suffix to NOT load any files
[oweals/gnunet.git] / src / include / gnunet_service_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013, 2016, 2017 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * Functions related to starting services
26  *
27  * @defgroup service  Service library
28  * Start service processes.
29  *
30  * @see [Documentation](https://gnunet.org/developer-handbook-util-services)
31  *
32  * @{
33  */
34
35 #ifndef GNUNET_SERVICE_LIB_H
36 #define GNUNET_SERVICE_LIB_H
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #if 0                           /* keep Emacsens' auto-indent happy */
42 }
43 #endif
44 #endif
45
46 #include "gnunet_configuration_lib.h"
47 #include "gnunet_mq_lib.h"
48
49
50 /**
51  * Options for the service (bitmask).
52  */
53 enum GNUNET_SERVICE_Options
54 {
55   /**
56    * Use defaults.  Terminates all client connections and the listen
57    * sockets immediately upon receiving the shutdown signal.
58    */
59   GNUNET_SERVICE_OPTION_NONE = 0,
60
61   /**
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().
65    */
66   GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
67
68   /**
69    * Trigger a SOFT server shutdown on signals, allowing active
70    * non-monitor clients to complete their transactions.
71    */
72   GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN = 2,
73
74   /**
75    * Bitmask over the shutdown options.
76    */
77   GNUNET_SERVICE_OPTION_SHUTDOWN_BITMASK = 3,
78
79   /**
80    * Instead of listening on lsocks passed by the parent,
81    * close them *after* opening our own listen socket(s).
82    */
83   GNUNET_SERVICE_OPTION_CLOSE_LSOCKS = 4
84 };
85
86
87 /* **************** NEW SERVICE API ********************** */
88
89 /**
90  * Handle to a service.
91  */
92 struct GNUNET_SERVICE_Handle;
93
94
95 /**
96  * Handle to a client that is connected to a service.
97  */
98 struct GNUNET_SERVICE_Client;
99
100
101 /**
102  * Callback to initialize a service, called exactly once when the service is run.
103  *
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
107  */
108 typedef void
109 (*GNUNET_SERVICE_InitCallback)(void *cls,
110                                const struct GNUNET_CONFIGURATION_Handle *cfg,
111                                struct GNUNET_SERVICE_Handle *sh);
112
113
114 /**
115  * Callback to be called when a client connects to the service.
116  *
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
121  */
122 typedef void *
123 (*GNUNET_SERVICE_ConnectHandler)(void *cls,
124                                  struct GNUNET_SERVICE_Client *c,
125                                  struct GNUNET_MQ_Handle *mq);
126
127
128 /**
129  * Callback to be called when a client disconnected from the service
130  *
131  * @param cls closure for the service
132  * @param c the client that disconnected
133  * @param internal_cls the client-specific (`internal') closure
134  */
135 typedef void
136 (*GNUNET_SERVICE_DisconnectHandler)(void *cls,
137                                     struct GNUNET_SERVICE_Client *c,
138                                     void *internal_cls);
139
140
141 /**
142  * Low-level function to start a service if the scheduler
143  * is already running.  Should only be used directly in
144  * special cases.
145  *
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.
153  *
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.
160  *
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().
164  *
165  * The service must be stopped using #GNUNET_SERVICE_stop().
166  *
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
176  */
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,
182                       void *cls,
183                       const struct GNUNET_MQ_MessageHandler *handlers);
184
185
186 /**
187  * Stops a service that was started with #GNUNET_SERVICE_start().
188  *
189  * @param srv service to stop
190  */
191 void
192 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Handle *srv);
193
194
195 /**
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).
200  *
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.
211  *
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.
218  *
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().
222  *
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
235  */
236 int
237 GNUNET_SERVICE_run_ (int argc,
238                      char *const *argv,
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,
244                      void *cls,
245                      const struct GNUNET_MQ_MessageHandler *handlers);
246
247
248 /**
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.
254  *
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.
265  *
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.
272  *
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().
276  *
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
288  *
289  * Sample invocation:
290  * <code>
291  * GNUNET_SERVICE_MAIN
292  * ("resolver",
293  *  GNUNET_SERVICE_OPTION_NONE,
294  *  &init_cb,
295  *  &connect_cb,
296  *  &disconnect_cb,
297  *  closure_for_cb,
298  *  GNUNET_MQ_hd_var_size (get,
299  *                         GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
300  *                         struct GNUNET_RESOLVER_GetMessage,
301  *                         NULL),
302  *  GNUNET_MQ_handler_end ());
303  * </code>
304  */
305 #define GNUNET_SERVICE_MAIN(service_name, service_options, init_cb, connect_cb, \
306                             disconnect_cb, cls, ...) \
307   int \
308   main (int argc, \
309         char *const *argv) \
310   { \
311     struct GNUNET_MQ_MessageHandler mh[] = { \
312       __VA_ARGS__ \
313     };                        \
314     return GNUNET_SERVICE_run_ (argc, \
315                                 argv, \
316                                 service_name, \
317                                 service_options, \
318                                 init_cb, \
319                                 connect_cb, \
320                                 disconnect_cb, \
321                                 cls, \
322                                 mh); \
323   }
324
325
326 /**
327  * Suspend accepting connections from the listen socket temporarily.
328  * Resume activity using #GNUNET_SERVICE_resume.
329  *
330  * @param sh service to stop accepting connections.
331  */
332 void
333 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh);
334
335
336 /**
337  * Resume accepting connections from the listen socket.
338  *
339  * @param sh service to resume accepting connections.
340  */
341 void
342 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh);
343
344
345 /**
346  * Continue receiving further messages from the given client.
347  * Must be called after each message received.
348  *
349  * @param c the client to continue receiving from
350  */
351 void
352 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c);
353
354
355 /**
356  * Obtain the message queue of @a c.  Convenience function.
357  *
358  * @param c the client to continue receiving from
359  * @return the message queue of @a c
360  */
361 struct GNUNET_MQ_Handle *
362 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c);
363
364
365 /**
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
369  * message.
370  *
371  * @param c client for which to disable the warning
372  */
373 void
374 GNUNET_SERVICE_client_disable_continue_warning (struct
375                                                 GNUNET_SERVICE_Client *c);
376
377
378 /**
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.
385  *
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
389  * from the service.
390  *
391  * @param c client to disconnect now
392  */
393 void
394 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c);
395
396
397 /**
398  * Explicitly stops the service.
399  *
400  * @param sh server to shutdown
401  */
402 void
403 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh);
404
405
406 /**
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.
415  *
416  * @param c client to mark as a monitor
417  */
418 void
419 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c);
420
421
422 /**
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.
426  *
427  * @param c client to persist the socket (never to be closed)
428  */
429 void
430 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c);
431
432
433 #if 0                           /* keep Emacsens' auto-indent happy */
434 {
435 #endif
436 #ifdef __cplusplus
437 }
438 #endif
439
440 /* ifndef GNUNET_SERVICE_LIB_H */
441 #endif
442
443 /** @} */  /* end of group service */
444
445 /* end of gnunet_service_lib.h */