src: for every AGPL3.0 file, add SPDX identifier.
[oweals/gnunet.git] / src / arm / mockup-service.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2007, 2008, 2009, 2016 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 #include <stdlib.h>
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_protocols.h"
25
26
27 static int special_ret = 0;
28
29 /**
30  * Handler for STOP message.
31  *
32  * @param cls client identification of the client
33  * @param message the actual message
34  */
35 static void
36 handle_stop (void *cls,
37              const struct GNUNET_MessageHeader *message)
38 {
39   struct GNUNET_SERVICE_Client *client = cls;
40
41   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
42               _("Initiating shutdown as requested by client.\n"));
43   GNUNET_SERVICE_client_persist (client);
44   GNUNET_SCHEDULER_shutdown ();
45   /* ARM won't exponentially increase restart delay if we
46    * terminate normally. This changes the return code.
47    */
48   special_ret = 1;
49 }
50
51
52 /**
53  * Callback called when a client connects to the service.
54  *
55  * @param cls closure for the service
56  * @param c the new client that connected to the service
57  * @param mq the message queue used to send messages to the client
58  * @return @a c
59  */
60 static void *
61 client_connect_cb (void *cls,
62                    struct GNUNET_SERVICE_Client *c,
63                    struct GNUNET_MQ_Handle *mq)
64 {
65   return c;
66 }
67
68
69 /**
70  * Callback called when a client disconnected from the service
71  *
72  * @param cls closure for the service
73  * @param c the client that disconnected
74  * @param internal_cls should be equal to @a c
75  */
76 static void
77 client_disconnect_cb (void *cls,
78                       struct GNUNET_SERVICE_Client *c,
79                       void *internal_cls)
80 {
81   GNUNET_assert (c == internal_cls);
82 }
83
84
85 static void
86 run (void *cls,
87      const struct GNUNET_CONFIGURATION_Handle *cfg,
88      struct GNUNET_SERVICE_Handle *service)
89 {
90   /* nothing to do */
91 }
92
93
94 /**
95  * Define "main" method using service macro.
96  */
97 GNUNET_SERVICE_MAIN
98 ("do-nothing",
99  GNUNET_SERVICE_OPTION_NONE,
100  &run,
101  &client_connect_cb,
102  &client_disconnect_cb,
103  NULL,
104  GNUNET_MQ_hd_fixed_size (stop,
105                           GNUNET_MESSAGE_TYPE_ARM_STOP,
106                           struct GNUNET_MessageHeader,
107                           NULL),
108  GNUNET_MQ_handler_end ());
109
110
111 /**
112  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
113  */
114 void __attribute__ ((destructor))
115 GNUNET_mockup_done ()
116 {
117   _exit (special_ret);
118 }