fix bad free
[oweals/gnunet.git] / src / include / gnunet_arm_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 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
19 /**
20  * @author Christian Grothoff
21  *
22  * @file
23  * API to access gnunet-arm
24  *
25  * @defgroup arm  ARM service
26  * Automatic Restart Manager
27  *
28  * @see [Documentation](https://gnunet.org/arm)
29  *
30  * @{
31  */
32
33 #ifndef GNUNET_ARM_SERVICE_H
34 #define GNUNET_ARM_SERVICE_H
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44 #include "gnunet_util_lib.h"
45
46 /**
47  * Version of the arm API.
48  */
49 #define GNUNET_ARM_VERSION 0x00000003
50
51
52 /**
53  * Statuses of the requests that client can send to ARM.
54  */
55 enum GNUNET_ARM_RequestStatus
56 {
57   /**
58    * Message was sent successfully.
59    */
60   GNUNET_ARM_REQUEST_SENT_OK = 0,
61
62   /**
63    * We disconnected from ARM, and request was not sent.
64    */
65   GNUNET_ARM_REQUEST_DISCONNECTED = 2
66
67 };
68
69
70 /**
71  * Statuses of services.
72  */
73 enum GNUNET_ARM_ServiceStatus
74 {
75   /**
76    * Dummy message.
77    */
78   GNUNET_ARM_SERVICE_MONITORING_STARTED = 0,
79
80   /**
81    * Service was stopped.
82    */
83   GNUNET_ARM_SERVICE_STOPPED = 1,
84
85   /**
86    * Service starting was initiated
87    */
88   GNUNET_ARM_SERVICE_STARTING = 2,
89
90   /**
91    * Service stopping was initiated
92    */
93   GNUNET_ARM_SERVICE_STOPPING = 3
94 };
95
96
97 /**
98  * Replies to ARM requests
99  */
100 enum GNUNET_ARM_Result
101 {
102   /**
103    * Service was stopped (never sent for ARM itself).
104    */
105   GNUNET_ARM_RESULT_STOPPED = 0,
106
107   /**
108    * ARM stopping was initiated (there's no "stopped" for ARM itself).
109    */
110   GNUNET_ARM_RESULT_STOPPING = 1,
111
112   /**
113    * Service starting was initiated
114    */
115   GNUNET_ARM_RESULT_STARTING = 2,
116
117   /**
118    * Asked to start it, but it's already starting.
119    */
120   GNUNET_ARM_RESULT_IS_STARTING_ALREADY = 3,
121
122   /**
123    * Asked to stop it, but it's already stopping.
124    */
125   GNUNET_ARM_RESULT_IS_STOPPING_ALREADY = 4,
126
127   /**
128    * Asked to start it, but it's already started.
129    */
130   GNUNET_ARM_RESULT_IS_STARTED_ALREADY = 5,
131
132   /**
133    * Asked to stop it, but it's already stopped.
134    */
135   GNUNET_ARM_RESULT_IS_STOPPED_ALREADY = 6,
136
137   /**
138    * Asked to start or stop a service, but it's not known.
139    */
140   GNUNET_ARM_RESULT_IS_NOT_KNOWN = 7,
141
142   /**
143    * Tried to start a service, but that failed for some reason.
144    */
145   GNUNET_ARM_RESULT_START_FAILED = 8,
146
147   /**
148    * Asked to start something, but ARM is shutting down and can't comply.
149    */
150   GNUNET_ARM_RESULT_IN_SHUTDOWN = 9
151 };
152
153
154 /**
155  * Handle for interacting with ARM.
156  */
157 struct GNUNET_ARM_Handle;
158
159 /**
160  * Handle for an ARM operation.
161  */
162 struct GNUNET_ARM_Operation;
163
164
165 /**
166  * Function called whenever we connect to or disconnect from ARM.
167  *
168  * @param cls closure
169  * @param connected #GNUNET_YES if connected, #GNUNET_NO if disconnected,
170  *                  #GNUNET_SYSERR if there was an error.
171  */
172 typedef void
173 (*GNUNET_ARM_ConnectionStatusCallback) (void *cls,
174                                         int connected);
175
176
177 /**
178  * Function called in response to a start/stop request.
179  * Will be called when request was not sent successfully,
180  * or when a reply comes. If the request was not sent successfully,
181  * @a rs will indicate that, and @a result will be undefined.
182  *
183  * @param cls closure
184  * @param rs status of the request
185  * @param result result of the operation
186  */
187 typedef void
188 (*GNUNET_ARM_ResultCallback) (void *cls,
189                               enum GNUNET_ARM_RequestStatus rs,
190                               enum GNUNET_ARM_Result result);
191
192
193 /**
194  * Callback function invoked when list operation is complete.
195  * Will be called when request was not sent successfully,
196  * or when a reply comes. If the request was not sent successfully,
197  * @a rs will indicate that, and @a count and @a list will be undefined.
198  *
199  * @param cls closure
200  * @param rs status of the request
201  * @param count number of strings in the list
202  * @param list list of running services
203  */
204 typedef void
205 (*GNUNET_ARM_ServiceListCallback) (void *cls,
206                                    enum GNUNET_ARM_RequestStatus rs,
207                                    unsigned int count,
208                                    const char *const*list);
209
210
211 /**
212  * Set up a context for communicating with ARM, then
213  * start connecting to the ARM service using that context.
214  *
215  * @param cfg configuration to use (needed to contact ARM;
216  *        the ARM service may internally use a different
217  *        configuration to determine how to start the service).
218  * @param conn_status will be called when connecting/disconnecting
219  * @param conn_status_cls closure for @a conn_status
220  * @return context to use for further ARM operations, NULL on error.
221  */
222 struct GNUNET_ARM_Handle *
223 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
224                     GNUNET_ARM_ConnectionStatusCallback conn_status,
225                     void *conn_status_cls);
226
227
228 /**
229  * Disconnect from the ARM service and destroy the handle.
230  *
231  * @param h the handle that was being used
232  */
233 void
234 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h);
235
236
237 /**
238  * Abort an operation.  Only prevents the callback from being
239  * called, the operation may still complete.
240  *
241  * @param op operation to cancel
242  */
243 void
244 GNUNET_ARM_operation_cancel (struct GNUNET_ARM_Operation *op);
245
246
247 /**
248  * Request a list of running services.
249  *
250  * @param h handle to ARM
251  * @param cont callback to invoke after request is sent or is not sent
252  * @param cont_cls closure for @a cont
253  * @return handle for the operation, NULL on error
254  */
255 struct GNUNET_ARM_Operation *
256 GNUNET_ARM_request_service_list (struct GNUNET_ARM_Handle *h,
257                                  GNUNET_ARM_ServiceListCallback cont,
258                                  void *cont_cls);
259
260
261 /**
262  * Request a service to be stopped.
263  * Stopping arm itself will not invalidate its handle, and
264  * ARM API will try to restore connection to the ARM service,
265  * even if ARM connection was lost because you asked for ARM to be stopped.
266  * Call #GNUNET_ARM_disconnect() to free the handle and prevent
267  * further connection attempts.
268  *
269  * @param h handle to ARM
270  * @param service_name name of the service
271  * @param cont callback to invoke after request is sent or is not sent
272  * @param cont_cls closure for @a cont
273  * @return handle for the operation, NULL on error
274  */
275 struct GNUNET_ARM_Operation *
276 GNUNET_ARM_request_service_stop (struct GNUNET_ARM_Handle *h,
277                                  const char *service_name,
278                                  GNUNET_ARM_ResultCallback cont,
279                                  void *cont_cls);
280
281
282 /**
283  * Request for a service to be started.
284  *
285  * @param h handle to ARM
286  * @param service_name name of the service
287  * @param std_inheritance inheritance of std streams
288  * @param cont callback to invoke after request is sent or not sent
289  * @param cont_cls closure for @a cont
290  * @return handle for the operation, NULL on error
291  */
292 struct GNUNET_ARM_Operation *
293 GNUNET_ARM_request_service_start (struct GNUNET_ARM_Handle *h,
294                                   const char *service_name,
295                                   enum GNUNET_OS_InheritStdioFlags std_inheritance,
296                                   GNUNET_ARM_ResultCallback cont,
297                                   void *cont_cls);
298
299
300 /**
301  * Handle for monitoring ARM.
302  */
303 struct GNUNET_ARM_MonitorHandle;
304
305
306 /**
307  * Function called in when a status update arrives.
308  *
309  * @param cls closure
310  * @param service service name
311  * @param status status of the service
312  */
313 typedef void
314 (*GNUNET_ARM_ServiceStatusCallback) (void *cls,
315                                      const char *service,
316                                      enum GNUNET_ARM_ServiceStatus status);
317
318
319 /**
320  * Setup a context for monitoring ARM, then
321  * start connecting to the ARM service for monitoring using that context.
322  *
323  * @param cfg configuration to use (needed to contact ARM;
324  *        the ARM service may internally use a different
325  *        configuration to determine how to start the service).
326  * @param cont callback to invoke on status updates
327  * @param cont_cls closure for @a cont
328  * @return context to use for further ARM monitor operations, NULL on error.
329  */
330 struct GNUNET_ARM_MonitorHandle *
331 GNUNET_ARM_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
332                           GNUNET_ARM_ServiceStatusCallback cont,
333                           void *cont_cls);
334
335
336 /**
337  * Disconnect from the ARM service and destroy the handle.
338  *
339  * @param h the handle that was being used
340  */
341 void
342 GNUNET_ARM_monitor_stop (struct GNUNET_ARM_MonitorHandle *h);
343
344 #if 0                           /* keep Emacsens' auto-indent happy */
345 {
346 #endif
347 #ifdef __cplusplus
348 }
349 #endif
350
351 #endif
352
353 /** @} */  /* end of group */