implement boolean json spec
[oweals/gnunet.git] / src / include / gnunet_helper_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011, 2012 Christian Grothoff
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @author Philipp Toelke
23  * @author Christian Grothoff
24  *
25  * @file
26  * API for dealing with SUID helper processes
27  *
28  * @defgroup helper  Helper library
29  * Dealing with SUID helper processes.
30  *
31  * Provides an API for dealing with (SUID) helper processes
32  * that communicate via GNUNET_MessageHeaders on STDIN/STDOUT.
33  *
34  * @{
35  */
36
37 #ifndef GNUNET_HELPER_LIB_H
38 #define GNUNET_HELPER_LIB_H
39
40 #include "gnunet_scheduler_lib.h"
41 #include "gnunet_mst_lib.h"
42
43
44 /**
45  * The handle to a helper process.
46  */
47 struct GNUNET_HELPER_Handle;
48
49
50 /**
51  * Callback that will be called when the helper process dies. This is not called
52  * when the helper process is stoped using GNUNET_HELPER_stop()
53  *
54  * @param cls the closure from GNUNET_HELPER_start()
55  */
56 typedef void
57 (*GNUNET_HELPER_ExceptionCallback) (void *cls);
58
59
60 /**
61  * Starts a helper and begins reading from it. The helper process is
62  * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
63  * or when the exp_cb callback is not NULL.
64  *
65  * @param with_control_pipe does the helper support the use of a control pipe for signalling?
66  * @param binary_name name of the binary to run
67  * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
68  *                    argument must not be modified by the client for
69  *                     the lifetime of the helper handle)
70  * @param cb function to call if we get messages from the helper
71  * @param exp_cb the exception callback to call. Set this to NULL if the helper
72  *          process has to be restarted automatically when it dies/crashes
73  * @param cb_cls closure for the above callbacks
74  * @return the new Handle, NULL on error
75  */
76 struct GNUNET_HELPER_Handle *
77 GNUNET_HELPER_start (int with_control_pipe,
78                      const char *binary_name,
79                      char *const binary_argv[],
80                      GNUNET_MessageTokenizerCallback cb,
81                      GNUNET_HELPER_ExceptionCallback exp_cb,
82                      void *cb_cls);
83
84
85 /**
86  * Sends termination signal to the helper process.  The helper process is not
87  * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.
88  *
89  * @param h the helper handle
90  * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
91  *          stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
92  * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
93  */
94 int
95 GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill);
96
97
98 /**
99  * Reap the helper process.  This call is blocking (!).  The helper process
100  * should either be sent a termination signal before or should be dead before
101  * calling this function
102  *
103  * @param h the helper handle
104  * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
105  */
106 int
107 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h);
108
109
110 /**
111  * Free's the resources occupied by the helper handle
112  *
113  * @param h the helper handle to free
114  */
115 void
116 GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h);
117
118
119 /**
120  * Kills the helper, closes the pipe, frees the handle and calls wait() on the
121  * helper process
122  *
123  * @param h handle to helper to stop
124  * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
125  *          stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
126  */
127 void
128 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill);
129
130
131 /**
132  * Continuation function.
133  *
134  * @param cls closure
135  * @param result #GNUNET_OK on success,
136  *               #GNUNET_NO if helper process died
137  *               #GNUNET_SYSERR during GNUNET_HELPER_destroy
138  */
139 typedef void (*GNUNET_HELPER_Continuation)(void *cls,
140                                            int result);
141
142
143 /**
144  * Handle to cancel 'send'
145  */
146 struct GNUNET_HELPER_SendHandle;
147
148
149 /**
150  * Send an message to the helper.
151  *
152  * @param h helper to send message to
153  * @param msg message to send
154  * @param can_drop can the message be dropped if there is already one in the queue?
155  * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
156  *             if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
157  * @param cont_cls closure for @a cont
158  * @return NULL if the message was dropped,
159  *         otherwise handle to cancel @a cont (actual transmission may
160  *         not be abortable)
161  */
162 struct GNUNET_HELPER_SendHandle *
163 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
164                     const struct GNUNET_MessageHeader *msg,
165                     int can_drop,
166                     GNUNET_HELPER_Continuation cont,
167                     void *cont_cls);
168
169
170 /**
171  * Cancel a #GNUNET_HELPER_send operation.  If possible, transmitting
172  * the message is also aborted, but at least 'cont' won't be called.
173  *
174  * @param sh operation to cancel
175  */
176 void
177 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh);
178
179
180 #endif
181 /* end of include guard: GNUNET_HELPER_LIB_H */
182
183 /** @} */  /* end of group */